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,
146 bool const localswitch = false)
150 // we need to revert the upcasing done in getPolyglossiaEnvName()
151 // in case we have a local polyglossia command (\textarabic).
152 string language = localswitch ? ascii_lowercase(lang) : lang;
153 result = subst(lang_begin_command, "$$lang", language);
155 string options = opts.empty() ?
156 string() : "[" + opts + "]";
157 result = subst(result, "$$opts", options);
163 struct TeXEnvironmentData
166 Layout const * style;
167 Language const * par_language;
168 Encoding const * prev_encoding;
169 bool leftindent_open;
173 static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
175 ParagraphList::const_iterator pit,
177 OutputParams const & runparams)
179 TeXEnvironmentData data;
181 BufferParams const & bparams = buf.params();
183 // FIXME This test should not be necessary.
184 // We should perhaps issue an error if it is.
185 Layout const & style = text.inset().forcePlainLayout() ?
186 bparams.documentClass().plainLayout() : pit->layout();
188 ParagraphList const & paragraphs = text.paragraphs();
189 ParagraphList::const_iterator const priorpit =
190 pit == paragraphs.begin() ? pit : prev(pit, 1);
192 OutputState * state = getOutputState();
193 bool const use_prev_env_language = state->prev_env_language_ != 0
194 && priorpit->layout().isEnvironment()
195 && (priorpit->getDepth() > pit->getDepth()
196 || (priorpit->getDepth() == pit->getDepth()
197 && priorpit->layout() != pit->layout()));
199 data.prev_encoding = runparams.encoding;
200 data.par_language = pit->getParLanguage(bparams);
201 Language const * const doc_language = bparams.language;
202 Language const * const prev_par_language =
203 (pit != paragraphs.begin())
204 ? (use_prev_env_language ? state->prev_env_language_
205 : priorpit->getParLanguage(bparams))
208 bool const use_polyglossia = runparams.use_polyglossia;
209 string const par_lang = use_polyglossia ?
210 getPolyglossiaEnvName(data.par_language) : data.par_language->babel();
211 string const prev_par_lang = use_polyglossia ?
212 getPolyglossiaEnvName(prev_par_language) : prev_par_language->babel();
213 string const doc_lang = use_polyglossia ?
214 getPolyglossiaEnvName(doc_language) : doc_language->babel();
215 string const lang_begin_command = use_polyglossia ?
216 "\\begin{$$lang}" : lyxrc.language_command_begin;
217 string const lang_end_command = use_polyglossia ?
218 "\\end{$$lang}" : lyxrc.language_command_end;
219 bool const using_begin_end = use_polyglossia ||
220 !lang_end_command.empty();
222 // For polyglossia, switch language outside of environment, if possible.
223 if (par_lang != prev_par_lang) {
224 if ((!using_begin_end || langOpenedAtThisLevel(state)) &&
225 !lang_end_command.empty() &&
226 prev_par_lang != doc_lang &&
227 !prev_par_lang.empty()) {
228 os << from_ascii(subst(
232 // the '%' is necessary to prevent unwanted whitespace
238 // If no language was explicitly opened and we are using
239 // polyglossia or begin/end commands, then the current
240 // language is the document language.
241 string const & cur_lang = using_begin_end
242 && state->lang_switch_depth_.size()
243 ? openLanguageName(state)
246 if ((lang_end_command.empty() ||
247 par_lang != doc_lang ||
248 par_lang != cur_lang) &&
250 string bc = use_polyglossia ?
251 getPolyglossiaBegin(lang_begin_command, par_lang,
252 data.par_language->polyglossiaOpts())
253 : subst(lang_begin_command, "$$lang", par_lang);
255 // the '%' is necessary to prevent unwanted whitespace
258 pushLanguageName(par_lang);
262 data.leftindent_open = false;
263 if (!pit->params().leftIndent().zero()) {
264 os << "\\begin{LyXParagraphLeftIndent}{"
265 << from_ascii(pit->params().leftIndent().asLatexString())
267 data.leftindent_open = true;
270 if (style.isEnvironment())
271 state->nest_level_ += 1;
273 if (style.isEnvironment() && !style.latexname().empty()) {
274 os << "\\begin{" << from_ascii(style.latexname()) << '}';
275 if (!style.latexargs().empty()) {
276 OutputParams rp = runparams;
277 rp.local_font = &pit->getFirstFontSettings(bparams);
278 latexArgInsets(paragraphs, pit, os, rp, style.latexargs());
280 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
282 << pit->params().labelWidthString()
284 } else if (style.labeltype == LABEL_BIBLIO) {
285 if (pit->params().labelWidthString().empty())
286 os << '{' << bibitemWidest(buf, runparams) << "}\n";
289 << pit->params().labelWidthString()
292 os << from_ascii(style.latexparam()) << '\n';
296 // in multilingual environments, the CJK tags have to be nested properly
297 data.cjk_nested = false;
298 if (data.par_language->encoding()->package() == Encoding::CJK &&
299 state->open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
300 if (prev_par_language->encoding()->package() == Encoding::CJK)
301 os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
302 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
303 state->open_encoding_ = CJK;
304 data.cjk_nested = true;
310 static void finishEnvironment(otexstream & os, OutputParams const & runparams,
311 TeXEnvironmentData const & data)
313 OutputState * state = getOutputState();
314 // BufferParams const & bparams = buf.params(); // FIXME: for speedup shortcut below, would require passing of "buf" as argument
315 if (state->open_encoding_ == CJK && data.cjk_nested) {
316 // We need to close the encoding even if it does not change
317 // to do correct environment nesting
318 os << "\\end{CJK}\n";
319 state->open_encoding_ = none;
322 if (data.style->isEnvironment()) {
324 bool const using_begin_end =
325 runparams.use_polyglossia ||
326 !lyxrc.language_command_end.empty();
327 // Close any language opened at this nest level
328 if (using_begin_end) {
329 while (langOpenedAtThisLevel(state)) {
330 if (isLocalSwitch(state)) {
334 << openLanguageName(state)
340 state->nest_level_ -= 1;
341 string const & name = data.style->latexname();
343 os << "\\end{" << from_ascii(name) << "}\n";
344 state->prev_env_language_ = data.par_language;
345 if (runparams.encoding != data.prev_encoding) {
346 runparams.encoding = data.prev_encoding;
347 os << setEncoding(data.prev_encoding->iconvName());
351 if (data.leftindent_open) {
352 os << breakln << "\\end{LyXParagraphLeftIndent}\n";
353 state->prev_env_language_ = data.par_language;
354 if (runparams.encoding != data.prev_encoding) {
355 runparams.encoding = data.prev_encoding;
356 os << setEncoding(data.prev_encoding->iconvName());
360 // Check whether we should output a blank line after the environment
361 if (!data.style->nextnoindent)
366 void TeXEnvironment(Buffer const & buf, Text const & text,
367 OutputParams const & runparams,
368 pit_type & pit, otexstream & os)
370 ParagraphList const & paragraphs = text.paragraphs();
371 ParagraphList::const_iterator ipar = paragraphs.constIterator(pit);
372 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
374 Layout const & current_layout = ipar->layout();
375 depth_type const current_depth = ipar->params().depth();
376 Length const & current_left_indent = ipar->params().leftIndent();
378 // This is for debugging purpose at the end.
379 pit_type const par_begin = pit;
380 for (; pit < runparams.par_end; ++pit) {
381 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
383 // check first if this is an higher depth paragraph.
384 bool go_out = (par->params().depth() < current_depth);
385 if (par->params().depth() == current_depth) {
386 // This environment is finished.
387 go_out |= (par->layout() != current_layout);
388 go_out |= (par->params().leftIndent() != current_left_indent);
391 // nothing to do here, restore pit and go out.
396 if (par->layout() == current_layout
397 && par->params().depth() == current_depth
398 && par->params().leftIndent() == current_left_indent) {
399 // We are still in the same environment so TeXOnePar and continue;
400 TeXOnePar(buf, text, pit, os, runparams);
404 // We are now in a deeper environment.
405 // Either par->layout() != current_layout
406 // Or par->params().depth() > current_depth
407 // Or par->params().leftIndent() != current_left_indent)
409 // FIXME This test should not be necessary.
410 // We should perhaps issue an error if it is.
411 bool const force_plain_layout = text.inset().forcePlainLayout();
412 Layout const & style = force_plain_layout
413 ? buf.params().documentClass().plainLayout()
416 if (!style.isEnvironment()) {
417 // This is a standard paragraph, no need to call TeXEnvironment.
418 TeXOnePar(buf, text, pit, os, runparams);
422 // This is a new environment.
423 TeXEnvironmentData const data =
424 prepareEnvironment(buf, text, par, os, runparams);
425 // Recursive call to TeXEnvironment!
426 TeXEnvironment(buf, text, runparams, pit, os);
427 finishEnvironment(os, runparams, data);
430 if (pit != runparams.par_end)
431 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << par_begin << " done.");
435 void getArgInsets(otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
436 map<int, lyx::InsetArgument const *> ilist, vector<string> required, string const & prefix)
438 unsigned int const argnr = latexargs.size();
442 // Default and preset args are always output, so if they require
443 // other arguments, consider this.
444 for (auto const & larg : latexargs) {
445 Layout::latexarg const & arg = larg.second;
446 if ((!arg.presetarg.empty() || !arg.defaultarg.empty()) && !arg.requires.empty()) {
447 vector<string> req = getVectorFromString(arg.requires);
448 required.insert(required.end(), req.begin(), req.end());
452 for (unsigned int i = 1; i <= argnr; ++i) {
453 map<int, InsetArgument const *>::const_iterator lit = ilist.find(i);
454 bool inserted = false;
455 if (lit != ilist.end()) {
456 InsetArgument const * ins = (*lit).second;
458 Layout::LaTeXArgMap::const_iterator const lait =
459 latexargs.find(ins->name());
460 if (lait != latexargs.end()) {
461 Layout::latexarg arg = (*lait).second;
462 docstring ldelim = arg.mandatory ?
463 from_ascii("{") : from_ascii("[");
464 docstring rdelim = arg.mandatory ?
465 from_ascii("}") : from_ascii("]");
466 if (!arg.ldelim.empty())
468 if (!arg.rdelim.empty())
470 ins->latexArgument(os, runparams, ldelim, rdelim, arg.presetarg);
476 Layout::LaTeXArgMap::const_iterator lait = latexargs.begin();
477 Layout::LaTeXArgMap::const_iterator const laend = latexargs.end();
478 for (; lait != laend; ++lait) {
479 string const name = prefix + convert<string>(i);
480 if ((*lait).first == name) {
481 Layout::latexarg arg = (*lait).second;
482 docstring preset = arg.presetarg;
483 if (!arg.defaultarg.empty()) {
486 preset += arg.defaultarg;
489 docstring ldelim = arg.ldelim.empty() ?
490 from_ascii("{") : arg.ldelim;
491 docstring rdelim = arg.rdelim.empty() ?
492 from_ascii("}") : arg.rdelim;
493 os << ldelim << preset << rdelim;
494 } else if (!preset.empty()) {
495 docstring ldelim = arg.ldelim.empty() ?
496 from_ascii("[") : arg.ldelim;
497 docstring rdelim = arg.rdelim.empty() ?
498 from_ascii("]") : arg.rdelim;
499 os << ldelim << preset << rdelim;
500 } else if (find(required.begin(), required.end(),
501 (*lait).first) != required.end()) {
502 docstring ldelim = arg.ldelim.empty() ?
503 from_ascii("[") : arg.ldelim;
504 docstring rdelim = arg.rdelim.empty() ?
505 from_ascii("]") : arg.rdelim;
506 os << ldelim << rdelim;
519 void pushLanguageName(string const & lang_name, bool localswitch)
521 OutputState * state = getOutputState();
523 int nest_level = localswitch ? -state->nest_level_ : state->nest_level_;
524 state->lang_switch_depth_.push(nest_level);
525 state->open_polyglossia_lang_.push(lang_name);
529 void popLanguageName()
531 OutputState * state = getOutputState();
533 state->lang_switch_depth_.pop();
534 state->open_polyglossia_lang_.pop();
538 string const & openLanguageName()
540 OutputState * state = getOutputState();
542 return openLanguageName(state);
548 void addArgInsets(Paragraph const & par, string const & prefix,
549 Layout::LaTeXArgMap const & latexargs,
550 map<int, InsetArgument const *> & ilist,
551 vector<string> & required)
553 for (auto const & table : par.insetList()) {
554 InsetArgument const * arg = table.inset->asInsetArgument();
557 if (arg->name().empty()) {
558 LYXERR0("Error: Unnamed argument inset!");
561 string const name = prefix.empty() ?
562 arg->name() : split(arg->name(), ':');
563 // why converting into an integer?
564 unsigned int const nr = convert<unsigned int>(name);
565 if (ilist.find(nr) == ilist.end())
567 Layout::LaTeXArgMap::const_iterator const lit =
568 latexargs.find(arg->name());
569 if (lit != latexargs.end()) {
570 Layout::latexarg const & larg = lit->second;
571 vector<string> req = getVectorFromString(larg.requires);
572 move(req.begin(), req.end(), back_inserter(required));
580 void latexArgInsets(Paragraph const & par, otexstream & os,
581 OutputParams const & runparams,
582 Layout::LaTeXArgMap const & latexargs,
583 string const & prefix)
585 map<int, InsetArgument const *> ilist;
586 vector<string> required;
587 addArgInsets(par, prefix, latexargs, ilist, required);
588 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
592 void latexArgInsets(ParagraphList const & pars,
593 ParagraphList::const_iterator pit,
594 otexstream & os, OutputParams const & runparams,
595 Layout::LaTeXArgMap const & latexargs,
596 string const & prefix)
598 map<int, InsetArgument const *> ilist;
599 vector<string> required;
601 depth_type const current_depth = pit->params().depth();
602 Layout const current_layout = pit->layout();
604 // get the first paragraph in sequence with this layout and depth
607 if (lyx::prev(pit, offset) == pars.begin())
609 ParagraphList::const_iterator priorpit = lyx::prev(pit, offset + 1);
610 if (priorpit->layout() == current_layout
611 && priorpit->params().depth() == current_depth)
617 ParagraphList::const_iterator spit = lyx::prev(pit, offset);
619 for (; spit != pars.end(); ++spit) {
620 if (spit->layout() != current_layout ||
621 spit->params().depth() < current_depth)
623 if (spit->params().depth() > current_depth)
625 addArgInsets(*spit, prefix, latexargs, ilist, required);
627 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
631 void latexArgInsetsForParent(ParagraphList const & pars, otexstream & os,
632 OutputParams const & runparams,
633 Layout::LaTeXArgMap const & latexargs,
634 string const & prefix)
636 map<int, InsetArgument const *> ilist;
637 vector<string> required;
639 for (Paragraph const & par : pars) {
640 if (par.layout().hasArgs())
641 // The InsetArguments inside this paragraph refer to this paragraph
643 addArgInsets(par, prefix, latexargs, ilist, required);
645 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
651 // output the proper paragraph start according to latextype.
652 void parStartCommand(Paragraph const & par, otexstream & os,
653 OutputParams const & runparams, Layout const & style)
655 switch (style.latextype) {
657 if (par.needsCProtection())
659 os << '\\' << from_ascii(style.latexname());
662 if (!style.latexargs().empty())
663 latexArgInsets(par, os, runparams, style.latexargs());
664 os << from_ascii(style.latexparam());
666 case LATEX_ITEM_ENVIRONMENT:
667 case LATEX_LIST_ENVIRONMENT:
668 os << "\\" + style.itemcommand();
670 if (!style.itemargs().empty())
671 latexArgInsets(par, os, runparams, style.itemargs(), "item:");
674 case LATEX_BIB_ENVIRONMENT:
675 // ignore this, the inset will write itself
684 // FIXME: this should be anonymous
685 void TeXOnePar(Buffer const & buf,
689 OutputParams const & runparams_in,
690 string const & everypar,
691 int start_pos, int end_pos,
694 BufferParams const & bparams = runparams_in.is_child
695 ? buf.masterParams() : buf.params();
696 ParagraphList const & paragraphs = text.paragraphs();
697 Paragraph const & par = paragraphs.at(pit);
698 // FIXME This check should not really be needed.
699 // Perhaps we should issue an error if it is.
700 Layout const & style = text.inset().forcePlainLayout() ?
701 bparams.documentClass().plainLayout() : par.layout();
703 if (style.inpreamble && !force)
706 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
709 OutputParams runparams = runparams_in;
710 runparams.isLastPar = (pit == pit_type(paragraphs.size() - 1));
711 // We reinitialze par begin and end to be on the safe side
712 // with embedded inset as we don't know if they set those
714 runparams.par_begin = 0;
715 runparams.par_end = 0;
717 bool const maintext = text.isMainText();
718 // we are at the beginning of an inset and CJK is already open;
719 // we count inheritation levels to get the inset nesting right.
720 OutputState * state = getOutputState();
721 if (pit == 0 && !maintext
722 && (state->cjk_inherited_ > 0 || state->open_encoding_ == CJK)) {
723 state->cjk_inherited_ += 1;
724 state->open_encoding_ = none;
727 if (text.inset().isPassThru()) {
728 Font const outerfont = text.outerFont(pit);
730 // No newline before first paragraph in this lyxtext
733 if (!text.inset().getLayout().parbreakIsNewline())
737 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos, force);
741 Paragraph const * nextpar = runparams.isLastPar
742 ? 0 : ¶graphs.at(pit + 1);
744 bool const intitle_command = style.intitle && style.latextype == LATEX_COMMAND;
746 if (style.pass_thru) {
747 Font const outerfont = text.outerFont(pit);
748 parStartCommand(par, os, runparams, style);
752 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos, force);
754 // I did not create a parEndCommand for this minuscule
755 // task because in the other user of parStartCommand
756 // the code is different (JMarc)
757 if (style.isCommand())
761 if (!style.parbreak_is_newline) {
763 } else if (nextpar && !style.isEnvironment()) {
764 Layout const nextstyle = text.inset().forcePlainLayout()
765 ? bparams.documentClass().plainLayout()
767 if (nextstyle.name() != style.name())
774 // This paragraph's language
775 Language const * const par_language = par.getParLanguage(bparams);
776 Language const * const nextpar_language = nextpar ?
777 nextpar->getParLanguage(bparams) : 0;
778 // The document's language
779 Language const * const doc_language = bparams.language;
780 // The language that was in effect when the environment this paragraph is
781 // inside of was opened
782 Language const * const outer_language =
783 (runparams.local_font != 0) ?
784 runparams.local_font->language() : doc_language;
786 Paragraph const * priorpar = (pit == 0) ? 0 : ¶graphs.at(pit - 1);
788 // The previous language that was in effect is the language of the
789 // previous paragraph, unless the previous paragraph is inside an
790 // environment with nesting depth greater than (or equal to, but with
791 // a different layout) the current one. If there is no previous
792 // paragraph, the previous language is the outer language.
793 bool const use_prev_env_language = state->prev_env_language_ != 0
795 && priorpar->layout().isEnvironment()
796 && (priorpar->getDepth() > par.getDepth()
797 || (priorpar->getDepth() == par.getDepth()
798 && priorpar->layout() != par.layout()));
799 Language const * const prev_language =
801 ? (use_prev_env_language ? state->prev_env_language_
802 : priorpar->getParLanguage(bparams))
806 bool const use_polyglossia = runparams.use_polyglossia;
807 string const par_lang = use_polyglossia ?
808 getPolyglossiaEnvName(par_language): par_language->babel();
809 string const prev_lang = use_polyglossia ?
810 getPolyglossiaEnvName(prev_language) : prev_language->babel();
811 string const outer_lang = use_polyglossia ?
812 getPolyglossiaEnvName(outer_language) : outer_language->babel();
813 string const nextpar_lang = nextpar_language ? (use_polyglossia ?
814 getPolyglossiaEnvName(nextpar_language) :
815 nextpar_language->babel()) : string();
816 string lang_begin_command = use_polyglossia ?
817 "\\begin{$$lang}$$opts" : lyxrc.language_command_begin;
818 string lang_end_command = use_polyglossia ?
819 "\\end{$$lang}" : lyxrc.language_command_end;
820 // the '%' is necessary to prevent unwanted whitespace
821 string lang_command_termination = "%\n";
822 bool const using_begin_end = use_polyglossia ||
823 !lang_end_command.empty();
825 // For InTitle commands, we need to switch the language inside the command
826 // (see #10849); thus open the command here.
827 if (intitle_command) {
828 parStartCommand(par, os, runparams, style);
832 // In some insets (such as Arguments), we cannot use \selectlanguage
833 bool const localswitch = text.inset().forceLocalFontSwitch()
834 || (using_begin_end && text.inset().forcePlainLayout());
836 lang_begin_command = use_polyglossia ?
837 "\\text$$lang$$opts{" : lyxrc.language_command_local;
838 lang_end_command = "}";
839 lang_command_termination.clear();
842 // localswitches need to be closed and reopened at each par
843 if ((par_lang != prev_lang || localswitch)
844 // check if we already put language command in TeXEnvironment()
845 && !(style.isEnvironment()
846 && (pit == 0 || (priorpar->layout() != par.layout()
847 && priorpar->getDepth() <= par.getDepth())
848 || priorpar->getDepth() < par.getDepth()))) {
850 && (!using_begin_end || langOpenedAtThisLevel(state))
851 && !lang_end_command.empty()
852 && prev_lang != outer_lang
853 && !prev_lang.empty()
854 && (!using_begin_end || !style.isEnvironment())) {
855 os << from_ascii(subst(lang_end_command,
858 << lang_command_termination;
863 // We need to open a new language if we couldn't close the previous
864 // one (because there's no language_command_end); and even if we closed
865 // the previous one, if the current language is different than the
866 // outer_language (which is currently in effect once the previous one
868 if ((lang_end_command.empty() || par_lang != outer_lang
870 || (style.isEnvironment() && par_lang != prev_lang)))
871 && !par_lang.empty()) {
872 // If we're inside an inset, and that inset is within an \L or \R
873 // (or equivalents), then within the inset, too, any opposite
874 // language paragraph should appear within an \L or \R (in addition
875 // to, outside of, the normal language switch commands).
876 // This behavior is not correct for ArabTeX, though.
879 && par_language->lang() != "arabic_arabtex"
880 && outer_language->lang() != "arabic_arabtex"
881 // are we in an inset?
882 && runparams.local_font != 0
883 // is the inset within an \L or \R?
885 // FIXME: currently, we don't check this; this means that
886 // we'll have unnnecessary \L and \R commands, but that
887 // doesn't seem to hurt (though latex will complain)
889 // is this paragraph in the opposite direction?
890 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()) {
891 // FIXME: I don't have a working copy of the Arabi package, so
892 // I'm not sure if the farsi and arabic_arabi stuff is correct
894 if (par_language->lang() == "farsi")
896 else if (outer_language->lang() == "farsi")
898 else if (par_language->lang() == "arabic_arabi")
900 else if (outer_language->lang() == "arabic_arabi")
902 // remaining RTL languages currently is hebrew
903 else if (par_language->rightToLeft())
908 // With CJK, the CJK tag has to be closed first (see below)
909 if (runparams.encoding->package() != Encoding::CJK
910 && (par_lang != openLanguageName(state) || localswitch)
911 && !par_lang.empty()) {
912 string bc = use_polyglossia ?
913 getPolyglossiaBegin(lang_begin_command, par_lang,
914 par_language->polyglossiaOpts(),
916 : subst(lang_begin_command, "$$lang", par_lang);
918 os << lang_command_termination;
920 pushLanguageName(par_lang, localswitch);
925 // Switch file encoding if necessary; no need to do this for "default"
926 // encoding, since this only affects the position of the outputted
927 // \inputencoding command; the encoding switch will occur when necessary
928 if (bparams.inputenc == "auto"
929 && !runparams.isFullUnicode() // Xe/LuaTeX use one document-wide encoding (see also switchEncoding())
930 && runparams.encoding->package() != Encoding::none) {
931 // Look ahead for future encoding changes.
932 // We try to output them at the beginning of the paragraph,
933 // since the \inputencoding command is not allowed e.g. in
934 // sections. For this reason we only set runparams.moving_arg
935 // after checking for the encoding change, otherwise the
936 // change would be always avoided by switchEncoding().
937 for (pos_type i = 0; i < par.size(); ++i) {
938 char_type const c = par.getChar(i);
939 Encoding const * const encoding =
940 par.getFontSettings(bparams, i).language()->encoding();
941 if (encoding->package() != Encoding::CJK
942 && runparams.encoding->package() == Encoding::inputenc
947 // All characters before c are in the ASCII range, and
948 // c is non-ASCII (but no inset), so change the
949 // encoding to that required by the language of c.
950 // With CJK, only add switch if we have CJK content at the beginning
952 if (i != 0 && encoding->package() == Encoding::CJK)
955 pair<bool, int> enc_switch = switchEncoding(os.os(),
956 bparams, runparams, *encoding);
957 // the following is necessary after a CJK environment in a multilingual
958 // context (nesting issue).
959 if (par_language->encoding()->package() == Encoding::CJK
960 && state->open_encoding_ != CJK && state->cjk_inherited_ == 0) {
961 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
962 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
963 state->open_encoding_ = CJK;
965 if (encoding->package() != Encoding::none && enc_switch.first) {
966 if (enc_switch.second > 0) {
967 // the '%' is necessary to prevent unwanted whitespace
970 // With CJK, the CJK tag had to be closed first (see above)
971 if (runparams.encoding->package() == Encoding::CJK
972 && par_lang != openLanguageName(state)
973 && !par_lang.empty()) {
974 string bc = use_polyglossia ?
975 getPolyglossiaBegin(lang_begin_command, par_lang,
976 par_language->polyglossiaOpts(),
978 : subst(lang_begin_command, "$$lang", par_lang);
980 << lang_command_termination;
982 pushLanguageName(par_lang, localswitch);
984 runparams.encoding = encoding;
990 runparams.moving_arg |= style.needprotect;
991 Encoding const * const prev_encoding = runparams.encoding;
993 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
994 if (par.allowParagraphCustomization()) {
995 if (par.params().startOfAppendix()) {
996 os << "\n\\appendix\n";
999 // InTitle commands must use switches (not environments)
1000 // inside the commands (see #9332)
1001 if (style.intitle) {
1002 if (!par.params().spacing().isDefault())
1004 if (runparams.moving_arg)
1006 os << from_ascii(par.params().spacing().writeCmd(useSetSpace));
1009 if (!par.params().spacing().isDefault()
1010 && (pit == 0 || !priorpar->hasSameLayout(par)))
1012 os << from_ascii(par.params().spacing().writeEnvirBegin(useSetSpace))
1016 if (style.isCommand()) {
1022 // For InTitle commands, we already started the command before
1023 // the language switch
1024 if (!intitle_command)
1025 parStartCommand(par, os, runparams, style);
1027 Font const outerfont = text.outerFont(pit);
1030 os << from_utf8(everypar);
1031 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos, force);
1033 Font const font = par.empty()
1034 ? par.getLayoutFont(bparams, outerfont)
1035 : par.getFont(bparams, par.size() - 1, outerfont);
1037 bool const is_command = style.isCommand();
1039 // InTitle commands need to be closed after the language has been closed.
1040 if (!intitle_command) {
1043 if (!style.postcommandargs().empty())
1044 latexArgInsets(par, os, runparams, style.postcommandargs(), "post:");
1045 if (runparams.encoding != prev_encoding) {
1046 runparams.encoding = prev_encoding;
1047 os << setEncoding(prev_encoding->iconvName());
1052 bool pending_newline = false;
1053 bool unskip_newline = false;
1054 bool close_lang_switch = false;
1055 switch (style.latextype) {
1056 case LATEX_ITEM_ENVIRONMENT:
1057 case LATEX_LIST_ENVIRONMENT:
1058 if ((nextpar && par_lang != nextpar_lang
1059 && nextpar->getDepth() == par.getDepth())
1060 || (atSameLastLangSwitchDepth(state) && nextpar
1061 && nextpar->getDepth() < par.getDepth()))
1062 close_lang_switch = using_begin_end;
1063 if (nextpar && par.params().depth() < nextpar->params().depth())
1064 pending_newline = true;
1066 case LATEX_ENVIRONMENT: {
1067 // if its the last paragraph of the current environment
1068 // skip it otherwise fall through
1070 && ((nextpar->layout() != par.layout()
1071 || nextpar->params().depth() != par.params().depth())
1072 || (!using_begin_end || par_lang != nextpar_lang)))
1074 close_lang_switch = using_begin_end;
1081 // we don't need it for the last paragraph and in InTitle commands!!!
1082 if (nextpar && !intitle_command)
1083 pending_newline = true;
1086 // InTitle commands use switches (not environments) for space settings
1087 if (par.allowParagraphCustomization() && !style.intitle) {
1088 if (!par.params().spacing().isDefault()
1089 && (runparams.isLastPar || !nextpar->hasSameLayout(par))) {
1090 if (pending_newline)
1093 string const endtag =
1094 par.params().spacing().writeEnvirEnd(useSetSpace);
1095 if (prefixIs(endtag, "\\end{"))
1098 os << from_ascii(endtag);
1099 pending_newline = true;
1103 // Closing the language is needed for the last paragraph in a given language
1104 // as well as for any InTitleCommand (since these set the language locally);
1105 // it is also needed if we're within an \L or \R that we may have opened above
1106 // (not necessarily in this paragraph) and are about to close.
1107 bool closing_rtl_ltr_environment = !using_begin_end
1109 && (par_language->lang() != "arabic_arabtex"
1110 && outer_language->lang() != "arabic_arabtex")
1111 // have we opened an \L or \R environment?
1112 && runparams.local_font != 0
1113 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()
1114 // are we about to close the language?
1115 &&((nextpar && par_lang != nextpar_lang)
1116 || (runparams.isLastPar && par_lang != outer_lang));
1119 || (intitle_command && using_begin_end)
1120 || closing_rtl_ltr_environment
1121 || ((runparams.isLastPar || close_lang_switch)
1122 && (par_lang != outer_lang || (using_begin_end
1123 && style.isEnvironment()
1124 && par_lang != nextpar_lang)))) {
1125 // Since \selectlanguage write the language to the aux file,
1126 // we need to reset the language at the end of footnote or
1129 if (!localswitch && (pending_newline || close_lang_switch))
1132 // when the paragraph uses CJK, the language has to be closed earlier
1133 if (font.language()->encoding()->package() != Encoding::CJK) {
1134 if (lang_end_command.empty()) {
1135 // If this is a child, we should restore the
1136 // master language after the last paragraph.
1137 Language const * const current_language =
1138 (runparams.isLastPar && runparams.master_language)
1139 ? runparams.master_language
1141 string const current_lang = use_polyglossia
1142 ? getPolyglossiaEnvName(current_language)
1143 : current_language->babel();
1144 if (!current_lang.empty()
1145 && current_lang != openLanguageName(state)) {
1146 string bc = use_polyglossia ?
1147 getPolyglossiaBegin(lang_begin_command, current_lang,
1148 current_language->polyglossiaOpts(),
1150 : subst(lang_begin_command, "$$lang", current_lang);
1152 pending_newline = !localswitch;
1153 unskip_newline = !localswitch;
1154 if (using_begin_end)
1155 pushLanguageName(current_lang, localswitch);
1157 } else if ((!using_begin_end ||
1158 langOpenedAtThisLevel(state)) &&
1159 !par_lang.empty()) {
1160 // If we are in an environment, we have to
1161 // close the "outer" language afterwards
1162 string const & cur_lang = openLanguageName(state);
1163 if (!style.isEnvironment()
1164 || (close_lang_switch
1165 && atSameLastLangSwitchDepth(state)
1166 && par_lang != outer_lang
1167 && (par_lang != cur_lang
1168 || (cur_lang != outer_lang
1170 && style != nextpar->layout())))
1171 || (atSameLastLangSwitchDepth(state)
1172 && state->lang_switch_depth_.size()
1173 && cur_lang != par_lang))
1175 if (using_begin_end && !localswitch)
1177 os << from_ascii(subst(
1181 pending_newline = !localswitch;
1182 unskip_newline = !localswitch;
1183 if (using_begin_end)
1189 if (closing_rtl_ltr_environment)
1192 // InTitle commands need to be closed after the language has been closed.
1193 if (intitle_command) {
1196 if (!style.postcommandargs().empty())
1197 latexArgInsets(par, os, runparams, style.postcommandargs(), "post:");
1198 if (runparams.encoding != prev_encoding) {
1199 runparams.encoding = prev_encoding;
1200 os << setEncoding(prev_encoding->iconvName());
1205 bool const last_was_separator =
1206 par.size() > 0 && par.isEnvSeparator(par.size() - 1);
1208 if (pending_newline) {
1210 // prevent unwanted whitespace
1212 if (!os.afterParbreak() && !last_was_separator)
1216 // if this is a CJK-paragraph and the next isn't, close CJK
1217 // also if the next paragraph is a multilingual environment (because of nesting)
1219 && state->open_encoding_ == CJK
1220 && (nextpar_language->encoding()->package() != Encoding::CJK
1221 || (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
1222 // inbetween environments, CJK has to be closed later (nesting!)
1223 && (!style.isEnvironment() || !nextpar->layout().isEnvironment())) {
1224 os << "\\end{CJK}\n";
1225 state->open_encoding_ = none;
1228 // If this is the last paragraph, close the CJK environment
1229 // if necessary. If it's an environment, we'll have to \end that first.
1230 if (runparams.isLastPar && !style.isEnvironment()) {
1231 switch (state->open_encoding_) {
1233 // do nothing at the end of child documents
1234 if (maintext && buf.masterBuffer() != &buf)
1238 os << "\n\\end{CJK}\n";
1242 state->open_encoding_ = none;
1247 state->open_encoding_ = none;
1257 // If this is the last paragraph, and a local_font was set upon entering
1258 // the inset, and we're using "auto" or "default" encoding, and not
1259 // compiling with XeTeX or LuaTeX, the encoding
1260 // should be set back to that local_font's encoding.
1261 if (runparams.isLastPar && runparams_in.local_font != 0
1262 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
1263 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
1264 && !runparams.isFullUnicode()
1266 runparams_in.encoding = runparams_in.local_font->language()->encoding();
1267 os << setEncoding(runparams_in.encoding->iconvName());
1269 // Otherwise, the current encoding should be set for the next paragraph.
1271 runparams_in.encoding = runparams.encoding;
1274 // we don't need a newline for the last paragraph!!!
1275 // Note from JMarc: we will re-add a \n explicitly in
1276 // TeXEnvironment, because it is needed in this case
1277 if (nextpar && !os.afterParbreak() && !last_was_separator) {
1278 // Make sure to start a new line
1280 Layout const & next_layout = nextpar->layout();
1281 // A newline '\n' is always output before a command,
1282 // so avoid doubling it.
1283 if (!next_layout.isCommand()) {
1284 // Here we now try to avoid spurious empty lines by
1285 // outputting a paragraph break only if: (case 1) the
1286 // paragraph style allows parbreaks and no \begin, \end
1287 // or \item tags are going to follow (i.e., if the next
1288 // isn't the first or the current isn't the last
1289 // paragraph of an environment or itemize) and the
1290 // depth and alignment of the following paragraph is
1291 // unchanged, or (case 2) the following is a
1292 // non-environment paragraph whose depth is increased
1293 // but whose alignment is unchanged, or (case 3) the
1294 // paragraph is not an environment and the next one is a
1295 // non-itemize-like env at lower depth, or (case 4) the
1296 // paragraph is a command not followed by an environment
1297 // and the alignment of the current and next paragraph
1298 // is unchanged, or (case 5) the current alignment is
1299 // changed and a standard paragraph follows.
1300 DocumentClass const & tclass = bparams.documentClass();
1301 if ((style == next_layout
1302 && !style.parbreak_is_newline
1303 && !text.inset().getLayout().parbreakIsNewline()
1304 && style.latextype != LATEX_ITEM_ENVIRONMENT
1305 && style.latextype != LATEX_LIST_ENVIRONMENT
1306 && style.align == par.getAlign()
1307 && nextpar->getDepth() == par.getDepth()
1308 && nextpar->getAlign() == par.getAlign())
1309 || (!next_layout.isEnvironment()
1310 && nextpar->getDepth() > par.getDepth()
1311 && nextpar->getAlign() == par.getAlign())
1312 || (!style.isEnvironment()
1313 && next_layout.latextype == LATEX_ENVIRONMENT
1314 && nextpar->getDepth() < par.getDepth())
1315 || (style.isCommand()
1316 && !next_layout.isEnvironment()
1317 && style.align == par.getAlign()
1318 && next_layout.align == nextpar->getAlign())
1319 || (style.align != par.getAlign()
1320 && tclass.isDefaultLayout(next_layout))) {
1326 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " done; ptr "
1327 << &par << " next " << nextpar);
1333 // LaTeX all paragraphs
1334 void latexParagraphs(Buffer const & buf,
1337 OutputParams const & runparams,
1338 string const & everypar)
1340 LASSERT(runparams.par_begin <= runparams.par_end,
1341 { os << "% LaTeX Output Error\n"; return; } );
1343 BufferParams const & bparams = buf.params();
1344 BufferParams const & mparams = buf.masterParams();
1346 bool const maintext = text.isMainText();
1347 bool const is_child = buf.masterBuffer() != &buf;
1348 bool const multibib_child = maintext && is_child
1349 && mparams.multibib == "child";
1351 if (multibib_child && mparams.useBiblatex())
1352 os << "\\newrefsection";
1353 else if (multibib_child && mparams.useBibtopic()) {
1354 os << "\\begin{btUnit}\n";
1355 runparams.openbtUnit = true;
1358 // Open a CJK environment at the beginning of the main buffer
1359 // if the document's language is a CJK language
1360 // (but not in child documents)
1361 OutputState * state = getOutputState();
1362 if (maintext && !is_child
1363 && bparams.encoding().package() == Encoding::CJK) {
1364 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
1365 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
1366 state->open_encoding_ = CJK;
1368 // if "auto begin" is switched off, explicitly switch the
1369 // language on at start
1370 string const mainlang = runparams.use_polyglossia
1371 ? getPolyglossiaEnvName(bparams.language)
1372 : bparams.language->babel();
1373 string const lang_begin_command = runparams.use_polyglossia ?
1374 "\\begin{$$lang}$$opts" : lyxrc.language_command_begin;
1375 string const lang_end_command = runparams.use_polyglossia ?
1376 "\\end{$$lang}" : lyxrc.language_command_end;
1377 bool const using_begin_end = runparams.use_polyglossia ||
1378 !lang_end_command.empty();
1380 if (maintext && !lyxrc.language_auto_begin &&
1381 !mainlang.empty()) {
1383 string bc = runparams.use_polyglossia ?
1384 getPolyglossiaBegin(lang_begin_command, mainlang,
1385 bparams.language->polyglossiaOpts())
1386 : subst(lang_begin_command, "$$lang", mainlang);
1389 if (using_begin_end)
1390 pushLanguageName(mainlang);
1393 ParagraphList const & paragraphs = text.paragraphs();
1395 if (runparams.par_begin == runparams.par_end) {
1396 // The full doc will be exported but it is easier to just rely on
1397 // runparams range parameters that will be passed TeXEnvironment.
1398 runparams.par_begin = 0;
1399 runparams.par_end = paragraphs.size();
1402 pit_type pit = runparams.par_begin;
1403 // lastpit is for the language check after the loop.
1404 pit_type lastpit = pit;
1405 // variables used in the loop:
1406 bool was_title = false;
1407 bool already_title = false;
1408 DocumentClass const & tclass = bparams.documentClass();
1410 // Did we already warn about inTitle layout mixing? (we only warn once)
1411 bool gave_layout_warning = false;
1412 for (; pit < runparams.par_end; ++pit) {
1414 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
1416 // FIXME This check should not be needed. We should
1417 // perhaps issue an error if it is.
1418 Layout const & layout = text.inset().forcePlainLayout() ?
1419 tclass.plainLayout() : par->layout();
1421 if (layout.intitle) {
1422 if (already_title) {
1423 if (!gave_layout_warning && !runparams.dryrun) {
1424 gave_layout_warning = true;
1425 frontend::Alert::warning(_("Error in latexParagraphs"),
1426 bformat(_("You are using at least one "
1427 "layout (%1$s) intended for the title, "
1428 "after using non-title layouts. This "
1429 "could lead to missing or incorrect output."
1432 } else if (!was_title) {
1434 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1436 << from_ascii(tclass.titlename())
1440 } else if (was_title && !already_title && !layout.inpreamble) {
1441 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1442 os << "\\end{" << from_ascii(tclass.titlename())
1446 os << "\\" << from_ascii(tclass.titlename())
1449 already_title = true;
1453 if (layout.isCommand() && !layout.latexname().empty()
1454 && layout.latexname() == bparams.multibib) {
1455 if (runparams.openbtUnit)
1456 os << "\\end{btUnit}\n";
1457 if (!bparams.useBiblatex()) {
1458 os << '\n' << "\\begin{btUnit}\n";
1459 runparams.openbtUnit = true;
1463 if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
1464 // This is a standard top level paragraph, TeX it and continue.
1465 TeXOnePar(buf, text, pit, os, runparams, everypar);
1469 TeXEnvironmentData const data =
1470 prepareEnvironment(buf, text, par, os, runparams);
1471 // pit can be changed in TeXEnvironment.
1472 TeXEnvironment(buf, text, runparams, pit, os);
1473 finishEnvironment(os, runparams, data);
1476 if (pit == runparams.par_end) {
1477 // Make sure that the last paragraph is
1478 // correctly terminated (because TeXOnePar does
1479 // not add a \n in this case)
1483 // It might be that we only have a title in this document
1484 if (was_title && !already_title) {
1485 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1486 os << "\\end{" << from_ascii(tclass.titlename())
1489 os << "\\" << from_ascii(tclass.titlename())
1494 if (maintext && !is_child && runparams.openbtUnit)
1495 os << "\\end{btUnit}\n";
1497 // if "auto end" is switched off, explicitly close the language at the end
1498 // but only if the last par is in a babel or polyglossia language
1499 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1500 paragraphs.at(lastpit).getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1501 os << from_utf8(subst(lang_end_command,
1505 if (using_begin_end)
1509 // If the last paragraph is an environment, we'll have to close
1510 // CJK at the very end to do proper nesting.
1511 if (maintext && !is_child && state->open_encoding_ == CJK) {
1512 os << "\\end{CJK}\n";
1513 state->open_encoding_ = none;
1515 // Likewise for polyglossia or when using begin/end commands
1516 string const & cur_lang = openLanguageName(state);
1517 if (maintext && !is_child && !cur_lang.empty()) {
1518 os << from_utf8(subst(lang_end_command,
1522 if (using_begin_end)
1526 // reset inherited encoding
1527 if (state->cjk_inherited_ > 0) {
1528 state->cjk_inherited_ -= 1;
1529 if (state->cjk_inherited_ == 0)
1530 state->open_encoding_ = CJK;
1533 if (multibib_child && mparams.useBibtopic()) {
1534 os << "\\end{btUnit}\n";
1535 runparams.openbtUnit = false;
1540 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1541 OutputParams const & runparams, Encoding const & newEnc,
1544 // XeTeX/LuaTeX use only one encoding per document:
1545 // * with useNonTeXFonts: "utf8plain",
1546 // * with XeTeX and TeX fonts: "ascii" (inputenc fails),
1547 // * with LuaTeX and TeX fonts: only one encoding accepted by luainputenc.
1548 if (runparams.isFullUnicode() || newEnc.name() == "inherit")
1549 return make_pair(false, 0);
1551 Encoding const & oldEnc = *runparams.encoding;
1552 bool moving_arg = runparams.moving_arg;
1553 // If we switch from/to CJK, we need to switch anyway, despite custom inputenc
1554 bool const from_to_cjk =
1555 (oldEnc.package() == Encoding::CJK && newEnc.package() != Encoding::CJK)
1556 || (oldEnc.package() != Encoding::CJK && newEnc.package() == Encoding::CJK);
1557 if (!force && !from_to_cjk
1558 && ((bparams.inputenc != "auto" && bparams.inputenc != "default") || moving_arg))
1559 return make_pair(false, 0);
1561 // Do nothing if the encoding is unchanged.
1562 if (oldEnc.name() == newEnc.name())
1563 return make_pair(false, 0);
1565 // FIXME We ignore encoding switches from/to encodings that do
1566 // neither support the inputenc package nor the CJK package here.
1567 // This does of course only work in special cases (e.g. switch from
1568 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1569 // but it is the best we can do
1570 if (oldEnc.package() == Encoding::none
1571 || newEnc.package() == Encoding::none)
1572 return make_pair(false, 0);
1574 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1575 << oldEnc.name() << " to " << newEnc.name());
1576 os << setEncoding(newEnc.iconvName());
1577 if (bparams.inputenc == "default")
1578 return make_pair(true, 0);
1580 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1581 OutputState * state = getOutputState();
1582 switch (newEnc.package()) {
1583 case Encoding::none:
1584 case Encoding::japanese:
1585 // shouldn't ever reach here, see above
1586 return make_pair(true, 0);
1587 case Encoding::inputenc: {
1588 int count = inputenc_arg.length();
1589 if (oldEnc.package() == Encoding::CJK &&
1590 state->open_encoding_ == CJK) {
1592 state->open_encoding_ = none;
1595 else if (oldEnc.package() == Encoding::inputenc &&
1596 state->open_encoding_ == inputenc) {
1598 state->open_encoding_ = none;
1601 if (runparams.local_font != 0
1602 && oldEnc.package() == Encoding::CJK) {
1603 // within insets, \inputenc switches need
1604 // to be embraced within \bgroup...\egroup;
1608 state->open_encoding_ = inputenc;
1610 // with the japanese option, inputenc is omitted.
1611 if (runparams.use_japanese)
1612 return make_pair(true, count);
1613 os << "\\inputencoding{" << inputenc_arg << '}';
1614 return make_pair(true, count + 16);
1616 case Encoding::CJK: {
1617 int count = inputenc_arg.length();
1618 if (oldEnc.package() == Encoding::CJK &&
1619 state->open_encoding_ == CJK) {
1623 if (oldEnc.package() == Encoding::inputenc &&
1624 state->open_encoding_ == inputenc) {
1628 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1629 << from_ascii(bparams.fonts_cjk) << "}";
1630 state->open_encoding_ = CJK;
1631 return make_pair(true, count + 15);
1634 // Dead code to avoid a warning:
1635 return make_pair(true, 0);