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