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