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 ParagraphList::const_iterator
60 TeXEnvironment(Buffer const & buf,
62 ParagraphList::const_iterator pit,
63 odocstream & os, TexRow & texrow,
64 OutputParams const & runparams);
67 ParagraphList::const_iterator
68 TeXDeeper(Buffer const & buf,
70 ParagraphList::const_iterator pit,
71 odocstream & os, TexRow & texrow,
72 OutputParams const & runparams)
74 LYXERR(Debug::LATEX, "TeXDeeper... " << &*pit);
75 ParagraphList::const_iterator par = pit;
77 ParagraphList const & paragraphs = text.paragraphs();
79 bool const force_plain_layout = text.inset().forcePlainLayout();
80 while (par != paragraphs.end() &&
81 par->params().depth() == pit->params().depth()) {
82 // FIXME This test should not be necessary.
83 // We should perhaps issue an error if it is.
84 Layout const & style = force_plain_layout
85 ? buf.params().documentClass().plainLayout() : par->layout();
86 if (style.isEnvironment()) {
87 par = TeXEnvironment(buf, text, par,
88 os, texrow, runparams);
90 TeXOnePar(buf, text, par, os, texrow, runparams);
91 if (par != text.paragraphs().end())
95 LYXERR(Debug::LATEX, "TeXDeeper...done ");
101 string const getPolyglossiaEnvName(Language const * lang)
103 string result = lang->polyglossia();
104 if (result == "arabic")
105 // exceptional spelling; see polyglossia docs.
111 ParagraphList::const_iterator
112 TeXEnvironment(Buffer const & buf,
114 ParagraphList::const_iterator pit,
115 odocstream & os, TexRow & texrow,
116 OutputParams const & runparams)
118 LYXERR(Debug::LATEX, "TeXEnvironment... " << &*pit);
120 BufferParams const & bparams = buf.params();
122 // FIXME This test should not be necessary.
123 // We should perhaps issue an error if it is.
124 Layout const & style = text.inset().forcePlainLayout() ?
125 bparams.documentClass().plainLayout() : pit->layout();
127 ParagraphList const & paragraphs = text.paragraphs();
128 ParagraphList::const_iterator const priorpit =
129 pit == paragraphs.begin() ? pit : boost::prior(pit);
131 bool const use_prev_env_language = prev_env_language_ != 0
132 && priorpit->layout().isEnvironment()
133 && (priorpit->getDepth() > pit->getDepth()
134 || (priorpit->getDepth() == pit->getDepth()
135 && priorpit->layout() != pit->layout()));
137 Encoding const * const prev_encoding = runparams.encoding;
138 Language const * const par_language = pit->getParLanguage(bparams);
139 Language const * const doc_language = bparams.language;
140 Language const * const prev_par_language =
141 (pit != paragraphs.begin())
142 ? (use_prev_env_language ? prev_env_language_
143 : priorpit->getParLanguage(bparams))
145 string par_lang = par_language->babel();
146 string prev_par_lang = prev_par_language->babel();
147 string doc_lang = doc_language->babel();
148 string lang_begin_command = lyxrc.language_command_begin;
149 string lang_end_command = lyxrc.language_command_end;
151 if (runparams.use_polyglossia) {
152 par_lang = getPolyglossiaEnvName(par_language);
153 prev_par_lang = getPolyglossiaEnvName(prev_par_language);
154 doc_lang = getPolyglossiaEnvName(doc_language);
155 lang_begin_command = "\\begin{$$lang}";
156 lang_end_command = "\\end{$$lang}";
159 if (par_lang != prev_par_lang) {
160 if (!lang_end_command.empty() &&
161 prev_par_lang != doc_lang &&
162 !prev_par_lang.empty()) {
163 os << from_ascii(subst(
167 // the '%' is necessary to prevent unwanted whitespace
172 if ((lang_end_command.empty() ||
173 par_lang != doc_lang) &&
175 os << from_ascii(subst(
179 if (runparams.use_polyglossia
180 && !par_language->polyglossiaOpts().empty())
182 << from_ascii(par_language->polyglossiaOpts())
184 // the '%' is necessary to prevent unwanted whitespace
190 bool leftindent_open = false;
191 if (!pit->params().leftIndent().zero()) {
192 os << "\\begin{LyXParagraphLeftIndent}{"
193 << from_ascii(pit->params().leftIndent().asLatexString())
196 leftindent_open = true;
199 if (style.isEnvironment()) {
200 os << "\\begin{" << from_ascii(style.latexname()) << '}';
201 if (style.optargs != 0 || style.reqargs != 0) {
202 int ret = latexArgInsets(*pit, os, runparams, style.reqargs, style.optargs);
208 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
210 << pit->params().labelWidthString()
212 } else if (style.labeltype == LABEL_BIBLIO) {
213 if (pit->params().labelWidthString().empty())
214 os << '{' << bibitemWidest(buf, runparams) << "}\n";
217 << pit->params().labelWidthString()
220 os << from_ascii(style.latexparam()) << '\n';
224 // in multilingual environments, the CJK tags have to be nested properly
225 bool cjk_nested = false;
226 if (par_language->encoding()->package() == Encoding::CJK &&
227 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
228 if (prev_par_language->encoding()->package() == Encoding::CJK)
229 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
230 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
231 open_encoding_ = CJK;
236 ParagraphList::const_iterator par = pit;
238 TeXOnePar(buf, text, par, os, texrow, runparams);
239 if (par != paragraphs.end())
242 if (par == paragraphs.end()) {
243 // Make sure that the last paragraph is
244 // correctly terminated (because TeXOnePar does
245 // not add a \n in this case)
248 } else if (par->params().depth() > pit->params().depth()) {
249 if (par->layout().isParagraph()) {
251 // How to handle this? (Lgb)
252 //&& !suffixIs(os, "\n\n")
254 // There should be at least one '\n' already
255 // but we need there to be two for Standard
256 // paragraphs that are depth-increment'ed to be
257 // output correctly. However, tables can
258 // also be paragraphs so don't adjust them.
261 // Will it ever harm to have one '\n' too
262 // many? i.e. that we sometimes will have
263 // three in a row. (Lgb)
267 par = TeXDeeper(buf, text, par, os, texrow,
270 } while (par != paragraphs.end()
271 && par->layout() == pit->layout()
272 && par->params().depth() == pit->params().depth()
273 && par->params().leftIndent() == pit->params().leftIndent());
275 if (open_encoding_ == CJK && cjk_nested) {
276 // We need to close the encoding even if it does not change
277 // to do correct environment nesting
278 os << "\\end{CJK}\n";
280 open_encoding_ = none;
283 if (style.isEnvironment()) {
284 os << "\\end{" << from_ascii(style.latexname()) << "}\n";
286 prev_env_language_ = par_language;
287 if (runparams.encoding != prev_encoding) {
288 runparams.encoding = prev_encoding;
289 if (!runparams.isFullUnicode())
290 os << setEncoding(prev_encoding->iconvName());
294 if (leftindent_open) {
295 os << "\\end{LyXParagraphLeftIndent}\n";
297 prev_env_language_ = par_language;
298 if (runparams.encoding != prev_encoding) {
299 runparams.encoding = prev_encoding;
300 if (!runparams.isFullUnicode())
301 os << setEncoding(prev_encoding->iconvName());
305 if (par != paragraphs.end())
306 LYXERR(Debug::LATEX, "TeXEnvironment...done " << &*par);
314 int latexArgInsets(Paragraph const & par, odocstream & os,
315 OutputParams const & runparams, unsigned int reqargs,
316 unsigned int optargs)
318 unsigned int totalargs = reqargs + optargs;
319 list<InsetArgument const *> ilist;
321 InsetList::const_iterator it = par.insetList().begin();
322 InsetList::const_iterator end = par.insetList().end();
323 for (; it != end; ++it) {
324 if (it->inset->lyxCode() == ARG_CODE) {
325 if (ilist.size() >= totalargs) {
326 LYXERR0("WARNING: Found extra argument inset.");
329 InsetArgument const * ins =
330 static_cast<InsetArgument const *>(it->inset);
331 ilist.push_back(ins);
335 if (!reqargs && ilist.size() == 0)
339 bool const have_optional_args = ilist.size() > reqargs;
340 if (have_optional_args) {
341 unsigned int todo = ilist.size() - reqargs;
342 for (unsigned int i = 0; i < todo; ++i) {
343 InsetArgument const * ins = ilist.front();
345 lines += ins->latexArgument(os, runparams, true);
349 // we should now have no more insets than there are required
351 LASSERT(ilist.size() <= reqargs, /* */);
355 for (unsigned int i = 0; i < reqargs; ++i) {
357 // a required argument wasn't given, so we output {}
360 InsetArgument const * ins = ilist.front();
362 lines += ins->latexArgument(os, runparams, false);
369 // FIXME: this should be anonymous
370 void TeXOnePar(Buffer const & buf,
372 ParagraphList::const_iterator const pit,
373 odocstream & os, TexRow & texrow,
374 OutputParams const & runparams_in,
375 string const & everypar,
376 int start_pos, int end_pos)
378 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
381 BufferParams const & bparams = buf.params();
382 // FIXME This check should not really be needed.
383 // Perhaps we should issue an error if it is.
384 Layout const style = text.inset().forcePlainLayout() ?
385 bparams.documentClass().plainLayout() : pit->layout();
387 ParagraphList const & paragraphs = text.paragraphs();
388 ParagraphList::const_iterator const priorpit =
389 pit == paragraphs.begin() ? pit : boost::prior(pit);
390 ParagraphList::const_iterator const nextpit =
391 pit == paragraphs.end() ? pit : boost::next(pit);
393 if (style.inpreamble)
396 OutputParams runparams = runparams_in;
397 runparams.isLastPar = nextpit == paragraphs.end();
399 bool const maintext = text.isMainText();
400 // we are at the beginning of an inset and CJK is already open;
401 // we count inheritation levels to get the inset nesting right.
402 if (pit == paragraphs.begin() && !maintext
403 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
405 open_encoding_ = none;
408 if (text.inset().getLayout().isPassThru()) {
409 int const dist = distance(paragraphs.begin(), pit);
410 Font const outerfont = text.outerFont(dist);
412 // No newline before first paragraph in this lyxtext
416 if (!text.inset().getLayout().parbreakIsNewline()) {
422 pit->latex(bparams, outerfont, os, texrow,
423 runparams, start_pos, end_pos);
427 if (style.pass_thru) {
428 int const dist = distance(paragraphs.begin(), pit);
429 Font const outerfont = text.outerFont(dist);
430 pit->latex(bparams, outerfont, os, texrow,
431 runparams, start_pos, end_pos);
434 if (!style.parbreak_is_newline) {
437 } else if (nextpit != paragraphs.end()) {
438 Layout const nextstyle = text.inset().forcePlainLayout() ?
439 bparams.documentClass().plainLayout() : nextpit->layout();
440 if (nextstyle.name() != style.name()) {
449 // This paragraph's language
450 Language const * const par_language = pit->getParLanguage(bparams);
451 // The document's language
452 Language const * const doc_language = bparams.language;
453 // The language that was in effect when the environment this paragraph is
454 // inside of was opened
455 Language const * const outer_language =
456 (runparams.local_font != 0) ?
457 runparams.local_font->language() : doc_language;
459 // The previous language that was in effect is the language of the
460 // previous paragraph, unless the previous paragraph is inside an
461 // environment with nesting depth greater than (or equal to, but with
462 // a different layout) the current one. If there is no previous
463 // paragraph, the previous language is the outer language.
464 bool const use_prev_env_language = prev_env_language_ != 0
465 && priorpit->layout().isEnvironment()
466 && (priorpit->getDepth() > pit->getDepth()
467 || (priorpit->getDepth() == pit->getDepth()
468 && priorpit->layout() != pit->layout()));
469 Language const * const prev_language =
470 (pit != paragraphs.begin())
471 ? (use_prev_env_language ? prev_env_language_
472 : priorpit->getParLanguage(bparams))
475 string par_lang = par_language->babel();
476 string prev_lang = prev_language->babel();
477 string doc_lang = doc_language->babel();
478 string outer_lang = outer_language->babel();
479 string lang_begin_command = lyxrc.language_command_begin;
480 string lang_end_command = lyxrc.language_command_end;
482 if (runparams.use_polyglossia) {
483 par_lang = getPolyglossiaEnvName(par_language);
484 prev_lang = getPolyglossiaEnvName(prev_language);
485 doc_lang = getPolyglossiaEnvName(doc_language);
486 outer_lang = getPolyglossiaEnvName(outer_language);
487 lang_begin_command = "\\begin{$$lang}";
488 lang_end_command = "\\end{$$lang}";
490 if (par_lang != prev_lang
491 // check if we already put language command in TeXEnvironment()
492 && !(style.isEnvironment()
493 && (pit == paragraphs.begin() ||
494 (priorpit->layout() != pit->layout() &&
495 priorpit->getDepth() <= pit->getDepth())
496 || priorpit->getDepth() < pit->getDepth())))
498 if (!lang_end_command.empty() &&
499 prev_lang != outer_lang &&
502 os << from_ascii(subst(lang_end_command,
505 // the '%' is necessary to prevent unwanted whitespace
510 // We need to open a new language if we couldn't close the previous
511 // one (because there's no language_command_end); and even if we closed
512 // the previous one, if the current language is different than the
513 // outer_language (which is currently in effect once the previous one
515 if ((lang_end_command.empty() ||
516 par_lang != outer_lang) &&
518 // If we're inside an inset, and that inset is within an \L or \R
519 // (or equivalents), then within the inset, too, any opposite
520 // language paragraph should appear within an \L or \R (in addition
521 // to, outside of, the normal language switch commands).
522 // This behavior is not correct for ArabTeX, though.
523 if (!runparams.use_polyglossia &&
525 (par_language->lang() != "arabic_arabtex" &&
526 outer_language->lang() != "arabic_arabtex") &&
527 // are we in an inset?
528 runparams.local_font != 0 &&
529 // is the inset within an \L or \R?
531 // FIXME: currently, we don't check this; this means that
532 // we'll have unnnecessary \L and \R commands, but that
533 // doesn't seem to hurt (though latex will complain)
535 // is this paragraph in the opposite direction?
536 runparams.local_font->isRightToLeft() !=
537 par_language->rightToLeft()
539 // FIXME: I don't have a working copy of the Arabi package, so
540 // I'm not sure if the farsi and arabic_arabi stuff is correct
542 if (par_language->lang() == "farsi")
544 else if (outer_language->lang() == "farsi")
546 else if (par_language->lang() == "arabic_arabi")
548 else if (outer_language->lang() == "arabic_arabi")
550 // remaining RTL languages currently is hebrew
551 else if (par_language->rightToLeft())
556 // With CJK, the CJK tag has to be closed first (see below)
557 if (runparams.encoding->package() != Encoding::CJK) {
558 os << from_ascii(subst(
562 if (runparams.use_polyglossia
563 && !par_language->polyglossiaOpts().empty())
565 << from_ascii(par_language->polyglossiaOpts())
567 // the '%' is necessary to prevent unwanted whitespace
574 // Switch file encoding if necessary; no need to do this for "default"
575 // encoding, since this only affects the position of the outputted
576 // \inputencoding command; the encoding switch will occur when necessary
577 if (bparams.inputenc == "auto" &&
578 runparams.encoding->package() != Encoding::none) {
579 // Look ahead for future encoding changes.
580 // We try to output them at the beginning of the paragraph,
581 // since the \inputencoding command is not allowed e.g. in
582 // sections. For this reason we only set runparams.moving_arg
583 // after checking for the encoding change, otherwise the
584 // change would be always avoided by switchEncoding().
585 for (pos_type i = 0; i < pit->size(); ++i) {
586 char_type const c = pit->getChar(i);
587 Encoding const * const encoding =
588 pit->getFontSettings(bparams, i).language()->encoding();
589 if (encoding->package() != Encoding::CJK &&
590 runparams.encoding->package() == Encoding::inputenc &&
595 // All characters before c are in the ASCII range, and
596 // c is non-ASCII (but no inset), so change the
597 // encoding to that required by the language of c.
598 // With CJK, only add switch if we have CJK content at the beginning
600 if (encoding->package() != Encoding::CJK || i == 0) {
601 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
603 // the following is necessary after a CJK environment in a multilingual
604 // context (nesting issue).
605 if (par_language->encoding()->package() == Encoding::CJK &&
606 open_encoding_ != CJK && cjk_inherited_ == 0) {
607 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
608 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
609 open_encoding_ = CJK;
612 if (encoding->package() != Encoding::none && enc_switch.first) {
613 if (enc_switch.second > 0) {
614 // the '%' is necessary to prevent unwanted whitespace
618 // With CJK, the CJK tag had to be closed first (see above)
619 if (runparams.encoding->package() == Encoding::CJK) {
620 os << from_ascii(subst(
624 // the '%' is necessary to prevent unwanted whitespace
628 runparams.encoding = encoding;
635 runparams.moving_arg |= style.needprotect;
636 Encoding const * const prev_encoding = runparams.encoding;
638 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
639 if (pit->allowParagraphCustomization()) {
640 if (pit->params().startOfAppendix()) {
641 os << "\\appendix\n";
645 if (!pit->params().spacing().isDefault()
646 && (pit == paragraphs.begin()
647 || !priorpit->hasSameLayout(*pit)))
649 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
654 if (style.isCommand()) {
660 switch (style.latextype) {
662 os << '\\' << from_ascii(style.latexname());
664 // Separate handling of optional argument inset.
665 if (style.optargs != 0 || style.reqargs != 0) {
666 int ret = latexArgInsets(*pit, os, runparams, style.reqargs, style.optargs);
673 os << from_ascii(style.latexparam());
675 case LATEX_ITEM_ENVIRONMENT:
676 case LATEX_LIST_ENVIRONMENT:
679 case LATEX_BIB_ENVIRONMENT:
680 // ignore this, the inset will write itself
686 Font const outerfont = text.outerFont(distance(paragraphs.begin(), pit));
689 os << from_utf8(everypar);
690 pit->latex(bparams, outerfont, os, texrow,
691 runparams, start_pos, end_pos);
693 // Make sure that \\par is done with the font of the last
694 // character if this has another size as the default.
695 // This is necessary because LaTeX (and LyX on the screen)
696 // calculates the space between the baselines according
697 // to this font. (Matthias)
699 // Is this really needed ? (Dekel)
700 // We do not need to use to change the font for the last paragraph
703 Font const font = pit->empty()
704 ? pit->getLayoutFont(bparams, outerfont)
705 : pit->getFont(bparams, pit->size() - 1, outerfont);
707 bool const is_command = style.isCommand();
709 if (style.resfont.size() != font.fontInfo().size()
710 && nextpit != paragraphs.end()
713 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
714 } else if (is_command) {
716 if (runparams.encoding != prev_encoding) {
717 runparams.encoding = prev_encoding;
718 if (!runparams.isFullUnicode())
719 os << setEncoding(prev_encoding->iconvName());
723 bool pending_newline = false;
724 switch (style.latextype) {
725 case LATEX_ITEM_ENVIRONMENT:
726 case LATEX_LIST_ENVIRONMENT:
727 if (nextpit != paragraphs.end()
728 && (pit->params().depth() < nextpit->params().depth()))
729 pending_newline = true;
731 case LATEX_ENVIRONMENT: {
732 // if its the last paragraph of the current environment
733 // skip it otherwise fall through
734 if (nextpit != paragraphs.end() &&
735 (nextpit->layout() != pit->layout()
736 || nextpit->params().depth() != pit->params().depth()))
740 // fall through possible
742 // we don't need it for the last paragraph!!!
743 if (nextpit != paragraphs.end())
744 pending_newline = true;
747 if (pit->allowParagraphCustomization()) {
748 if (!pit->params().spacing().isDefault()
749 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
751 if (pending_newline) {
755 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
756 pending_newline = true;
760 // Closing the language is needed for the last paragraph; it is also
761 // needed if we're within an \L or \R that we may have opened above (not
762 // necessarily in this paragraph) and are about to close.
763 bool closing_rtl_ltr_environment =
764 !runparams.use_polyglossia &&
766 (par_language->lang() != "arabic_arabtex" &&
767 outer_language->lang() != "arabic_arabtex") &&
768 // have we opened and \L or \R environment?
769 runparams.local_font != 0 &&
770 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
771 // are we about to close the language?
772 ((nextpit != paragraphs.end() &&
773 par_language->babel() !=
774 (nextpit->getParLanguage(bparams))->babel()) ||
775 (nextpit == paragraphs.end() &&
776 par_language->babel() != outer_language->babel()));
778 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
779 && par_language->babel() != outer_language->babel())) {
780 // Since \selectlanguage write the language to the aux file,
781 // we need to reset the language at the end of footnote or
784 if (pending_newline) {
788 // when the paragraph uses CJK, the language has to be closed earlier
789 if (font.language()->encoding()->package() != Encoding::CJK) {
790 if (lang_end_command.empty()) {
791 // If this is a child, we should restore the
792 // master language after the last paragraph.
793 Language const * const current_language =
794 (nextpit == paragraphs.end()
795 && runparams.master_language)
796 ? runparams.master_language
798 string const current_lang = runparams.use_polyglossia ?
799 getPolyglossiaEnvName(current_language)
800 : current_language->babel();
801 if (!current_lang.empty()) {
802 os << from_ascii(subst(
806 pending_newline = true;
808 } else if (!par_lang.empty()) {
809 os << from_ascii(subst(
813 pending_newline = true;
817 if (closing_rtl_ltr_environment)
820 if (pending_newline) {
825 // if this is a CJK-paragraph and the next isn't, close CJK
826 // also if the next paragraph is a multilingual environment (because of nesting)
827 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
828 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
829 (nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams)))
830 // inbetween environments, CJK has to be closed later (nesting!)
831 && (!style.isEnvironment() || !nextpit->layout().isEnvironment())) {
832 os << "\\end{CJK}\n";
833 open_encoding_ = none;
836 // If this is the last paragraph, close the CJK environment
837 // if necessary. If it's an environment, we'll have to \end that first.
838 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
839 switch (open_encoding_) {
841 // do nothing at the end of child documents
842 if (maintext && buf.masterBuffer() != &buf)
848 os << "\\end{CJK}\n";
853 open_encoding_ = none;
858 open_encoding_ = none;
868 // If this is the last paragraph, and a local_font was set upon entering
869 // the inset, and we're using "auto" or "default" encoding, the encoding
870 // should be set back to that local_font's encoding.
871 // However, do not change the encoding when a fully unicode aware backend
872 // such as XeTeX is used.
873 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
874 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
875 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
876 && (!runparams.isFullUnicode())) {
877 runparams_in.encoding = runparams_in.local_font->language()->encoding();
878 os << setEncoding(runparams_in.encoding->iconvName());
880 // Otherwise, the current encoding should be set for the next paragraph.
882 runparams_in.encoding = runparams.encoding;
885 // we don't need a newline for the last paragraph!!!
886 // Note from JMarc: we will re-add a \n explicitly in
887 // TeXEnvironment, because it is needed in this case
888 if (nextpit != paragraphs.end()) {
889 Layout const & next_layout = nextpit->layout();
890 if (style == next_layout
891 // no blank lines before environments!
892 || !next_layout.isEnvironment()
893 // unless there's a depth change
894 // FIXME What we really want to do here is put every \begin and \end
895 // tag on a new line (which was not the case with nested environments).
896 // But in the present state of play, we don't have access to the
897 // information whether the current TeX row is empty or not.
898 // For some ideas about how to fix this, see this thread:
899 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
900 || nextpit->params().depth() != pit->params().depth()) {
906 if (nextpit != paragraphs.end())
907 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*nextpit);
913 // LaTeX all paragraphs
914 void latexParagraphs(Buffer const & buf,
918 OutputParams const & runparams,
919 string const & everypar)
921 bool was_title = false;
922 bool already_title = false;
923 BufferParams const & bparams = buf.params();
924 DocumentClass const & tclass = bparams.documentClass();
925 ParagraphList const & paragraphs = text.paragraphs();
926 ParagraphList::const_iterator par = paragraphs.begin();
927 ParagraphList::const_iterator endpar = paragraphs.end();
929 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
930 // if only part of the paragraphs will be outputed
931 if (runparams.par_begin != runparams.par_end) {
932 par = boost::next(paragraphs.begin(), runparams.par_begin);
933 endpar = boost::next(paragraphs.begin(), runparams.par_end);
934 // runparams will be passed to nested paragraphs, so
935 // we have to reset the range parameters.
936 const_cast<OutputParams&>(runparams).par_begin = 0;
937 const_cast<OutputParams&>(runparams).par_end = 0;
940 bool const maintext = text.isMainText();
941 bool const is_child = buf.masterBuffer() != &buf;
943 // Open a CJK environment at the beginning of the main buffer
944 // if the document's language is a CJK language
945 // (but not in child documents)
946 if (maintext && !is_child
947 && bparams.encoding().package() == Encoding::CJK) {
948 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
949 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
951 open_encoding_ = CJK;
953 // if "auto begin" is switched off, explicitly switch the
954 // language on at start
955 string const mainlang = runparams.use_polyglossia ?
956 getPolyglossiaEnvName(bparams.language)
957 : bparams.language->babel();
958 string const lang_begin_command = runparams.use_polyglossia ?
959 "\\begin{$$lang}" : lyxrc.language_command_begin;
961 if (maintext && !lyxrc.language_auto_begin &&
964 os << from_utf8(subst(lang_begin_command,
967 if (runparams.use_polyglossia
968 && !bparams.language->polyglossiaOpts().empty())
970 << from_ascii(bparams.language->polyglossiaOpts())
976 ParagraphList::const_iterator lastpar;
978 while (par != endpar) {
980 // FIXME This check should not be needed. We should
981 // perhaps issue an error if it is.
982 Layout const & layout = text.inset().forcePlainLayout() ?
983 tclass.plainLayout() : par->layout();
985 if (layout.intitle) {
987 lyxerr << "Error in latexParagraphs: You"
988 " should not mix title layouts"
989 " with normal ones." << endl;
990 } else if (!was_title) {
992 if (tclass.titletype() == TITLE_ENVIRONMENT) {
994 << from_ascii(tclass.titlename())
999 } else if (was_title && !already_title) {
1000 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1001 os << "\\end{" << from_ascii(tclass.titlename())
1005 os << "\\" << from_ascii(tclass.titlename())
1009 already_title = true;
1013 if (layout.isEnvironment() ||
1014 !par->params().leftIndent().zero()) {
1015 par = TeXEnvironment(buf, text, par, os,
1018 TeXOnePar(buf, text, par, os, texrow, runparams, everypar);
1019 if (par != paragraphs.end())
1022 if (distance(lastpar, par) >= distance(lastpar, endpar))
1026 // It might be that we only have a title in this document
1027 if (was_title && !already_title) {
1028 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1029 os << "\\end{" << from_ascii(tclass.titlename())
1033 os << "\\" << from_ascii(tclass.titlename())
1039 // if "auto end" is switched off, explicitly close the language at the end
1040 // but only if the last par is in a babel language
1041 string const lang_end_command = runparams.use_polyglossia ?
1042 "\\end{$$lang}" : lyxrc.language_command_end;
1043 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1044 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1045 os << from_utf8(subst(lang_end_command,
1052 // If the last paragraph is an environment, we'll have to close
1053 // CJK at the very end to do proper nesting.
1054 if (maintext && !is_child && open_encoding_ == CJK) {
1055 os << "\\end{CJK}\n";
1057 open_encoding_ = none;
1060 // reset inherited encoding
1061 if (cjk_inherited_ > 0) {
1062 cjk_inherited_ -= 1;
1063 if (cjk_inherited_ == 0)
1064 open_encoding_ = CJK;
1069 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1070 OutputParams const & runparams, Encoding const & newEnc,
1073 Encoding const & oldEnc = *runparams.encoding;
1074 bool moving_arg = runparams.moving_arg;
1075 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
1077 return make_pair(false, 0);
1079 // Do nothing if the encoding is unchanged.
1080 if (oldEnc.name() == newEnc.name())
1081 return make_pair(false, 0);
1083 // FIXME We ignore encoding switches from/to encodings that do
1084 // neither support the inputenc package nor the CJK package here.
1085 // This does of course only work in special cases (e.g. switch from
1086 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1087 // but it is the best we can do
1088 if (oldEnc.package() == Encoding::none
1089 || newEnc.package() == Encoding::none)
1090 return make_pair(false, 0);
1092 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1093 << oldEnc.name() << " to " << newEnc.name());
1094 os << setEncoding(newEnc.iconvName());
1095 if (bparams.inputenc == "default")
1096 return make_pair(true, 0);
1098 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1099 switch (newEnc.package()) {
1100 case Encoding::none:
1101 case Encoding::japanese:
1102 // shouldn't ever reach here, see above
1103 return make_pair(true, 0);
1104 case Encoding::inputenc: {
1105 int count = inputenc_arg.length();
1106 if (oldEnc.package() == Encoding::CJK &&
1107 open_encoding_ == CJK) {
1109 open_encoding_ = none;
1112 else if (oldEnc.package() == Encoding::inputenc &&
1113 open_encoding_ == inputenc) {
1115 open_encoding_ = none;
1118 if (runparams.local_font != 0
1119 && oldEnc.package() == Encoding::CJK) {
1120 // within insets, \inputenc switches need
1121 // to be embraced within \bgroup...\egroup;
1125 open_encoding_ = inputenc;
1127 // with the japanese option, inputenc is omitted.
1128 if (runparams.use_japanese)
1129 return make_pair(true, count);
1130 os << "\\inputencoding{" << inputenc_arg << '}';
1131 return make_pair(true, count + 16);
1133 case Encoding::CJK: {
1134 int count = inputenc_arg.length();
1135 if (oldEnc.package() == Encoding::CJK &&
1136 open_encoding_ == CJK) {
1140 if (oldEnc.package() == Encoding::inputenc &&
1141 open_encoding_ == inputenc) {
1145 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1146 << from_ascii(bparams.fonts_cjk) << "}";
1147 open_encoding_ = CJK;
1148 return make_pair(true, count + 15);
1151 // Dead code to avoid a warning:
1152 return make_pair(true, 0);