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