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 & openLanguageName(OutputState const * state)
90 // Return a reference to the last active language opened with
91 // polyglossia or when using begin/end commands. If none or when
92 // using babel with only a begin command, return a reference to
95 static string const empty;
97 return state->open_polyglossia_lang_.empty()
99 : state->open_polyglossia_lang_.top();
103 bool atSameLastLangSwitchDepth(OutputState const * state)
105 // Return true if the actual nest level is the same at which the
106 // language was switched when using polyglossia or begin/end
107 // commands. Instead, return always true when using babel with
108 // only a begin command.
110 return state->lang_switch_depth_.size() == 0
112 : abs(state->lang_switch_depth_.top()) == state->nest_level_;
116 bool isLocalSwitch(OutputState const * state)
118 // Return true if the language was opened by a local command switch.
120 return state->lang_switch_depth_.size()
121 && state->lang_switch_depth_.top() < 0;
125 bool langOpenedAtThisLevel(OutputState const * state)
127 // Return true if the language was opened at the current nesting level.
129 return state->lang_switch_depth_.size()
130 && abs(state->lang_switch_depth_.top()) == state->nest_level_;
134 string const getPolyglossiaEnvName(Language const * lang)
136 string result = lang->polyglossia();
137 if (result == "arabic")
138 // exceptional spelling; see polyglossia docs.
144 string const getPolyglossiaBegin(string const & lang_begin_command,
145 string const & lang, string const & opts)
149 result = subst(lang_begin_command, "$$lang", lang);
150 string options = opts.empty() ?
151 string() : "[" + opts + "]";
152 result = subst(result, "$$opts", options);
158 struct TeXEnvironmentData
161 Layout const * style;
162 Language const * par_language;
163 Encoding const * prev_encoding;
164 bool leftindent_open;
168 static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
170 ParagraphList::const_iterator pit,
172 OutputParams const & runparams)
174 TeXEnvironmentData data;
176 BufferParams const & bparams = buf.params();
178 // FIXME This test should not be necessary.
179 // We should perhaps issue an error if it is.
180 Layout const & style = text.inset().forcePlainLayout() ?
181 bparams.documentClass().plainLayout() : pit->layout();
183 ParagraphList const & paragraphs = text.paragraphs();
184 ParagraphList::const_iterator const priorpit =
185 pit == paragraphs.begin() ? pit : prev(pit, 1);
187 OutputState * state = getOutputState();
188 bool const use_prev_env_language = state->prev_env_language_ != 0
189 && priorpit->layout().isEnvironment()
190 && (priorpit->getDepth() > pit->getDepth()
191 || (priorpit->getDepth() == pit->getDepth()
192 && priorpit->layout() != pit->layout()));
194 data.prev_encoding = runparams.encoding;
195 data.par_language = pit->getParLanguage(bparams);
196 Language const * const doc_language = bparams.language;
197 Language const * const prev_par_language =
198 (pit != paragraphs.begin())
199 ? (use_prev_env_language ? state->prev_env_language_
200 : priorpit->getParLanguage(bparams))
203 bool const use_polyglossia = runparams.use_polyglossia;
204 string const par_lang = use_polyglossia ?
205 getPolyglossiaEnvName(data.par_language) : data.par_language->babel();
206 string const prev_par_lang = use_polyglossia ?
207 getPolyglossiaEnvName(prev_par_language) : prev_par_language->babel();
208 string const doc_lang = use_polyglossia ?
209 getPolyglossiaEnvName(doc_language) : doc_language->babel();
210 string const lang_begin_command = use_polyglossia ?
211 "\\begin{$$lang}" : lyxrc.language_command_begin;
212 string const lang_end_command = use_polyglossia ?
213 "\\end{$$lang}" : lyxrc.language_command_end;
214 bool const using_begin_end = use_polyglossia ||
215 !lang_end_command.empty();
217 // For polyglossia, switch language outside of environment, if possible.
218 if (par_lang != prev_par_lang) {
219 if ((!using_begin_end || langOpenedAtThisLevel(state)) &&
220 !lang_end_command.empty() &&
221 prev_par_lang != doc_lang &&
222 !prev_par_lang.empty()) {
223 os << from_ascii(subst(
227 // the '%' is necessary to prevent unwanted whitespace
233 // If no language was explicitly opened and we are using
234 // polyglossia or begin/end commands, then the current
235 // language is the document language.
236 string const & cur_lang = using_begin_end
237 && state->lang_switch_depth_.size()
238 ? openLanguageName(state)
241 if ((lang_end_command.empty() ||
242 par_lang != doc_lang ||
243 par_lang != cur_lang) &&
245 string bc = use_polyglossia ?
246 getPolyglossiaBegin(lang_begin_command, par_lang,
247 data.par_language->polyglossiaOpts())
248 : subst(lang_begin_command, "$$lang", par_lang);
250 // the '%' is necessary to prevent unwanted whitespace
253 pushLanguageName(par_lang);
257 data.leftindent_open = false;
258 if (!pit->params().leftIndent().zero()) {
259 os << "\\begin{LyXParagraphLeftIndent}{"
260 << from_ascii(pit->params().leftIndent().asLatexString())
262 data.leftindent_open = true;
265 if (style.isEnvironment())
266 state->nest_level_ += 1;
268 if (style.isEnvironment() && !style.latexname().empty()) {
269 os << "\\begin{" << from_ascii(style.latexname()) << '}';
270 if (!style.latexargs().empty()) {
271 OutputParams rp = runparams;
272 rp.local_font = &pit->getFirstFontSettings(bparams);
273 latexArgInsets(paragraphs, pit, os, rp, style.latexargs());
275 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
277 << pit->params().labelWidthString()
279 } else if (style.labeltype == LABEL_BIBLIO) {
280 if (pit->params().labelWidthString().empty())
281 os << '{' << bibitemWidest(buf, runparams) << "}\n";
284 << pit->params().labelWidthString()
287 os << from_ascii(style.latexparam()) << '\n';
291 // in multilingual environments, the CJK tags have to be nested properly
292 data.cjk_nested = false;
293 if (data.par_language->encoding()->package() == Encoding::CJK &&
294 state->open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
295 if (prev_par_language->encoding()->package() == Encoding::CJK)
296 os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
297 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
298 state->open_encoding_ = CJK;
299 data.cjk_nested = true;
305 static void finishEnvironment(otexstream & os, OutputParams const & runparams,
306 TeXEnvironmentData const & data)
308 OutputState * state = getOutputState();
309 // BufferParams const & bparams = buf.params(); // FIXME: for speedup shortcut below, would require passing of "buf" as argument
310 if (state->open_encoding_ == CJK && data.cjk_nested) {
311 // We need to close the encoding even if it does not change
312 // to do correct environment nesting
313 os << "\\end{CJK}\n";
314 state->open_encoding_ = none;
317 if (data.style->isEnvironment()) {
319 bool const using_begin_end =
320 runparams.use_polyglossia ||
321 !lyxrc.language_command_end.empty();
322 // Close any language opened at this nest level
323 if (using_begin_end) {
324 while (langOpenedAtThisLevel(state)) {
325 if (isLocalSwitch(state)) {
329 << openLanguageName(state)
335 state->nest_level_ -= 1;
336 string const & name = data.style->latexname();
338 os << "\\end{" << from_ascii(name) << "}\n";
339 state->prev_env_language_ = data.par_language;
340 if (runparams.encoding != data.prev_encoding) {
341 runparams.encoding = data.prev_encoding;
342 os << setEncoding(data.prev_encoding->iconvName());
346 if (data.leftindent_open) {
347 os << breakln << "\\end{LyXParagraphLeftIndent}\n";
348 state->prev_env_language_ = data.par_language;
349 if (runparams.encoding != data.prev_encoding) {
350 runparams.encoding = data.prev_encoding;
351 os << setEncoding(data.prev_encoding->iconvName());
355 // Check whether we should output a blank line after the environment
356 if (!data.style->nextnoindent)
361 void TeXEnvironment(Buffer const & buf, Text const & text,
362 OutputParams const & runparams,
363 pit_type & pit, otexstream & os)
365 ParagraphList const & paragraphs = text.paragraphs();
366 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
367 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
369 Layout const & current_layout = par->layout();
370 depth_type const current_depth = par->params().depth();
371 Length const & current_left_indent = par->params().leftIndent();
373 // This is for debugging purpose at the end.
374 pit_type const par_begin = pit;
375 for (; pit < runparams.par_end; ++pit) {
376 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
378 // check first if this is an higher depth paragraph.
379 bool go_out = (par->params().depth() < current_depth);
380 if (par->params().depth() == current_depth) {
381 // This environment is finished.
382 go_out |= (par->layout() != current_layout);
383 go_out |= (par->params().leftIndent() != current_left_indent);
386 // nothing to do here, restore pit and go out.
391 if (par->layout() == current_layout
392 && par->params().depth() == current_depth
393 && par->params().leftIndent() == current_left_indent) {
394 // We are still in the same environment so TeXOnePar and continue;
395 TeXOnePar(buf, text, pit, os, runparams);
399 // We are now in a deeper environment.
400 // Either par->layout() != current_layout
401 // Or par->params().depth() > current_depth
402 // Or par->params().leftIndent() != current_left_indent)
404 // FIXME This test should not be necessary.
405 // We should perhaps issue an error if it is.
406 bool const force_plain_layout = text.inset().forcePlainLayout();
407 Layout const & style = force_plain_layout
408 ? buf.params().documentClass().plainLayout()
411 if (!style.isEnvironment()) {
412 // This is a standard paragraph, no need to call TeXEnvironment.
413 TeXOnePar(buf, text, pit, os, runparams);
417 // This is a new environment.
418 TeXEnvironmentData const data =
419 prepareEnvironment(buf, text, par, os, runparams);
420 // Recursive call to TeXEnvironment!
421 TeXEnvironment(buf, text, runparams, pit, os);
422 finishEnvironment(os, runparams, data);
425 if (pit != runparams.par_end)
426 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << par_begin << " done.");
430 void getArgInsets(otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
431 map<int, lyx::InsetArgument const *> ilist, vector<string> required, string const & prefix)
433 unsigned int const argnr = latexargs.size();
437 // Default and preset args are always output, so if they require
438 // other arguments, consider this.
439 Layout::LaTeXArgMap::const_iterator lit = latexargs.begin();
440 Layout::LaTeXArgMap::const_iterator const lend = latexargs.end();
441 for (; lit != lend; ++lit) {
442 Layout::latexarg arg = (*lit).second;
443 if ((!arg.presetarg.empty() || !arg.defaultarg.empty()) && !arg.requires.empty()) {
444 vector<string> req = getVectorFromString(arg.requires);
445 required.insert(required.end(), req.begin(), req.end());
449 for (unsigned int i = 1; i <= argnr; ++i) {
450 map<int, InsetArgument const *>::const_iterator lit = ilist.find(i);
451 bool inserted = false;
452 if (lit != ilist.end()) {
453 InsetArgument const * ins = (*lit).second;
455 Layout::LaTeXArgMap::const_iterator const lait =
456 latexargs.find(ins->name());
457 if (lait != latexargs.end()) {
458 Layout::latexarg arg = (*lait).second;
459 docstring ldelim = arg.mandatory ?
460 from_ascii("{") : from_ascii("[");
461 docstring rdelim = arg.mandatory ?
462 from_ascii("}") : from_ascii("]");
463 if (!arg.ldelim.empty())
465 if (!arg.rdelim.empty())
467 ins->latexArgument(os, runparams, ldelim, rdelim, arg.presetarg);
473 Layout::LaTeXArgMap::const_iterator lait = latexargs.begin();
474 Layout::LaTeXArgMap::const_iterator const laend = latexargs.end();
475 for (; lait != laend; ++lait) {
476 string const name = prefix + convert<string>(i);
477 if ((*lait).first == name) {
478 Layout::latexarg arg = (*lait).second;
479 docstring preset = arg.presetarg;
480 if (!arg.defaultarg.empty()) {
483 preset += arg.defaultarg;
486 docstring ldelim = arg.ldelim.empty() ?
487 from_ascii("{") : arg.ldelim;
488 docstring rdelim = arg.rdelim.empty() ?
489 from_ascii("}") : arg.rdelim;
490 os << ldelim << preset << rdelim;
491 } else if (!preset.empty()) {
492 docstring ldelim = arg.ldelim.empty() ?
493 from_ascii("[") : arg.ldelim;
494 docstring rdelim = arg.rdelim.empty() ?
495 from_ascii("]") : arg.rdelim;
496 os << ldelim << preset << rdelim;
497 } else if (find(required.begin(), required.end(),
498 (*lait).first) != required.end()) {
499 docstring ldelim = arg.ldelim.empty() ?
500 from_ascii("[") : arg.ldelim;
501 docstring rdelim = arg.rdelim.empty() ?
502 from_ascii("]") : arg.rdelim;
503 os << ldelim << rdelim;
516 void pushLanguageName(string const & lang_name, bool localswitch)
518 OutputState * state = getOutputState();
520 int nest_level = localswitch ? -state->nest_level_ : state->nest_level_;
521 state->lang_switch_depth_.push(nest_level);
522 state->open_polyglossia_lang_.push(lang_name);
526 void popLanguageName()
528 OutputState * state = getOutputState();
530 state->lang_switch_depth_.pop();
531 state->open_polyglossia_lang_.pop();
535 string const & openLanguageName()
537 OutputState * state = getOutputState();
539 return openLanguageName(state);
545 void addArgInsets(Paragraph const & par, string const & prefix,
546 Layout::LaTeXArgMap const & latexargs,
547 map<int, InsetArgument const *> & ilist,
548 vector<string> & required)
550 for (auto const & table : par.insetList()) {
551 InsetArgument const * arg = table.inset->asInsetArgument();
554 if (arg->name().empty()) {
555 LYXERR0("Error: Unnamed argument inset!");
558 string const name = prefix.empty() ?
559 arg->name() : split(arg->name(), ':');
560 // why converting into an integer?
561 unsigned int const nr = convert<unsigned int>(name);
562 if (ilist.find(nr) == ilist.end())
564 Layout::LaTeXArgMap::const_iterator const lit =
565 latexargs.find(arg->name());
566 if (lit != latexargs.end()) {
567 Layout::latexarg const & larg = lit->second;
568 vector<string> req = getVectorFromString(larg.requires);
569 move(req.begin(), req.end(), back_inserter(required));
577 void latexArgInsets(Paragraph const & par, otexstream & os,
578 OutputParams const & runparams,
579 Layout::LaTeXArgMap const & latexargs,
580 string const & prefix)
582 map<int, InsetArgument const *> ilist;
583 vector<string> required;
584 addArgInsets(par, prefix, latexargs, ilist, required);
585 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
589 void latexArgInsets(ParagraphList const & pars,
590 ParagraphList::const_iterator pit,
591 otexstream & os, OutputParams const & runparams,
592 Layout::LaTeXArgMap const & latexargs,
593 string const & prefix)
595 map<int, InsetArgument const *> ilist;
596 vector<string> required;
598 depth_type const current_depth = pit->params().depth();
599 Layout const current_layout = pit->layout();
601 // get the first paragraph in sequence with this layout and depth
604 if (lyx::prev(pit, offset) == pars.begin())
606 ParagraphList::const_iterator priorpit = lyx::prev(pit, offset + 1);
607 if (priorpit->layout() == current_layout
608 && priorpit->params().depth() == current_depth)
614 ParagraphList::const_iterator spit = lyx::prev(pit, offset);
616 for (; spit != pars.end(); ++spit) {
617 if (spit->layout() != current_layout ||
618 spit->params().depth() < current_depth)
620 if (spit->params().depth() > current_depth)
622 addArgInsets(*spit, prefix, latexargs, ilist, required);
624 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
628 void latexArgInsetsForParent(ParagraphList const & pars, otexstream & os,
629 OutputParams const & runparams,
630 Layout::LaTeXArgMap const & latexargs,
631 string const & prefix)
633 map<int, InsetArgument const *> ilist;
634 vector<string> required;
636 for (Paragraph const & par : pars) {
637 if (par.layout().hasArgs())
638 // The InsetArguments inside this paragraph refer to this paragraph
640 addArgInsets(par, prefix, latexargs, ilist, required);
642 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
648 // output the proper paragraph start according to latextype.
649 void parStartCommand(Paragraph const & par, otexstream & os,
650 OutputParams const & runparams, Layout const & style)
652 switch (style.latextype) {
654 os << '\\' << from_ascii(style.latexname());
657 if (!style.latexargs().empty())
658 latexArgInsets(par, os, runparams, style.latexargs());
659 os << from_ascii(style.latexparam());
661 case LATEX_ITEM_ENVIRONMENT:
662 case LATEX_LIST_ENVIRONMENT:
663 os << "\\" + style.itemcommand();
665 if (!style.itemargs().empty())
666 latexArgInsets(par, os, runparams, style.itemargs(), "item:");
669 case LATEX_BIB_ENVIRONMENT:
670 // ignore this, the inset will write itself
679 // FIXME: this should be anonymous
680 void TeXOnePar(Buffer const & buf,
684 OutputParams const & runparams_in,
685 string const & everypar,
686 int start_pos, int end_pos,
689 BufferParams const & bparams = runparams_in.is_child
690 ? buf.masterParams() : buf.params();
691 ParagraphList const & paragraphs = text.paragraphs();
692 Paragraph const & par = paragraphs.at(pit);
693 // FIXME This check should not really be needed.
694 // Perhaps we should issue an error if it is.
695 Layout const & style = text.inset().forcePlainLayout() ?
696 bparams.documentClass().plainLayout() : par.layout();
698 if (style.inpreamble && !force)
701 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
704 OutputParams runparams = runparams_in;
705 runparams.isLastPar = (pit == pit_type(paragraphs.size() - 1));
706 // We reinitialze par begin and end to be on the safe side
707 // with embedded inset as we don't know if they set those
709 runparams.par_begin = 0;
710 runparams.par_end = 0;
712 bool const maintext = text.isMainText();
713 // we are at the beginning of an inset and CJK is already open;
714 // we count inheritation levels to get the inset nesting right.
715 OutputState * state = getOutputState();
716 if (pit == 0 && !maintext
717 && (state->cjk_inherited_ > 0 || state->open_encoding_ == CJK)) {
718 state->cjk_inherited_ += 1;
719 state->open_encoding_ = none;
722 if (text.inset().isPassThru()) {
723 Font const outerfont = text.outerFont(pit);
725 // No newline before first paragraph in this lyxtext
728 if (!text.inset().getLayout().parbreakIsNewline())
732 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos, force);
736 Paragraph const * nextpar = runparams.isLastPar
737 ? 0 : ¶graphs.at(pit + 1);
739 if (style.pass_thru) {
740 Font const outerfont = text.outerFont(pit);
741 parStartCommand(par, os, runparams, style);
743 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos, force);
745 // I did not create a parEndCommand for this minuscule
746 // task because in the other user of parStartCommand
747 // the code is different (JMarc)
748 if (style.isCommand())
752 if (!style.parbreak_is_newline) {
754 } else if (nextpar && !style.isEnvironment()) {
755 Layout const nextstyle = text.inset().forcePlainLayout()
756 ? bparams.documentClass().plainLayout()
758 if (nextstyle.name() != style.name())
765 // This paragraph's language
766 Language const * const par_language = par.getParLanguage(bparams);
767 Language const * const nextpar_language = nextpar ?
768 nextpar->getParLanguage(bparams) : 0;
769 // The document's language
770 Language const * const doc_language = bparams.language;
771 // The language that was in effect when the environment this paragraph is
772 // inside of was opened
773 Language const * const outer_language =
774 (runparams.local_font != 0) ?
775 runparams.local_font->language() : doc_language;
777 Paragraph const * priorpar = (pit == 0) ? 0 : ¶graphs.at(pit - 1);
779 // The previous language that was in effect is the language of the
780 // previous paragraph, unless the previous paragraph is inside an
781 // environment with nesting depth greater than (or equal to, but with
782 // a different layout) the current one. If there is no previous
783 // paragraph, the previous language is the outer language.
784 bool const use_prev_env_language = state->prev_env_language_ != 0
786 && priorpar->layout().isEnvironment()
787 && (priorpar->getDepth() > par.getDepth()
788 || (priorpar->getDepth() == par.getDepth()
789 && priorpar->layout() != par.layout()));
790 Language const * const prev_language =
792 ? (use_prev_env_language ? state->prev_env_language_
793 : priorpar->getParLanguage(bparams))
797 bool const use_polyglossia = runparams.use_polyglossia;
798 string const par_lang = use_polyglossia ?
799 getPolyglossiaEnvName(par_language): par_language->babel();
800 string const prev_lang = use_polyglossia ?
801 getPolyglossiaEnvName(prev_language) : prev_language->babel();
802 string const outer_lang = use_polyglossia ?
803 getPolyglossiaEnvName(outer_language) : outer_language->babel();
804 string const nextpar_lang = nextpar_language ? (use_polyglossia ?
805 getPolyglossiaEnvName(nextpar_language) :
806 nextpar_language->babel()) : string();
807 string lang_begin_command = use_polyglossia ?
808 "\\begin{$$lang}$$opts" : lyxrc.language_command_begin;
809 string lang_end_command = use_polyglossia ?
810 "\\end{$$lang}" : lyxrc.language_command_end;
811 // the '%' is necessary to prevent unwanted whitespace
812 string lang_command_termination = "%\n";
813 bool const using_begin_end = use_polyglossia ||
814 !lang_end_command.empty();
816 // For InTitle commands, we need to switch the language inside the command
817 // (see #10849); thus open the command here.
818 bool const intitle_command = style.intitle && style.latextype == LATEX_COMMAND;
819 if (intitle_command) {
820 parStartCommand(par, os, runparams, style);
824 // In some insets (such as Arguments), we cannot use \selectlanguage
825 bool const localswitch = text.inset().forceLocalFontSwitch()
826 || (using_begin_end && text.inset().forcePlainLayout());
828 lang_begin_command = use_polyglossia ?
829 "\\text$$lang$$opts{" : lyxrc.language_command_local;
830 lang_end_command = "}";
831 lang_command_termination.clear();
834 if (par_lang != prev_lang
835 // check if we already put language command in TeXEnvironment()
836 && !(style.isEnvironment()
837 && (pit == 0 || (priorpar->layout() != par.layout()
838 && priorpar->getDepth() <= par.getDepth())
839 || priorpar->getDepth() < par.getDepth())))
841 if ((!using_begin_end || langOpenedAtThisLevel(state)) &&
842 !lang_end_command.empty() &&
843 prev_lang != outer_lang &&
844 !prev_lang.empty() &&
845 (!using_begin_end || !style.isEnvironment()))
847 os << from_ascii(subst(lang_end_command,
850 << lang_command_termination;
855 // We need to open a new language if we couldn't close the previous
856 // one (because there's no language_command_end); and even if we closed
857 // the previous one, if the current language is different than the
858 // outer_language (which is currently in effect once the previous one
860 if ((lang_end_command.empty() || par_lang != outer_lang
862 || (style.isEnvironment() && par_lang != prev_lang)))
863 && !par_lang.empty()) {
864 // If we're inside an inset, and that inset is within an \L or \R
865 // (or equivalents), then within the inset, too, any opposite
866 // language paragraph should appear within an \L or \R (in addition
867 // to, outside of, the normal language switch commands).
868 // This behavior is not correct for ArabTeX, though.
871 && par_language->lang() != "arabic_arabtex"
872 && outer_language->lang() != "arabic_arabtex"
873 // are we in an inset?
874 && runparams.local_font != 0
875 // is the inset within an \L or \R?
877 // FIXME: currently, we don't check this; this means that
878 // we'll have unnnecessary \L and \R commands, but that
879 // doesn't seem to hurt (though latex will complain)
881 // is this paragraph in the opposite direction?
882 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()) {
883 // FIXME: I don't have a working copy of the Arabi package, so
884 // I'm not sure if the farsi and arabic_arabi stuff is correct
886 if (par_language->lang() == "farsi")
888 else if (outer_language->lang() == "farsi")
890 else if (par_language->lang() == "arabic_arabi")
892 else if (outer_language->lang() == "arabic_arabi")
894 // remaining RTL languages currently is hebrew
895 else if (par_language->rightToLeft())
900 // With CJK, the CJK tag has to be closed first (see below)
901 if (runparams.encoding->package() != Encoding::CJK
902 && (par_lang != openLanguageName(state) || localswitch)
903 && !par_lang.empty()) {
904 string bc = use_polyglossia ?
905 getPolyglossiaBegin(lang_begin_command, par_lang, par_language->polyglossiaOpts())
906 : subst(lang_begin_command, "$$lang", par_lang);
908 os << lang_command_termination;
910 pushLanguageName(par_lang, localswitch);
915 // Switch file encoding if necessary; no need to do this for "default"
916 // encoding, since this only affects the position of the outputted
917 // \inputencoding command; the encoding switch will occur when necessary
918 if (bparams.inputenc == "auto"
919 && !runparams.isFullUnicode() // Xe/LuaTeX use one document-wide encoding (see also switchEncoding())
920 && runparams.encoding->package() != Encoding::none) {
921 // Look ahead for future encoding changes.
922 // We try to output them at the beginning of the paragraph,
923 // since the \inputencoding command is not allowed e.g. in
924 // sections. For this reason we only set runparams.moving_arg
925 // after checking for the encoding change, otherwise the
926 // change would be always avoided by switchEncoding().
927 for (pos_type i = 0; i < par.size(); ++i) {
928 char_type const c = par.getChar(i);
929 Encoding const * const encoding =
930 par.getFontSettings(bparams, i).language()->encoding();
931 if (encoding->package() != Encoding::CJK
932 && runparams.encoding->package() == Encoding::inputenc
937 // All characters before c are in the ASCII range, and
938 // c is non-ASCII (but no inset), so change the
939 // encoding to that required by the language of c.
940 // With CJK, only add switch if we have CJK content at the beginning
942 if (i != 0 && encoding->package() == Encoding::CJK)
945 pair<bool, int> enc_switch = switchEncoding(os.os(),
946 bparams, runparams, *encoding);
947 // the following is necessary after a CJK environment in a multilingual
948 // context (nesting issue).
949 if (par_language->encoding()->package() == Encoding::CJK
950 && state->open_encoding_ != CJK && state->cjk_inherited_ == 0) {
951 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
952 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
953 state->open_encoding_ = CJK;
955 if (encoding->package() != Encoding::none && enc_switch.first) {
956 if (enc_switch.second > 0) {
957 // the '%' is necessary to prevent unwanted whitespace
960 // With CJK, the CJK tag had to be closed first (see above)
961 if (runparams.encoding->package() == Encoding::CJK
962 && par_lang != openLanguageName(state)
963 && !par_lang.empty()) {
964 os << from_ascii(subst(
968 << lang_command_termination;
970 pushLanguageName(par_lang, localswitch);
972 runparams.encoding = encoding;
978 runparams.moving_arg |= style.needprotect;
979 Encoding const * const prev_encoding = runparams.encoding;
981 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
982 if (par.allowParagraphCustomization()) {
983 if (par.params().startOfAppendix()) {
984 os << "\n\\appendix\n";
987 // InTitle commands must use switches (not environments)
988 // inside the commands (see #9332)
990 if (!par.params().spacing().isDefault())
992 if (runparams.moving_arg)
994 os << from_ascii(par.params().spacing().writeCmd(useSetSpace));
997 if (!par.params().spacing().isDefault()
998 && (pit == 0 || !priorpar->hasSameLayout(par)))
1000 os << from_ascii(par.params().spacing().writeEnvirBegin(useSetSpace))
1004 if (style.isCommand()) {
1010 // For InTitle commands, we already started the command before
1011 // the language switch
1012 if (!intitle_command)
1013 parStartCommand(par, os, runparams, style);
1015 Font const outerfont = text.outerFont(pit);
1018 os << from_utf8(everypar);
1019 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos, force);
1021 Font const font = par.empty()
1022 ? par.getLayoutFont(bparams, outerfont)
1023 : par.getFont(bparams, par.size() - 1, outerfont);
1025 bool const is_command = style.isCommand();
1027 // InTitle commands need to be closed after the language has been closed.
1028 if (!intitle_command) {
1031 if (!style.postcommandargs().empty())
1032 latexArgInsets(par, os, runparams, style.postcommandargs(), "post:");
1033 if (runparams.encoding != prev_encoding) {
1034 runparams.encoding = prev_encoding;
1035 os << setEncoding(prev_encoding->iconvName());
1040 bool pending_newline = false;
1041 bool unskip_newline = false;
1042 bool close_lang_switch = false;
1043 switch (style.latextype) {
1044 case LATEX_ITEM_ENVIRONMENT:
1045 case LATEX_LIST_ENVIRONMENT:
1046 if ((nextpar && par_lang != nextpar_lang
1047 && nextpar->getDepth() == par.getDepth())
1048 || (atSameLastLangSwitchDepth(state) && nextpar
1049 && nextpar->getDepth() < par.getDepth()))
1050 close_lang_switch = using_begin_end;
1051 if (nextpar && par.params().depth() < nextpar->params().depth())
1052 pending_newline = true;
1054 case LATEX_ENVIRONMENT: {
1055 // if its the last paragraph of the current environment
1056 // skip it otherwise fall through
1058 && ((nextpar->layout() != par.layout()
1059 || nextpar->params().depth() != par.params().depth())
1060 || (!using_begin_end || par_lang != nextpar_lang)))
1062 close_lang_switch = using_begin_end;
1069 // we don't need it for the last paragraph and in InTitle commands!!!
1070 if (nextpar && !intitle_command)
1071 pending_newline = true;
1074 // InTitle commands use switches (not environments) for space settings
1075 if (par.allowParagraphCustomization() && !style.intitle) {
1076 if (!par.params().spacing().isDefault()
1077 && (runparams.isLastPar || !nextpar->hasSameLayout(par))) {
1078 if (pending_newline)
1081 string const endtag =
1082 par.params().spacing().writeEnvirEnd(useSetSpace);
1083 if (prefixIs(endtag, "\\end{"))
1086 os << from_ascii(endtag);
1087 pending_newline = true;
1091 // Closing the language is needed for the last paragraph in a given language
1092 // as well as for any InTitleCommand (since these set the language locally);
1093 // it is also needed if we're within an \L or \R that we may have opened above
1094 // (not necessarily in this paragraph) and are about to close.
1095 bool closing_rtl_ltr_environment = !using_begin_end
1097 && (par_language->lang() != "arabic_arabtex"
1098 && outer_language->lang() != "arabic_arabtex")
1099 // have we opened an \L or \R environment?
1100 && runparams.local_font != 0
1101 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()
1102 // are we about to close the language?
1103 &&((nextpar && par_lang != nextpar_lang)
1104 || (runparams.isLastPar && par_lang != outer_lang));
1106 if ((intitle_command && using_begin_end)
1107 || closing_rtl_ltr_environment
1108 || ((runparams.isLastPar || close_lang_switch)
1109 && (par_lang != outer_lang || (using_begin_end
1110 && style.isEnvironment()
1111 && par_lang != nextpar_lang)))) {
1112 // Since \selectlanguage write the language to the aux file,
1113 // we need to reset the language at the end of footnote or
1116 if (pending_newline || close_lang_switch)
1119 // when the paragraph uses CJK, the language has to be closed earlier
1120 if (font.language()->encoding()->package() != Encoding::CJK) {
1121 if (lang_end_command.empty()) {
1122 // If this is a child, we should restore the
1123 // master language after the last paragraph.
1124 Language const * const current_language =
1125 (runparams.isLastPar && runparams.master_language)
1126 ? runparams.master_language
1128 string const current_lang = use_polyglossia
1129 ? getPolyglossiaEnvName(current_language)
1130 : current_language->babel();
1131 if (!current_lang.empty()
1132 && current_lang != openLanguageName(state)) {
1133 string bc = use_polyglossia ?
1134 getPolyglossiaBegin(lang_begin_command, current_lang,
1135 current_language->polyglossiaOpts())
1136 : subst(lang_begin_command, "$$lang", current_lang);
1138 pending_newline = !localswitch;
1139 unskip_newline = !localswitch;
1140 if (using_begin_end)
1141 pushLanguageName(current_lang, localswitch);
1143 } else if ((!using_begin_end ||
1144 langOpenedAtThisLevel(state)) &&
1145 !par_lang.empty()) {
1146 // If we are in an environment, we have to
1147 // close the "outer" language afterwards
1148 string const & cur_lang = openLanguageName(state);
1149 if (!style.isEnvironment()
1150 || (close_lang_switch
1151 && atSameLastLangSwitchDepth(state)
1152 && par_lang != outer_lang
1153 && (par_lang != cur_lang
1154 || (cur_lang != outer_lang
1156 && style != nextpar->layout())))
1157 || (atSameLastLangSwitchDepth(state)
1158 && state->lang_switch_depth_.size()
1159 && cur_lang != par_lang))
1161 if (using_begin_end && !localswitch)
1163 os << from_ascii(subst(
1167 pending_newline = !localswitch;
1168 unskip_newline = !localswitch;
1169 if (using_begin_end)
1175 if (closing_rtl_ltr_environment)
1178 // InTitle commands need to be closed after the language has been closed.
1179 if (intitle_command) {
1182 if (!style.postcommandargs().empty())
1183 latexArgInsets(par, os, runparams, style.postcommandargs(), "post:");
1184 if (runparams.encoding != prev_encoding) {
1185 runparams.encoding = prev_encoding;
1186 os << setEncoding(prev_encoding->iconvName());
1191 bool const last_was_separator =
1192 par.size() > 0 && par.isEnvSeparator(par.size() - 1);
1194 if (pending_newline) {
1196 // prevent unwanted whitespace
1198 if (!os.afterParbreak() && !last_was_separator)
1202 // if this is a CJK-paragraph and the next isn't, close CJK
1203 // also if the next paragraph is a multilingual environment (because of nesting)
1205 && state->open_encoding_ == CJK
1206 && (nextpar_language->encoding()->package() != Encoding::CJK
1207 || (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
1208 // inbetween environments, CJK has to be closed later (nesting!)
1209 && (!style.isEnvironment() || !nextpar->layout().isEnvironment())) {
1210 os << "\\end{CJK}\n";
1211 state->open_encoding_ = none;
1214 // If this is the last paragraph, close the CJK environment
1215 // if necessary. If it's an environment, we'll have to \end that first.
1216 if (runparams.isLastPar && !style.isEnvironment()) {
1217 switch (state->open_encoding_) {
1219 // do nothing at the end of child documents
1220 if (maintext && buf.masterBuffer() != &buf)
1224 os << "\n\\end{CJK}\n";
1228 state->open_encoding_ = none;
1233 state->open_encoding_ = none;
1243 // If this is the last paragraph, and a local_font was set upon entering
1244 // the inset, and we're using "auto" or "default" encoding, and not
1245 // compiling with XeTeX or LuaTeX, the encoding
1246 // should be set back to that local_font's encoding.
1247 if (runparams.isLastPar && runparams_in.local_font != 0
1248 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
1249 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
1250 && !runparams.isFullUnicode()
1252 runparams_in.encoding = runparams_in.local_font->language()->encoding();
1253 os << setEncoding(runparams_in.encoding->iconvName());
1255 // Otherwise, the current encoding should be set for the next paragraph.
1257 runparams_in.encoding = runparams.encoding;
1260 // we don't need a newline for the last paragraph!!!
1261 // Note from JMarc: we will re-add a \n explicitly in
1262 // TeXEnvironment, because it is needed in this case
1263 if (nextpar && !os.afterParbreak() && !last_was_separator) {
1264 // Make sure to start a new line
1266 Layout const & next_layout = nextpar->layout();
1267 // A newline '\n' is always output before a command,
1268 // so avoid doubling it.
1269 if (!next_layout.isCommand()) {
1270 // Here we now try to avoid spurious empty lines by
1271 // outputting a paragraph break only if: (case 1) the
1272 // paragraph style allows parbreaks and no \begin, \end
1273 // or \item tags are going to follow (i.e., if the next
1274 // isn't the first or the current isn't the last
1275 // paragraph of an environment or itemize) and the
1276 // depth and alignment of the following paragraph is
1277 // unchanged, or (case 2) the following is a
1278 // non-environment paragraph whose depth is increased
1279 // but whose alignment is unchanged, or (case 3) the
1280 // paragraph is not an environment and the next one is a
1281 // non-itemize-like env at lower depth, or (case 4) the
1282 // paragraph is a command not followed by an environment
1283 // and the alignment of the current and next paragraph
1284 // is unchanged, or (case 5) the current alignment is
1285 // changed and a standard paragraph follows.
1286 DocumentClass const & tclass = bparams.documentClass();
1287 if ((style == next_layout
1288 && !style.parbreak_is_newline
1289 && !text.inset().getLayout().parbreakIsNewline()
1290 && style.latextype != LATEX_ITEM_ENVIRONMENT
1291 && style.latextype != LATEX_LIST_ENVIRONMENT
1292 && style.align == par.getAlign()
1293 && nextpar->getDepth() == par.getDepth()
1294 && nextpar->getAlign() == par.getAlign())
1295 || (!next_layout.isEnvironment()
1296 && nextpar->getDepth() > par.getDepth()
1297 && nextpar->getAlign() == par.getAlign())
1298 || (!style.isEnvironment()
1299 && next_layout.latextype == LATEX_ENVIRONMENT
1300 && nextpar->getDepth() < par.getDepth())
1301 || (style.isCommand()
1302 && !next_layout.isEnvironment()
1303 && style.align == par.getAlign()
1304 && next_layout.align == nextpar->getAlign())
1305 || (style.align != par.getAlign()
1306 && tclass.isDefaultLayout(next_layout))) {
1312 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " done; ptr "
1313 << &par << " next " << nextpar);
1319 // LaTeX all paragraphs
1320 void latexParagraphs(Buffer const & buf,
1323 OutputParams const & runparams,
1324 string const & everypar)
1326 LASSERT(runparams.par_begin <= runparams.par_end,
1327 { os << "% LaTeX Output Error\n"; return; } );
1329 BufferParams const & bparams = buf.params();
1330 BufferParams const & mparams = buf.masterParams();
1332 bool const maintext = text.isMainText();
1333 bool const is_child = buf.masterBuffer() != &buf;
1334 bool const multibib_child = maintext && is_child
1335 && mparams.multibib == "child";
1337 if (multibib_child && mparams.useBiblatex())
1338 os << "\\newrefsection";
1339 else if (multibib_child && mparams.useBibtopic()) {
1340 os << "\\begin{btUnit}\n";
1341 runparams.openbtUnit = true;
1344 // Open a CJK environment at the beginning of the main buffer
1345 // if the document's language is a CJK language
1346 // (but not in child documents)
1347 OutputState * state = getOutputState();
1348 if (maintext && !is_child
1349 && bparams.encoding().package() == Encoding::CJK) {
1350 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
1351 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
1352 state->open_encoding_ = CJK;
1354 // if "auto begin" is switched off, explicitly switch the
1355 // language on at start
1356 string const mainlang = runparams.use_polyglossia
1357 ? getPolyglossiaEnvName(bparams.language)
1358 : bparams.language->babel();
1359 string const lang_begin_command = runparams.use_polyglossia ?
1360 "\\begin{$$lang}$$opts" : lyxrc.language_command_begin;
1361 string const lang_end_command = runparams.use_polyglossia ?
1362 "\\end{$$lang}" : lyxrc.language_command_end;
1363 bool const using_begin_end = runparams.use_polyglossia ||
1364 !lang_end_command.empty();
1366 if (maintext && !lyxrc.language_auto_begin &&
1367 !mainlang.empty()) {
1369 string bc = runparams.use_polyglossia ?
1370 getPolyglossiaBegin(lang_begin_command, mainlang,
1371 bparams.language->polyglossiaOpts())
1372 : subst(lang_begin_command, "$$lang", mainlang);
1375 if (using_begin_end)
1376 pushLanguageName(mainlang);
1379 ParagraphList const & paragraphs = text.paragraphs();
1381 if (runparams.par_begin == runparams.par_end) {
1382 // The full doc will be exported but it is easier to just rely on
1383 // runparams range parameters that will be passed TeXEnvironment.
1384 runparams.par_begin = 0;
1385 runparams.par_end = paragraphs.size();
1388 pit_type pit = runparams.par_begin;
1389 // lastpit is for the language check after the loop.
1390 pit_type lastpit = pit;
1391 // variables used in the loop:
1392 bool was_title = false;
1393 bool already_title = false;
1394 DocumentClass const & tclass = bparams.documentClass();
1396 // Did we already warn about inTitle layout mixing? (we only warn once)
1397 bool gave_layout_warning = false;
1398 for (; pit < runparams.par_end; ++pit) {
1400 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
1402 // FIXME This check should not be needed. We should
1403 // perhaps issue an error if it is.
1404 Layout const & layout = text.inset().forcePlainLayout() ?
1405 tclass.plainLayout() : par->layout();
1407 if (layout.intitle) {
1408 if (already_title) {
1409 if (!gave_layout_warning && !runparams.dryrun) {
1410 gave_layout_warning = true;
1411 frontend::Alert::warning(_("Error in latexParagraphs"),
1412 bformat(_("You are using at least one "
1413 "layout (%1$s) intended for the title, "
1414 "after using non-title layouts. This "
1415 "could lead to missing or incorrect output."
1418 } else if (!was_title) {
1420 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1422 << from_ascii(tclass.titlename())
1426 } else if (was_title && !already_title && !layout.inpreamble) {
1427 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1428 os << "\\end{" << from_ascii(tclass.titlename())
1432 os << "\\" << from_ascii(tclass.titlename())
1435 already_title = true;
1439 if (layout.isCommand() && !layout.latexname().empty()
1440 && layout.latexname() == bparams.multibib) {
1441 if (runparams.openbtUnit)
1442 os << "\\end{btUnit}\n";
1443 if (!bparams.useBiblatex()) {
1444 os << '\n' << "\\begin{btUnit}\n";
1445 runparams.openbtUnit = true;
1449 if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
1450 // This is a standard top level paragraph, TeX it and continue.
1451 TeXOnePar(buf, text, pit, os, runparams, everypar);
1455 TeXEnvironmentData const data =
1456 prepareEnvironment(buf, text, par, os, runparams);
1457 // pit can be changed in TeXEnvironment.
1458 TeXEnvironment(buf, text, runparams, pit, os);
1459 finishEnvironment(os, runparams, data);
1462 if (pit == runparams.par_end) {
1463 // Make sure that the last paragraph is
1464 // correctly terminated (because TeXOnePar does
1465 // not add a \n in this case)
1469 // It might be that we only have a title in this document
1470 if (was_title && !already_title) {
1471 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1472 os << "\\end{" << from_ascii(tclass.titlename())
1475 os << "\\" << from_ascii(tclass.titlename())
1480 if (maintext && !is_child && runparams.openbtUnit)
1481 os << "\\end{btUnit}\n";
1483 // if "auto end" is switched off, explicitly close the language at the end
1484 // but only if the last par is in a babel or polyglossia language
1485 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1486 paragraphs.at(lastpit).getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1487 os << from_utf8(subst(lang_end_command,
1491 if (using_begin_end)
1495 // If the last paragraph is an environment, we'll have to close
1496 // CJK at the very end to do proper nesting.
1497 if (maintext && !is_child && state->open_encoding_ == CJK) {
1498 os << "\\end{CJK}\n";
1499 state->open_encoding_ = none;
1501 // Likewise for polyglossia or when using begin/end commands
1502 string const & cur_lang = openLanguageName(state);
1503 if (maintext && !is_child && !cur_lang.empty()) {
1504 os << from_utf8(subst(lang_end_command,
1508 if (using_begin_end)
1512 // reset inherited encoding
1513 if (state->cjk_inherited_ > 0) {
1514 state->cjk_inherited_ -= 1;
1515 if (state->cjk_inherited_ == 0)
1516 state->open_encoding_ = CJK;
1519 if (multibib_child && mparams.useBibtopic()) {
1520 os << "\\end{btUnit}\n";
1521 runparams.openbtUnit = false;
1526 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1527 OutputParams const & runparams, Encoding const & newEnc,
1530 // XeTeX/LuaTeX use only one encoding per document:
1531 // * with useNonTeXFonts: "utf8plain",
1532 // * with XeTeX and TeX fonts: "ascii" (inputenc fails),
1533 // * with LuaTeX and TeX fonts: only one encoding accepted by luainputenc.
1534 if (runparams.isFullUnicode() || newEnc.name() == "inherit")
1535 return make_pair(false, 0);
1537 Encoding const & oldEnc = *runparams.encoding;
1538 bool moving_arg = runparams.moving_arg;
1539 // If we switch from/to CJK, we need to switch anyway, despite custom inputenc
1540 bool const from_to_cjk =
1541 (oldEnc.package() == Encoding::CJK && newEnc.package() != Encoding::CJK)
1542 || (oldEnc.package() != Encoding::CJK && newEnc.package() == Encoding::CJK);
1543 if (!force && !from_to_cjk
1544 && ((bparams.inputenc != "auto" && bparams.inputenc != "default") || moving_arg))
1545 return make_pair(false, 0);
1547 // Do nothing if the encoding is unchanged.
1548 if (oldEnc.name() == newEnc.name())
1549 return make_pair(false, 0);
1551 // FIXME We ignore encoding switches from/to encodings that do
1552 // neither support the inputenc package nor the CJK package here.
1553 // This does of course only work in special cases (e.g. switch from
1554 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1555 // but it is the best we can do
1556 if (oldEnc.package() == Encoding::none
1557 || newEnc.package() == Encoding::none)
1558 return make_pair(false, 0);
1560 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1561 << oldEnc.name() << " to " << newEnc.name());
1562 os << setEncoding(newEnc.iconvName());
1563 if (bparams.inputenc == "default")
1564 return make_pair(true, 0);
1566 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1567 OutputState * state = getOutputState();
1568 switch (newEnc.package()) {
1569 case Encoding::none:
1570 case Encoding::japanese:
1571 // shouldn't ever reach here, see above
1572 return make_pair(true, 0);
1573 case Encoding::inputenc: {
1574 int count = inputenc_arg.length();
1575 if (oldEnc.package() == Encoding::CJK &&
1576 state->open_encoding_ == CJK) {
1578 state->open_encoding_ = none;
1581 else if (oldEnc.package() == Encoding::inputenc &&
1582 state->open_encoding_ == inputenc) {
1584 state->open_encoding_ = none;
1587 if (runparams.local_font != 0
1588 && oldEnc.package() == Encoding::CJK) {
1589 // within insets, \inputenc switches need
1590 // to be embraced within \bgroup...\egroup;
1594 state->open_encoding_ = inputenc;
1596 // with the japanese option, inputenc is omitted.
1597 if (runparams.use_japanese)
1598 return make_pair(true, count);
1599 os << "\\inputencoding{" << inputenc_arg << '}';
1600 return make_pair(true, count + 16);
1602 case Encoding::CJK: {
1603 int count = inputenc_arg.length();
1604 if (oldEnc.package() == Encoding::CJK &&
1605 state->open_encoding_ == CJK) {
1609 if (oldEnc.package() == Encoding::inputenc &&
1610 state->open_encoding_ == inputenc) {
1614 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1615 << from_ascii(bparams.fonts_cjk) << "}";
1616 state->open_encoding_ = CJK;
1617 return make_pair(true, count + 15);
1620 // Dead code to avoid a warning:
1621 return make_pair(true, 0);