2 * \file output_latex.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "output_latex.h"
16 #include "BufferParams.h"
21 #include "OutputParams.h"
22 #include "Paragraph.h"
23 #include "paragraph_funcs.h"
24 #include "ParagraphParameters.h"
28 #include "insets/InsetBibitem.h"
29 #include "insets/InsetOptArg.h"
31 #include "support/lstrings.h"
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);
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());
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)
67 LYXERR(Debug::LATEX) << "TeXDeeper... " << &*pit << endl;
68 ParagraphList::const_iterator par = pit;
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);
76 par = TeXOnePar(buf, paragraphs, par,
77 os, texrow, runparams);
80 LYXERR(Debug::LATEX) << "TeXDeeper...done " << endl;
86 ParagraphList::const_iterator
87 TeXEnvironment(Buffer const & buf,
88 ParagraphList const & paragraphs,
89 ParagraphList::const_iterator pit,
90 odocstream & os, TexRow & texrow,
91 OutputParams const & runparams)
93 LYXERR(Debug::LATEX) << "TeXEnvironment... " << &*pit << endl;
95 BufferParams const & bparams = buf.params();
97 Layout_ptr const & style = pit->layout();
99 Language const * const par_language = pit->getParLanguage(bparams);
100 Language const * const doc_language = bparams.language;
101 Language const * const prev_par_language =
102 (pit != paragraphs.begin())
103 ? boost::prior(pit)->getParLanguage(bparams)
105 if (par_language->babel() != prev_par_language->babel()) {
107 if (!lyxrc.language_command_end.empty() &&
108 prev_par_language->babel() != doc_language->babel() &&
109 !prev_par_language->babel().empty()) {
110 os << from_ascii(subst(
111 lyxrc.language_command_end,
113 prev_par_language->babel()))
118 if ((lyxrc.language_command_end.empty() ||
119 par_language->babel() != doc_language->babel()) &&
120 !par_language->babel().empty()) {
121 os << from_ascii(subst(
122 lyxrc.language_command_begin,
124 par_language->babel()))
130 bool leftindent_open = false;
131 if (!pit->params().leftIndent().zero()) {
132 os << "\\begin{LyXParagraphLeftIndent}{"
133 << from_ascii(pit->params().leftIndent().asLatexString())
136 leftindent_open = true;
139 if (style->isEnvironment()) {
140 os << "\\begin{" << from_ascii(style->latexname()) << '}';
141 if (style->optionalargs > 0) {
142 int ret = latexOptArgInsets(buf, *pit, os, runparams,
143 style->optionalargs);
149 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
151 << pit->params().labelWidthString()
153 } else if (style->labeltype == LABEL_BIBLIO) {
155 os << '{' << bibitemWidest(buf) << "}\n";
157 os << from_ascii(style->latexparam()) << '\n';
160 ParagraphList::const_iterator par = pit;
162 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
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)
170 } else if (par->params().depth() > pit->params().depth()) {
171 if (par->layout()->isParagraph()) {
174 // How to handle this? (Lgb)
175 //&& !suffixIs(os, "\n\n")
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.
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)
190 par = TeXDeeper(buf, paragraphs, par, os, texrow,
193 } while (par != paragraphs.end()
194 && par->layout() == pit->layout()
195 && par->params().depth() == pit->params().depth()
196 && par->params().leftIndent() == pit->params().leftIndent());
198 if (style->isEnvironment()) {
199 os << "\\end{" << from_ascii(style->latexname()) << "}\n";
203 if (leftindent_open) {
204 os << "\\end{LyXParagraphLeftIndent}\n";
208 if (par != paragraphs.end())
209 LYXERR(Debug::LATEX) << "TeXEnvironment...done " << &*par << endl;
216 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
217 odocstream & os, OutputParams const & runparams, int number)
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() == Inset::OPTARG_CODE) {
226 static_cast<InsetOptArg *>(it->inset);
227 lines += ins->latexOptional(buf, os, runparams);
237 ParagraphList::const_iterator
238 TeXOnePar(Buffer const & buf,
239 ParagraphList const & paragraphs,
240 ParagraphList::const_iterator pit,
241 odocstream & os, TexRow & texrow,
242 OutputParams const & runparams_in,
243 string const & everypar)
245 LYXERR(Debug::LATEX) << "TeXOnePar... " << &*pit << " '"
246 << everypar << "'" << endl;
247 BufferParams const & bparams = buf.params();
250 // In an inset with unlimited length (all in one row),
251 // force layout to default
252 if (!pit->forceDefaultParagraphs())
253 style = pit->layout();
255 style = bparams.getTextClass().defaultLayout();
257 OutputParams runparams = runparams_in;
258 runparams.moving_arg |= style->needprotect;
260 Language const * const par_language = pit->getParLanguage(bparams);
261 Language const * const doc_language = bparams.language;
262 Language const * const prev_par_language =
263 (pit != paragraphs.begin())
264 ? boost::prior(pit)->getParLanguage(bparams)
267 if (par_language->babel() != prev_par_language->babel()
268 // check if we already put language command in TeXEnvironment()
269 && !(style->isEnvironment()
270 && (pit == paragraphs.begin() ||
271 (boost::prior(pit)->layout() != pit->layout() &&
272 boost::prior(pit)->getDepth() <= pit->getDepth())
273 || boost::prior(pit)->getDepth() < pit->getDepth())))
275 if (!lyxrc.language_command_end.empty() &&
276 prev_par_language->babel() != doc_language->babel() &&
277 !prev_par_language->babel().empty())
279 os << from_ascii(subst(lyxrc.language_command_end,
281 prev_par_language->babel()))
286 if ((lyxrc.language_command_end.empty() ||
287 par_language->babel() != doc_language->babel()) &&
288 !par_language->babel().empty()) {
289 os << from_ascii(subst(
290 lyxrc.language_command_begin,
292 par_language->babel()))
298 // Switch file encoding if necessary
299 if (bparams.inputenc == "auto" &&
300 runparams.encoding->package() == Encoding::inputenc) {
301 // Look ahead for future encoding changes.
302 // We try to output them at the beginning of the paragraph,
303 // since the \inputencoding command is not allowed e.g. in
305 for (pos_type i = 0; i < pit->size(); ++i) {
306 char_type const c = pit->getChar(i);
311 // All characters before c are in the ASCII range, and
312 // c is non-ASCII (but no inset), so change the
313 // encoding to that required by the language of c.
314 Encoding const * const encoding =
315 pit->getFontSettings(bparams, i).language()->encoding();
316 if (encoding->package() == Encoding::inputenc &&
317 switchEncoding(os, bparams, false,
318 *(runparams.encoding), *encoding) > 0) {
319 runparams.encoding = encoding;
327 // In an inset with unlimited length (all in one row),
328 // don't allow any special options in the paragraph
329 if (!pit->forceDefaultParagraphs()) {
330 if (pit->params().startOfAppendix()) {
331 os << "\\appendix\n";
335 if (!pit->params().spacing().isDefault()
336 && (pit == paragraphs.begin()
337 || !boost::prior(pit)->hasSameLayout(*pit)))
339 os << from_ascii(pit->params().spacing().writeEnvirBegin())
344 if (style->isCommand()) {
350 switch (style->latextype) {
352 os << '\\' << from_ascii(style->latexname());
354 // Separate handling of optional argument inset.
355 if (style->optionalargs > 0) {
356 int ret = latexOptArgInsets(buf, *pit, os, runparams,
357 style->optionalargs);
364 os << from_ascii(style->latexparam());
366 case LATEX_ITEM_ENVIRONMENT:
367 case LATEX_LIST_ENVIRONMENT:
370 case LATEX_BIB_ENVIRONMENT:
371 // ignore this, the inset will write itself
377 Font const outerfont =
378 outerFont(std::distance(paragraphs.begin(), pit),
382 os << from_utf8(everypar);
383 bool need_par = pit->simpleTeXOnePar(buf, bparams, outerfont,
384 os, texrow, runparams);
386 // Make sure that \\par is done with the font of the last
387 // character if this has another size as the default.
388 // This is necessary because LaTeX (and LyX on the screen)
389 // calculates the space between the baselines according
390 // to this font. (Matthias)
392 // Is this really needed ? (Dekel)
393 // We do not need to use to change the font for the last paragraph
398 ? pit->getLayoutFont(bparams, outerfont)
399 : pit->getFont(bparams, pit->size() - 1, outerfont));
401 bool is_command = style->isCommand();
403 if (style->resfont.size() != font.size()
404 && boost::next(pit) != paragraphs.end()
408 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
409 } else if (need_par) {
411 } else if (is_command)
414 bool pending_newline = false;
415 switch (style->latextype) {
416 case LATEX_ITEM_ENVIRONMENT:
417 case LATEX_LIST_ENVIRONMENT:
418 if (boost::next(pit) != paragraphs.end()
419 && (pit->params().depth() < boost::next(pit)->params().depth()))
420 pending_newline = true;
422 case LATEX_ENVIRONMENT: {
423 // if its the last paragraph of the current environment
424 // skip it otherwise fall through
425 ParagraphList::const_iterator next = boost::next(pit);
427 if (next != paragraphs.end()
428 && (next->layout() != pit->layout()
429 || next->params().depth() != pit->params().depth()))
433 // fall through possible
435 // we don't need it for the last paragraph!!!
436 if (boost::next(pit) != paragraphs.end())
437 pending_newline = true;
440 if (!pit->forceDefaultParagraphs()) {
441 if (!pit->params().spacing().isDefault()
442 && (boost::next(pit) == paragraphs.end()
443 || !boost::next(pit)->hasSameLayout(*pit)))
445 if (pending_newline) {
449 os << from_ascii(pit->params().spacing().writeEnvirEnd());
450 pending_newline = true;
454 if (boost::next(pit) == paragraphs.end()
455 && par_language->babel() != doc_language->babel()) {
456 // Since \selectlanguage write the language to the aux file,
457 // we need to reset the language at the end of footnote or
460 if (pending_newline) {
464 if (lyxrc.language_command_end.empty()) {
465 if (!doc_language->babel().empty()) {
466 os << from_ascii(subst(
467 lyxrc.language_command_begin,
469 doc_language->babel()));
470 pending_newline = true;
472 } else if (!par_language->babel().empty()) {
473 os << from_ascii(subst(
474 lyxrc.language_command_end,
476 par_language->babel()));
477 pending_newline = true;
481 if (pending_newline) {
485 runparams_in.encoding = runparams.encoding;
487 // we don't need it for the last paragraph!!!
488 // Note from JMarc: we will re-add a \n explicitely in
489 // TeXEnvironment, because it is needed in this case
490 if (boost::next(pit) != paragraphs.end()) {
495 if (boost::next(pit) != paragraphs.end())
496 LYXERR(Debug::LATEX) << "TeXOnePar...done " << &*boost::next(pit) << endl;
504 // LaTeX all paragraphs
505 void latexParagraphs(Buffer const & buf,
506 ParagraphList const & paragraphs,
509 OutputParams const & runparams,
510 string const & everypar)
512 bool was_title = false;
513 bool already_title = false;
514 TextClass const & tclass = buf.params().getTextClass();
515 ParagraphList::const_iterator par = paragraphs.begin();
516 ParagraphList::const_iterator endpar = paragraphs.end();
518 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
519 // if only part of the paragraphs will be outputed
520 if (runparams.par_begin != runparams.par_end) {
521 par = boost::next(paragraphs.begin(), runparams.par_begin);
522 endpar = boost::next(paragraphs.begin(), runparams.par_end);
523 // runparams will be passed to nested paragraphs, so
524 // we have to reset the range parameters.
525 const_cast<OutputParams&>(runparams).par_begin = 0;
526 const_cast<OutputParams&>(runparams).par_end = 0;
530 while (par != endpar) {
531 ParagraphList::const_iterator lastpar = par;
532 // well we have to check if we are in an inset with unlimited
533 // length (all in one row) if that is true then we don't allow
534 // any special options in the paragraph and also we don't allow
535 // any environment other than the default layout of the
536 // text class to be valid!
537 if (!par->forceDefaultParagraphs()) {
538 Layout_ptr const & layout = par->layout();
540 if (layout->intitle) {
542 lyxerr << "Error in latexParagraphs: You"
543 " should not mix title layouts"
544 " with normal ones." << endl;
545 } else if (!was_title) {
547 if (tclass.titletype() == TITLE_ENVIRONMENT) {
549 << from_ascii(tclass.titlename())
554 } else if (was_title && !already_title) {
555 if (tclass.titletype() == TITLE_ENVIRONMENT) {
556 os << "\\end{" << from_ascii(tclass.titlename())
560 os << "\\" << from_ascii(tclass.titlename())
564 already_title = true;
568 if (layout->is_environment) {
569 par = TeXOnePar(buf, paragraphs, par, os, texrow,
570 runparams, everypar);
571 } else if (layout->isEnvironment() ||
572 !par->params().leftIndent().zero()) {
573 par = TeXEnvironment(buf, paragraphs, par, os,
576 par = TeXOnePar(buf, paragraphs, par, os, texrow,
577 runparams, everypar);
580 par = TeXOnePar(buf, paragraphs, par, os, texrow,
581 runparams, everypar);
583 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
586 // It might be that we only have a title in this document
587 if (was_title && !already_title) {
588 if (tclass.titletype() == TITLE_ENVIRONMENT) {
589 os << "\\end{" << from_ascii(tclass.titlename())
593 os << "\\" << from_ascii(tclass.titlename())
601 int switchEncoding(odocstream & os, BufferParams const & bparams,
602 bool moving_arg, Encoding const & oldEnc,
603 Encoding const & newEnc)
605 if ((bparams.inputenc != "auto" || moving_arg)
606 && bparams.inputenc != "default")
609 // Do nothing if the encoding is unchanged.
610 if (oldEnc.name() == newEnc.name())
613 // FIXME We ignore encoding switches from/to encodings that do
614 // neither support the inputenc package nor the CJK package here.
615 // This does of course only work in special cases (e.g. switch from
616 // tis620-0 to latin1, but the text in latin1 contains ASCII only,
617 // but it is the best we can do
618 if (oldEnc.package() == Encoding::none
619 || newEnc.package() == Encoding::none)
622 LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
623 << oldEnc.name() << " to "
624 << newEnc.name() << endl;
625 os << setEncoding(newEnc.iconvName());
626 if (bparams.inputenc == "default")
629 docstring const inputenc(from_ascii(newEnc.latexName()));
630 switch (newEnc.package()) {
633 case Encoding::inputenc: {
634 int count = inputenc.length();
635 if (oldEnc.package() == Encoding::CJK) {
639 os << "\\inputencoding{" << inputenc << '}';
642 case Encoding::CJK: {
643 int count = inputenc.length();
644 if (oldEnc.package() == Encoding::CJK) {
648 os << "\\begin{CJK}{" << inputenc << "}{}";
652 // Dead code to avoid a warning: