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 "ParagraphParameters.h"
26 #include "TextClass.h"
30 #include "insets/InsetBibitem.h"
31 #include "insets/InsetArgument.h"
33 #include "support/lassert.h"
34 #include "support/debug.h"
35 #include "support/lstrings.h"
37 #include <boost/next_prior.hpp>
41 using namespace lyx::support;
54 static int open_encoding_ = none;
55 static int cjk_inherited_ = 0;
56 Language const * prev_env_language_ = 0;
59 string const getPolyglossiaEnvName(Language const * lang)
61 string result = lang->polyglossia();
62 if (result == "arabic")
63 // exceptional spelling; see polyglossia docs.
69 struct TeXEnvironementData
73 Language const * par_language;
74 Encoding const * prev_encoding;
79 static TeXEnvironementData prepareEnvironement(Buffer const & buf, Text const & text,
80 ParagraphList::const_iterator pit, odocstream & os, TexRow & texrow,
81 OutputParams const & runparams)
83 TeXEnvironementData data;
85 BufferParams const & bparams = buf.params();
87 // FIXME This test should not be necessary.
88 // We should perhaps issue an error if it is.
89 Layout const & style = text.inset().forcePlainLayout() ?
90 bparams.documentClass().plainLayout() : pit->layout();
92 ParagraphList const & paragraphs = text.paragraphs();
93 ParagraphList::const_iterator const priorpit =
94 pit == paragraphs.begin() ? pit : boost::prior(pit);
96 bool const use_prev_env_language = prev_env_language_ != 0
97 && priorpit->layout().isEnvironment()
98 && (priorpit->getDepth() > pit->getDepth()
99 || (priorpit->getDepth() == pit->getDepth()
100 && priorpit->layout() != pit->layout()));
102 data.prev_encoding = runparams.encoding;
103 data.par_language = pit->getParLanguage(bparams);
104 Language const * const doc_language = bparams.language;
105 Language const * const prev_par_language =
106 (pit != paragraphs.begin())
107 ? (use_prev_env_language ? prev_env_language_
108 : priorpit->getParLanguage(bparams))
110 string par_lang = data.par_language->babel();
111 string prev_par_lang = prev_par_language->babel();
112 string doc_lang = doc_language->babel();
113 string lang_begin_command = lyxrc.language_command_begin;
114 string lang_end_command = lyxrc.language_command_end;
116 if (runparams.use_polyglossia) {
117 par_lang = getPolyglossiaEnvName(data.par_language);
118 prev_par_lang = getPolyglossiaEnvName(prev_par_language);
119 doc_lang = getPolyglossiaEnvName(doc_language);
120 lang_begin_command = "\\begin{$$lang}";
121 lang_end_command = "\\end{$$lang}";
124 if (par_lang != prev_par_lang) {
125 if (!lang_end_command.empty() &&
126 prev_par_lang != doc_lang &&
127 !prev_par_lang.empty()) {
128 os << from_ascii(subst(
132 // the '%' is necessary to prevent unwanted whitespace
137 if ((lang_end_command.empty() ||
138 par_lang != doc_lang) &&
140 os << from_ascii(subst(
144 if (runparams.use_polyglossia
145 && !data.par_language->polyglossiaOpts().empty())
147 << from_ascii(data.par_language->polyglossiaOpts())
149 // the '%' is necessary to prevent unwanted whitespace
155 data.leftindent_open = false;
156 if (!pit->params().leftIndent().zero()) {
157 os << "\\begin{LyXParagraphLeftIndent}{"
158 << from_ascii(pit->params().leftIndent().asLatexString())
161 data.leftindent_open = true;
164 if (style.isEnvironment()) {
165 os << "\\begin{" << from_ascii(style.latexname()) << '}';
166 if (style.optargs != 0 || style.reqargs != 0) {
167 int ret = latexArgInsets(*pit, os, runparams, style.reqargs, style.optargs);
173 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
175 << pit->params().labelWidthString()
177 } else if (style.labeltype == LABEL_BIBLIO) {
178 if (pit->params().labelWidthString().empty())
179 os << '{' << bibitemWidest(buf, runparams) << "}\n";
182 << pit->params().labelWidthString()
185 os << from_ascii(style.latexparam()) << '\n';
190 // in multilingual environments, the CJK tags have to be nested properly
191 data.cjk_nested = false;
192 if (data.par_language->encoding()->package() == Encoding::CJK &&
193 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
194 if (prev_par_language->encoding()->package() == Encoding::CJK)
195 os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
196 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
197 open_encoding_ = CJK;
198 data.cjk_nested = true;
205 static void finishEnvironement(odocstream & os, TexRow & texrow,
206 OutputParams const & runparams, TeXEnvironementData const & data)
208 if (open_encoding_ == CJK && data.cjk_nested) {
209 // We need to close the encoding even if it does not change
210 // to do correct environment nesting
211 os << "\\end{CJK}\n";
213 open_encoding_ = none;
216 if (data.style->isEnvironment()) {
217 os << "\\end{" << from_ascii(data.style->latexname()) << "}\n";
219 prev_env_language_ = data.par_language;
220 if (runparams.encoding != data.prev_encoding) {
221 runparams.encoding = data.prev_encoding;
222 if (!runparams.isFullUnicode())
223 os << setEncoding(data.prev_encoding->iconvName());
227 if (data.leftindent_open) {
228 os << "\\end{LyXParagraphLeftIndent}\n";
230 prev_env_language_ = data.par_language;
231 if (runparams.encoding != data.prev_encoding) {
232 runparams.encoding = data.prev_encoding;
233 if (!runparams.isFullUnicode())
234 os << setEncoding(data.prev_encoding->iconvName());
240 ParagraphList::const_iterator
241 TeXEnvironment(Buffer const & buf,
243 ParagraphList::const_iterator pit,
244 odocstream & os, TexRow & texrow,
245 OutputParams const & runparams,
246 TeXEnvironementData const & data)
248 LYXERR(Debug::LATEX, "TeXEnvironment... " << &*pit);
249 ParagraphList const & paragraphs = text.paragraphs();
251 ParagraphList::const_iterator par = pit;
253 TeXOnePar(buf, text, par, os, texrow, runparams);
254 if (par != paragraphs.end())
257 if (par == paragraphs.end()) {
258 // Make sure that the last paragraph is
259 // correctly terminated (because TeXOnePar does
260 // not add a \n in this case)
263 } else if (par->params().depth() > pit->params().depth()) {
264 if (par->layout().isParagraph()) {
266 // How to handle this? (Lgb)
267 //&& !suffixIs(os, "\n\n")
269 // There should be at least one '\n' already
270 // but we need there to be two for Standard
271 // paragraphs that are depth-increment'ed to be
272 // output correctly. However, tables can
273 // also be paragraphs so don't adjust them.
276 // Will it ever harm to have one '\n' too
277 // many? i.e. that we sometimes will have
278 // three in a row. (Lgb)
283 LYXERR(Debug::LATEX, "TeXDeeper... " << &*par);
284 bool const force_plain_layout = text.inset().forcePlainLayout();
285 depth_type const max_depth = par->params().depth();
287 // FIXME: move that to a for loop!
288 while (par != paragraphs.end()
289 && par->params().depth() == max_depth) {
290 // FIXME This test should not be necessary.
291 // We should perhaps issue an error if it is.
292 Layout const & style = force_plain_layout
293 ? buf.params().documentClass().plainLayout() : par->layout();
294 if (style.isEnvironment()) {
295 TeXEnvironementData const inner_data = prepareEnvironement(buf,
296 text, par, os, texrow, runparams);
297 // Recursive call to TeXEnvironment!
298 par = TeXEnvironment(buf, text, par, os, texrow, runparams,
300 finishEnvironement(os, texrow, runparams, inner_data);
302 TeXOnePar(buf, text, par, os, texrow, runparams);
303 if (par != text.paragraphs().end())
307 LYXERR(Debug::LATEX, "TeXDeeper...done ");
309 } while (par != paragraphs.end()
310 && par->layout() == pit->layout()
311 && par->params().depth() == pit->params().depth()
312 && par->params().leftIndent() == pit->params().leftIndent());
314 if (par != paragraphs.end())
315 LYXERR(Debug::LATEX, "TeXEnvironment...done " << &*par);
323 int latexArgInsets(Paragraph const & par, odocstream & os,
324 OutputParams const & runparams, unsigned int reqargs,
325 unsigned int optargs)
327 unsigned int totalargs = reqargs + optargs;
328 list<InsetArgument const *> ilist;
330 InsetList::const_iterator it = par.insetList().begin();
331 InsetList::const_iterator end = par.insetList().end();
332 for (; it != end; ++it) {
333 if (it->inset->lyxCode() == ARG_CODE) {
334 if (ilist.size() >= totalargs) {
335 LYXERR0("WARNING: Found extra argument inset.");
338 InsetArgument const * ins =
339 static_cast<InsetArgument const *>(it->inset);
340 ilist.push_back(ins);
344 if (!reqargs && ilist.size() == 0)
348 bool const have_optional_args = ilist.size() > reqargs;
349 if (have_optional_args) {
350 unsigned int todo = ilist.size() - reqargs;
351 for (unsigned int i = 0; i < todo; ++i) {
352 InsetArgument const * ins = ilist.front();
354 lines += ins->latexArgument(os, runparams, true);
358 // we should now have no more insets than there are required
360 LASSERT(ilist.size() <= reqargs, /* */);
364 for (unsigned int i = 0; i < reqargs; ++i) {
366 // a required argument wasn't given, so we output {}
369 InsetArgument const * ins = ilist.front();
371 lines += ins->latexArgument(os, runparams, false);
378 // FIXME: this should be anonymous
379 void TeXOnePar(Buffer const & buf,
381 ParagraphList::const_iterator const pit,
382 odocstream & os, TexRow & texrow,
383 OutputParams const & runparams_in,
384 string const & everypar,
385 int start_pos, int end_pos)
387 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
390 BufferParams const & bparams = buf.params();
391 // FIXME This check should not really be needed.
392 // Perhaps we should issue an error if it is.
393 Layout const style = text.inset().forcePlainLayout() ?
394 bparams.documentClass().plainLayout() : pit->layout();
396 ParagraphList const & paragraphs = text.paragraphs();
397 ParagraphList::const_iterator const priorpit =
398 pit == paragraphs.begin() ? pit : boost::prior(pit);
399 ParagraphList::const_iterator const nextpit =
400 pit == paragraphs.end() ? pit : boost::next(pit);
402 if (style.inpreamble)
405 OutputParams runparams = runparams_in;
406 runparams.isLastPar = nextpit == paragraphs.end();
408 bool const maintext = text.isMainText();
409 // we are at the beginning of an inset and CJK is already open;
410 // we count inheritation levels to get the inset nesting right.
411 if (pit == paragraphs.begin() && !maintext
412 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
414 open_encoding_ = none;
417 if (text.inset().getLayout().isPassThru()) {
418 int const dist = distance(paragraphs.begin(), pit);
419 Font const outerfont = text.outerFont(dist);
421 // No newline before first paragraph in this lyxtext
425 if (!text.inset().getLayout().parbreakIsNewline()) {
431 pit->latex(bparams, outerfont, os, texrow,
432 runparams, start_pos, end_pos);
436 if (style.pass_thru) {
437 int const dist = distance(paragraphs.begin(), pit);
438 Font const outerfont = text.outerFont(dist);
439 pit->latex(bparams, outerfont, os, texrow,
440 runparams, start_pos, end_pos);
443 if (!style.parbreak_is_newline) {
446 } else if (nextpit != paragraphs.end()) {
447 Layout const nextstyle = text.inset().forcePlainLayout() ?
448 bparams.documentClass().plainLayout() : nextpit->layout();
449 if (nextstyle.name() != style.name()) {
458 // This paragraph's language
459 Language const * const par_language = pit->getParLanguage(bparams);
460 // The document's language
461 Language const * const doc_language = bparams.language;
462 // The language that was in effect when the environment this paragraph is
463 // inside of was opened
464 Language const * const outer_language =
465 (runparams.local_font != 0) ?
466 runparams.local_font->language() : doc_language;
468 // The previous language that was in effect is the language of the
469 // previous paragraph, unless the previous paragraph is inside an
470 // environment with nesting depth greater than (or equal to, but with
471 // a different layout) the current one. If there is no previous
472 // paragraph, the previous language is the outer language.
473 bool const use_prev_env_language = prev_env_language_ != 0
474 && priorpit->layout().isEnvironment()
475 && (priorpit->getDepth() > pit->getDepth()
476 || (priorpit->getDepth() == pit->getDepth()
477 && priorpit->layout() != pit->layout()));
478 Language const * const prev_language =
479 (pit != paragraphs.begin())
480 ? (use_prev_env_language ? prev_env_language_
481 : priorpit->getParLanguage(bparams))
484 string par_lang = par_language->babel();
485 string prev_lang = prev_language->babel();
486 string doc_lang = doc_language->babel();
487 string outer_lang = outer_language->babel();
488 string lang_begin_command = lyxrc.language_command_begin;
489 string lang_end_command = lyxrc.language_command_end;
491 if (runparams.use_polyglossia) {
492 par_lang = getPolyglossiaEnvName(par_language);
493 prev_lang = getPolyglossiaEnvName(prev_language);
494 doc_lang = getPolyglossiaEnvName(doc_language);
495 outer_lang = getPolyglossiaEnvName(outer_language);
496 lang_begin_command = "\\begin{$$lang}";
497 lang_end_command = "\\end{$$lang}";
499 if (par_lang != prev_lang
500 // check if we already put language command in TeXEnvironment()
501 && !(style.isEnvironment()
502 && (pit == paragraphs.begin() ||
503 (priorpit->layout() != pit->layout() &&
504 priorpit->getDepth() <= pit->getDepth())
505 || priorpit->getDepth() < pit->getDepth())))
507 if (!lang_end_command.empty() &&
508 prev_lang != outer_lang &&
511 os << from_ascii(subst(lang_end_command,
514 // the '%' is necessary to prevent unwanted whitespace
519 // We need to open a new language if we couldn't close the previous
520 // one (because there's no language_command_end); and even if we closed
521 // the previous one, if the current language is different than the
522 // outer_language (which is currently in effect once the previous one
524 if ((lang_end_command.empty() ||
525 par_lang != outer_lang) &&
527 // If we're inside an inset, and that inset is within an \L or \R
528 // (or equivalents), then within the inset, too, any opposite
529 // language paragraph should appear within an \L or \R (in addition
530 // to, outside of, the normal language switch commands).
531 // This behavior is not correct for ArabTeX, though.
532 if (!runparams.use_polyglossia &&
534 (par_language->lang() != "arabic_arabtex" &&
535 outer_language->lang() != "arabic_arabtex") &&
536 // are we in an inset?
537 runparams.local_font != 0 &&
538 // is the inset within an \L or \R?
540 // FIXME: currently, we don't check this; this means that
541 // we'll have unnnecessary \L and \R commands, but that
542 // doesn't seem to hurt (though latex will complain)
544 // is this paragraph in the opposite direction?
545 runparams.local_font->isRightToLeft() !=
546 par_language->rightToLeft()
548 // FIXME: I don't have a working copy of the Arabi package, so
549 // I'm not sure if the farsi and arabic_arabi stuff is correct
551 if (par_language->lang() == "farsi")
553 else if (outer_language->lang() == "farsi")
555 else if (par_language->lang() == "arabic_arabi")
557 else if (outer_language->lang() == "arabic_arabi")
559 // remaining RTL languages currently is hebrew
560 else if (par_language->rightToLeft())
565 // With CJK, the CJK tag has to be closed first (see below)
566 if (runparams.encoding->package() != Encoding::CJK) {
567 os << from_ascii(subst(
571 if (runparams.use_polyglossia
572 && !par_language->polyglossiaOpts().empty())
574 << from_ascii(par_language->polyglossiaOpts())
576 // the '%' is necessary to prevent unwanted whitespace
583 // Switch file encoding if necessary; no need to do this for "default"
584 // encoding, since this only affects the position of the outputted
585 // \inputencoding command; the encoding switch will occur when necessary
586 if (bparams.inputenc == "auto" &&
587 runparams.encoding->package() != Encoding::none) {
588 // Look ahead for future encoding changes.
589 // We try to output them at the beginning of the paragraph,
590 // since the \inputencoding command is not allowed e.g. in
591 // sections. For this reason we only set runparams.moving_arg
592 // after checking for the encoding change, otherwise the
593 // change would be always avoided by switchEncoding().
594 for (pos_type i = 0; i < pit->size(); ++i) {
595 char_type const c = pit->getChar(i);
596 Encoding const * const encoding =
597 pit->getFontSettings(bparams, i).language()->encoding();
598 if (encoding->package() != Encoding::CJK &&
599 runparams.encoding->package() == Encoding::inputenc &&
604 // All characters before c are in the ASCII range, and
605 // c is non-ASCII (but no inset), so change the
606 // encoding to that required by the language of c.
607 // With CJK, only add switch if we have CJK content at the beginning
609 if (encoding->package() != Encoding::CJK || i == 0) {
610 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
612 // the following is necessary after a CJK environment in a multilingual
613 // context (nesting issue).
614 if (par_language->encoding()->package() == Encoding::CJK &&
615 open_encoding_ != CJK && cjk_inherited_ == 0) {
616 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
617 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
618 open_encoding_ = CJK;
621 if (encoding->package() != Encoding::none && enc_switch.first) {
622 if (enc_switch.second > 0) {
623 // the '%' is necessary to prevent unwanted whitespace
627 // With CJK, the CJK tag had to be closed first (see above)
628 if (runparams.encoding->package() == Encoding::CJK) {
629 os << from_ascii(subst(
633 // the '%' is necessary to prevent unwanted whitespace
637 runparams.encoding = encoding;
644 runparams.moving_arg |= style.needprotect;
645 Encoding const * const prev_encoding = runparams.encoding;
647 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
648 if (pit->allowParagraphCustomization()) {
649 if (pit->params().startOfAppendix()) {
650 os << "\\appendix\n";
654 if (!pit->params().spacing().isDefault()
655 && (pit == paragraphs.begin()
656 || !priorpit->hasSameLayout(*pit)))
658 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
663 if (style.isCommand()) {
669 switch (style.latextype) {
671 os << '\\' << from_ascii(style.latexname());
673 // Separate handling of optional argument inset.
674 if (style.optargs != 0 || style.reqargs != 0) {
675 int ret = latexArgInsets(*pit, os, runparams, style.reqargs, style.optargs);
682 os << from_ascii(style.latexparam());
684 case LATEX_ITEM_ENVIRONMENT:
685 case LATEX_LIST_ENVIRONMENT:
688 case LATEX_BIB_ENVIRONMENT:
689 // ignore this, the inset will write itself
695 Font const outerfont = text.outerFont(distance(paragraphs.begin(), pit));
698 os << from_utf8(everypar);
699 pit->latex(bparams, outerfont, os, texrow,
700 runparams, start_pos, end_pos);
702 // Make sure that \\par is done with the font of the last
703 // character if this has another size as the default.
704 // This is necessary because LaTeX (and LyX on the screen)
705 // calculates the space between the baselines according
706 // to this font. (Matthias)
708 // Is this really needed ? (Dekel)
709 // We do not need to use to change the font for the last paragraph
712 Font const font = pit->empty()
713 ? pit->getLayoutFont(bparams, outerfont)
714 : pit->getFont(bparams, pit->size() - 1, outerfont);
716 bool const is_command = style.isCommand();
718 if (style.resfont.size() != font.fontInfo().size()
719 && nextpit != paragraphs.end()
722 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
723 } else if (is_command) {
725 if (runparams.encoding != prev_encoding) {
726 runparams.encoding = prev_encoding;
727 if (!runparams.isFullUnicode())
728 os << setEncoding(prev_encoding->iconvName());
732 bool pending_newline = false;
733 switch (style.latextype) {
734 case LATEX_ITEM_ENVIRONMENT:
735 case LATEX_LIST_ENVIRONMENT:
736 if (nextpit != paragraphs.end()
737 && (pit->params().depth() < nextpit->params().depth()))
738 pending_newline = true;
740 case LATEX_ENVIRONMENT: {
741 // if its the last paragraph of the current environment
742 // skip it otherwise fall through
743 if (nextpit != paragraphs.end() &&
744 (nextpit->layout() != pit->layout()
745 || nextpit->params().depth() != pit->params().depth()))
749 // fall through possible
751 // we don't need it for the last paragraph!!!
752 if (nextpit != paragraphs.end())
753 pending_newline = true;
756 if (pit->allowParagraphCustomization()) {
757 if (!pit->params().spacing().isDefault()
758 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
760 if (pending_newline) {
764 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
765 pending_newline = true;
769 // Closing the language is needed for the last paragraph; it is also
770 // needed if we're within an \L or \R that we may have opened above (not
771 // necessarily in this paragraph) and are about to close.
772 bool closing_rtl_ltr_environment =
773 !runparams.use_polyglossia &&
775 (par_language->lang() != "arabic_arabtex" &&
776 outer_language->lang() != "arabic_arabtex") &&
777 // have we opened and \L or \R environment?
778 runparams.local_font != 0 &&
779 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
780 // are we about to close the language?
781 ((nextpit != paragraphs.end() &&
782 par_language->babel() !=
783 (nextpit->getParLanguage(bparams))->babel()) ||
784 (nextpit == paragraphs.end() &&
785 par_language->babel() != outer_language->babel()));
787 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
788 && par_language->babel() != outer_language->babel())) {
789 // Since \selectlanguage write the language to the aux file,
790 // we need to reset the language at the end of footnote or
793 if (pending_newline) {
797 // when the paragraph uses CJK, the language has to be closed earlier
798 if (font.language()->encoding()->package() != Encoding::CJK) {
799 if (lang_end_command.empty()) {
800 // If this is a child, we should restore the
801 // master language after the last paragraph.
802 Language const * const current_language =
803 (nextpit == paragraphs.end()
804 && runparams.master_language)
805 ? runparams.master_language
807 string const current_lang = runparams.use_polyglossia ?
808 getPolyglossiaEnvName(current_language)
809 : current_language->babel();
810 if (!current_lang.empty()) {
811 os << from_ascii(subst(
815 pending_newline = true;
817 } else if (!par_lang.empty()) {
818 os << from_ascii(subst(
822 pending_newline = true;
826 if (closing_rtl_ltr_environment)
829 if (pending_newline) {
834 // if this is a CJK-paragraph and the next isn't, close CJK
835 // also if the next paragraph is a multilingual environment (because of nesting)
836 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
837 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
838 (nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams)))
839 // inbetween environments, CJK has to be closed later (nesting!)
840 && (!style.isEnvironment() || !nextpit->layout().isEnvironment())) {
841 os << "\\end{CJK}\n";
842 open_encoding_ = none;
845 // If this is the last paragraph, close the CJK environment
846 // if necessary. If it's an environment, we'll have to \end that first.
847 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
848 switch (open_encoding_) {
850 // do nothing at the end of child documents
851 if (maintext && buf.masterBuffer() != &buf)
857 os << "\\end{CJK}\n";
862 open_encoding_ = none;
867 open_encoding_ = none;
877 // If this is the last paragraph, and a local_font was set upon entering
878 // the inset, and we're using "auto" or "default" encoding, the encoding
879 // should be set back to that local_font's encoding.
880 // However, do not change the encoding when a fully unicode aware backend
881 // such as XeTeX is used.
882 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
883 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
884 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
885 && (!runparams.isFullUnicode())) {
886 runparams_in.encoding = runparams_in.local_font->language()->encoding();
887 os << setEncoding(runparams_in.encoding->iconvName());
889 // Otherwise, the current encoding should be set for the next paragraph.
891 runparams_in.encoding = runparams.encoding;
894 // we don't need a newline for the last paragraph!!!
895 // Note from JMarc: we will re-add a \n explicitly in
896 // TeXEnvironment, because it is needed in this case
897 if (nextpit != paragraphs.end()) {
898 Layout const & next_layout = nextpit->layout();
899 if (style == next_layout
900 // no blank lines before environments!
901 || !next_layout.isEnvironment()
902 // unless there's a depth change
903 // FIXME What we really want to do here is put every \begin and \end
904 // tag on a new line (which was not the case with nested environments).
905 // But in the present state of play, we don't have access to the
906 // information whether the current TeX row is empty or not.
907 // For some ideas about how to fix this, see this thread:
908 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
909 || nextpit->params().depth() != pit->params().depth()) {
915 if (nextpit != paragraphs.end())
916 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*nextpit);
922 // LaTeX all paragraphs
923 void latexParagraphs(Buffer const & buf,
927 OutputParams const & runparams,
928 string const & everypar)
930 bool was_title = false;
931 bool already_title = false;
932 BufferParams const & bparams = buf.params();
933 DocumentClass const & tclass = bparams.documentClass();
934 ParagraphList const & paragraphs = text.paragraphs();
935 ParagraphList::const_iterator par = paragraphs.begin();
936 ParagraphList::const_iterator endpar = paragraphs.end();
938 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
939 // if only part of the paragraphs will be outputed
940 if (runparams.par_begin != runparams.par_end) {
941 par = boost::next(paragraphs.begin(), runparams.par_begin);
942 endpar = boost::next(paragraphs.begin(), runparams.par_end);
943 // runparams will be passed to nested paragraphs, so
944 // we have to reset the range parameters.
945 const_cast<OutputParams&>(runparams).par_begin = 0;
946 const_cast<OutputParams&>(runparams).par_end = 0;
949 bool const maintext = text.isMainText();
950 bool const is_child = buf.masterBuffer() != &buf;
952 // Open a CJK environment at the beginning of the main buffer
953 // if the document's language is a CJK language
954 // (but not in child documents)
955 if (maintext && !is_child
956 && bparams.encoding().package() == Encoding::CJK) {
957 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
958 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
960 open_encoding_ = CJK;
962 // if "auto begin" is switched off, explicitly switch the
963 // language on at start
964 string const mainlang = runparams.use_polyglossia ?
965 getPolyglossiaEnvName(bparams.language)
966 : bparams.language->babel();
967 string const lang_begin_command = runparams.use_polyglossia ?
968 "\\begin{$$lang}" : lyxrc.language_command_begin;
970 if (maintext && !lyxrc.language_auto_begin &&
973 os << from_utf8(subst(lang_begin_command,
976 if (runparams.use_polyglossia
977 && !bparams.language->polyglossiaOpts().empty())
979 << from_ascii(bparams.language->polyglossiaOpts())
985 ParagraphList::const_iterator lastpar;
987 while (par != endpar) {
989 // FIXME This check should not be needed. We should
990 // perhaps issue an error if it is.
991 Layout const & layout = text.inset().forcePlainLayout() ?
992 tclass.plainLayout() : par->layout();
994 if (layout.intitle) {
996 lyxerr << "Error in latexParagraphs: You"
997 " should not mix title layouts"
998 " with normal ones." << endl;
999 } else if (!was_title) {
1001 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1003 << from_ascii(tclass.titlename())
1008 } else if (was_title && !already_title) {
1009 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1010 os << "\\end{" << from_ascii(tclass.titlename())
1014 os << "\\" << from_ascii(tclass.titlename())
1018 already_title = true;
1022 if (layout.isEnvironment() ||
1023 !par->params().leftIndent().zero()) {
1024 TeXEnvironementData const data = prepareEnvironement(buf, text,
1025 par, os, texrow, runparams);
1026 par = TeXEnvironment(buf, text, par, os, texrow, runparams, data);
1027 finishEnvironement(os, texrow, runparams, data);
1029 TeXOnePar(buf, text, par, os, texrow, runparams, everypar);
1030 if (par != paragraphs.end())
1033 if (distance(lastpar, par) >= distance(lastpar, endpar))
1037 // It might be that we only have a title in this document
1038 if (was_title && !already_title) {
1039 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1040 os << "\\end{" << from_ascii(tclass.titlename())
1044 os << "\\" << from_ascii(tclass.titlename())
1050 // if "auto end" is switched off, explicitly close the language at the end
1051 // but only if the last par is in a babel language
1052 string const lang_end_command = runparams.use_polyglossia ?
1053 "\\end{$$lang}" : lyxrc.language_command_end;
1054 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1055 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1056 os << from_utf8(subst(lang_end_command,
1063 // If the last paragraph is an environment, we'll have to close
1064 // CJK at the very end to do proper nesting.
1065 if (maintext && !is_child && open_encoding_ == CJK) {
1066 os << "\\end{CJK}\n";
1068 open_encoding_ = none;
1071 // reset inherited encoding
1072 if (cjk_inherited_ > 0) {
1073 cjk_inherited_ -= 1;
1074 if (cjk_inherited_ == 0)
1075 open_encoding_ = CJK;
1080 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1081 OutputParams const & runparams, Encoding const & newEnc,
1084 Encoding const & oldEnc = *runparams.encoding;
1085 bool moving_arg = runparams.moving_arg;
1086 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
1088 return make_pair(false, 0);
1090 // Do nothing if the encoding is unchanged.
1091 if (oldEnc.name() == newEnc.name())
1092 return make_pair(false, 0);
1094 // FIXME We ignore encoding switches from/to encodings that do
1095 // neither support the inputenc package nor the CJK package here.
1096 // This does of course only work in special cases (e.g. switch from
1097 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1098 // but it is the best we can do
1099 if (oldEnc.package() == Encoding::none
1100 || newEnc.package() == Encoding::none)
1101 return make_pair(false, 0);
1103 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1104 << oldEnc.name() << " to " << newEnc.name());
1105 os << setEncoding(newEnc.iconvName());
1106 if (bparams.inputenc == "default")
1107 return make_pair(true, 0);
1109 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1110 switch (newEnc.package()) {
1111 case Encoding::none:
1112 case Encoding::japanese:
1113 // shouldn't ever reach here, see above
1114 return make_pair(true, 0);
1115 case Encoding::inputenc: {
1116 int count = inputenc_arg.length();
1117 if (oldEnc.package() == Encoding::CJK &&
1118 open_encoding_ == CJK) {
1120 open_encoding_ = none;
1123 else if (oldEnc.package() == Encoding::inputenc &&
1124 open_encoding_ == inputenc) {
1126 open_encoding_ = none;
1129 if (runparams.local_font != 0
1130 && oldEnc.package() == Encoding::CJK) {
1131 // within insets, \inputenc switches need
1132 // to be embraced within \bgroup...\egroup;
1136 open_encoding_ = inputenc;
1138 // with the japanese option, inputenc is omitted.
1139 if (runparams.use_japanese)
1140 return make_pair(true, count);
1141 os << "\\inputencoding{" << inputenc_arg << '}';
1142 return make_pair(true, count + 16);
1144 case Encoding::CJK: {
1145 int count = inputenc_arg.length();
1146 if (oldEnc.package() == Encoding::CJK &&
1147 open_encoding_ == CJK) {
1151 if (oldEnc.package() == Encoding::inputenc &&
1152 open_encoding_ == inputenc) {
1156 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1157 << from_ascii(bparams.fonts_cjk) << "}";
1158 open_encoding_ = CJK;
1159 return make_pair(true, count + 15);
1162 // Dead code to avoid a warning:
1163 return make_pair(true, 0);