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