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"
19 #include "InsetList.h"
23 #include "OutputParams.h"
24 #include "Paragraph.h"
25 #include "paragraph_funcs.h"
26 #include "ParagraphParameters.h"
30 #include "insets/InsetBibitem.h"
31 #include "insets/InsetOptArg.h"
33 #include "support/lstrings.h"
35 #include <boost/next_prior.hpp>
49 ParagraphList::const_iterator
50 TeXEnvironment(Buffer const & buf,
51 ParagraphList const & paragraphs,
52 ParagraphList::const_iterator pit,
53 odocstream & os, TexRow & texrow,
54 OutputParams const & runparams);
56 ParagraphList::const_iterator
57 TeXOnePar(Buffer const & buf,
58 ParagraphList const & paragraphs,
59 ParagraphList::const_iterator pit,
60 odocstream & os, TexRow & texrow,
61 OutputParams const & runparams,
62 string const & everypar = string());
65 ParagraphList::const_iterator
66 TeXDeeper(Buffer const & buf,
67 ParagraphList const & paragraphs,
68 ParagraphList::const_iterator pit,
69 odocstream & os, TexRow & texrow,
70 OutputParams const & runparams)
72 LYXERR(Debug::LATEX) << "TeXDeeper... " << &*pit << endl;
73 ParagraphList::const_iterator par = pit;
75 while (par != paragraphs.end() &&
76 par->params().depth() == pit->params().depth()) {
77 if (par->layout()->isEnvironment()) {
78 par = TeXEnvironment(buf, paragraphs, par,
79 os, texrow, runparams);
81 par = TeXOnePar(buf, paragraphs, par,
82 os, texrow, runparams);
85 LYXERR(Debug::LATEX) << "TeXDeeper...done " << endl;
91 ParagraphList::const_iterator
92 TeXEnvironment(Buffer const & buf,
93 ParagraphList const & paragraphs,
94 ParagraphList::const_iterator pit,
95 odocstream & os, TexRow & texrow,
96 OutputParams const & runparams)
98 LYXERR(Debug::LATEX) << "TeXEnvironment... " << &*pit << endl;
100 BufferParams const & bparams = buf.params();
102 LayoutPtr const & style = pit->layout();
104 Language const * const par_language = pit->getParLanguage(bparams);
105 Language const * const doc_language = bparams.language;
106 Language const * const prev_par_language =
107 (pit != paragraphs.begin())
108 ? boost::prior(pit)->getParLanguage(bparams)
110 if (par_language->babel() != prev_par_language->babel()) {
112 if (!lyxrc.language_command_end.empty() &&
113 prev_par_language->babel() != doc_language->babel() &&
114 !prev_par_language->babel().empty()) {
115 os << from_ascii(subst(
116 lyxrc.language_command_end,
118 prev_par_language->babel()))
123 if ((lyxrc.language_command_end.empty() ||
124 par_language->babel() != doc_language->babel()) &&
125 !par_language->babel().empty()) {
126 os << from_ascii(subst(
127 lyxrc.language_command_begin,
129 par_language->babel()))
135 bool leftindent_open = false;
136 if (!pit->params().leftIndent().zero()) {
137 os << "\\begin{LyXParagraphLeftIndent}{"
138 << from_ascii(pit->params().leftIndent().asLatexString())
141 leftindent_open = true;
144 if (style->isEnvironment()) {
145 os << "\\begin{" << from_ascii(style->latexname()) << '}';
146 if (style->optionalargs > 0) {
147 int ret = latexOptArgInsets(buf, *pit, os, runparams,
148 style->optionalargs);
154 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
156 << pit->params().labelWidthString()
158 } else if (style->labeltype == LABEL_BIBLIO) {
160 os << '{' << bibitemWidest(buf) << "}\n";
162 os << from_ascii(style->latexparam()) << '\n';
165 ParagraphList::const_iterator par = pit;
167 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
169 if (par == paragraphs.end()) {
170 // Make sure that the last paragraph is
171 // correctly terminated (because TeXOnePar does
172 // not add a \n in this case)
175 } else if (par->params().depth() > pit->params().depth()) {
176 if (par->layout()->isParagraph()) {
178 // How to handle this? (Lgb)
179 //&& !suffixIs(os, "\n\n")
182 // There should be at least one '\n' already
183 // but we need there to be two for Standard
184 // paragraphs that are depth-increment'ed to be
185 // output correctly. However, tables can
186 // also be paragraphs so don't adjust them.
189 // Will it ever harm to have one '\n' too
190 // many? i.e. that we sometimes will have
191 // three in a row. (Lgb)
195 par = TeXDeeper(buf, paragraphs, par, os, texrow,
198 } while (par != paragraphs.end()
199 && par->layout() == pit->layout()
200 && par->params().depth() == pit->params().depth()
201 && par->params().leftIndent() == pit->params().leftIndent());
203 if (style->isEnvironment()) {
204 os << "\\end{" << from_ascii(style->latexname()) << "}\n";
208 if (leftindent_open) {
209 os << "\\end{LyXParagraphLeftIndent}\n";
213 if (par != paragraphs.end()) {
214 LYXERR(Debug::LATEX) << "TeXEnvironment...done " << &*par << endl;
222 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
223 odocstream & os, OutputParams const & runparams, int number)
227 InsetList::const_iterator it = par.insetList().begin();
228 InsetList::const_iterator end = par.insetList().end();
229 for (; it != end && number > 0 ; ++it) {
230 if (it->inset->lyxCode() == OPTARG_CODE) {
232 static_cast<InsetOptArg *>(it->inset);
233 lines += ins->latexOptional(buf, os, runparams);
243 ParagraphList::const_iterator
244 TeXOnePar(Buffer const & buf,
245 ParagraphList const & paragraphs,
246 ParagraphList::const_iterator pit,
247 odocstream & os, TexRow & texrow,
248 OutputParams const & runparams_in,
249 string const & everypar)
251 LYXERR(Debug::LATEX) << "TeXOnePar... " << &*pit << " '"
252 << everypar << "'" << endl;
253 BufferParams const & bparams = buf.params();
256 // In an inset with unlimited length (all in one row),
257 // force layout to default
258 if (!pit->forceDefaultParagraphs())
259 style = pit->layout();
261 style = bparams.getTextClass().defaultLayout();
263 OutputParams runparams = runparams_in;
264 runparams.moving_arg |= style->needprotect;
266 // This paragraph's language
267 Language const * const par_language = pit->getParLanguage(bparams);
268 // The document's language
269 Language const * const doc_language = bparams.language;
270 // The language that was in effect when the environemnt this paragraph is
271 // inside of was opened
272 Language const * const outer_language =
273 (runparams.local_font != 0) ?
274 runparams.local_font->language() : doc_language;
275 // The previous language that was in effect is either the language of
276 // the previous paragraph, if there is one, or else the outer language
277 // if there is no previous paragraph
278 Language const * const prev_language =
279 (pit != paragraphs.begin()) ?
280 boost::prior(pit)->getParLanguage(bparams) : outer_language;
282 if (par_language->babel() != prev_language->babel()
283 // check if we already put language command in TeXEnvironment()
284 && !(style->isEnvironment()
285 && (pit == paragraphs.begin() ||
286 (boost::prior(pit)->layout() != pit->layout() &&
287 boost::prior(pit)->getDepth() <= pit->getDepth())
288 || boost::prior(pit)->getDepth() < pit->getDepth())))
290 if (!lyxrc.language_command_end.empty() &&
291 prev_language->babel() != outer_language->babel() &&
292 !prev_language->babel().empty())
294 os << from_ascii(subst(lyxrc.language_command_end,
296 prev_language->babel()))
301 // We need to open a new language if we couldn't close the previous
302 // one (because there's no language_command_end); and even if we closed
303 // the previous one, if the current language is different than the
304 // outer_language (which is currently in effect once the previous one
306 if ((lyxrc.language_command_end.empty() ||
307 par_language->babel() != outer_language->babel()) &&
308 !par_language->babel().empty()) {
309 // If we're inside an inset, and that inset is within an \L or \R
310 // (or equivalents), then within the inset, too, any opposite
311 // language paragraph should appear within an \L or \R (in addition
312 // to, outside of, the normal language switch commands).
313 // This behavior is not correct for ArabTeX, though.
314 if ( // not for ArabTeX
315 (par_language->lang() != "arabic_arabtex" &&
316 outer_language->lang() != "arabic_arabtex") &&
317 // are we in an inset?
318 runparams.local_font != 0 &&
319 // is the inset within an \L or \R?
321 // FIXME: currently, we don't check this; this means that
322 // we'll have unnnecessary \L and \R commands, but that
323 // doesn't seem to hurt (though latex will complain)
325 // is this paragraph in the opposite direction?
326 runparams.local_font->isRightToLeft() !=
327 par_language->rightToLeft()
329 // FIXME: I don't have a working copy of the Arabi package, so
330 // I'm not sure if the farsi and arabic_arabi stuff is correct
332 if (par_language->lang() == "farsi")
334 else if (outer_language->lang() == "farsi")
336 else if (par_language->lang() == "arabic_arabi")
338 else if (outer_language->lang() == "arabic_arabi")
340 // remaining RTL languages currently is hebrew
341 else if (par_language->rightToLeft())
346 os << from_ascii(subst(
347 lyxrc.language_command_begin,
349 par_language->babel()))
355 // Switch file encoding if necessary; no need to do this for "default"
356 // encoding, since this only affects the position of the outputted
357 // \inputencoding command; the encoding switch will occur when necessary
358 if (bparams.inputenc == "auto" &&
359 runparams.encoding->package() == Encoding::inputenc) {
360 // Look ahead for future encoding changes.
361 // We try to output them at the beginning of the paragraph,
362 // since the \inputencoding command is not allowed e.g. in
364 for (pos_type i = 0; i < pit->size(); ++i) {
365 char_type const c = pit->getChar(i);
370 // All characters before c are in the ASCII range, and
371 // c is non-ASCII (but no inset), so change the
372 // encoding to that required by the language of c.
373 Encoding const * const encoding =
374 pit->getFontSettings(bparams, i).language()->encoding();
375 pair<bool, int> enc_switch = switchEncoding(os, bparams, false,
376 *(runparams.encoding), *encoding);
377 if (encoding->package() == Encoding::inputenc && enc_switch.first) {
378 runparams.encoding = encoding;
379 if (enc_switch.second > 0) {
388 // In an inset with unlimited length (all in one row),
389 // don't allow any special options in the paragraph
390 if (!pit->forceDefaultParagraphs()) {
391 if (pit->params().startOfAppendix()) {
392 os << "\\appendix\n";
396 if (!pit->params().spacing().isDefault()
397 && (pit == paragraphs.begin()
398 || !boost::prior(pit)->hasSameLayout(*pit)))
400 os << from_ascii(pit->params().spacing().writeEnvirBegin())
405 if (style->isCommand()) {
411 switch (style->latextype) {
413 os << '\\' << from_ascii(style->latexname());
415 // Separate handling of optional argument inset.
416 if (style->optionalargs > 0) {
417 int ret = latexOptArgInsets(buf, *pit, os, runparams,
418 style->optionalargs);
425 os << from_ascii(style->latexparam());
427 case LATEX_ITEM_ENVIRONMENT:
428 case LATEX_LIST_ENVIRONMENT:
431 case LATEX_BIB_ENVIRONMENT:
432 // ignore this, the inset will write itself
438 Font const outerfont =
439 outerFont(std::distance(paragraphs.begin(), pit),
443 os << from_utf8(everypar);
444 bool need_par = pit->latex(buf, bparams, outerfont,
445 os, texrow, runparams);
447 // Make sure that \\par is done with the font of the last
448 // character if this has another size as the default.
449 // This is necessary because LaTeX (and LyX on the screen)
450 // calculates the space between the baselines according
451 // to this font. (Matthias)
453 // Is this really needed ? (Dekel)
454 // We do not need to use to change the font for the last paragraph
459 ? pit->getLayoutFont(bparams, outerfont)
460 : pit->getFont(bparams, pit->size() - 1, outerfont));
462 bool is_command = style->isCommand();
464 if (style->resfont.size() != font.size()
465 && boost::next(pit) != paragraphs.end()
469 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
470 } else if (need_par) {
472 } else if (is_command)
475 bool pending_newline = false;
476 switch (style->latextype) {
477 case LATEX_ITEM_ENVIRONMENT:
478 case LATEX_LIST_ENVIRONMENT:
479 if (boost::next(pit) != paragraphs.end()
480 && (pit->params().depth() < boost::next(pit)->params().depth()))
481 pending_newline = true;
483 case LATEX_ENVIRONMENT: {
484 // if its the last paragraph of the current environment
485 // skip it otherwise fall through
486 ParagraphList::const_iterator next = boost::next(pit);
488 if (next != paragraphs.end()
489 && (next->layout() != pit->layout()
490 || next->params().depth() != pit->params().depth()))
494 // fall through possible
496 // we don't need it for the last paragraph!!!
497 if (boost::next(pit) != paragraphs.end())
498 pending_newline = true;
501 if (!pit->forceDefaultParagraphs()) {
502 if (!pit->params().spacing().isDefault()
503 && (boost::next(pit) == paragraphs.end()
504 || !boost::next(pit)->hasSameLayout(*pit)))
506 if (pending_newline) {
510 os << from_ascii(pit->params().spacing().writeEnvirEnd());
511 pending_newline = true;
515 // Closing the language is needed for the last paragraph; it is also
516 // needed if we're within an \L or \R that we may have opened above (not
517 // necessarily in this paragraph) and are about to close.
518 bool closing_rtl_ltr_environment =
520 (par_language->lang() != "arabic_arabtex" &&
521 outer_language->lang() != "arabic_arabtex") &&
522 // have we opened and \L or \R environment?
523 runparams.local_font != 0 &&
524 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
525 // are we about to close the language?
526 ((boost::next(pit) != paragraphs.end() &&
527 par_language->babel() !=
528 (boost::next(pit)->getParLanguage(bparams))->babel()) ||
529 (boost::next(pit) == paragraphs.end() &&
530 par_language->babel() != outer_language->babel()));
532 if (closing_rtl_ltr_environment || (boost::next(pit) == paragraphs.end()
533 && par_language->babel() != outer_language->babel())) {
534 // Since \selectlanguage write the language to the aux file,
535 // we need to reset the language at the end of footnote or
538 if (pending_newline) {
542 if (lyxrc.language_command_end.empty()) {
543 if (!prev_language->babel().empty()) {
544 os << from_ascii(subst(
545 lyxrc.language_command_begin,
547 prev_language->babel()));
548 pending_newline = true;
550 } else if (!par_language->babel().empty()) {
551 os << from_ascii(subst(
552 lyxrc.language_command_end,
554 par_language->babel()));
555 pending_newline = true;
558 if (closing_rtl_ltr_environment)
561 if (pending_newline && !runparams.verbatim) {
566 // If this is the last paragraph, and a local_font was set upon entering
567 // the inset, the encoding should be set back to that local_font's
568 // encoding. We don't use switchEncoding(), because no explicit encoding
569 // switch command is needed, since latex will automatically revert to it
570 // when this inset closes.
571 // This switch is only necessary if we're using "auto" or "default"
573 if (boost::next(pit) == paragraphs.end() && runparams_in.local_font != 0) {
574 runparams_in.encoding = runparams_in.local_font->language()->encoding();
575 if (bparams.inputenc == "auto" || bparams.inputenc == "default")
576 os << setEncoding(runparams_in.encoding->iconvName());
579 // Otherwise, the current encoding should be set for the next paragraph.
581 runparams_in.encoding = runparams.encoding;
584 // we don't need it for the last paragraph!!!
585 // Note from JMarc: we will re-add a \n explicitely in
586 // TeXEnvironment, because it is needed in this case
587 if (boost::next(pit) != paragraphs.end()) {
592 if (boost::next(pit) != paragraphs.end()) {
593 LYXERR(Debug::LATEX) << "TeXOnePar...done " << &*boost::next(pit) << endl;
602 // LaTeX all paragraphs
603 void latexParagraphs(Buffer const & buf,
604 ParagraphList const & paragraphs,
607 OutputParams const & runparams,
608 string const & everypar)
610 bool was_title = false;
611 bool already_title = false;
612 TextClass const & tclass = buf.params().getTextClass();
613 ParagraphList::const_iterator par = paragraphs.begin();
614 ParagraphList::const_iterator endpar = paragraphs.end();
616 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
617 // if only part of the paragraphs will be outputed
618 if (runparams.par_begin != runparams.par_end) {
619 par = boost::next(paragraphs.begin(), runparams.par_begin);
620 endpar = boost::next(paragraphs.begin(), runparams.par_end);
621 // runparams will be passed to nested paragraphs, so
622 // we have to reset the range parameters.
623 const_cast<OutputParams&>(runparams).par_begin = 0;
624 const_cast<OutputParams&>(runparams).par_end = 0;
628 while (par != endpar) {
629 ParagraphList::const_iterator lastpar = par;
630 // well we have to check if we are in an inset with unlimited
631 // length (all in one row) if that is true then we don't allow
632 // any special options in the paragraph and also we don't allow
633 // any environment other than the default layout of the
634 // text class to be valid!
635 if (!par->forceDefaultParagraphs()) {
636 LayoutPtr const & layout = par->layout();
638 if (layout->intitle) {
640 lyxerr << "Error in latexParagraphs: You"
641 " should not mix title layouts"
642 " with normal ones." << endl;
643 } else if (!was_title) {
645 if (tclass.titletype() == TITLE_ENVIRONMENT) {
647 << from_ascii(tclass.titlename())
652 } else if (was_title && !already_title) {
653 if (tclass.titletype() == TITLE_ENVIRONMENT) {
654 os << "\\end{" << from_ascii(tclass.titlename())
658 os << "\\" << from_ascii(tclass.titlename())
662 already_title = true;
666 if (layout->is_environment) {
667 par = TeXOnePar(buf, paragraphs, par, os, texrow,
668 runparams, everypar);
669 } else if (layout->isEnvironment() ||
670 !par->params().leftIndent().zero()) {
671 par = TeXEnvironment(buf, paragraphs, par, os,
674 par = TeXOnePar(buf, paragraphs, par, os, texrow,
675 runparams, everypar);
678 par = TeXOnePar(buf, paragraphs, par, os, texrow,
679 runparams, everypar);
681 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
684 // It might be that we only have a title in this document
685 if (was_title && !already_title) {
686 if (tclass.titletype() == TITLE_ENVIRONMENT) {
687 os << "\\end{" << from_ascii(tclass.titlename())
691 os << "\\" << from_ascii(tclass.titlename())
699 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
700 bool moving_arg, Encoding const & oldEnc,
701 Encoding const & newEnc)
703 if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
705 return make_pair(false, 0);
707 // Do nothing if the encoding is unchanged.
708 if (oldEnc.name() == newEnc.name())
709 return make_pair(false, 0);
711 // FIXME We ignore encoding switches from/to encodings that do
712 // neither support the inputenc package nor the CJK package here.
713 // This does of course only work in special cases (e.g. switch from
714 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
715 // but it is the best we can do
716 if (oldEnc.package() == Encoding::none
717 || newEnc.package() == Encoding::none)
718 return make_pair(false, 0);
720 LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
721 << oldEnc.name() << " to "
722 << newEnc.name() << endl;
723 os << setEncoding(newEnc.iconvName());
724 if (bparams.inputenc == "default")
725 return make_pair(true, 0);
727 docstring const inputenc(from_ascii(newEnc.latexName()));
728 switch (newEnc.package()) {
730 // shouldn't ever reach here, see above
731 return make_pair(true, 0);
732 case Encoding::inputenc: {
733 int count = inputenc.length();
734 if (oldEnc.package() == Encoding::CJK) {
738 os << "\\inputencoding{" << inputenc << '}';
739 return make_pair(true, count + 16);
741 case Encoding::CJK: {
742 int count = inputenc.length();
743 if (oldEnc.package() == Encoding::CJK) {
747 os << "\\begin{CJK}{" << inputenc << "}{}";
748 return make_pair(true, count + 15);
751 // Dead code to avoid a warning:
752 return make_pair(true, 0);