]> git.lyx.org Git - lyx.git/blob - src/output_latex.C
2b3e5befb8e204a40c6eae8ec62851c51cc15a76
[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         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         switch (style->latextype) {
397         case LATEX_ITEM_ENVIRONMENT:
398         case LATEX_LIST_ENVIRONMENT:
399                 if (boost::next(pit) != paragraphs.end()
400                     && (pit->params().depth() < boost::next(pit)->params().depth())) {
401                         os << '\n';
402                         texrow.newline();
403                 }
404                 break;
405         case LATEX_ENVIRONMENT: {
406                 // if its the last paragraph of the current environment
407                 // skip it otherwise fall through
408                 ParagraphList::const_iterator next = boost::next(pit);
409
410                 if (next != paragraphs.end()
411                     && (next->layout() != pit->layout()
412                         || next->params().depth() != pit->params().depth()))
413                         break;
414         }
415
416                 // fall through possible
417         default:
418                 // we don't need it for the last paragraph!!!
419                 if (boost::next(pit) != paragraphs.end()) {
420                         os << '\n';
421                         texrow.newline();
422                 }
423         }
424
425         if (!pit->forceDefaultParagraphs()) {
426                 if (!pit->params().spacing().isDefault()
427                         && (boost::next(pit) == paragraphs.end()
428                             || !boost::next(pit)->hasSameLayout(*pit)))
429                 {
430                         os << from_ascii(pit->params().spacing().writeEnvirEnd())
431                            << '\n';
432                         texrow.newline();
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 (lyxrc.language_command_end.empty())
443                         os << from_ascii(subst(
444                                 lyxrc.language_command_begin,
445                                 "$$lang",
446                                 doc_language->babel()))
447                            << '\n';
448                 else
449                         os << from_ascii(subst(
450                                 lyxrc.language_command_end,
451                                 "$$lang",
452                                 language->babel()))
453                            << '\n';
454                 texrow.newline();
455         }
456
457         // FIXME we switch from the encoding of this paragraph to the
458         // outer encoding, since I could not figure out the correct logic
459         // to take the encoding of the next paragraph into account.
460         // This may result in some unneeded encoding changes.
461         basefont = pit->getLayoutFont(bparams, outerfont);
462         if (switchEncoding(os, bparams, *(basefont.language()->encoding()),
463                            outer_encoding)) {
464                 os << '\n';
465                 texrow.newline();
466         }
467
468         // we don't need it for the last paragraph!!!
469         // Note from JMarc: we will re-add a \n explicitely in
470         // TeXEnvironment, because it is needed in this case
471         if (boost::next(pit) != paragraphs.end()) {
472                 os << '\n';
473                 texrow.newline();
474         }
475
476         if (boost::next(pit) != paragraphs.end() &&
477             lyxerr.debugging(Debug::LATEX))
478                 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
479
480         return ++pit;
481 }
482
483 } // anon namespace
484
485
486 // LaTeX all paragraphs
487 void latexParagraphs(Buffer const & buf,
488                      ParagraphList const & paragraphs,
489                      odocstream & os,
490                      TexRow & texrow,
491                      OutputParams const & runparams,
492                      string const & everypar)
493 {
494         bool was_title = false;
495         bool already_title = false;
496         LyXTextClass const & tclass = buf.params().getLyXTextClass();
497         ParagraphList::const_iterator par = paragraphs.begin();
498         ParagraphList::const_iterator endpar = paragraphs.end();
499
500         BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
501         // if only part of the paragraphs will be outputed
502         if (runparams.par_begin !=  runparams.par_end) {
503                 par = boost::next(paragraphs.begin(), runparams.par_begin);
504                 endpar = boost::next(paragraphs.begin(), runparams.par_end);
505                 // runparams will be passed to nested paragraphs, so
506                 // we have to reset the range parameters.
507                 const_cast<OutputParams&>(runparams).par_begin = 0;
508                 const_cast<OutputParams&>(runparams).par_end = 0;
509         }
510
511         // if only_body
512         while (par != endpar) {
513                 ParagraphList::const_iterator lastpar = par;
514                 // well we have to check if we are in an inset with unlimited
515                 // length (all in one row) if that is true then we don't allow
516                 // any special options in the paragraph and also we don't allow
517                 // any environment other than the default layout of the
518                 // text class to be valid!
519                 if (!par->forceDefaultParagraphs()) {
520                         LyXLayout_ptr const & layout = par->layout();
521
522                         if (layout->intitle) {
523                                 if (already_title) {
524                                         lyxerr << "Error in latexParagraphs: You"
525                                                 " should not mix title layouts"
526                                                 " with normal ones." << endl;
527                                 } else if (!was_title) {
528                                         was_title = true;
529                                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
530                                                 os << "\\begin{"
531                                                     << from_ascii(tclass.titlename())
532                                                     << "}\n";
533                                                 texrow.newline();
534                                         }
535                                 }
536                         } else if (was_title && !already_title) {
537                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
538                                         os << "\\end{" << from_ascii(tclass.titlename())
539                                             << "}\n";
540                                 }
541                                 else {
542                                         os << "\\" << from_ascii(tclass.titlename())
543                                             << "\n";
544                                 }
545                                 texrow.newline();
546                                 already_title = true;
547                                 was_title = false;
548                         }
549
550                         if (layout->is_environment) {
551                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
552                                                 runparams, everypar);
553                         } else if (layout->isEnvironment() ||
554                                    !par->params().leftIndent().zero()) {
555                                 par = TeXEnvironment(buf, paragraphs, par, os,
556                                                      texrow, runparams);
557                         } else {
558                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
559                                                 runparams, everypar);
560                         }
561                 } else {
562                         par = TeXOnePar(buf, paragraphs, par, os, texrow,
563                                         runparams, everypar);
564                 }
565                 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
566                         break;
567         }
568         // It might be that we only have a title in this document
569         if (was_title && !already_title) {
570                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
571                         os << "\\end{" << from_ascii(tclass.titlename())
572                             << "}\n";
573                 }
574                 else {
575                         os << "\\" << from_ascii(tclass.titlename())
576                             << "\n";
577                                 }
578                 texrow.newline();
579         }
580 }
581
582
583 int switchEncoding(odocstream & os, BufferParams const & bparams,
584                    Encoding const & oldEnc, Encoding const & newEnc)
585 {
586         // FIXME thailatex does not support the inputenc package, so we
587         // ignore switches from/to tis620-0 encoding here. This does of
588         // course only work as long as the non-thai text contains ASCII
589         // only, but it is the best we can do.
590         if ((bparams.inputenc == "auto" || bparams.inputenc == "default") &&
591             oldEnc.name() != newEnc.name() &&
592             oldEnc.name() != "tis620-0" && newEnc.name() != "tis620-0") {
593                 lyxerr[Debug::LATEX] << "Changing LaTeX encoding from "
594                                      << oldEnc.name() << " to "
595                                      << newEnc.name() << endl;
596                 os << setEncoding(newEnc.iconvName());
597                 if (bparams.inputenc != "default") {
598                         docstring const inputenc(from_ascii(newEnc.latexName()));
599                         os << "\\inputencoding{" << inputenc << '}';
600                         return 16 + inputenc.length();
601                 }
602         }
603         return 0;
604 }
605
606 } // namespace lyx