]> git.lyx.org Git - lyx.git/blob - src/output_latex.C
* src/tabular.[Ch]: simplify plaintext methods, because there
[lyx.git] / src / output_latex.C
1 /**
2  * \file output_latex.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "output_latex.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "debug.h"
18 #include "encoding.h"
19 #include "language.h"
20 #include "lyxrc.h"
21 #include "outputparams.h"
22 #include "paragraph.h"
23 #include "paragraph_funcs.h"
24 #include "ParagraphParameters.h"
25 #include "texrow.h"
26 #include "vspace.h"
27
28 #include "insets/insetbibitem.h"
29 #include "insets/insetoptarg.h"
30
31 #include "support/lstrings.h"
32
33
34 namespace lyx {
35
36 using support::subst;
37
38 using std::endl;
39 using std::string;
40
41
42 namespace {
43
44 ParagraphList::const_iterator
45 TeXEnvironment(Buffer const & buf,
46                ParagraphList const & paragraphs,
47                ParagraphList::const_iterator pit,
48                odocstream & os, TexRow & texrow,
49                OutputParams const & runparams);
50
51 ParagraphList::const_iterator
52 TeXOnePar(Buffer const & buf,
53           ParagraphList const & paragraphs,
54           ParagraphList::const_iterator pit,
55           odocstream & os, TexRow & texrow,
56           OutputParams const & runparams,
57           string const & everypar = string());
58
59
60 ParagraphList::const_iterator
61 TeXDeeper(Buffer const & buf,
62           ParagraphList const & paragraphs,
63           ParagraphList::const_iterator pit,
64           odocstream & os, TexRow & texrow,
65           OutputParams const & runparams)
66 {
67         lyxerr[Debug::LATEX] << "TeXDeeper...     " << &*pit << endl;
68         ParagraphList::const_iterator par = pit;
69
70         while (par != paragraphs.end() &&
71                      par->params().depth() == pit->params().depth()) {
72                 if (par->layout()->isEnvironment()) {
73                         par = TeXEnvironment(buf, paragraphs, par,
74                                              os, texrow, runparams);
75                 } else {
76                         par = TeXOnePar(buf, paragraphs, par,
77                                              os, texrow, runparams);
78                 }
79         }
80         lyxerr[Debug::LATEX] << "TeXDeeper...done " << endl;
81
82         return par;
83 }
84
85
86 ParagraphList::const_iterator
87 TeXEnvironment(Buffer const & buf,
88                ParagraphList const & paragraphs,
89                ParagraphList::const_iterator pit,
90                odocstream & os, TexRow & texrow,
91                OutputParams const & runparams)
92 {
93         lyxerr[Debug::LATEX] << "TeXEnvironment...     " << &*pit << endl;
94
95         BufferParams const & bparams = buf.params();
96
97         LyXLayout_ptr const & style = pit->layout();
98
99         Language const * language = pit->getParLanguage(bparams);
100         Language const * doc_language = bparams.language;
101         Language const * previous_language =
102                 (pit != paragraphs.begin())
103                 ? boost::prior(pit)->getParLanguage(bparams)
104                 : doc_language;
105         if (language->babel() != previous_language->babel()) {
106
107                 if (!lyxrc.language_command_end.empty() &&
108                     previous_language->babel() != doc_language->babel()) {
109                         os << from_ascii(subst(
110                                 lyxrc.language_command_end,
111                                 "$$lang",
112                                 previous_language->babel()))
113                            << '\n';
114                         texrow.newline();
115                 }
116
117                 if (lyxrc.language_command_end.empty() ||
118                     language->babel() != doc_language->babel()) {
119                         os << from_ascii(subst(
120                                 lyxrc.language_command_begin,
121                                 "$$lang",
122                                 language->babel()))
123                            << '\n';
124                         texrow.newline();
125                 }
126         }
127
128         bool leftindent_open = false;
129         if (!pit->params().leftIndent().zero()) {
130                 os << "\\begin{LyXParagraphLeftIndent}{"
131                    << from_ascii(pit->params().leftIndent().asLatexString())
132                    << "}\n";
133                 texrow.newline();
134                 leftindent_open = true;
135         }
136
137         if (style->isEnvironment()) {
138                 os << "\\begin{" << from_ascii(style->latexname()) << '}';
139                 if (style->optionalargs > 0) {
140                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
141                                                     style->optionalargs);
142                         while (ret > 0) {
143                                 texrow.newline();
144                                 --ret;
145                         }
146                 }
147                 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
148                         os << '{'
149                            << pit->params().labelWidthString()
150                            << "}\n";
151                 } else if (style->labeltype == LABEL_BIBLIO) {
152                         // ale970405
153                         os << '{' << bibitemWidest(buf) << "}\n";
154                 } else
155                         os << from_ascii(style->latexparam()) << '\n';
156                 texrow.newline();
157         }
158         ParagraphList::const_iterator par = pit;
159         do {
160                 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
161
162                 if (par == paragraphs.end()) {
163                         // Make sure that the last paragraph is
164                         // correctly terminated (because TeXOnePar does
165                         // not add a \n in this case)
166                         os << '\n';
167                         texrow.newline();
168                 } else if (par->params().depth() > pit->params().depth()) {
169                             if (par->layout()->isParagraph()) {
170
171                             // Thinko!
172                             // How to handle this? (Lgb)
173                             //&& !suffixIs(os, "\n\n")
174                                     //) {
175                                 // There should be at least one '\n' already
176                                 // but we need there to be two for Standard
177                                 // paragraphs that are depth-increment'ed to be
178                                 // output correctly.  However, tables can
179                                 // also be paragraphs so don't adjust them.
180                                 // ARRae
181                                 // Thinkee:
182                                 // Will it ever harm to have one '\n' too
183                                 // many? i.e. that we sometimes will have
184                                 // three in a row. (Lgb)
185                                 os << '\n';
186                                 texrow.newline();
187                         }
188                         par = TeXDeeper(buf, paragraphs, par, os, texrow,
189                                         runparams);
190                 }
191         } while (par != paragraphs.end()
192                  && par->layout() == pit->layout()
193                  && par->params().depth() == pit->params().depth()
194                  && par->params().leftIndent() == pit->params().leftIndent());
195
196         if (style->isEnvironment()) {
197                 os << "\\end{" << from_ascii(style->latexname()) << "}\n";
198                 texrow.newline();
199         }
200
201         if (leftindent_open) {
202                 os << "\\end{LyXParagraphLeftIndent}\n";
203                 texrow.newline();
204         }
205
206         if (par != paragraphs.end() && lyxerr.debugging(Debug::LATEX))
207                 lyxerr << "TeXEnvironment...done " << &*par << endl;
208         return par;
209 }
210
211 }
212
213
214 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
215                       odocstream & os, OutputParams const & runparams, int number)
216 {
217         int lines = 0;
218
219         InsetList::const_iterator it = par.insetlist.begin();
220         InsetList::const_iterator end = par.insetlist.end();
221         for (; it != end && number > 0 ; ++it) {
222                 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) {
223                         InsetOptArg * ins =
224                                 static_cast<InsetOptArg *>(it->inset);
225                         lines += ins->latexOptional(buf, os, runparams);
226                         --number;
227                 }
228         }
229         return lines;
230 }
231
232
233 namespace {
234
235 ParagraphList::const_iterator
236 TeXOnePar(Buffer const & buf,
237           ParagraphList const & paragraphs,
238           ParagraphList::const_iterator pit,
239           odocstream & os, TexRow & texrow,
240           OutputParams const & runparams_in,
241           string const & everypar)
242 {
243         lyxerr[Debug::LATEX] << "TeXOnePar...     " << &*pit << " '"
244                 << everypar << "'" << endl;
245         BufferParams const & bparams = buf.params();
246         LyXLayout_ptr style;
247
248         // In an inset with unlimited length (all in one row),
249         // force layout to default
250         if (!pit->forceDefaultParagraphs())
251                 style = pit->layout();
252         else
253                 style = bparams.getLyXTextClass().defaultLayout();
254
255         OutputParams runparams = runparams_in;
256         runparams.moving_arg |= style->needprotect;
257
258         Language const * language = pit->getParLanguage(bparams);
259         Language const * doc_language = bparams.language;
260         Language const * previous_language =
261                 (pit != paragraphs.begin())
262                 ? boost::prior(pit)->getParLanguage(bparams)
263                 : doc_language;
264
265         if (language->babel() != previous_language->babel()
266             // check if we already put language command in TeXEnvironment()
267             && !(style->isEnvironment()
268                  && (pit == paragraphs.begin() ||
269                      (boost::prior(pit)->layout() != pit->layout() &&
270                       boost::prior(pit)->getDepth() <= pit->getDepth())
271                      || boost::prior(pit)->getDepth() < pit->getDepth())))
272         {
273                 if (!lyxrc.language_command_end.empty() &&
274                     previous_language->babel() != doc_language->babel())
275                 {
276                         os << from_ascii(subst(lyxrc.language_command_end,
277                                 "$$lang",
278                                 previous_language->babel()))
279                            << '\n';
280                         texrow.newline();
281                 }
282
283                 if (lyxrc.language_command_end.empty() ||
284                     language->babel() != doc_language->babel())
285                 {
286                         os << from_ascii(subst(
287                                 lyxrc.language_command_begin,
288                                 "$$lang",
289                                 language->babel()))
290                            << '\n';
291                         texrow.newline();
292                 }
293         }
294
295         LyXFont const outerfont =
296                 outerFont(std::distance(paragraphs.begin(), pit),
297                           paragraphs);
298         // This must be identical to basefont in Paragraph::simpleTeXOnePar
299         LyXFont basefont = (pit->beginOfBody() > 0) ?
300                         pit->getLabelFont(bparams, outerfont) :
301                         pit->getLayoutFont(bparams, outerfont);
302         Encoding const & outer_encoding(*(outerfont.language()->encoding()));
303         // FIXME we switch from the outer encoding to the encoding of
304         // this paragraph, since I could not figure out the correct
305         // logic to take the encoding of the previous paragraph into
306         // account. This may result in some unneeded encoding changes.
307         if (switchEncoding(os, bparams, outer_encoding,
308                            *(basefont.language()->encoding()))) {
309                 os << '\n';
310                 texrow.newline();
311         }
312
313         // In an inset with unlimited length (all in one row),
314         // don't allow any special options in the paragraph
315         if (!pit->forceDefaultParagraphs()) {
316                 if (pit->params().startOfAppendix()) {
317                         os << "\\appendix\n";
318                         texrow.newline();
319                 }
320
321                 if (!pit->params().spacing().isDefault()
322                         && (pit == paragraphs.begin()
323                             || !boost::prior(pit)->hasSameLayout(*pit)))
324                 {
325                         os << from_ascii(pit->params().spacing().writeEnvirBegin())
326                             << '\n';
327                         texrow.newline();
328                 }
329
330                 if (style->isCommand()) {
331                         os << '\n';
332                         texrow.newline();
333                 }
334         }
335
336         switch (style->latextype) {
337         case LATEX_COMMAND:
338                 os << '\\' << from_ascii(style->latexname());
339
340                 // Separate handling of optional argument inset.
341                 if (style->optionalargs > 0) {
342                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
343                                                     style->optionalargs);
344                         while (ret > 0) {
345                                 texrow.newline();
346                                 --ret;
347                         }
348                 }
349                 else
350                         os << from_ascii(style->latexparam());
351                 break;
352         case LATEX_ITEM_ENVIRONMENT:
353         case LATEX_LIST_ENVIRONMENT:
354                 os << "\\item ";
355                 break;
356         case LATEX_BIB_ENVIRONMENT:
357                 // ignore this, the inset will write itself
358                 break;
359         default:
360                 break;
361         }
362
363         // FIXME UNICODE
364         os << from_utf8(everypar);
365         bool need_par = pit->simpleTeXOnePar(buf, bparams, outerfont,
366                                              os, texrow, runparams);
367
368         // Make sure that \\par is done with the font of the last
369         // character if this has another size as the default.
370         // This is necessary because LaTeX (and LyX on the screen)
371         // calculates the space between the baselines according
372         // to this font. (Matthias)
373         //
374         // Is this really needed ? (Dekel)
375         // We do not need to use to change the font for the last paragraph
376         // or for a command.
377
378         LyXFont const font =
379                 (pit->empty()
380                  ? pit->getLayoutFont(bparams, outerfont)
381                  : pit->getFont(bparams, pit->size() - 1, outerfont));
382
383         bool is_command = style->isCommand();
384
385         if (style->resfont.size() != font.size()
386             && boost::next(pit) != paragraphs.end()
387             && !is_command) {
388                 if (!need_par)
389                         os << '{';
390                 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
391         } else if (need_par) {
392                 os << "\\par}";
393         } else if (is_command)
394                 os << '}';
395
396         bool pending_newline = false;
397         switch (style->latextype) {
398         case LATEX_ITEM_ENVIRONMENT:
399         case LATEX_LIST_ENVIRONMENT:
400                 if (boost::next(pit) != paragraphs.end()
401                     && (pit->params().depth() < boost::next(pit)->params().depth()))
402                         pending_newline = true;
403                 break;
404         case LATEX_ENVIRONMENT: {
405                 // if its the last paragraph of the current environment
406                 // skip it otherwise fall through
407                 ParagraphList::const_iterator next = boost::next(pit);
408
409                 if (next != paragraphs.end()
410                     && (next->layout() != pit->layout()
411                         || next->params().depth() != pit->params().depth()))
412                         break;
413         }
414
415                 // fall through possible
416         default:
417                 // we don't need it for the last paragraph!!!
418                 if (boost::next(pit) != paragraphs.end())
419                         pending_newline = true;
420         }
421
422         if (!pit->forceDefaultParagraphs()) {
423                 if (!pit->params().spacing().isDefault()
424                         && (boost::next(pit) == paragraphs.end()
425                             || !boost::next(pit)->hasSameLayout(*pit)))
426                 {
427                         if (pending_newline) {
428                                 os << '\n';
429                                 texrow.newline();
430                         }
431                         os << from_ascii(pit->params().spacing().writeEnvirEnd());
432                         pending_newline = true;
433                 }
434         }
435
436         if (boost::next(pit) == paragraphs.end()
437             && language->babel() != doc_language->babel()) {
438                 // Since \selectlanguage write the language to the aux file,
439                 // we need to reset the language at the end of footnote or
440                 // float.
441
442                 if (pending_newline) {
443                         os << '\n';
444                         texrow.newline();
445                 }
446                 if (lyxrc.language_command_end.empty())
447                         os << from_ascii(subst(
448                                 lyxrc.language_command_begin,
449                                 "$$lang",
450                                 doc_language->babel()));
451                 else
452                         os << from_ascii(subst(
453                                 lyxrc.language_command_end,
454                                 "$$lang",
455                                 language->babel()));
456                 pending_newline = true;
457         }
458
459         // FIXME we switch from the encoding of this paragraph to the
460         // outer encoding, since I could not figure out the correct logic
461         // to take the encoding of the next paragraph into account.
462         // This may result in some unneeded encoding changes.
463         basefont = pit->getLayoutFont(bparams, outerfont);
464         switchEncoding(os, bparams, *(basefont.language()->encoding()),
465                        outer_encoding);
466         if (pending_newline) {
467                 os << '\n';
468                 texrow.newline();
469         }
470
471         // we don't need it for the last paragraph!!!
472         // Note from JMarc: we will re-add a \n explicitely in
473         // TeXEnvironment, because it is needed in this case
474         if (boost::next(pit) != paragraphs.end()) {
475                 os << '\n';
476                 texrow.newline();
477         }
478
479         if (boost::next(pit) != paragraphs.end() &&
480             lyxerr.debugging(Debug::LATEX))
481                 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
482
483         return ++pit;
484 }
485
486 } // anon namespace
487
488
489 // LaTeX all paragraphs
490 void latexParagraphs(Buffer const & buf,
491                      ParagraphList const & paragraphs,
492                      odocstream & os,
493                      TexRow & texrow,
494                      OutputParams const & runparams,
495                      string const & everypar)
496 {
497         bool was_title = false;
498         bool already_title = false;
499         LyXTextClass const & tclass = buf.params().getLyXTextClass();
500         ParagraphList::const_iterator par = paragraphs.begin();
501         ParagraphList::const_iterator endpar = paragraphs.end();
502
503         BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
504         // if only part of the paragraphs will be outputed
505         if (runparams.par_begin !=  runparams.par_end) {
506                 par = boost::next(paragraphs.begin(), runparams.par_begin);
507                 endpar = boost::next(paragraphs.begin(), runparams.par_end);
508                 // runparams will be passed to nested paragraphs, so
509                 // we have to reset the range parameters.
510                 const_cast<OutputParams&>(runparams).par_begin = 0;
511                 const_cast<OutputParams&>(runparams).par_end = 0;
512         }
513
514         // if only_body
515         while (par != endpar) {
516                 ParagraphList::const_iterator lastpar = par;
517                 // well we have to check if we are in an inset with unlimited
518                 // length (all in one row) if that is true then we don't allow
519                 // any special options in the paragraph and also we don't allow
520                 // any environment other than the default layout of the
521                 // text class to be valid!
522                 if (!par->forceDefaultParagraphs()) {
523                         LyXLayout_ptr const & layout = par->layout();
524
525                         if (layout->intitle) {
526                                 if (already_title) {
527                                         lyxerr << "Error in latexParagraphs: You"
528                                                 " should not mix title layouts"
529                                                 " with normal ones." << endl;
530                                 } else if (!was_title) {
531                                         was_title = true;
532                                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
533                                                 os << "\\begin{"
534                                                     << from_ascii(tclass.titlename())
535                                                     << "}\n";
536                                                 texrow.newline();
537                                         }
538                                 }
539                         } else if (was_title && !already_title) {
540                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
541                                         os << "\\end{" << from_ascii(tclass.titlename())
542                                             << "}\n";
543                                 }
544                                 else {
545                                         os << "\\" << from_ascii(tclass.titlename())
546                                             << "\n";
547                                 }
548                                 texrow.newline();
549                                 already_title = true;
550                                 was_title = false;
551                         }
552
553                         if (layout->is_environment) {
554                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
555                                                 runparams, everypar);
556                         } else if (layout->isEnvironment() ||
557                                    !par->params().leftIndent().zero()) {
558                                 par = TeXEnvironment(buf, paragraphs, par, os,
559                                                      texrow, runparams);
560                         } else {
561                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
562                                                 runparams, everypar);
563                         }
564                 } else {
565                         par = TeXOnePar(buf, paragraphs, par, os, texrow,
566                                         runparams, everypar);
567                 }
568                 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
569                         break;
570         }
571         // It might be that we only have a title in this document
572         if (was_title && !already_title) {
573                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
574                         os << "\\end{" << from_ascii(tclass.titlename())
575                             << "}\n";
576                 }
577                 else {
578                         os << "\\" << from_ascii(tclass.titlename())
579                             << "\n";
580                                 }
581                 texrow.newline();
582         }
583 }
584
585
586 int switchEncoding(odocstream & os, BufferParams const & bparams,
587                    Encoding const & oldEnc, Encoding const & newEnc)
588 {
589         // FIXME thailatex does not support the inputenc package, so we
590         // ignore switches from/to tis620-0 encoding here. This does of
591         // course only work as long as the non-thai text contains ASCII
592         // only, but it is the best we can do.
593         if ((bparams.inputenc == "auto" || bparams.inputenc == "default") &&
594             oldEnc.name() != newEnc.name() &&
595             oldEnc.name() != "ascii" && newEnc.name() != "ascii" &&
596             oldEnc.name() != "tis620-0" && newEnc.name() != "tis620-0") {
597                 lyxerr[Debug::LATEX] << "Changing LaTeX encoding from "
598                                      << oldEnc.name() << " to "
599                                      << newEnc.name() << endl;
600                 os << setEncoding(newEnc.iconvName());
601                 if (bparams.inputenc != "default") {
602                         docstring const inputenc(from_ascii(newEnc.latexName()));
603                         os << "\\inputencoding{" << inputenc << '}';
604                         return 16 + inputenc.length();
605                 }
606         }
607         return 0;
608 }
609
610 } // namespace lyx