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