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/lassert.h"
34 #include "support/debug.h"
35 #include "support/lstrings.h"
37 #include <boost/next_prior.hpp>
40 using namespace lyx::support;
53 static int open_encoding_ = none;
54 static int cjk_inherited_ = 0;
57 ParagraphList::const_iterator
58 TeXEnvironment(Buffer const & buf,
60 ParagraphList::const_iterator pit,
61 odocstream & os, TexRow & texrow,
62 OutputParams const & runparams);
64 ParagraphList::const_iterator
65 TeXOnePar(Buffer const & buf,
67 ParagraphList::const_iterator pit,
68 odocstream & os, TexRow & texrow,
69 OutputParams const & runparams,
70 string const & everypar = string());
73 ParagraphList::const_iterator
74 TeXDeeper(Buffer const & buf,
76 ParagraphList::const_iterator pit,
77 odocstream & os, TexRow & texrow,
78 OutputParams const & runparams)
80 LYXERR(Debug::LATEX, "TeXDeeper... " << &*pit);
81 ParagraphList::const_iterator par = pit;
83 ParagraphList const & paragraphs = text.paragraphs();
85 while (par != paragraphs.end() &&
86 par->params().depth() == pit->params().depth()) {
87 if (par->layout().isEnvironment()) {
88 par = TeXEnvironment(buf, text, par,
89 os, texrow, runparams);
91 par = TeXOnePar(buf, text, par,
92 os, texrow, runparams);
95 LYXERR(Debug::LATEX, "TeXDeeper...done ");
101 ParagraphList::const_iterator
102 TeXEnvironment(Buffer const & buf,
104 ParagraphList::const_iterator pit,
105 odocstream & os, TexRow & texrow,
106 OutputParams const & runparams)
108 LYXERR(Debug::LATEX, "TeXEnvironment... " << &*pit);
110 BufferParams const & bparams = buf.params();
112 Layout const & style = pit->forceEmptyLayout() ?
113 bparams.documentClass().emptyLayout() : pit->layout();
115 ParagraphList const & paragraphs = text.paragraphs();
117 Language const * const par_language = pit->getParLanguage(bparams);
118 Language const * const doc_language = bparams.language;
119 Language const * const prev_par_language =
120 (pit != paragraphs.begin())
121 ? boost::prior(pit)->getParLanguage(bparams)
123 if (par_language->babel() != prev_par_language->babel()) {
125 if (!lyxrc.language_command_end.empty() &&
126 prev_par_language->babel() != doc_language->babel() &&
127 !prev_par_language->babel().empty()) {
128 os << from_ascii(subst(
129 lyxrc.language_command_end,
131 prev_par_language->babel()))
132 // the '%' is necessary to prevent unwanted whitespace
137 if ((lyxrc.language_command_end.empty() ||
138 par_language->babel() != doc_language->babel()) &&
139 !par_language->babel().empty()) {
140 os << from_ascii(subst(
141 lyxrc.language_command_begin,
143 par_language->babel()))
144 // the '%' is necessary to prevent unwanted whitespace
150 bool leftindent_open = false;
151 if (!pit->params().leftIndent().zero()) {
152 os << "\\begin{LyXParagraphLeftIndent}{"
153 << from_ascii(pit->params().leftIndent().asLatexString())
156 leftindent_open = true;
159 if (style.isEnvironment()) {
160 os << "\\begin{" << from_ascii(style.latexname()) << '}';
161 if (style.optionalargs > 0) {
162 int ret = latexOptArgInsets(*pit, os, runparams,
169 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
171 << pit->params().labelWidthString()
173 } else if (style.labeltype == LABEL_BIBLIO) {
175 os << '{' << bibitemWidest(buf) << "}\n";
177 os << from_ascii(style.latexparam()) << '\n';
181 // in multilingual environments, the CJK tags have to be nested properly
182 bool cjk_nested = false;
183 if (par_language->encoding()->package() == Encoding::CJK &&
184 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
185 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
186 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
187 open_encoding_ = CJK;
192 ParagraphList::const_iterator par = pit;
194 par = TeXOnePar(buf, text, par, os, texrow, runparams);
196 if (par == paragraphs.end()) {
197 // Make sure that the last paragraph is
198 // correctly terminated (because TeXOnePar does
199 // not add a \n in this case)
202 } else if (par->params().depth() > pit->params().depth()) {
203 if (par->layout().isParagraph()) {
205 // How to handle this? (Lgb)
206 //&& !suffixIs(os, "\n\n")
208 // There should be at least one '\n' already
209 // but we need there to be two for Standard
210 // paragraphs that are depth-increment'ed to be
211 // output correctly. However, tables can
212 // also be paragraphs so don't adjust them.
215 // Will it ever harm to have one '\n' too
216 // many? i.e. that we sometimes will have
217 // three in a row. (Lgb)
221 par = TeXDeeper(buf, text, par, os, texrow,
224 } while (par != paragraphs.end()
225 && par->layout() == pit->layout()
226 && par->params().depth() == pit->params().depth()
227 && par->params().leftIndent() == pit->params().leftIndent());
229 if (open_encoding_ == CJK && cjk_nested) {
230 // We need to close the encoding even if it does not change
231 // to do correct environment nesting
232 os << "\\end{CJK}\n";
234 open_encoding_ = none;
237 if (style.isEnvironment()) {
238 os << "\\end{" << from_ascii(style.latexname()) << "}\n";
242 if (leftindent_open) {
243 os << "\\end{LyXParagraphLeftIndent}\n";
247 if (par != paragraphs.end())
248 LYXERR(Debug::LATEX, "TeXEnvironment...done " << &*par);
256 int latexOptArgInsets(Paragraph const & par, odocstream & os,
257 OutputParams const & runparams, int number)
261 InsetList::const_iterator it = par.insetList().begin();
262 InsetList::const_iterator end = par.insetList().end();
263 for (; it != end && number > 0 ; ++it) {
264 if (it->inset->lyxCode() == OPTARG_CODE) {
266 static_cast<InsetOptArg *>(it->inset);
267 lines += ins->latexOptional(os, runparams);
277 ParagraphList::const_iterator
278 TeXOnePar(Buffer const & buf,
280 ParagraphList::const_iterator const pit,
281 odocstream & os, TexRow & texrow,
282 OutputParams const & runparams_in,
283 string const & everypar)
285 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
287 BufferParams const & bparams = buf.params();
288 ParagraphList const & paragraphs = text.paragraphs();
290 ParagraphList::const_iterator priorpit = pit;
291 if (priorpit != paragraphs.begin())
293 ParagraphList::const_iterator nextpit = pit;
294 if (nextpit != paragraphs.end())
297 if (runparams_in.verbatim) {
298 int const dist = distance(paragraphs.begin(), pit);
299 Font const outerfont = outerFont(dist, paragraphs);
301 // No newline if only one paragraph in this lyxtext
307 /*bool need_par = */ pit->latex(bparams, outerfont,
308 os, texrow, runparams_in);
312 Layout const style = pit->forceEmptyLayout() ?
313 bparams.documentClass().emptyLayout() : pit->layout();
315 OutputParams runparams = runparams_in;
316 runparams.moving_arg |= style.needprotect;
318 bool const maintext = text.isMainText(buf);
319 // we are at the beginning of an inset and CJK is already open;
320 // we count inheritation levels to get the inset nesting right.
321 if (pit == paragraphs.begin() && !maintext
322 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
324 open_encoding_ = none;
327 // This paragraph's language
328 Language const * const par_language = pit->getParLanguage(bparams);
329 // The document's language
330 Language const * const doc_language = bparams.language;
331 // The language that was in effect when the environment this paragraph is
332 // inside of was opened
333 Language const * const outer_language =
334 (runparams.local_font != 0) ?
335 runparams.local_font->language() : doc_language;
336 // The previous language that was in effect is either the language of
337 // the previous paragraph, if there is one, or else the outer language
338 // if there is no previous paragraph
339 Language const * const prev_language =
340 (pit != paragraphs.begin()) ?
341 priorpit->getParLanguage(bparams) : outer_language;
343 if (par_language->babel() != prev_language->babel()
344 // check if we already put language command in TeXEnvironment()
345 && !(style.isEnvironment()
346 && (pit == paragraphs.begin() ||
347 (priorpit->layout() != pit->layout() &&
348 priorpit->getDepth() <= pit->getDepth())
349 || priorpit->getDepth() < pit->getDepth())))
351 if (!lyxrc.language_command_end.empty() &&
352 prev_language->babel() != outer_language->babel() &&
353 !prev_language->babel().empty())
355 os << from_ascii(subst(lyxrc.language_command_end,
357 prev_language->babel()))
358 // the '%' is necessary to prevent unwanted whitespace
363 // We need to open a new language if we couldn't close the previous
364 // one (because there's no language_command_end); and even if we closed
365 // the previous one, if the current language is different than the
366 // outer_language (which is currently in effect once the previous one
368 if ((lyxrc.language_command_end.empty() ||
369 par_language->babel() != outer_language->babel()) &&
370 !par_language->babel().empty()) {
371 // If we're inside an inset, and that inset is within an \L or \R
372 // (or equivalents), then within the inset, too, any opposite
373 // language paragraph should appear within an \L or \R (in addition
374 // to, outside of, the normal language switch commands).
375 // This behavior is not correct for ArabTeX, though.
376 if ( // not for ArabTeX
377 (par_language->lang() != "arabic_arabtex" &&
378 outer_language->lang() != "arabic_arabtex") &&
379 // are we in an inset?
380 runparams.local_font != 0 &&
381 // is the inset within an \L or \R?
383 // FIXME: currently, we don't check this; this means that
384 // we'll have unnnecessary \L and \R commands, but that
385 // doesn't seem to hurt (though latex will complain)
387 // is this paragraph in the opposite direction?
388 runparams.local_font->isRightToLeft() !=
389 par_language->rightToLeft()
391 // FIXME: I don't have a working copy of the Arabi package, so
392 // I'm not sure if the farsi and arabic_arabi stuff is correct
394 if (par_language->lang() == "farsi")
396 else if (outer_language->lang() == "farsi")
398 else if (par_language->lang() == "arabic_arabi")
400 else if (outer_language->lang() == "arabic_arabi")
402 // remaining RTL languages currently is hebrew
403 else if (par_language->rightToLeft())
408 // With CJK, the CJK tag has to be closed first (see below)
409 if (runparams.encoding->package() != Encoding::CJK) {
410 os << from_ascii(subst(
411 lyxrc.language_command_begin,
413 par_language->babel()))
414 // the '%' is necessary to prevent unwanted whitespace
421 // Switch file encoding if necessary; no need to do this for "default"
422 // encoding, since this only affects the position of the outputted
423 // \inputencoding command; the encoding switch will occur when necessary
424 if (bparams.inputenc == "auto" &&
425 runparams.encoding->package() != Encoding::none) {
426 // Look ahead for future encoding changes.
427 // We try to output them at the beginning of the paragraph,
428 // since the \inputencoding command is not allowed e.g. in
430 for (pos_type i = 0; i < pit->size(); ++i) {
431 char_type const c = pit->getChar(i);
432 Encoding const * const encoding =
433 pit->getFontSettings(bparams, i).language()->encoding();
434 if (encoding->package() != Encoding::CJK &&
435 runparams.encoding->package() == Encoding::inputenc &&
440 // All characters before c are in the ASCII range, and
441 // c is non-ASCII (but no inset), so change the
442 // encoding to that required by the language of c.
443 // With CJK, only add switch if we have CJK content at the beginning
445 if (encoding->package() != Encoding::CJK || i == 0) {
446 OutputParams tmp_rp = runparams;
447 runparams.moving_arg = false;
448 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
451 // the following is necessary after a CJK environment in a multilingual
452 // context (nesting issue).
453 if (par_language->encoding()->package() == Encoding::CJK &&
454 open_encoding_ != CJK && cjk_inherited_ == 0) {
455 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
456 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
457 open_encoding_ = CJK;
460 if (encoding->package() != Encoding::none && enc_switch.first) {
461 if (enc_switch.second > 0) {
462 // the '%' is necessary to prevent unwanted whitespace
466 // With CJK, the CJK tag had to be closed first (see above)
467 if (runparams.encoding->package() == Encoding::CJK) {
468 os << from_ascii(subst(
469 lyxrc.language_command_begin,
471 par_language->babel()))
472 // the '%' is necessary to prevent unwanted whitespace
476 runparams.encoding = encoding;
483 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
484 if (pit->allowParagraphCustomization()) {
485 if (pit->params().startOfAppendix()) {
486 os << "\\appendix\n";
490 if (!pit->params().spacing().isDefault()
491 && (pit == paragraphs.begin()
492 || !priorpit->hasSameLayout(*pit)))
494 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
499 if (style.isCommand()) {
505 switch (style.latextype) {
507 os << '\\' << from_ascii(style.latexname());
509 // Separate handling of optional argument inset.
510 if (style.optionalargs > 0) {
511 int ret = latexOptArgInsets(*pit, os, runparams,
519 os << from_ascii(style.latexparam());
521 case LATEX_ITEM_ENVIRONMENT:
522 case LATEX_LIST_ENVIRONMENT:
525 case LATEX_BIB_ENVIRONMENT:
526 // ignore this, the inset will write itself
532 Font const outerfont = outerFont(distance(paragraphs.begin(), pit),
536 os << from_utf8(everypar);
537 bool need_par = pit->latex(bparams, outerfont,
538 os, texrow, runparams);
540 // Make sure that \\par is done with the font of the last
541 // character if this has another size as the default.
542 // This is necessary because LaTeX (and LyX on the screen)
543 // calculates the space between the baselines according
544 // to this font. (Matthias)
546 // Is this really needed ? (Dekel)
547 // We do not need to use to change the font for the last paragraph
550 Font const font = pit->empty()
551 ? pit->getLayoutFont(bparams, outerfont)
552 : pit->getFont(bparams, pit->size() - 1, outerfont);
554 bool is_command = style.isCommand();
556 if (style.resfont.size() != font.fontInfo().size()
557 && nextpit != paragraphs.end()
561 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
562 } else if (need_par) {
564 } else if (is_command)
567 bool pending_newline = false;
568 switch (style.latextype) {
569 case LATEX_ITEM_ENVIRONMENT:
570 case LATEX_LIST_ENVIRONMENT:
571 if (nextpit != paragraphs.end()
572 && (pit->params().depth() < nextpit->params().depth()))
573 pending_newline = true;
575 case LATEX_ENVIRONMENT: {
576 // if its the last paragraph of the current environment
577 // skip it otherwise fall through
578 ParagraphList::const_iterator next = nextpit;
580 if (next != paragraphs.end() && (next->layout() != pit->layout()
581 || next->params().depth() != pit->params().depth()))
585 // fall through possible
587 // we don't need it for the last paragraph!!!
588 if (nextpit != paragraphs.end())
589 pending_newline = true;
592 if (pit->allowParagraphCustomization()) {
593 if (!pit->params().spacing().isDefault()
594 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
596 if (pending_newline) {
600 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
601 pending_newline = true;
605 // Closing the language is needed for the last paragraph; it is also
606 // needed if we're within an \L or \R that we may have opened above (not
607 // necessarily in this paragraph) and are about to close.
608 bool closing_rtl_ltr_environment =
610 (par_language->lang() != "arabic_arabtex" &&
611 outer_language->lang() != "arabic_arabtex") &&
612 // have we opened and \L or \R environment?
613 runparams.local_font != 0 &&
614 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
615 // are we about to close the language?
616 ((nextpit != paragraphs.end() &&
617 par_language->babel() !=
618 (nextpit->getParLanguage(bparams))->babel()) ||
619 (nextpit == paragraphs.end() &&
620 par_language->babel() != outer_language->babel()));
622 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
623 && par_language->babel() != outer_language->babel())) {
624 // Since \selectlanguage write the language to the aux file,
625 // we need to reset the language at the end of footnote or
628 if (pending_newline) {
632 // when the paragraph uses CJK, the language has to be closed earlier
633 if (font.language()->encoding()->package() != Encoding::CJK) {
634 if (lyxrc.language_command_end.empty()) {
635 if (!prev_language->babel().empty()) {
636 os << from_ascii(subst(
637 lyxrc.language_command_begin,
639 prev_language->babel()));
640 pending_newline = true;
642 } else if (!par_language->babel().empty()) {
643 os << from_ascii(subst(
644 lyxrc.language_command_end,
646 par_language->babel()));
647 pending_newline = true;
651 if (closing_rtl_ltr_environment)
654 if (pending_newline) {
659 // if this is a CJK-paragraph and the next isn't, close CJK
660 // also if the next paragraph is a multilingual environment (because of nesting)
661 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
662 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
663 nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams))
664 // in environments, CJK has to be closed later (nesting!)
665 && !style.isEnvironment()) {
666 os << "\\end{CJK}\n";
667 open_encoding_ = none;
670 // If this is the last paragraph, close the CJK environment
671 // if necessary. If it's an environment, we'll have to \end that first.
672 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
673 switch (open_encoding_) {
679 os << "\\end{CJK}\n";
684 open_encoding_ = none;
689 open_encoding_ = none;
699 // If this is the last paragraph, and a local_font was set upon entering
700 // the inset, and we're using "auto" or "default" encoding, the encoding
701 // should be set back to that local_font's encoding.
702 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
703 && (bparams.inputenc == "auto" || bparams.inputenc == "default")) {
704 runparams_in.encoding = runparams_in.local_font->language()->encoding();
705 os << setEncoding(runparams_in.encoding->iconvName());
708 // Otherwise, the current encoding should be set for the next paragraph.
710 runparams_in.encoding = runparams.encoding;
713 // we don't need it for the last paragraph!!!
714 // Note from JMarc: we will re-add a \n explicitely in
715 // TeXEnvironment, because it is needed in this case
716 if (nextpit != paragraphs.end()) {
717 Layout const & next_layout = nextpit->layout();
718 // no blank lines before environments!
719 if (!next_layout.isEnvironment() || style == next_layout) {
725 if (nextpit != paragraphs.end())
726 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*nextpit);
734 // LaTeX all paragraphs
735 void latexParagraphs(Buffer const & buf,
739 OutputParams const & runparams,
740 string const & everypar)
742 bool was_title = false;
743 bool already_title = false;
744 BufferParams const & bparams = buf.params();
745 DocumentClass const & tclass = bparams.documentClass();
746 ParagraphList const & paragraphs = text.paragraphs();
747 ParagraphList::const_iterator par = paragraphs.begin();
748 ParagraphList::const_iterator endpar = paragraphs.end();
750 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
751 // if only part of the paragraphs will be outputed
752 if (runparams.par_begin != runparams.par_end) {
753 par = boost::next(paragraphs.begin(), runparams.par_begin);
754 endpar = boost::next(paragraphs.begin(), runparams.par_end);
755 // runparams will be passed to nested paragraphs, so
756 // we have to reset the range parameters.
757 const_cast<OutputParams&>(runparams).par_begin = 0;
758 const_cast<OutputParams&>(runparams).par_end = 0;
761 bool const maintext = text.isMainText(buf);
763 // Open a CJK environment at the beginning of the main buffer
764 // if the document's language is a CJK language
765 if (maintext && bparams.encoding().package() == Encoding::CJK) {
766 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
767 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
769 open_encoding_ = CJK;
771 // if "auto begin" is switched off, explicitely switch the
772 // language on at start
773 if (maintext && !lyxrc.language_auto_begin &&
774 !bparams.language->babel().empty()) {
776 os << from_utf8(subst(lyxrc.language_command_begin,
778 bparams.language->babel()))
783 ParagraphList::const_iterator lastpar;
785 while (par != endpar) {
787 Layout const & layout = par->forceEmptyLayout() ?
788 tclass.emptyLayout() :
791 if (layout.intitle) {
793 lyxerr << "Error in latexParagraphs: You"
794 " should not mix title layouts"
795 " with normal ones." << endl;
796 } else if (!was_title) {
798 if (tclass.titletype() == TITLE_ENVIRONMENT) {
800 << from_ascii(tclass.titlename())
805 } else if (was_title && !already_title) {
806 if (tclass.titletype() == TITLE_ENVIRONMENT) {
807 os << "\\end{" << from_ascii(tclass.titlename())
811 os << "\\" << from_ascii(tclass.titlename())
815 already_title = true;
819 if (layout.is_environment) {
820 par = TeXOnePar(buf, text, par, os, texrow,
821 runparams, everypar);
822 } else if (layout.isEnvironment() ||
823 !par->params().leftIndent().zero()) {
824 par = TeXEnvironment(buf, text, par, os,
827 par = TeXOnePar(buf, text, par, os, texrow,
828 runparams, everypar);
830 if (distance(lastpar, par) >= distance(lastpar, endpar))
834 // It might be that we only have a title in this document
835 if (was_title && !already_title) {
836 if (tclass.titletype() == TITLE_ENVIRONMENT) {
837 os << "\\end{" << from_ascii(tclass.titlename())
841 os << "\\" << from_ascii(tclass.titlename())
847 // if "auto end" is switched off, explicitely close the language at the end
848 // but only if the last par is in a babel language
849 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
850 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
851 os << from_utf8(subst(lyxrc.language_command_end,
853 bparams.language->babel()))
858 // If the last paragraph is an environment, we'll have to close
859 // CJK at the very end to do proper nesting.
860 if (maintext && open_encoding_ == CJK) {
861 os << "\\end{CJK}\n";
863 open_encoding_ = none;
866 // reset inherited encoding
867 if (cjk_inherited_ > 0) {
869 if (cjk_inherited_ == 0)
870 open_encoding_ = CJK;
875 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
876 OutputParams const & runparams, Encoding const & newEnc)
878 Encoding const oldEnc = *runparams.encoding;
879 bool moving_arg = runparams.moving_arg;
880 if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
882 return make_pair(false, 0);
884 // Do nothing if the encoding is unchanged.
885 if (oldEnc.name() == newEnc.name())
886 return make_pair(false, 0);
888 // FIXME We ignore encoding switches from/to encodings that do
889 // neither support the inputenc package nor the CJK package here.
890 // This does of course only work in special cases (e.g. switch from
891 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
892 // but it is the best we can do
893 if (oldEnc.package() == Encoding::none
894 || newEnc.package() == Encoding::none)
895 return make_pair(false, 0);
897 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
898 << oldEnc.name() << " to " << newEnc.name());
899 os << setEncoding(newEnc.iconvName());
900 if (bparams.inputenc == "default")
901 return make_pair(true, 0);
903 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
904 switch (newEnc.package()) {
906 // shouldn't ever reach here, see above
907 return make_pair(true, 0);
908 case Encoding::inputenc: {
909 int count = inputenc_arg.length();
910 if (oldEnc.package() == Encoding::CJK &&
911 open_encoding_ == CJK) {
913 open_encoding_ = none;
916 else if (oldEnc.package() == Encoding::inputenc &&
917 open_encoding_ == inputenc) {
919 open_encoding_ = none;
922 if (runparams.local_font != 0 && oldEnc.package() == Encoding::CJK) {
923 // within insets, \inputenc switches need to be
924 // embraced within \bgroup ... \egroup; else CJK fails.
927 open_encoding_ = inputenc;
929 os << "\\inputencoding{" << inputenc_arg << '}';
930 return make_pair(true, count + 16);
932 case Encoding::CJK: {
933 int count = inputenc_arg.length();
934 if (oldEnc.package() == Encoding::CJK &&
935 open_encoding_ == CJK) {
939 if (oldEnc.package() == Encoding::inputenc &&
940 open_encoding_ == inputenc) {
944 os << "\\begin{CJK}{" << inputenc_arg << "}{"
945 << from_ascii(bparams.fontsCJK) << "}";
946 open_encoding_ = CJK;
947 return make_pair(true, count + 15);
950 // Dead code to avoid a warning:
951 return make_pair(true, 0);