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 if (runparams_in.verbatim) {
257 int const dist = std::distance(paragraphs.begin(), pit);
258 Font const outerfont = outerFont(dist, paragraphs);
260 // No newline if only one paragraph in this lyxtext
266 /*bool need_par = */ pit->latex(buf, bparams, outerfont,
267 os, texrow, runparams_in);
272 // In an inset with unlimited length (all in one row),
273 // force layout to default
274 if (!pit->forceDefaultParagraphs())
275 style = pit->layout();
277 style = bparams.getTextClass().defaultLayout();
279 OutputParams runparams = runparams_in;
280 runparams.moving_arg |= style->needprotect;
282 // This paragraph's language
283 Language const * const par_language = pit->getParLanguage(bparams);
284 // The document's language
285 Language const * const doc_language = bparams.language;
286 // The language that was in effect when the environemnt this paragraph is
287 // inside of was opened
288 Language const * const outer_language =
289 (runparams.local_font != 0) ?
290 runparams.local_font->language() : doc_language;
291 // The previous language that was in effect is either the language of
292 // the previous paragraph, if there is one, or else the outer language
293 // if there is no previous paragraph
294 Language const * const prev_language =
295 (pit != paragraphs.begin()) ?
296 boost::prior(pit)->getParLanguage(bparams) : outer_language;
298 if (par_language->babel() != prev_language->babel()
299 // check if we already put language command in TeXEnvironment()
300 && !(style->isEnvironment()
301 && (pit == paragraphs.begin() ||
302 (boost::prior(pit)->layout() != pit->layout() &&
303 boost::prior(pit)->getDepth() <= pit->getDepth())
304 || boost::prior(pit)->getDepth() < pit->getDepth())))
306 if (!lyxrc.language_command_end.empty() &&
307 prev_language->babel() != outer_language->babel() &&
308 !prev_language->babel().empty())
310 os << from_ascii(subst(lyxrc.language_command_end,
312 prev_language->babel()))
317 // We need to open a new language if we couldn't close the previous
318 // one (because there's no language_command_end); and even if we closed
319 // the previous one, if the current language is different than the
320 // outer_language (which is currently in effect once the previous one
322 if ((lyxrc.language_command_end.empty() ||
323 par_language->babel() != outer_language->babel()) &&
324 !par_language->babel().empty()) {
325 // If we're inside an inset, and that inset is within an \L or \R
326 // (or equivalents), then within the inset, too, any opposite
327 // language paragraph should appear within an \L or \R (in addition
328 // to, outside of, the normal language switch commands).
329 // This behavior is not correct for ArabTeX, though.
330 if ( // not for ArabTeX
331 (par_language->lang() != "arabic_arabtex" &&
332 outer_language->lang() != "arabic_arabtex") &&
333 // are we in an inset?
334 runparams.local_font != 0 &&
335 // is the inset within an \L or \R?
337 // FIXME: currently, we don't check this; this means that
338 // we'll have unnnecessary \L and \R commands, but that
339 // doesn't seem to hurt (though latex will complain)
341 // is this paragraph in the opposite direction?
342 runparams.local_font->isRightToLeft() !=
343 par_language->rightToLeft()
345 // FIXME: I don't have a working copy of the Arabi package, so
346 // I'm not sure if the farsi and arabic_arabi stuff is correct
348 if (par_language->lang() == "farsi")
350 else if (outer_language->lang() == "farsi")
352 else if (par_language->lang() == "arabic_arabi")
354 else if (outer_language->lang() == "arabic_arabi")
356 // remaining RTL languages currently is hebrew
357 else if (par_language->rightToLeft())
362 os << from_ascii(subst(
363 lyxrc.language_command_begin,
365 par_language->babel()))
371 // Switch file encoding if necessary; no need to do this for "default"
372 // encoding, since this only affects the position of the outputted
373 // \inputencoding command; the encoding switch will occur when necessary
374 if (bparams.inputenc == "auto" &&
375 runparams.encoding->package() == Encoding::inputenc) {
376 // Look ahead for future encoding changes.
377 // We try to output them at the beginning of the paragraph,
378 // since the \inputencoding command is not allowed e.g. in
380 for (pos_type i = 0; i < pit->size(); ++i) {
381 char_type const c = pit->getChar(i);
386 // All characters before c are in the ASCII range, and
387 // c is non-ASCII (but no inset), so change the
388 // encoding to that required by the language of c.
389 Encoding const * const encoding =
390 pit->getFontSettings(bparams, i).language()->encoding();
391 pair<bool, int> enc_switch = switchEncoding(os, bparams, false,
392 *(runparams.encoding), *encoding);
393 if (encoding->package() == Encoding::inputenc && enc_switch.first) {
394 runparams.encoding = encoding;
395 if (enc_switch.second > 0) {
404 // In an inset with unlimited length (all in one row),
405 // don't allow any special options in the paragraph
406 if (!pit->forceDefaultParagraphs()) {
407 if (pit->params().startOfAppendix()) {
408 os << "\\appendix\n";
412 if (!pit->params().spacing().isDefault()
413 && (pit == paragraphs.begin()
414 || !boost::prior(pit)->hasSameLayout(*pit)))
416 os << from_ascii(pit->params().spacing().writeEnvirBegin())
421 if (style->isCommand()) {
427 switch (style->latextype) {
429 os << '\\' << from_ascii(style->latexname());
431 // Separate handling of optional argument inset.
432 if (style->optionalargs > 0) {
433 int ret = latexOptArgInsets(buf, *pit, os, runparams,
434 style->optionalargs);
441 os << from_ascii(style->latexparam());
443 case LATEX_ITEM_ENVIRONMENT:
444 case LATEX_LIST_ENVIRONMENT:
447 case LATEX_BIB_ENVIRONMENT:
448 // ignore this, the inset will write itself
454 Font const outerfont =
455 outerFont(std::distance(paragraphs.begin(), pit),
459 os << from_utf8(everypar);
460 bool need_par = pit->latex(buf, bparams, outerfont,
461 os, texrow, runparams);
463 // Make sure that \\par is done with the font of the last
464 // character if this has another size as the default.
465 // This is necessary because LaTeX (and LyX on the screen)
466 // calculates the space between the baselines according
467 // to this font. (Matthias)
469 // Is this really needed ? (Dekel)
470 // We do not need to use to change the font for the last paragraph
475 ? pit->getLayoutFont(bparams, outerfont)
476 : pit->getFont(bparams, pit->size() - 1, outerfont));
478 bool is_command = style->isCommand();
480 if (style->resfont.size() != font.fontInfo().size()
481 && boost::next(pit) != paragraphs.end()
485 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
486 } else if (need_par) {
488 } else if (is_command)
491 bool pending_newline = false;
492 switch (style->latextype) {
493 case LATEX_ITEM_ENVIRONMENT:
494 case LATEX_LIST_ENVIRONMENT:
495 if (boost::next(pit) != paragraphs.end()
496 && (pit->params().depth() < boost::next(pit)->params().depth()))
497 pending_newline = true;
499 case LATEX_ENVIRONMENT: {
500 // if its the last paragraph of the current environment
501 // skip it otherwise fall through
502 ParagraphList::const_iterator next = boost::next(pit);
504 if (next != paragraphs.end()
505 && (next->layout() != pit->layout()
506 || next->params().depth() != pit->params().depth()))
510 // fall through possible
512 // we don't need it for the last paragraph!!!
513 if (boost::next(pit) != paragraphs.end())
514 pending_newline = true;
517 if (!pit->forceDefaultParagraphs()) {
518 if (!pit->params().spacing().isDefault()
519 && (boost::next(pit) == paragraphs.end()
520 || !boost::next(pit)->hasSameLayout(*pit)))
522 if (pending_newline) {
526 os << from_ascii(pit->params().spacing().writeEnvirEnd());
527 pending_newline = true;
531 // Closing the language is needed for the last paragraph; it is also
532 // needed if we're within an \L or \R that we may have opened above (not
533 // necessarily in this paragraph) and are about to close.
534 bool closing_rtl_ltr_environment =
536 (par_language->lang() != "arabic_arabtex" &&
537 outer_language->lang() != "arabic_arabtex") &&
538 // have we opened and \L or \R environment?
539 runparams.local_font != 0 &&
540 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
541 // are we about to close the language?
542 ((boost::next(pit) != paragraphs.end() &&
543 par_language->babel() !=
544 (boost::next(pit)->getParLanguage(bparams))->babel()) ||
545 (boost::next(pit) == paragraphs.end() &&
546 par_language->babel() != outer_language->babel()));
548 if (closing_rtl_ltr_environment || (boost::next(pit) == paragraphs.end()
549 && par_language->babel() != outer_language->babel())) {
550 // Since \selectlanguage write the language to the aux file,
551 // we need to reset the language at the end of footnote or
554 if (pending_newline) {
558 if (lyxrc.language_command_end.empty()) {
559 if (!prev_language->babel().empty()) {
560 os << from_ascii(subst(
561 lyxrc.language_command_begin,
563 prev_language->babel()));
564 pending_newline = true;
566 } else if (!par_language->babel().empty()) {
567 os << from_ascii(subst(
568 lyxrc.language_command_end,
570 par_language->babel()));
571 pending_newline = true;
574 if (closing_rtl_ltr_environment)
577 if (pending_newline) {
582 // If this is the last paragraph, and a local_font was set upon entering
583 // the inset, the encoding should be set back to that local_font's
584 // encoding. We don't use switchEncoding(), because no explicit encoding
585 // switch command is needed, since latex will automatically revert to it
586 // when this inset closes.
587 // This switch is only necessary if we're using "auto" or "default"
589 if (boost::next(pit) == paragraphs.end() && runparams_in.local_font != 0) {
590 runparams_in.encoding = runparams_in.local_font->language()->encoding();
591 if (bparams.inputenc == "auto" || bparams.inputenc == "default")
592 os << setEncoding(runparams_in.encoding->iconvName());
595 // Otherwise, the current encoding should be set for the next paragraph.
597 runparams_in.encoding = runparams.encoding;
600 // we don't need it for the last paragraph!!!
601 // Note from JMarc: we will re-add a \n explicitely in
602 // TeXEnvironment, because it is needed in this case
603 if (boost::next(pit) != paragraphs.end()) {
608 if (boost::next(pit) != paragraphs.end()) {
609 LYXERR(Debug::LATEX) << "TeXOnePar...done " << &*boost::next(pit) << endl;
618 // LaTeX all paragraphs
619 void latexParagraphs(Buffer const & buf,
620 ParagraphList const & paragraphs,
623 OutputParams const & runparams,
624 string const & everypar)
626 bool was_title = false;
627 bool already_title = false;
628 TextClass const & tclass = buf.params().getTextClass();
629 ParagraphList::const_iterator par = paragraphs.begin();
630 ParagraphList::const_iterator endpar = paragraphs.end();
632 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
633 // if only part of the paragraphs will be outputed
634 if (runparams.par_begin != runparams.par_end) {
635 par = boost::next(paragraphs.begin(), runparams.par_begin);
636 endpar = boost::next(paragraphs.begin(), runparams.par_end);
637 // runparams will be passed to nested paragraphs, so
638 // we have to reset the range parameters.
639 const_cast<OutputParams&>(runparams).par_begin = 0;
640 const_cast<OutputParams&>(runparams).par_end = 0;
644 while (par != endpar) {
645 ParagraphList::const_iterator lastpar = par;
646 // well we have to check if we are in an inset with unlimited
647 // length (all in one row) if that is true then we don't allow
648 // any special options in the paragraph and also we don't allow
649 // any environment other than the default layout of the
650 // text class to be valid!
651 if (!par->forceDefaultParagraphs()) {
652 LayoutPtr const & layout = par->layout();
654 if (layout->intitle) {
656 lyxerr << "Error in latexParagraphs: You"
657 " should not mix title layouts"
658 " with normal ones." << endl;
659 } else if (!was_title) {
661 if (tclass.titletype() == TITLE_ENVIRONMENT) {
663 << from_ascii(tclass.titlename())
668 } else if (was_title && !already_title) {
669 if (tclass.titletype() == TITLE_ENVIRONMENT) {
670 os << "\\end{" << from_ascii(tclass.titlename())
674 os << "\\" << from_ascii(tclass.titlename())
678 already_title = true;
682 if (layout->is_environment) {
683 par = TeXOnePar(buf, paragraphs, par, os, texrow,
684 runparams, everypar);
685 } else if (layout->isEnvironment() ||
686 !par->params().leftIndent().zero()) {
687 par = TeXEnvironment(buf, paragraphs, par, os,
690 par = TeXOnePar(buf, paragraphs, par, os, texrow,
691 runparams, everypar);
694 par = TeXOnePar(buf, paragraphs, par, os, texrow,
695 runparams, everypar);
697 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
700 // It might be that we only have a title in this document
701 if (was_title && !already_title) {
702 if (tclass.titletype() == TITLE_ENVIRONMENT) {
703 os << "\\end{" << from_ascii(tclass.titlename())
707 os << "\\" << from_ascii(tclass.titlename())
715 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
716 bool moving_arg, Encoding const & oldEnc,
717 Encoding const & newEnc)
719 if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
721 return make_pair(false, 0);
723 // Do nothing if the encoding is unchanged.
724 if (oldEnc.name() == newEnc.name())
725 return make_pair(false, 0);
727 // FIXME We ignore encoding switches from/to encodings that do
728 // neither support the inputenc package nor the CJK package here.
729 // This does of course only work in special cases (e.g. switch from
730 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
731 // but it is the best we can do
732 if (oldEnc.package() == Encoding::none
733 || newEnc.package() == Encoding::none)
734 return make_pair(false, 0);
736 LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
737 << oldEnc.name() << " to "
738 << newEnc.name() << endl;
739 os << setEncoding(newEnc.iconvName());
740 if (bparams.inputenc == "default")
741 return make_pair(true, 0);
743 docstring const inputenc(from_ascii(newEnc.latexName()));
744 switch (newEnc.package()) {
746 // shouldn't ever reach here, see above
747 return make_pair(true, 0);
748 case Encoding::inputenc: {
749 int count = inputenc.length();
750 if (oldEnc.package() == Encoding::CJK) {
754 os << "\\inputencoding{" << inputenc << '}';
755 return make_pair(true, count + 16);
757 case Encoding::CJK: {
758 int count = inputenc.length();
759 if (oldEnc.package() == Encoding::CJK) {
763 os << "\\begin{CJK}{" << inputenc << "}{}";
764 return make_pair(true, count + 15);
767 // Dead code to avoid a warning:
768 return make_pair(true, 0);