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