]> git.lyx.org Git - lyx.git/blob - src/output_latex.C
0cde7bef10d2fc287709e367001901f57feffcd8
[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 (bparams.inputenc == "auto" &&
296             language->encoding() != previous_language->encoding()) {
297                 os << "\\inputencoding{"
298                    << lyx::from_ascii(language->encoding()->latexName())
299                    << "}\n";
300                 texrow.newline();
301         }
302
303         // In an an inset with unlimited length (all in one row),
304         // don't allow any special options in the paragraph
305         if (!pit->forceDefaultParagraphs()) {
306                 if (pit->params().startOfAppendix()) {
307                         os << "\\appendix\n";
308                         texrow.newline();
309                 }
310
311                 if (!pit->params().spacing().isDefault()
312                         && (pit == paragraphs.begin()
313                             || !boost::prior(pit)->hasSameLayout(*pit)))
314                 {
315                         os << lyx::from_ascii(pit->params().spacing().writeEnvirBegin())
316                             << '\n';
317                         texrow.newline();
318                 }
319
320                 if (style->isCommand()) {
321                         os << '\n';
322                         texrow.newline();
323                 }
324
325                 if (further_blank_line) {
326                         os << '\n';
327                         texrow.newline();
328                 }
329         }
330
331         switch (style->latextype) {
332         case LATEX_COMMAND:
333                 os << '\\' << lyx::from_ascii(style->latexname());
334
335                 // Separate handling of optional argument inset.
336                 if (style->optionalargs > 0) {
337                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
338                                                     style->optionalargs);
339                         while (ret > 0) {
340                                 texrow.newline();
341                                 --ret;
342                         }
343                 }
344                 else
345                         os << lyx::from_ascii(style->latexparam());
346                 break;
347         case LATEX_ITEM_ENVIRONMENT:
348         case LATEX_LIST_ENVIRONMENT:
349                 os << "\\item ";
350                 break;
351         case LATEX_BIB_ENVIRONMENT:
352                 // ignore this, the inset will write itself
353                 break;
354         default:
355                 break;
356         }
357
358         // FIXME UNICODE
359         os << lyx::from_utf8(everypar);
360         bool need_par = pit->simpleTeXOnePar(buf, bparams,
361                                              outerFont(std::distance(paragraphs.begin(), pit), paragraphs),
362                                              os, texrow, runparams);
363
364         // Make sure that \\par is done with the font of the last
365         // character if this has another size as the default.
366         // This is necessary because LaTeX (and LyX on the screen)
367         // calculates the space between the baselines according
368         // to this font. (Matthias)
369         //
370         // Is this really needed ? (Dekel)
371         // We do not need to use to change the font for the last paragraph
372         // or for a command.
373         LyXFont const outerfont =
374                 outerFont(std::distance(paragraphs.begin(), pit),
375                           paragraphs);
376
377         LyXFont const font =
378                 (pit->empty()
379                  ? pit->getLayoutFont(bparams, outerfont)
380                  : pit->getFont(bparams, pit->size() - 1, outerfont));
381
382         bool is_command = style->isCommand();
383
384         if (style->resfont.size() != font.size()
385             && boost::next(pit) != paragraphs.end()
386             && !is_command) {
387                 if (!need_par)
388                         os << '{';
389                 os << "\\" << lyx::from_ascii(font.latexSize()) << " \\par}";
390         } else if (need_par) {
391                 os << "\\par}";
392         } else if (is_command)
393                 os << '}';
394
395         switch (style->latextype) {
396         case LATEX_ITEM_ENVIRONMENT:
397         case LATEX_LIST_ENVIRONMENT:
398                 if (boost::next(pit) != paragraphs.end()
399                     && (pit->params().depth() < boost::next(pit)->params().depth())) {
400                         os << '\n';
401                         texrow.newline();
402                 }
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                         os << '\n';
420                         texrow.newline();
421                 }
422         }
423
424         if (!pit->forceDefaultParagraphs()) {
425                 further_blank_line = false;
426
427                 if (further_blank_line) {
428                         os << '\n';
429                         texrow.newline();
430                 }
431
432                 if (!pit->params().spacing().isDefault()
433                         && (boost::next(pit) == paragraphs.end()
434                             || !boost::next(pit)->hasSameLayout(*pit)))
435                 {
436                         os << lyx::from_ascii(pit->params().spacing().writeEnvirEnd())
437                            << '\n';
438                         texrow.newline();
439                 }
440         }
441
442         if (boost::next(pit) == paragraphs.end()
443             && language->babel() != doc_language->babel()) {
444                 // Since \selectlanguage write the language to the aux file,
445                 // we need to reset the language at the end of footnote or
446                 // float.
447
448                 if (lyxrc.language_command_end.empty())
449                         os << lyx::from_ascii(subst(
450                                 lyxrc.language_command_begin,
451                                 "$$lang",
452                                 doc_language->babel()))
453                            << endl;
454                 else
455                         os << lyx::from_ascii(subst(
456                                 lyxrc.language_command_end,
457                                 "$$lang",
458                                 language->babel()))
459                            << endl;
460                 texrow.newline();
461         }
462
463         // we don't need it for the last paragraph!!!
464         // Note from JMarc: we will re-add a \n explicitely in
465         // TeXEnvironment, because it is needed in this case
466         if (boost::next(pit) != paragraphs.end()) {
467                 os << '\n';
468                 texrow.newline();
469         }
470
471         if (boost::next(pit) != paragraphs.end() &&
472             lyxerr.debugging(Debug::LATEX))
473                 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
474         return ++pit;
475 }
476
477 } // anon namespace
478
479
480 // LaTeX all paragraphs
481 void latexParagraphs(Buffer const & buf,
482                      ParagraphList const & paragraphs,
483                      odocstream & os,
484                      TexRow & texrow,
485                      OutputParams const & runparams,
486                      string const & everypar)
487 {
488         bool was_title = false;
489         bool already_title = false;
490         LyXTextClass const & tclass = buf.params().getLyXTextClass();
491         ParagraphList::const_iterator par = paragraphs.begin();
492         ParagraphList::const_iterator endpar = paragraphs.end();
493
494         BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
495         // if only part of the paragraphs will be outputed
496         if (runparams.par_begin !=  runparams.par_end) {
497                 par = boost::next(paragraphs.begin(), runparams.par_begin);
498                 endpar = boost::next(paragraphs.begin(), runparams.par_end);
499                 // runparams will be passed to nested paragraphs, so
500                 // we have to reset the range parameters.
501                 const_cast<OutputParams&>(runparams).par_begin = 0;
502                 const_cast<OutputParams&>(runparams).par_end = 0;
503         }
504
505         // if only_body
506         while (par != endpar) {
507                 ParagraphList::const_iterator lastpar = par;
508                 // well we have to check if we are in an inset with unlimited
509                 // length (all in one row) if that is true then we don't allow
510                 // any special options in the paragraph and also we don't allow
511                 // any environment other then "Standard" to be valid!
512                 if (!par->forceDefaultParagraphs()) {
513                         LyXLayout_ptr const & layout = par->layout();
514
515                         if (layout->intitle) {
516                                 if (already_title) {
517                                         lyxerr << "Error in latexParagraphs: You"
518                                                 " should not mix title layouts"
519                                                 " with normal ones." << endl;
520                                 } else if (!was_title) {
521                                         was_title = true;
522                                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
523                                                 os << "\\begin{"
524                                                     << lyx::from_ascii(tclass.titlename())
525                                                     << "}\n";
526                                                 texrow.newline();
527                                         }
528                                 }
529                         } else if (was_title && !already_title) {
530                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
531                                         os << "\\end{" << lyx::from_ascii(tclass.titlename())
532                                             << "}\n";
533                                 }
534                                 else {
535                                         os << "\\" << lyx::from_ascii(tclass.titlename())
536                                             << "\n";
537                                 }
538                                 texrow.newline();
539                                 already_title = true;
540                                 was_title = false;
541                         }
542
543                         if (layout->is_environment) {
544                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
545                                                 runparams, everypar);
546                         } else if (layout->isEnvironment() ||
547                                 !par->params().leftIndent().zero())
548                         {
549                                 par = TeXEnvironment(buf, paragraphs, par, os,
550                                                      texrow, runparams);
551                         } else {
552                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
553                                                 runparams, everypar);
554                         }
555                 } else {
556                         par = TeXOnePar(buf, paragraphs, par, os, texrow,
557                                         runparams, everypar);
558                 }
559                 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
560                         break;
561         }
562         // It might be that we only have a title in this document
563         if (was_title && !already_title) {
564                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
565                         os << "\\end{" << lyx::from_ascii(tclass.titlename())
566                             << "}\n";
567                 }
568                 else {
569                         os << "\\" << lyx::from_ascii(tclass.titlename())
570                             << "\n";
571                                 }
572                 texrow.newline();
573         }
574 }