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"
18 #include "InsetList.h"
22 #include "OutputParams.h"
23 #include "Paragraph.h"
24 #include "paragraph_funcs.h"
25 #include "ParagraphParameters.h"
26 #include "TextClass.h"
30 #include "insets/InsetBibitem.h"
31 #include "insets/InsetOptArg.h"
33 #include "support/debug.h"
34 #include "support/lstrings.h"
36 #include <boost/next_prior.hpp>
39 using namespace lyx::support;
52 static int open_encoding_ = none;
53 static bool cjk_inherited_ = false;
56 ParagraphList::const_iterator
57 TeXEnvironment(Buffer const & buf,
59 ParagraphList::const_iterator pit,
60 odocstream & os, TexRow & texrow,
61 OutputParams const & runparams);
63 ParagraphList::const_iterator
64 TeXOnePar(Buffer const & buf,
66 ParagraphList::const_iterator pit,
67 odocstream & os, TexRow & texrow,
68 OutputParams const & runparams,
69 string const & everypar = string());
72 ParagraphList::const_iterator
73 TeXDeeper(Buffer const & buf,
75 ParagraphList::const_iterator pit,
76 odocstream & os, TexRow & texrow,
77 OutputParams const & runparams)
79 LYXERR(Debug::LATEX, "TeXDeeper... " << &*pit);
80 ParagraphList::const_iterator par = pit;
82 ParagraphList const & paragraphs = text.paragraphs();
84 while (par != paragraphs.end() &&
85 par->params().depth() == pit->params().depth()) {
86 if (par->layout().isEnvironment()) {
87 par = TeXEnvironment(buf, text, par,
88 os, texrow, runparams);
90 par = TeXOnePar(buf, text, par,
91 os, texrow, runparams);
94 LYXERR(Debug::LATEX, "TeXDeeper...done ");
100 ParagraphList::const_iterator
101 TeXEnvironment(Buffer const & buf,
103 ParagraphList::const_iterator pit,
104 odocstream & os, TexRow & texrow,
105 OutputParams const & runparams)
107 LYXERR(Debug::LATEX, "TeXEnvironment... " << &*pit);
109 BufferParams const & bparams = buf.params();
111 Layout const & style = pit->forceEmptyLayout() ?
112 bparams.documentClass().emptyLayout() : pit->layout();
114 ParagraphList const & paragraphs = text.paragraphs();
116 Language const * const par_language = pit->getParLanguage(bparams);
117 Language const * const doc_language = bparams.language;
118 Language const * const prev_par_language =
119 (pit != paragraphs.begin())
120 ? boost::prior(pit)->getParLanguage(bparams)
122 if (par_language->babel() != prev_par_language->babel()) {
124 if (!lyxrc.language_command_end.empty() &&
125 prev_par_language->babel() != doc_language->babel() &&
126 !prev_par_language->babel().empty()) {
127 os << from_ascii(subst(
128 lyxrc.language_command_end,
130 prev_par_language->babel()))
131 // the '%' is necessary to prevent unwanted whitespace
136 if ((lyxrc.language_command_end.empty() ||
137 par_language->babel() != doc_language->babel()) &&
138 !par_language->babel().empty()) {
139 os << from_ascii(subst(
140 lyxrc.language_command_begin,
142 par_language->babel()))
143 // the '%' is necessary to prevent unwanted whitespace
149 bool leftindent_open = false;
150 if (!pit->params().leftIndent().zero()) {
151 os << "\\begin{LyXParagraphLeftIndent}{"
152 << from_ascii(pit->params().leftIndent().asLatexString())
155 leftindent_open = true;
158 if (style.isEnvironment()) {
159 os << "\\begin{" << from_ascii(style.latexname()) << '}';
160 if (style.optionalargs > 0) {
161 int ret = latexOptArgInsets(*pit, os, runparams,
168 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
170 << pit->params().labelWidthString()
172 } else if (style.labeltype == LABEL_BIBLIO) {
174 os << '{' << bibitemWidest(buf) << "}\n";
176 os << from_ascii(style.latexparam()) << '\n';
180 // in multilingual environments, the CJK tags have to be nested properly
181 bool cjk_nested = false;
182 if (par_language->encoding()->package() == Encoding::CJK &&
183 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
184 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
186 open_encoding_ = CJK;
191 ParagraphList::const_iterator par = pit;
193 par = TeXOnePar(buf, text, par, os, texrow, runparams);
195 if (par == paragraphs.end()) {
196 // Make sure that the last paragraph is
197 // correctly terminated (because TeXOnePar does
198 // not add a \n in this case)
201 } else if (par->params().depth() > pit->params().depth()) {
202 if (par->layout().isParagraph()) {
204 // How to handle this? (Lgb)
205 //&& !suffixIs(os, "\n\n")
207 // There should be at least one '\n' already
208 // but we need there to be two for Standard
209 // paragraphs that are depth-increment'ed to be
210 // output correctly. However, tables can
211 // also be paragraphs so don't adjust them.
214 // Will it ever harm to have one '\n' too
215 // many? i.e. that we sometimes will have
216 // three in a row. (Lgb)
220 par = TeXDeeper(buf, text, par, os, texrow,
223 } while (par != paragraphs.end()
224 && par->layout() == pit->layout()
225 && par->params().depth() == pit->params().depth()
226 && par->params().leftIndent() == pit->params().leftIndent());
228 if (open_encoding_ == CJK && cjk_nested) {
229 // We need to close the encoding even if it does not change
230 // to do correct environment nesting
231 os << "\\end{CJK}\n";
233 open_encoding_ = none;
236 if (style.isEnvironment()) {
237 os << "\\end{" << from_ascii(style.latexname()) << "}\n";
241 if (leftindent_open) {
242 os << "\\end{LyXParagraphLeftIndent}\n";
246 if (par != paragraphs.end())
247 LYXERR(Debug::LATEX, "TeXEnvironment...done " << &*par);
255 int latexOptArgInsets(Paragraph const & par, odocstream & os,
256 OutputParams const & runparams, int number)
260 InsetList::const_iterator it = par.insetList().begin();
261 InsetList::const_iterator end = par.insetList().end();
262 for (; it != end && number > 0 ; ++it) {
263 if (it->inset->lyxCode() == OPTARG_CODE) {
265 static_cast<InsetOptArg *>(it->inset);
266 lines += ins->latexOptional(os, runparams);
276 ParagraphList::const_iterator
277 TeXOnePar(Buffer const & buf,
279 ParagraphList::const_iterator const pit,
280 odocstream & os, TexRow & texrow,
281 OutputParams const & runparams_in,
282 string const & everypar)
284 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
286 BufferParams const & bparams = buf.params();
287 ParagraphList const & paragraphs = text.paragraphs();
289 ParagraphList::const_iterator priorpit = pit;
290 if (priorpit != paragraphs.begin())
292 ParagraphList::const_iterator nextpit = pit;
293 if (nextpit != paragraphs.end())
296 if (runparams_in.verbatim) {
297 int const dist = distance(paragraphs.begin(), pit);
298 Font const outerfont = outerFont(dist, paragraphs);
300 // No newline if only one paragraph in this lyxtext
306 /*bool need_par = */ pit->latex(bparams, outerfont,
307 os, texrow, runparams_in);
311 // FIXME This comment doesn't make sense. What's the
312 // length got to do with forceEmptyLayout()? I.e., what
313 // was forceDefaultParagraphs()?
314 // In an inset with unlimited length (all in one row),
315 // force layout to default
316 Layout const style = pit->forceEmptyLayout() ?
317 bparams.documentClass().emptyLayout() : pit->layout();
319 OutputParams runparams = runparams_in;
320 runparams.moving_arg |= style.needprotect;
322 bool const maintext = text.isMainText(buf);
323 // we are at the beginning of an inset and CJK is already open.
324 if (pit == paragraphs.begin() && !maintext && open_encoding_ == CJK) {
325 cjk_inherited_ = true;
326 open_encoding_ = none;
329 // This paragraph's language
330 Language const * const par_language = pit->getParLanguage(bparams);
331 // The document's language
332 Language const * const doc_language = bparams.language;
333 // The language that was in effect when the environment this paragraph is
334 // inside of was opened
335 Language const * const outer_language =
336 (runparams.local_font != 0) ?
337 runparams.local_font->language() : doc_language;
338 // The previous language that was in effect is either the language of
339 // the previous paragraph, if there is one, or else the outer language
340 // if there is no previous paragraph
341 Language const * const prev_language =
342 (pit != paragraphs.begin()) ?
343 priorpit->getParLanguage(bparams) : outer_language;
345 if (par_language->babel() != prev_language->babel()
346 // check if we already put language command in TeXEnvironment()
347 && !(style.isEnvironment()
348 && (pit == paragraphs.begin() ||
349 (priorpit->layout() != pit->layout() &&
350 priorpit->getDepth() <= pit->getDepth())
351 || priorpit->getDepth() < pit->getDepth())))
353 if (!lyxrc.language_command_end.empty() &&
354 prev_language->babel() != outer_language->babel() &&
355 !prev_language->babel().empty())
357 os << from_ascii(subst(lyxrc.language_command_end,
359 prev_language->babel()))
360 // the '%' is necessary to prevent unwanted whitespace
365 // We need to open a new language if we couldn't close the previous
366 // one (because there's no language_command_end); and even if we closed
367 // the previous one, if the current language is different than the
368 // outer_language (which is currently in effect once the previous one
370 if ((lyxrc.language_command_end.empty() ||
371 par_language->babel() != outer_language->babel()) &&
372 !par_language->babel().empty()) {
373 // If we're inside an inset, and that inset is within an \L or \R
374 // (or equivalents), then within the inset, too, any opposite
375 // language paragraph should appear within an \L or \R (in addition
376 // to, outside of, the normal language switch commands).
377 // This behavior is not correct for ArabTeX, though.
378 if ( // not for ArabTeX
379 (par_language->lang() != "arabic_arabtex" &&
380 outer_language->lang() != "arabic_arabtex") &&
381 // are we in an inset?
382 runparams.local_font != 0 &&
383 // is the inset within an \L or \R?
385 // FIXME: currently, we don't check this; this means that
386 // we'll have unnnecessary \L and \R commands, but that
387 // doesn't seem to hurt (though latex will complain)
389 // is this paragraph in the opposite direction?
390 runparams.local_font->isRightToLeft() !=
391 par_language->rightToLeft()
393 // FIXME: I don't have a working copy of the Arabi package, so
394 // I'm not sure if the farsi and arabic_arabi stuff is correct
396 if (par_language->lang() == "farsi")
398 else if (outer_language->lang() == "farsi")
400 else if (par_language->lang() == "arabic_arabi")
402 else if (outer_language->lang() == "arabic_arabi")
404 // remaining RTL languages currently is hebrew
405 else if (par_language->rightToLeft())
410 // With CJK, the CJK tag has to be closed first (see below)
411 if (runparams.encoding->package() != Encoding::CJK) {
412 os << from_ascii(subst(
413 lyxrc.language_command_begin,
415 par_language->babel()))
416 // the '%' is necessary to prevent unwanted whitespace
423 // Switch file encoding if necessary; no need to do this for "default"
424 // encoding, since this only affects the position of the outputted
425 // \inputencoding command; the encoding switch will occur when necessary
426 if (bparams.inputenc == "auto" &&
427 runparams.encoding->package() != Encoding::none) {
428 // Look ahead for future encoding changes.
429 // We try to output them at the beginning of the paragraph,
430 // since the \inputencoding command is not allowed e.g. in
432 for (pos_type i = 0; i < pit->size(); ++i) {
433 char_type const c = pit->getChar(i);
434 Encoding const * const encoding =
435 pit->getFontSettings(bparams, i).language()->encoding();
436 if (encoding->package() != Encoding::CJK &&
437 runparams.encoding->package() == Encoding::inputenc &&
442 // All characters before c are in the ASCII range, and
443 // c is non-ASCII (but no inset), so change the
444 // encoding to that required by the language of c.
445 // With CJK, only add switch if we have CJK content at the beginning
447 if (encoding->package() != Encoding::CJK || i == 0) {
448 OutputParams tmp_rp = runparams;
449 runparams.moving_arg = false;
450 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
453 // the following is necessary after a CJK environment in a multilingual
454 // context (nesting issue).
455 if (par_language->encoding()->package() == Encoding::CJK &&
456 open_encoding_ != CJK && !cjk_inherited_) {
457 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
459 open_encoding_ = CJK;
462 if (encoding->package() != Encoding::none && enc_switch.first) {
463 if (enc_switch.second > 0) {
464 // the '%' is necessary to prevent unwanted whitespace
468 // With CJK, the CJK tag had to be closed first (see above)
469 if (runparams.encoding->package() == Encoding::CJK) {
470 os << from_ascii(subst(
471 lyxrc.language_command_begin,
473 par_language->babel()))
474 // the '%' is necessary to prevent unwanted whitespace
478 runparams.encoding = encoding;
485 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
486 if (pit->allowParagraphCustomization()) {
487 if (pit->params().startOfAppendix()) {
488 os << "\\appendix\n";
492 if (!pit->params().spacing().isDefault()
493 && (pit == paragraphs.begin()
494 || !priorpit->hasSameLayout(*pit)))
496 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
501 if (style.isCommand()) {
507 switch (style.latextype) {
509 os << '\\' << from_ascii(style.latexname());
511 // Separate handling of optional argument inset.
512 if (style.optionalargs > 0) {
513 int ret = latexOptArgInsets(*pit, os, runparams,
521 os << from_ascii(style.latexparam());
523 case LATEX_ITEM_ENVIRONMENT:
524 case LATEX_LIST_ENVIRONMENT:
527 case LATEX_BIB_ENVIRONMENT:
528 // ignore this, the inset will write itself
534 Font const outerfont = outerFont(distance(paragraphs.begin(), pit),
538 os << from_utf8(everypar);
539 bool need_par = pit->latex(bparams, outerfont,
540 os, texrow, runparams);
542 // Make sure that \\par is done with the font of the last
543 // character if this has another size as the default.
544 // This is necessary because LaTeX (and LyX on the screen)
545 // calculates the space between the baselines according
546 // to this font. (Matthias)
548 // Is this really needed ? (Dekel)
549 // We do not need to use to change the font for the last paragraph
552 Font const font = pit->empty()
553 ? pit->getLayoutFont(bparams, outerfont)
554 : pit->getFont(bparams, pit->size() - 1, outerfont);
556 bool is_command = style.isCommand();
558 if (style.resfont.size() != font.fontInfo().size()
559 && nextpit != paragraphs.end()
563 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
564 } else if (need_par) {
566 } else if (is_command)
569 bool pending_newline = false;
570 switch (style.latextype) {
571 case LATEX_ITEM_ENVIRONMENT:
572 case LATEX_LIST_ENVIRONMENT:
573 if (nextpit != paragraphs.end()
574 && (pit->params().depth() < nextpit->params().depth()))
575 pending_newline = true;
577 case LATEX_ENVIRONMENT: {
578 // if its the last paragraph of the current environment
579 // skip it otherwise fall through
580 ParagraphList::const_iterator next = nextpit;
582 if (next != paragraphs.end() && (next->layout() != pit->layout()
583 || next->params().depth() != pit->params().depth()))
587 // fall through possible
589 // we don't need it for the last paragraph!!!
590 if (nextpit != paragraphs.end())
591 pending_newline = true;
594 if (pit->allowParagraphCustomization()) {
595 if (!pit->params().spacing().isDefault()
596 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
598 if (pending_newline) {
602 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
603 pending_newline = true;
607 // Closing the language is needed for the last paragraph; it is also
608 // needed if we're within an \L or \R that we may have opened above (not
609 // necessarily in this paragraph) and are about to close.
610 bool closing_rtl_ltr_environment =
612 (par_language->lang() != "arabic_arabtex" &&
613 outer_language->lang() != "arabic_arabtex") &&
614 // have we opened and \L or \R environment?
615 runparams.local_font != 0 &&
616 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
617 // are we about to close the language?
618 ((nextpit != paragraphs.end() &&
619 par_language->babel() !=
620 (nextpit->getParLanguage(bparams))->babel()) ||
621 (nextpit == paragraphs.end() &&
622 par_language->babel() != outer_language->babel()));
624 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
625 && par_language->babel() != outer_language->babel())) {
626 // Since \selectlanguage write the language to the aux file,
627 // we need to reset the language at the end of footnote or
630 if (pending_newline) {
634 // when the paragraph uses CJK, the language has to be closed earlier
635 if (font.language()->encoding()->package() != Encoding::CJK) {
636 if (lyxrc.language_command_end.empty()) {
637 if (!prev_language->babel().empty()) {
638 os << from_ascii(subst(
639 lyxrc.language_command_begin,
641 prev_language->babel()));
642 pending_newline = true;
644 } else if (!par_language->babel().empty()) {
645 os << from_ascii(subst(
646 lyxrc.language_command_end,
648 par_language->babel()));
649 pending_newline = true;
653 if (closing_rtl_ltr_environment)
656 if (pending_newline) {
661 // if this is a CJK-paragraph and the next isn't, close CJK
662 // also if the next paragraph is a multilingual environment (because of nesting)
663 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
664 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
665 nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams))
666 // in environments, CJK has to be closed later (nesting!)
667 && !style.isEnvironment()) {
668 os << "\\end{CJK}\n";
669 open_encoding_ = none;
672 // If this is the last paragraph, close the CJK environment
673 // if necessary. If it's an environment, we'll have to \end that first.
674 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
675 switch (open_encoding_) {
681 os << "\\end{CJK}\n";
686 open_encoding_ = none;
691 open_encoding_ = none;
701 // If this is the last paragraph, and a local_font was set upon entering
702 // the inset, and we're using "auto" or "default" encoding, the encoding
703 // should be set back to that local_font's encoding.
704 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
705 && (bparams.inputenc == "auto" || bparams.inputenc == "default")) {
706 runparams_in.encoding = runparams_in.local_font->language()->encoding();
707 os << setEncoding(runparams_in.encoding->iconvName());
710 // Otherwise, the current encoding should be set for the next paragraph.
712 runparams_in.encoding = runparams.encoding;
715 // we don't need it for the last paragraph!!!
716 // Note from JMarc: we will re-add a \n explicitely in
717 // TeXEnvironment, because it is needed in this case
718 if (nextpit != paragraphs.end()) {
723 if (nextpit != paragraphs.end())
724 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*nextpit);
732 // LaTeX all paragraphs
733 void latexParagraphs(Buffer const & buf,
737 OutputParams const & runparams,
738 string const & everypar)
740 bool was_title = false;
741 bool already_title = false;
742 BufferParams const & bparams = buf.params();
743 DocumentClass const & tclass = bparams.documentClass();
744 ParagraphList const & paragraphs = text.paragraphs();
745 ParagraphList::const_iterator par = paragraphs.begin();
746 ParagraphList::const_iterator endpar = paragraphs.end();
748 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
749 // if only part of the paragraphs will be outputed
750 if (runparams.par_begin != runparams.par_end) {
751 par = boost::next(paragraphs.begin(), runparams.par_begin);
752 endpar = boost::next(paragraphs.begin(), runparams.par_end);
753 // runparams will be passed to nested paragraphs, so
754 // we have to reset the range parameters.
755 const_cast<OutputParams&>(runparams).par_begin = 0;
756 const_cast<OutputParams&>(runparams).par_end = 0;
759 bool const maintext = text.isMainText(buf);
761 // Open a CJK environment at the beginning of the main buffer
762 // if the document's language is a CJK language
763 if (maintext && bparams.encoding().package() == Encoding::CJK) {
764 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
767 open_encoding_ = CJK;
769 // if "auto begin" is switched off, explicitely switch the
770 // language on at start
771 if (maintext && !lyxrc.language_auto_begin &&
772 !bparams.language->babel().empty()) {
774 os << from_utf8(subst(lyxrc.language_command_begin,
776 bparams.language->babel()))
781 ParagraphList::const_iterator lastpar;
783 while (par != endpar) {
785 // well we have to check if we are in an inset with unlimited
786 // length (all in one row) if that is true then we don't allow
787 // any special options in the paragraph and also we don't allow
788 // any environment other than the default layout of the
789 // text class to be valid!
790 if (par->allowParagraphCustomization()) {
791 Layout const & layout = par->forceEmptyLayout() ?
792 tclass.emptyLayout() :
795 if (layout.intitle) {
797 lyxerr << "Error in latexParagraphs: You"
798 " should not mix title layouts"
799 " with normal ones." << endl;
800 } else if (!was_title) {
802 if (tclass.titletype() == TITLE_ENVIRONMENT) {
804 << from_ascii(tclass.titlename())
809 } else if (was_title && !already_title) {
810 if (tclass.titletype() == TITLE_ENVIRONMENT) {
811 os << "\\end{" << from_ascii(tclass.titlename())
815 os << "\\" << from_ascii(tclass.titlename())
819 already_title = true;
823 if (layout.is_environment) {
824 par = TeXOnePar(buf, text, par, os, texrow,
825 runparams, everypar);
826 } else if (layout.isEnvironment() ||
827 !par->params().leftIndent().zero()) {
828 par = TeXEnvironment(buf, text, par, os,
831 par = TeXOnePar(buf, text, par, os, texrow,
832 runparams, everypar);
835 par = TeXOnePar(buf, text, par, os, texrow,
836 runparams, everypar);
838 if (distance(lastpar, par) >= distance(lastpar, endpar))
841 // It might be that we only have a title in this document
842 if (was_title && !already_title) {
843 if (tclass.titletype() == TITLE_ENVIRONMENT) {
844 os << "\\end{" << from_ascii(tclass.titlename())
848 os << "\\" << from_ascii(tclass.titlename())
853 // if "auto end" is switched off, explicitely close the language at the end
854 // but only if the last par is in a babel language
855 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
856 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
857 os << from_utf8(subst(lyxrc.language_command_end,
859 bparams.language->babel()))
863 // If the last paragraph is an environment, we'll have to close
864 // CJK at the very end to do proper nesting.
865 if (maintext && open_encoding_ == CJK) {
866 os << "\\end{CJK}\n";
868 open_encoding_ = none;
870 // reset inherited encoding
871 if (cjk_inherited_) {
872 open_encoding_ = CJK;
873 cjk_inherited_ = false;
878 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
879 OutputParams const & runparams, Encoding const & newEnc)
881 Encoding const oldEnc = *runparams.encoding;
882 bool moving_arg = runparams.moving_arg;
883 if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
885 return make_pair(false, 0);
887 // Do nothing if the encoding is unchanged.
888 if (oldEnc.name() == newEnc.name())
889 return make_pair(false, 0);
891 // FIXME We ignore encoding switches from/to encodings that do
892 // neither support the inputenc package nor the CJK package here.
893 // This does of course only work in special cases (e.g. switch from
894 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
895 // but it is the best we can do
896 if (oldEnc.package() == Encoding::none
897 || newEnc.package() == Encoding::none)
898 return make_pair(false, 0);
900 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
901 << oldEnc.name() << " to " << newEnc.name());
902 os << setEncoding(newEnc.iconvName());
903 if (bparams.inputenc == "default")
904 return make_pair(true, 0);
906 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
907 switch (newEnc.package()) {
909 // shouldn't ever reach here, see above
910 return make_pair(true, 0);
911 case Encoding::inputenc: {
912 int count = inputenc_arg.length();
913 if (oldEnc.package() == Encoding::CJK &&
914 open_encoding_ == CJK) {
916 open_encoding_ = none;
919 else if (oldEnc.package() == Encoding::inputenc &&
920 open_encoding_ == inputenc) {
922 open_encoding_ = none;
925 if (runparams.local_font != 0 && oldEnc.package() == Encoding::CJK) {
926 // within insets, \inputenc switches need to be
927 // embraced within \bgroup ... \egroup; else CJK fails.
930 open_encoding_ = inputenc;
932 os << "\\inputencoding{" << inputenc_arg << '}';
933 return make_pair(true, count + 16);
935 case Encoding::CJK: {
936 int count = inputenc_arg.length();
937 if (oldEnc.package() == Encoding::CJK &&
938 open_encoding_ == CJK) {
942 if (oldEnc.package() == Encoding::inputenc &&
943 open_encoding_ == inputenc) {
947 os << "\\begin{CJK}{" << inputenc_arg << "}{}";
948 open_encoding_ = CJK;
949 return make_pair(true, count + 15);
952 // Dead code to avoid a warning:
953 return make_pair(true, 0);