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 "texstream.h"
27 #include "TextClass.h"
29 #include "insets/InsetBibitem.h"
30 #include "insets/InsetArgument.h"
32 #include "frontends/alert.h"
34 #include "support/lassert.h"
35 #include "support/convert.h"
36 #include "support/debug.h"
37 #include "support/lstrings.h"
38 #include "support/lyxalgo.h"
39 #include "support/textutils.h"
40 #include "support/gettext.h"
42 #include <QThreadStorage>
48 using namespace lyx::support;
64 OutputState() : open_encoding_(none), cjk_inherited_(0),
65 prev_env_language_(0), nest_level_(0)
68 OpenEncoding open_encoding_;
70 Language const * prev_env_language_;
72 stack<int> lang_switch_depth_; // Both are always empty when
73 stack<string> open_polyglossia_lang_; // not using polyglossia
77 OutputState * getOutputState()
79 // FIXME An instance of OutputState should be kept around for each export
80 // instead of using local thread storage
81 static QThreadStorage<OutputState *> outputstate;
82 if (!outputstate.hasLocalData())
83 outputstate.setLocalData(new OutputState);
84 return outputstate.localData();
88 string const & openPolyglossiaLang(OutputState const * state)
90 // Return a reference to the last active language opened with
91 // polyglossia. If none or when using babel, return a reference
92 // to an empty string.
94 static string const empty;
96 return state->open_polyglossia_lang_.empty()
98 : state->open_polyglossia_lang_.top();
102 bool atSameLastLangSwitchDepth(OutputState const * state)
104 // Return true if the actual nest level is the same at which the
105 // language was switched when using polyglossia. Instead, return
106 // always true when using babel.
108 return state->lang_switch_depth_.size() == 0
110 : abs(state->lang_switch_depth_.top()) == state->nest_level_;
114 bool isLocalSwitch(OutputState const * state)
116 // Return true if the language was opened by the \text<lang> command.
118 return state->lang_switch_depth_.size()
119 && state->lang_switch_depth_.top() < 0;
123 bool langOpenedAtThisLevel(OutputState const * state)
125 // Return true if the language was opened at the current nesting level.
127 return state->lang_switch_depth_.size()
128 && abs(state->lang_switch_depth_.top()) == state->nest_level_;
132 string const getPolyglossiaEnvName(Language const * lang)
134 string result = lang->polyglossia();
135 if (result == "arabic")
136 // exceptional spelling; see polyglossia docs.
142 string const getPolyglossiaBegin(string const & lang_begin_command,
143 string const & lang, string const & opts)
147 result = subst(lang_begin_command, "$$lang", lang);
148 string options = opts.empty() ?
149 string() : "[" + opts + "]";
150 result = subst(result, "$$opts", options);
156 struct TeXEnvironmentData
159 Layout const * style;
160 Language const * par_language;
161 Encoding const * prev_encoding;
162 bool leftindent_open;
166 static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
168 ParagraphList::const_iterator pit,
170 OutputParams const & runparams)
172 TeXEnvironmentData data;
174 BufferParams const & bparams = buf.params();
176 // FIXME This test should not be necessary.
177 // We should perhaps issue an error if it is.
178 Layout const & style = text.inset().forcePlainLayout() ?
179 bparams.documentClass().plainLayout() : pit->layout();
181 ParagraphList const & paragraphs = text.paragraphs();
182 ParagraphList::const_iterator const priorpit =
183 pit == paragraphs.begin() ? pit : prev(pit, 1);
185 OutputState * state = getOutputState();
186 bool const use_prev_env_language = state->prev_env_language_ != 0
187 && priorpit->layout().isEnvironment()
188 && (priorpit->getDepth() > pit->getDepth()
189 || (priorpit->getDepth() == pit->getDepth()
190 && priorpit->layout() != pit->layout()));
192 data.prev_encoding = runparams.encoding;
193 data.par_language = pit->getParLanguage(bparams);
194 Language const * const doc_language = bparams.language;
195 Language const * const prev_par_language =
196 (pit != paragraphs.begin())
197 ? (use_prev_env_language ? state->prev_env_language_
198 : priorpit->getParLanguage(bparams))
201 bool const use_polyglossia = runparams.use_polyglossia;
202 string const par_lang = use_polyglossia ?
203 getPolyglossiaEnvName(data.par_language) : data.par_language->babel();
204 string const prev_par_lang = use_polyglossia ?
205 getPolyglossiaEnvName(prev_par_language) : prev_par_language->babel();
206 string const doc_lang = use_polyglossia ?
207 getPolyglossiaEnvName(doc_language) : doc_language->babel();
208 string const lang_begin_command = use_polyglossia ?
209 "\\begin{$$lang}" : lyxrc.language_command_begin;
210 string const lang_end_command = use_polyglossia ?
211 "\\end{$$lang}" : lyxrc.language_command_end;
213 // For polyglossia, switch language outside of environment, if possible.
214 if (par_lang != prev_par_lang) {
215 if (!lang_end_command.empty() &&
216 prev_par_lang != doc_lang &&
217 atSameLastLangSwitchDepth(state) &&
218 !prev_par_lang.empty()) {
219 os << from_ascii(subst(
223 // the '%' is necessary to prevent unwanted whitespace
226 popPolyglossiaLang();
229 // If no language was explicitly opened and we are using
230 // polyglossia, then the current polyglossia language is
231 // the document language.
232 string const & pol_lang = use_polyglossia
233 && state->lang_switch_depth_.size()
234 ? openPolyglossiaLang(state)
237 if ((lang_end_command.empty() ||
238 par_lang != doc_lang ||
239 par_lang != pol_lang) &&
241 string bc = use_polyglossia ?
242 getPolyglossiaBegin(lang_begin_command, par_lang,
243 data.par_language->polyglossiaOpts())
244 : subst(lang_begin_command, "$$lang", par_lang);
246 // the '%' is necessary to prevent unwanted whitespace
249 pushPolyglossiaLang(par_lang);
253 data.leftindent_open = false;
254 if (!pit->params().leftIndent().zero()) {
255 os << "\\begin{LyXParagraphLeftIndent}{"
256 << from_ascii(pit->params().leftIndent().asLatexString())
258 data.leftindent_open = true;
261 if (style.isEnvironment()) {
262 state->nest_level_ += 1;
263 os << "\\begin{" << from_ascii(style.latexname()) << '}';
264 if (!style.latexargs().empty()) {
265 OutputParams rp = runparams;
266 rp.local_font = &pit->getFirstFontSettings(bparams);
267 latexArgInsets(paragraphs, pit, os, rp, style.latexargs());
269 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
271 << pit->params().labelWidthString()
273 } else if (style.labeltype == LABEL_BIBLIO) {
274 if (pit->params().labelWidthString().empty())
275 os << '{' << bibitemWidest(buf, runparams) << "}\n";
278 << pit->params().labelWidthString()
281 os << from_ascii(style.latexparam()) << '\n';
285 // in multilingual environments, the CJK tags have to be nested properly
286 data.cjk_nested = false;
287 if (data.par_language->encoding()->package() == Encoding::CJK &&
288 state->open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
289 if (prev_par_language->encoding()->package() == Encoding::CJK)
290 os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
291 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
292 state->open_encoding_ = CJK;
293 data.cjk_nested = true;
299 static void finishEnvironment(otexstream & os, OutputParams const & runparams,
300 TeXEnvironmentData const & data)
302 OutputState * state = getOutputState();
303 // BufferParams const & bparams = buf.params(); // FIXME: for speedup shortcut below, would require passing of "buf" as argument
304 if (state->open_encoding_ == CJK && data.cjk_nested) {
305 // We need to close the encoding even if it does not change
306 // to do correct environment nesting
307 os << "\\end{CJK}\n";
308 state->open_encoding_ = none;
311 if (data.style->isEnvironment()) {
313 // Close any polyglossia language opened at this nest level
314 if (runparams.use_polyglossia) {
315 while (langOpenedAtThisLevel(state)) {
316 if (isLocalSwitch(state)) {
320 << openPolyglossiaLang(state)
323 popPolyglossiaLang();
326 state->nest_level_ -= 1;
327 os << "\\end{" << from_ascii(data.style->latexname()) << "}\n";
328 state->prev_env_language_ = data.par_language;
329 if (runparams.encoding != data.prev_encoding) {
330 runparams.encoding = data.prev_encoding;
331 os << setEncoding(data.prev_encoding->iconvName());
335 if (data.leftindent_open) {
336 os << breakln << "\\end{LyXParagraphLeftIndent}\n";
337 state->prev_env_language_ = data.par_language;
338 if (runparams.encoding != data.prev_encoding) {
339 runparams.encoding = data.prev_encoding;
340 os << setEncoding(data.prev_encoding->iconvName());
344 // Check whether we should output a blank line after the environment
345 if (!data.style->nextnoindent)
350 void TeXEnvironment(Buffer const & buf, Text const & text,
351 OutputParams const & runparams,
352 pit_type & pit, otexstream & os)
354 ParagraphList const & paragraphs = text.paragraphs();
355 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
356 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
358 Layout const & current_layout = par->layout();
359 depth_type const current_depth = par->params().depth();
360 Length const & current_left_indent = par->params().leftIndent();
362 // This is for debugging purpose at the end.
363 pit_type const par_begin = pit;
364 for (; pit < runparams.par_end; ++pit) {
365 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
367 // check first if this is an higher depth paragraph.
368 bool go_out = (par->params().depth() < current_depth);
369 if (par->params().depth() == current_depth) {
370 // This environment is finished.
371 go_out |= (par->layout() != current_layout);
372 go_out |= (par->params().leftIndent() != current_left_indent);
375 // nothing to do here, restore pit and go out.
380 if (par->layout() == current_layout
381 && par->params().depth() == current_depth
382 && par->params().leftIndent() == current_left_indent) {
383 // We are still in the same environment so TeXOnePar and continue;
384 TeXOnePar(buf, text, pit, os, runparams);
388 // We are now in a deeper environment.
389 // Either par->layout() != current_layout
390 // Or par->params().depth() > current_depth
391 // Or par->params().leftIndent() != current_left_indent)
393 // FIXME This test should not be necessary.
394 // We should perhaps issue an error if it is.
395 bool const force_plain_layout = text.inset().forcePlainLayout();
396 Layout const & style = force_plain_layout
397 ? buf.params().documentClass().plainLayout()
400 if (!style.isEnvironment()) {
401 // This is a standard paragraph, no need to call TeXEnvironment.
402 TeXOnePar(buf, text, pit, os, runparams);
406 // This is a new environment.
407 TeXEnvironmentData const data =
408 prepareEnvironment(buf, text, par, os, runparams);
409 // Recursive call to TeXEnvironment!
410 TeXEnvironment(buf, text, runparams, pit, os);
411 finishEnvironment(os, runparams, data);
414 if (pit != runparams.par_end)
415 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << par_begin << " done.");
419 void getArgInsets(otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
420 map<int, lyx::InsetArgument const *> ilist, vector<string> required, string const & prefix)
422 unsigned int const argnr = latexargs.size();
426 // Default and preset args are always output, so if they require
427 // other arguments, consider this.
428 Layout::LaTeXArgMap::const_iterator lit = latexargs.begin();
429 Layout::LaTeXArgMap::const_iterator const lend = latexargs.end();
430 for (; lit != lend; ++lit) {
431 Layout::latexarg arg = (*lit).second;
432 if ((!arg.presetarg.empty() || !arg.defaultarg.empty()) && !arg.requires.empty()) {
433 vector<string> req = getVectorFromString(arg.requires);
434 required.insert(required.end(), req.begin(), req.end());
438 for (unsigned int i = 1; i <= argnr; ++i) {
439 map<int, InsetArgument const *>::const_iterator lit = ilist.find(i);
440 bool inserted = false;
441 if (lit != ilist.end()) {
442 InsetArgument const * ins = (*lit).second;
444 Layout::LaTeXArgMap::const_iterator const lait =
445 latexargs.find(ins->name());
446 if (lait != latexargs.end()) {
447 Layout::latexarg arg = (*lait).second;
448 docstring ldelim = arg.mandatory ?
449 from_ascii("{") : from_ascii("[");
450 docstring rdelim = arg.mandatory ?
451 from_ascii("}") : from_ascii("]");
452 if (!arg.ldelim.empty())
454 if (!arg.rdelim.empty())
456 ins->latexArgument(os, runparams, ldelim, rdelim, arg.presetarg);
462 Layout::LaTeXArgMap::const_iterator lait = latexargs.begin();
463 Layout::LaTeXArgMap::const_iterator const laend = latexargs.end();
464 for (; lait != laend; ++lait) {
465 string const name = prefix + convert<string>(i);
466 if ((*lait).first == name) {
467 Layout::latexarg arg = (*lait).second;
468 docstring preset = arg.presetarg;
469 if (!arg.defaultarg.empty()) {
472 preset += arg.defaultarg;
475 docstring ldelim = arg.ldelim.empty() ?
476 from_ascii("{") : arg.ldelim;
477 docstring rdelim = arg.rdelim.empty() ?
478 from_ascii("}") : arg.rdelim;
479 os << ldelim << preset << rdelim;
480 } else if (!preset.empty()) {
481 docstring ldelim = arg.ldelim.empty() ?
482 from_ascii("[") : arg.ldelim;
483 docstring rdelim = arg.rdelim.empty() ?
484 from_ascii("]") : arg.rdelim;
485 os << ldelim << preset << rdelim;
486 } else if (find(required.begin(), required.end(),
487 (*lait).first) != required.end()) {
488 docstring ldelim = arg.ldelim.empty() ?
489 from_ascii("[") : arg.ldelim;
490 docstring rdelim = arg.rdelim.empty() ?
491 from_ascii("]") : arg.rdelim;
492 os << ldelim << rdelim;
505 void pushPolyglossiaLang(string const & lang_name, bool localswitch)
507 OutputState * state = getOutputState();
509 int nest_level = localswitch ? -state->nest_level_ : state->nest_level_;
510 state->lang_switch_depth_.push(nest_level);
511 state->open_polyglossia_lang_.push(lang_name);
515 void popPolyglossiaLang()
517 OutputState * state = getOutputState();
519 state->lang_switch_depth_.pop();
520 state->open_polyglossia_lang_.pop();
524 void latexArgInsets(Paragraph const & par, otexstream & os,
525 OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs, string const & prefix)
527 map<int, InsetArgument const *> ilist;
528 vector<string> required;
530 InsetList::const_iterator it = par.insetList().begin();
531 InsetList::const_iterator end = par.insetList().end();
532 for (; it != end; ++it) {
533 if (it->inset->lyxCode() == ARG_CODE) {
534 InsetArgument const * ins =
535 static_cast<InsetArgument const *>(it->inset);
536 if (ins->name().empty())
537 LYXERR0("Error: Unnamed argument inset!");
539 string const name = prefix.empty() ? ins->name() : split(ins->name(), ':');
540 unsigned int const nr = convert<unsigned int>(name);
542 Layout::LaTeXArgMap::const_iterator const lit =
543 latexargs.find(ins->name());
544 if (lit != latexargs.end()) {
545 Layout::latexarg const & arg = (*lit).second;
546 if (!arg.requires.empty()) {
547 vector<string> req = getVectorFromString(arg.requires);
548 required.insert(required.end(), req.begin(), req.end());
554 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
558 void latexArgInsets(ParagraphList const & pars, ParagraphList::const_iterator pit,
559 otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
560 string const & prefix)
562 map<int, InsetArgument const *> ilist;
563 vector<string> required;
565 depth_type const current_depth = pit->params().depth();
566 Layout const current_layout = pit->layout();
568 // get the first paragraph in sequence with this layout and depth
571 if (lyx::prev(pit, offset) == pars.begin())
573 ParagraphList::const_iterator priorpit = lyx::prev(pit, offset + 1);
574 if (priorpit->layout() == current_layout
575 && priorpit->params().depth() == current_depth)
581 ParagraphList::const_iterator spit = lyx::prev(pit, offset);
583 for (; spit != pars.end(); ++spit) {
584 if (spit->layout() != current_layout || spit->params().depth() < current_depth)
586 if (spit->params().depth() > current_depth)
588 InsetList::const_iterator it = spit->insetList().begin();
589 InsetList::const_iterator end = spit->insetList().end();
590 for (; it != end; ++it) {
591 if (it->inset->lyxCode() == ARG_CODE) {
592 InsetArgument const * ins =
593 static_cast<InsetArgument const *>(it->inset);
594 if (ins->name().empty())
595 LYXERR0("Error: Unnamed argument inset!");
597 string const name = prefix.empty() ? ins->name() : split(ins->name(), ':');
598 unsigned int const nr = convert<unsigned int>(name);
599 if (ilist.find(nr) == ilist.end())
601 Layout::LaTeXArgMap::const_iterator const lit =
602 latexargs.find(ins->name());
603 if (lit != latexargs.end()) {
604 Layout::latexarg const & arg = (*lit).second;
605 if (!arg.requires.empty()) {
606 vector<string> req = getVectorFromString(arg.requires);
607 required.insert(required.end(), req.begin(), req.end());
614 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
619 // output the proper paragraph start according to latextype.
620 void parStartCommand(Paragraph const & par, otexstream & os,
621 OutputParams const & runparams, Layout const & style)
623 switch (style.latextype) {
625 os << '\\' << from_ascii(style.latexname());
628 if (!style.latexargs().empty())
629 latexArgInsets(par, os, runparams, style.latexargs());
630 os << from_ascii(style.latexparam());
632 case LATEX_ITEM_ENVIRONMENT:
633 case LATEX_LIST_ENVIRONMENT:
634 os << "\\" + style.itemcommand();
636 if (!style.itemargs().empty())
637 latexArgInsets(par, os, runparams, style.itemargs(), "item:");
640 case LATEX_BIB_ENVIRONMENT:
641 // ignore this, the inset will write itself
650 // FIXME: this should be anonymous
651 void TeXOnePar(Buffer const & buf,
655 OutputParams const & runparams_in,
656 string const & everypar,
657 int start_pos, int end_pos)
659 BufferParams const & bparams = runparams_in.is_child
660 ? buf.masterParams() : buf.params();
661 ParagraphList const & paragraphs = text.paragraphs();
662 Paragraph const & par = paragraphs.at(pit);
663 // FIXME This check should not really be needed.
664 // Perhaps we should issue an error if it is.
665 Layout const & style = text.inset().forcePlainLayout() ?
666 bparams.documentClass().plainLayout() : par.layout();
668 if (style.inpreamble)
671 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
674 OutputParams runparams = runparams_in;
675 runparams.isLastPar = (pit == pit_type(paragraphs.size() - 1));
676 // We reinitialze par begin and end to be on the safe side
677 // with embedded inset as we don't know if they set those
679 runparams.par_begin = 0;
680 runparams.par_end = 0;
682 bool const maintext = text.isMainText();
683 // we are at the beginning of an inset and CJK is already open;
684 // we count inheritation levels to get the inset nesting right.
685 OutputState * state = getOutputState();
686 if (pit == 0 && !maintext
687 && (state->cjk_inherited_ > 0 || state->open_encoding_ == CJK)) {
688 state->cjk_inherited_ += 1;
689 state->open_encoding_ = none;
692 if (text.inset().isPassThru()) {
693 Font const outerfont = text.outerFont(pit);
695 // No newline before first paragraph in this lyxtext
698 if (!text.inset().getLayout().parbreakIsNewline())
702 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
706 Paragraph const * nextpar = runparams.isLastPar
707 ? 0 : ¶graphs.at(pit + 1);
709 if (style.pass_thru) {
710 Font const outerfont = text.outerFont(pit);
711 parStartCommand(par, os, runparams, style);
713 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
715 // I did not create a parEndCommand for this minuscule
716 // task because in the other user of parStartCommand
717 // the code is different (JMarc)
718 if (style.isCommand())
722 if (!style.parbreak_is_newline) {
724 } else if (nextpar && !style.isEnvironment()) {
725 Layout const nextstyle = text.inset().forcePlainLayout()
726 ? bparams.documentClass().plainLayout()
728 if (nextstyle.name() != style.name())
735 // This paragraph's language
736 Language const * const par_language = par.getParLanguage(bparams);
737 Language const * const nextpar_language = nextpar ?
738 nextpar->getParLanguage(bparams) : 0;
739 // The document's language
740 Language const * const doc_language = bparams.language;
741 // The language that was in effect when the environment this paragraph is
742 // inside of was opened
743 Language const * const outer_language =
744 (runparams.local_font != 0) ?
745 runparams.local_font->language() : doc_language;
747 Paragraph const * priorpar = (pit == 0) ? 0 : ¶graphs.at(pit - 1);
749 // The previous language that was in effect is the language of the
750 // previous paragraph, unless the previous paragraph is inside an
751 // environment with nesting depth greater than (or equal to, but with
752 // a different layout) the current one. If there is no previous
753 // paragraph, the previous language is the outer language.
754 bool const use_prev_env_language = state->prev_env_language_ != 0
756 && priorpar->layout().isEnvironment()
757 && (priorpar->getDepth() > par.getDepth()
758 || (priorpar->getDepth() == par.getDepth()
759 && priorpar->layout() != par.layout()));
760 Language const * const prev_language =
762 ? (use_prev_env_language ? state->prev_env_language_
763 : priorpar->getParLanguage(bparams))
767 bool const use_polyglossia = runparams.use_polyglossia;
768 string const par_lang = use_polyglossia ?
769 getPolyglossiaEnvName(par_language): par_language->babel();
770 string const prev_lang = use_polyglossia ?
771 getPolyglossiaEnvName(prev_language) : prev_language->babel();
772 string const outer_lang = use_polyglossia ?
773 getPolyglossiaEnvName(outer_language) : outer_language->babel();
774 string const nextpar_lang = nextpar_language ? (use_polyglossia ?
775 getPolyglossiaEnvName(nextpar_language) :
776 nextpar_language->babel()) : string();
777 string lang_begin_command = use_polyglossia ?
778 "\\begin{$$lang}$$opts" : lyxrc.language_command_begin;
779 string lang_end_command = use_polyglossia ?
780 "\\end{$$lang}" : lyxrc.language_command_end;
781 // the '%' is necessary to prevent unwanted whitespace
782 string lang_command_termination = "%\n";
784 // In some insets (such as Arguments), we cannot use \selectlanguage
785 bool const localswitch = text.inset().forceLocalFontSwitch()
786 || (use_polyglossia && text.inset().forcePlainLayout());
788 lang_begin_command = use_polyglossia ?
789 "\\text$$lang$$opts{" : lyxrc.language_command_local;
790 lang_end_command = "}";
791 lang_command_termination.clear();
794 if (par_lang != prev_lang
795 // check if we already put language command in TeXEnvironment()
796 && !(style.isEnvironment()
797 && (pit == 0 || (priorpar->layout() != par.layout()
798 && priorpar->getDepth() <= par.getDepth())
799 || priorpar->getDepth() < par.getDepth())))
801 if (!lang_end_command.empty() &&
802 prev_lang != outer_lang &&
803 !prev_lang.empty() &&
804 (!use_polyglossia || !style.isEnvironment()))
806 os << from_ascii(subst(lang_end_command,
809 << lang_command_termination;
811 popPolyglossiaLang();
814 // We need to open a new language if we couldn't close the previous
815 // one (because there's no language_command_end); and even if we closed
816 // the previous one, if the current language is different than the
817 // outer_language (which is currently in effect once the previous one
819 if ((lang_end_command.empty() || par_lang != outer_lang
821 || (style.isEnvironment() && par_lang != prev_lang)))
822 && !par_lang.empty()) {
823 // If we're inside an inset, and that inset is within an \L or \R
824 // (or equivalents), then within the inset, too, any opposite
825 // language paragraph should appear within an \L or \R (in addition
826 // to, outside of, the normal language switch commands).
827 // This behavior is not correct for ArabTeX, though.
830 && par_language->lang() != "arabic_arabtex"
831 && outer_language->lang() != "arabic_arabtex"
832 // are we in an inset?
833 && runparams.local_font != 0
834 // is the inset within an \L or \R?
836 // FIXME: currently, we don't check this; this means that
837 // we'll have unnnecessary \L and \R commands, but that
838 // doesn't seem to hurt (though latex will complain)
840 // is this paragraph in the opposite direction?
841 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()) {
842 // FIXME: I don't have a working copy of the Arabi package, so
843 // I'm not sure if the farsi and arabic_arabi stuff is correct
845 if (par_language->lang() == "farsi")
847 else if (outer_language->lang() == "farsi")
849 else if (par_language->lang() == "arabic_arabi")
851 else if (outer_language->lang() == "arabic_arabi")
853 // remaining RTL languages currently is hebrew
854 else if (par_language->rightToLeft())
859 // With CJK, the CJK tag has to be closed first (see below)
860 if (runparams.encoding->package() != Encoding::CJK
861 && par_lang != openPolyglossiaLang(state)
862 && !par_lang.empty()) {
863 string bc = use_polyglossia ?
864 getPolyglossiaBegin(lang_begin_command, par_lang, par_language->polyglossiaOpts())
865 : subst(lang_begin_command, "$$lang", par_lang);
867 os << lang_command_termination;
869 pushPolyglossiaLang(par_lang, localswitch);
874 // Switch file encoding if necessary; no need to do this for "default"
875 // encoding, since this only affects the position of the outputted
876 // \inputencoding command; the encoding switch will occur when necessary
877 if (bparams.inputenc == "auto"
878 && !runparams.isFullUnicode() // Xe/LuaTeX use one document-wide encoding (see also switchEncoding())
879 && runparams.encoding->package() != Encoding::none) {
880 // Look ahead for future encoding changes.
881 // We try to output them at the beginning of the paragraph,
882 // since the \inputencoding command is not allowed e.g. in
883 // sections. For this reason we only set runparams.moving_arg
884 // after checking for the encoding change, otherwise the
885 // change would be always avoided by switchEncoding().
886 for (pos_type i = 0; i < par.size(); ++i) {
887 char_type const c = par.getChar(i);
888 Encoding const * const encoding =
889 par.getFontSettings(bparams, i).language()->encoding();
890 if (encoding->package() != Encoding::CJK
891 && runparams.encoding->package() == Encoding::inputenc
896 // All characters before c are in the ASCII range, and
897 // c is non-ASCII (but no inset), so change the
898 // encoding to that required by the language of c.
899 // With CJK, only add switch if we have CJK content at the beginning
901 if (i != 0 && encoding->package() == Encoding::CJK)
904 pair<bool, int> enc_switch = switchEncoding(os.os(),
905 bparams, runparams, *encoding);
906 // the following is necessary after a CJK environment in a multilingual
907 // context (nesting issue).
908 if (par_language->encoding()->package() == Encoding::CJK
909 && state->open_encoding_ != CJK && state->cjk_inherited_ == 0) {
910 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
911 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
912 state->open_encoding_ = CJK;
914 if (encoding->package() != Encoding::none && enc_switch.first) {
915 if (enc_switch.second > 0) {
916 // the '%' is necessary to prevent unwanted whitespace
919 // With CJK, the CJK tag had to be closed first (see above)
920 if (runparams.encoding->package() == Encoding::CJK
921 && par_lang != openPolyglossiaLang(state)
922 && !par_lang.empty()) {
923 os << from_ascii(subst(
927 << lang_command_termination;
929 pushPolyglossiaLang(par_lang, localswitch);
931 runparams.encoding = encoding;
937 runparams.moving_arg |= style.needprotect;
938 Encoding const * const prev_encoding = runparams.encoding;
940 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
941 if (par.allowParagraphCustomization()) {
942 if (par.params().startOfAppendix()) {
943 os << "\n\\appendix\n";
946 if (!par.params().spacing().isDefault()
947 && (pit == 0 || !priorpar->hasSameLayout(par)))
949 os << from_ascii(par.params().spacing().writeEnvirBegin(useSetSpace))
953 if (style.isCommand()) {
958 parStartCommand(par, os, runparams, style);
959 Font const outerfont = text.outerFont(pit);
962 os << from_utf8(everypar);
963 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
965 // Make sure that \\par is done with the font of the last
966 // character if this has another size as the default.
967 // This is necessary because LaTeX (and LyX on the screen)
968 // calculates the space between the baselines according
969 // to this font. (Matthias)
971 // We must not change the font for the last paragraph
972 // of non-multipar insets, tabular cells or commands,
973 // since this produces unwanted whitespace.
975 Font const font = par.empty()
976 ? par.getLayoutFont(bparams, outerfont)
977 : par.getFont(bparams, par.size() - 1, outerfont);
979 bool const is_command = style.isCommand();
981 if (style.resfont.size() != font.fontInfo().size()
982 && (nextpar || maintext
983 || (text.inset().paragraphs().size() > 1
984 && text.inset().lyxCode() != CELL_CODE))
987 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
988 } else if (is_command) {
990 if (!style.postcommandargs().empty())
991 latexArgInsets(par, os, runparams, style.postcommandargs(), "post:");
992 if (runparams.encoding != prev_encoding) {
993 runparams.encoding = prev_encoding;
994 os << setEncoding(prev_encoding->iconvName());
998 bool pending_newline = false;
999 bool unskip_newline = false;
1000 bool close_lang_switch = false;
1001 switch (style.latextype) {
1002 case LATEX_ITEM_ENVIRONMENT:
1003 case LATEX_LIST_ENVIRONMENT:
1004 if ((nextpar && par_lang != nextpar_lang
1005 && nextpar->getDepth() == par.getDepth())
1006 || (atSameLastLangSwitchDepth(state) && nextpar
1007 && nextpar->getDepth() < par.getDepth()))
1008 close_lang_switch = use_polyglossia;
1009 if (nextpar && par.params().depth() < nextpar->params().depth())
1010 pending_newline = true;
1012 case LATEX_ENVIRONMENT: {
1013 // if its the last paragraph of the current environment
1014 // skip it otherwise fall through
1016 && ((nextpar->layout() != par.layout()
1017 || nextpar->params().depth() != par.params().depth())
1018 || (!use_polyglossia || par_lang != nextpar_lang)))
1020 close_lang_switch = use_polyglossia;
1025 // fall through possible
1027 // we don't need it for the last paragraph!!!
1029 pending_newline = true;
1032 if (par.allowParagraphCustomization()) {
1033 if (!par.params().spacing().isDefault()
1034 && (runparams.isLastPar || !nextpar->hasSameLayout(par))) {
1035 if (pending_newline)
1038 string const endtag =
1039 par.params().spacing().writeEnvirEnd(useSetSpace);
1040 if (prefixIs(endtag, "\\end{"))
1043 os << from_ascii(endtag);
1044 pending_newline = true;
1048 // Closing the language is needed for the last paragraph; it is also
1049 // needed if we're within an \L or \R that we may have opened above (not
1050 // necessarily in this paragraph) and are about to close.
1051 bool closing_rtl_ltr_environment = !use_polyglossia
1053 && (par_language->lang() != "arabic_arabtex"
1054 && outer_language->lang() != "arabic_arabtex")
1055 // have we opened an \L or \R environment?
1056 && runparams.local_font != 0
1057 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()
1058 // are we about to close the language?
1059 &&((nextpar && par_lang != nextpar_lang)
1060 || (runparams.isLastPar && par_lang != outer_lang));
1062 if (closing_rtl_ltr_environment
1063 || ((runparams.isLastPar || close_lang_switch)
1064 && (par_lang != outer_lang || (use_polyglossia
1065 && style.isEnvironment()
1066 && par_lang != nextpar_lang)))) {
1067 // Since \selectlanguage write the language to the aux file,
1068 // we need to reset the language at the end of footnote or
1071 if (pending_newline || close_lang_switch)
1074 // when the paragraph uses CJK, the language has to be closed earlier
1075 if (font.language()->encoding()->package() != Encoding::CJK) {
1076 if (lang_end_command.empty()) {
1077 // If this is a child, we should restore the
1078 // master language after the last paragraph.
1079 Language const * const current_language =
1080 (runparams.isLastPar && runparams.master_language)
1081 ? runparams.master_language
1083 string const current_lang = use_polyglossia
1084 ? getPolyglossiaEnvName(current_language)
1085 : current_language->babel();
1086 if (!current_lang.empty()
1087 && current_lang != openPolyglossiaLang(state)) {
1088 string bc = use_polyglossia ?
1089 getPolyglossiaBegin(lang_begin_command, current_lang,
1090 current_language->polyglossiaOpts())
1091 : subst(lang_begin_command, "$$lang", current_lang);
1093 pending_newline = !localswitch;
1094 unskip_newline = !localswitch;
1095 if (use_polyglossia)
1096 pushPolyglossiaLang(current_lang, localswitch);
1098 } else if (!par_lang.empty()) {
1099 // If we are in an environment, we have to close the "outer" language afterwards
1100 string const & pol_lang = openPolyglossiaLang(state);
1101 if (!style.isEnvironment()
1102 || (close_lang_switch
1103 && atSameLastLangSwitchDepth(state)
1104 && par_lang != outer_lang
1105 && (par_lang != pol_lang
1106 || (pol_lang != outer_lang
1108 && style != nextpar->layout())))
1109 || (atSameLastLangSwitchDepth(state)
1110 && state->lang_switch_depth_.size()
1111 && pol_lang != par_lang))
1113 if (use_polyglossia && !localswitch)
1115 os << from_ascii(subst(
1119 pending_newline = !localswitch;
1120 unskip_newline = !localswitch;
1121 if (use_polyglossia)
1122 popPolyglossiaLang();
1127 if (closing_rtl_ltr_environment)
1130 bool const last_was_separator =
1131 par.size() > 0 && par.isEnvSeparator(par.size() - 1);
1133 if (pending_newline) {
1135 // prevent unwanted whitespace
1137 if (!os.afterParbreak() && !last_was_separator)
1141 // if this is a CJK-paragraph and the next isn't, close CJK
1142 // also if the next paragraph is a multilingual environment (because of nesting)
1144 && state->open_encoding_ == CJK
1145 && (nextpar_language->encoding()->package() != Encoding::CJK
1146 || (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
1147 // inbetween environments, CJK has to be closed later (nesting!)
1148 && (!style.isEnvironment() || !nextpar->layout().isEnvironment())) {
1149 os << "\\end{CJK}\n";
1150 state->open_encoding_ = none;
1153 // If this is the last paragraph, close the CJK environment
1154 // if necessary. If it's an environment, we'll have to \end that first.
1155 if (runparams.isLastPar && !style.isEnvironment()) {
1156 switch (state->open_encoding_) {
1158 // do nothing at the end of child documents
1159 if (maintext && buf.masterBuffer() != &buf)
1163 os << "\n\\end{CJK}\n";
1167 state->open_encoding_ = none;
1172 state->open_encoding_ = none;
1182 // If this is the last paragraph, and a local_font was set upon entering
1183 // the inset, and we're using "auto" or "default" encoding, and not
1184 // compiling with XeTeX or LuaTeX, the encoding
1185 // should be set back to that local_font's encoding.
1186 if (runparams.isLastPar && runparams_in.local_font != 0
1187 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
1188 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
1189 && !runparams.isFullUnicode()
1191 runparams_in.encoding = runparams_in.local_font->language()->encoding();
1192 os << setEncoding(runparams_in.encoding->iconvName());
1194 // Otherwise, the current encoding should be set for the next paragraph.
1196 runparams_in.encoding = runparams.encoding;
1199 // we don't need a newline for the last paragraph!!!
1200 // Note from JMarc: we will re-add a \n explicitly in
1201 // TeXEnvironment, because it is needed in this case
1202 if (nextpar && !os.afterParbreak() && !last_was_separator) {
1203 // Make sure to start a new line
1205 Layout const & next_layout = nextpar->layout();
1206 // A newline '\n' is always output before a command,
1207 // so avoid doubling it.
1208 if (!next_layout.isCommand()) {
1209 // Here we now try to avoid spurious empty lines by
1210 // outputting a paragraph break only if: (case 1) the
1211 // paragraph style allows parbreaks and no \begin, \end
1212 // or \item tags are going to follow (i.e., if the next
1213 // isn't the first or the current isn't the last
1214 // paragraph of an environment or itemize) and the
1215 // depth and alignment of the following paragraph is
1216 // unchanged, or (case 2) the following is a
1217 // non-environment paragraph whose depth is increased
1218 // but whose alignment is unchanged, or (case 3) the
1219 // paragraph is not an environment and the next one is a
1220 // non-itemize-like env at lower depth, or (case 4) the
1221 // paragraph is a command not followed by an environment
1222 // and the alignment of the current and next paragraph
1223 // is unchanged, or (case 5) the current alignment is
1224 // changed and a standard paragraph follows.
1225 DocumentClass const & tclass = bparams.documentClass();
1226 if ((style == next_layout
1227 && !style.parbreak_is_newline
1228 && !text.inset().getLayout().parbreakIsNewline()
1229 && style.latextype != LATEX_ITEM_ENVIRONMENT
1230 && style.latextype != LATEX_LIST_ENVIRONMENT
1231 && style.align == par.getAlign()
1232 && nextpar->getDepth() == par.getDepth()
1233 && nextpar->getAlign() == par.getAlign())
1234 || (!next_layout.isEnvironment()
1235 && nextpar->getDepth() > par.getDepth()
1236 && nextpar->getAlign() == par.getAlign())
1237 || (!style.isEnvironment()
1238 && next_layout.latextype == LATEX_ENVIRONMENT
1239 && nextpar->getDepth() < par.getDepth())
1240 || (style.isCommand()
1241 && !next_layout.isEnvironment()
1242 && style.align == par.getAlign()
1243 && next_layout.align == nextpar->getAlign())
1244 || (style.align != par.getAlign()
1245 && tclass.isDefaultLayout(next_layout))) {
1251 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " done; ptr "
1252 << &par << " next " << nextpar);
1258 // LaTeX all paragraphs
1259 void latexParagraphs(Buffer const & buf,
1262 OutputParams const & runparams,
1263 string const & everypar)
1265 LASSERT(runparams.par_begin <= runparams.par_end,
1266 { os << "% LaTeX Output Error\n"; return; } );
1268 BufferParams const & bparams = buf.params();
1269 BufferParams const & mparams = buf.masterParams();
1271 bool const maintext = text.isMainText();
1272 bool const is_child = buf.masterBuffer() != &buf;
1273 bool const multibib_child = maintext && is_child
1274 && mparams.multibib == "child";
1276 if (multibib_child && mparams.useBiblatex())
1277 os << "\\newrefsection";
1278 else if (multibib_child && mparams.useBibtopic()) {
1279 os << "\\begin{btUnit}\n";
1280 runparams.openbtUnit = true;
1283 // Open a CJK environment at the beginning of the main buffer
1284 // if the document's language is a CJK language
1285 // (but not in child documents)
1286 OutputState * state = getOutputState();
1287 if (maintext && !is_child
1288 && bparams.encoding().package() == Encoding::CJK) {
1289 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
1290 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
1291 state->open_encoding_ = CJK;
1293 // if "auto begin" is switched off, explicitly switch the
1294 // language on at start
1295 string const mainlang = runparams.use_polyglossia
1296 ? getPolyglossiaEnvName(bparams.language)
1297 : bparams.language->babel();
1298 string const lang_begin_command = runparams.use_polyglossia ?
1299 "\\begin{$$lang}$$opts" : lyxrc.language_command_begin;
1301 if (maintext && !lyxrc.language_auto_begin &&
1302 !mainlang.empty()) {
1304 string bc = runparams.use_polyglossia ?
1305 getPolyglossiaBegin(lang_begin_command, mainlang,
1306 bparams.language->polyglossiaOpts())
1307 : subst(lang_begin_command, "$$lang", mainlang);
1310 if (runparams.use_polyglossia)
1311 pushPolyglossiaLang(mainlang);
1314 ParagraphList const & paragraphs = text.paragraphs();
1316 if (runparams.par_begin == runparams.par_end) {
1317 // The full doc will be exported but it is easier to just rely on
1318 // runparams range parameters that will be passed TeXEnvironment.
1319 runparams.par_begin = 0;
1320 runparams.par_end = paragraphs.size();
1323 pit_type pit = runparams.par_begin;
1324 // lastpit is for the language check after the loop.
1325 pit_type lastpit = pit;
1326 // variables used in the loop:
1327 bool was_title = false;
1328 bool already_title = false;
1329 DocumentClass const & tclass = bparams.documentClass();
1331 // Did we already warn about inTitle layout mixing? (we only warn once)
1332 bool gave_layout_warning = false;
1333 for (; pit < runparams.par_end; ++pit) {
1335 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
1337 // FIXME This check should not be needed. We should
1338 // perhaps issue an error if it is.
1339 Layout const & layout = text.inset().forcePlainLayout() ?
1340 tclass.plainLayout() : par->layout();
1342 if (layout.intitle) {
1343 if (already_title) {
1344 if (!gave_layout_warning) {
1345 gave_layout_warning = true;
1346 frontend::Alert::warning(_("Error in latexParagraphs"),
1347 bformat(_("You are using at least one "
1348 "layout (%1$s) intended for the title, "
1349 "after using non-title layouts. This "
1350 "could lead to missing or incorrect output."
1353 } else if (!was_title) {
1355 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1357 << from_ascii(tclass.titlename())
1361 } else if (was_title && !already_title) {
1362 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1363 os << "\\end{" << from_ascii(tclass.titlename())
1367 os << "\\" << from_ascii(tclass.titlename())
1370 already_title = true;
1374 if (layout.isCommand() && !layout.latexname().empty()
1375 && layout.latexname() == bparams.multibib) {
1376 if (runparams.openbtUnit)
1377 os << "\\end{btUnit}\n";
1378 if (!bparams.useBiblatex()) {
1379 os << '\n' << "\\begin{btUnit}\n";
1380 runparams.openbtUnit = true;
1384 if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
1385 // This is a standard top level paragraph, TeX it and continue.
1386 TeXOnePar(buf, text, pit, os, runparams, everypar);
1390 TeXEnvironmentData const data =
1391 prepareEnvironment(buf, text, par, os, runparams);
1392 // pit can be changed in TeXEnvironment.
1393 TeXEnvironment(buf, text, runparams, pit, os);
1394 finishEnvironment(os, runparams, data);
1397 if (pit == runparams.par_end) {
1398 // Make sure that the last paragraph is
1399 // correctly terminated (because TeXOnePar does
1400 // not add a \n in this case)
1404 // It might be that we only have a title in this document
1405 if (was_title && !already_title) {
1406 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1407 os << "\\end{" << from_ascii(tclass.titlename())
1410 os << "\\" << from_ascii(tclass.titlename())
1415 if (maintext && !is_child && runparams.openbtUnit)
1416 os << "\\end{btUnit}\n";
1418 // if "auto end" is switched off, explicitly close the language at the end
1419 // but only if the last par is in a babel or polyglossia language
1420 string const lang_end_command = runparams.use_polyglossia ?
1421 "\\end{$$lang}" : lyxrc.language_command_end;
1422 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1423 paragraphs.at(lastpit).getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1424 os << from_utf8(subst(lang_end_command,
1428 if (runparams.use_polyglossia)
1429 popPolyglossiaLang();
1432 // If the last paragraph is an environment, we'll have to close
1433 // CJK at the very end to do proper nesting.
1434 if (maintext && !is_child && state->open_encoding_ == CJK) {
1435 os << "\\end{CJK}\n";
1436 state->open_encoding_ = none;
1438 // Likewise for polyglossia
1439 string const & pol_lang = openPolyglossiaLang(state);
1440 if (maintext && !is_child && !pol_lang.empty()) {
1441 os << from_utf8(subst(lang_end_command,
1445 if (runparams.use_polyglossia)
1446 popPolyglossiaLang();
1449 // reset inherited encoding
1450 if (state->cjk_inherited_ > 0) {
1451 state->cjk_inherited_ -= 1;
1452 if (state->cjk_inherited_ == 0)
1453 state->open_encoding_ = CJK;
1456 if (multibib_child && mparams.useBibtopic()) {
1457 os << "\\end{btUnit}\n";
1458 runparams.openbtUnit = false;
1463 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1464 OutputParams const & runparams, Encoding const & newEnc,
1467 // XeTeX/LuaTeX use only one encoding per document:
1468 // * with useNonTeXFonts: "utf8plain",
1469 // * with XeTeX and TeX fonts: "ascii" (inputenc fails),
1470 // * with LuaTeX and TeX fonts: only one encoding accepted by luainputenc.
1471 if (runparams.isFullUnicode())
1472 return make_pair(false, 0);
1474 Encoding const & oldEnc = *runparams.encoding;
1475 bool moving_arg = runparams.moving_arg;
1476 // If we switch from/to CJK, we need to switch anyway, despite custom inputenc
1477 bool const from_to_cjk =
1478 (oldEnc.package() == Encoding::CJK && newEnc.package() != Encoding::CJK)
1479 || (oldEnc.package() != Encoding::CJK && newEnc.package() == Encoding::CJK);
1480 if (!force && !from_to_cjk
1481 && ((bparams.inputenc != "auto" && bparams.inputenc != "default") || moving_arg))
1482 return make_pair(false, 0);
1484 // Do nothing if the encoding is unchanged.
1485 if (oldEnc.name() == newEnc.name())
1486 return make_pair(false, 0);
1488 // FIXME We ignore encoding switches from/to encodings that do
1489 // neither support the inputenc package nor the CJK package here.
1490 // This does of course only work in special cases (e.g. switch from
1491 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1492 // but it is the best we can do
1493 if (oldEnc.package() == Encoding::none
1494 || newEnc.package() == Encoding::none)
1495 return make_pair(false, 0);
1497 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1498 << oldEnc.name() << " to " << newEnc.name());
1499 os << setEncoding(newEnc.iconvName());
1500 if (bparams.inputenc == "default")
1501 return make_pair(true, 0);
1503 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1504 OutputState * state = getOutputState();
1505 switch (newEnc.package()) {
1506 case Encoding::none:
1507 case Encoding::japanese:
1508 // shouldn't ever reach here, see above
1509 return make_pair(true, 0);
1510 case Encoding::inputenc: {
1511 int count = inputenc_arg.length();
1512 if (oldEnc.package() == Encoding::CJK &&
1513 state->open_encoding_ == CJK) {
1515 state->open_encoding_ = none;
1518 else if (oldEnc.package() == Encoding::inputenc &&
1519 state->open_encoding_ == inputenc) {
1521 state->open_encoding_ = none;
1524 if (runparams.local_font != 0
1525 && oldEnc.package() == Encoding::CJK) {
1526 // within insets, \inputenc switches need
1527 // to be embraced within \bgroup...\egroup;
1531 state->open_encoding_ = inputenc;
1533 // with the japanese option, inputenc is omitted.
1534 if (runparams.use_japanese)
1535 return make_pair(true, count);
1536 os << "\\inputencoding{" << inputenc_arg << '}';
1537 return make_pair(true, count + 16);
1539 case Encoding::CJK: {
1540 int count = inputenc_arg.length();
1541 if (oldEnc.package() == Encoding::CJK &&
1542 state->open_encoding_ == CJK) {
1546 if (oldEnc.package() == Encoding::inputenc &&
1547 state->open_encoding_ == inputenc) {
1551 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1552 << from_ascii(bparams.fonts_cjk) << "}";
1553 state->open_encoding_ = CJK;
1554 return make_pair(true, count + 15);
1557 // Dead code to avoid a warning:
1558 return make_pair(true, 0);