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"
17 #include "support/debug.h"
19 #include "InsetList.h"
23 #include "OutputParams.h"
24 #include "Paragraph.h"
25 #include "paragraph_funcs.h"
26 #include "ParagraphParameters.h"
27 #include "TextClass.h"
31 #include "insets/InsetBibitem.h"
32 #include "insets/InsetOptArg.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 LayoutPtr const & style = pit->layout();
113 ParagraphList const & paragraphs = text.paragraphs();
115 Language const * const par_language = pit->getParLanguage(bparams);
116 Language const * const doc_language = bparams.language;
117 Language const * const prev_par_language =
118 (pit != paragraphs.begin())
119 ? boost::prior(pit)->getParLanguage(bparams)
121 if (par_language->babel() != prev_par_language->babel()) {
123 if (!lyxrc.language_command_end.empty() &&
124 prev_par_language->babel() != doc_language->babel() &&
125 !prev_par_language->babel().empty()) {
126 os << from_ascii(subst(
127 lyxrc.language_command_end,
129 prev_par_language->babel()))
130 // the '%' is necessary to prevent unwanted whitespace
135 if ((lyxrc.language_command_end.empty() ||
136 par_language->babel() != doc_language->babel()) &&
137 !par_language->babel().empty()) {
138 os << from_ascii(subst(
139 lyxrc.language_command_begin,
141 par_language->babel()))
142 // the '%' is necessary to prevent unwanted whitespace
148 bool leftindent_open = false;
149 if (!pit->params().leftIndent().zero()) {
150 os << "\\begin{LyXParagraphLeftIndent}{"
151 << from_ascii(pit->params().leftIndent().asLatexString())
154 leftindent_open = true;
157 if (style->isEnvironment()) {
158 os << "\\begin{" << from_ascii(style->latexname()) << '}';
159 if (style->optionalargs > 0) {
160 int ret = latexOptArgInsets(buf, *pit, os, runparams,
161 style->optionalargs);
167 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
169 << pit->params().labelWidthString()
171 } else if (style->labeltype == LABEL_BIBLIO) {
173 os << '{' << bibitemWidest(buf) << "}\n";
175 os << from_ascii(style->latexparam()) << '\n';
179 // in multilingual environments, the CJK tags have to be nested properly
180 bool cjk_nested = false;
181 if (par_language->encoding()->package() == Encoding::CJK &&
182 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
183 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
185 open_encoding_ = CJK;
190 ParagraphList::const_iterator par = pit;
192 par = TeXOnePar(buf, text, par, os, texrow, runparams);
194 if (par == paragraphs.end()) {
195 // Make sure that the last paragraph is
196 // correctly terminated (because TeXOnePar does
197 // not add a \n in this case)
200 } else if (par->params().depth() > pit->params().depth()) {
201 if (par->layout()->isParagraph()) {
203 // How to handle this? (Lgb)
204 //&& !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(Buffer const & buf, Paragraph const & par,
256 odocstream & os, 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(buf, os, runparams);
276 ParagraphList::const_iterator
277 TeXOnePar(Buffer const & buf,
279 ParagraphList::const_iterator 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();
288 ParagraphList const & paragraphs = text.paragraphs();
290 if (runparams_in.verbatim) {
291 int const dist = distance(paragraphs.begin(), pit);
292 Font const outerfont = outerFont(dist, paragraphs);
294 // No newline if only one paragraph in this lyxtext
300 /*bool need_par = */ pit->latex(buf, bparams, outerfont,
301 os, texrow, runparams_in);
306 // In an inset with unlimited length (all in one row),
307 // force layout to default
308 if (!pit->forceDefaultParagraphs())
309 style = pit->layout();
311 style = bparams.getTextClass().defaultLayout();
313 OutputParams runparams = runparams_in;
314 runparams.moving_arg |= style->needprotect;
316 bool const maintext = text.isMainText(buf);
317 // we are at the beginning of an inset and CJK is already open.
318 if (pit == paragraphs.begin() && !maintext && open_encoding_ == CJK) {
319 cjk_inherited_ = true;
320 open_encoding_ = none;
323 // This paragraph's language
324 Language const * const par_language = pit->getParLanguage(bparams);
325 // The document's language
326 Language const * const doc_language = bparams.language;
327 // The language that was in effect when the environment this paragraph is
328 // inside of was opened
329 Language const * const outer_language =
330 (runparams.local_font != 0) ?
331 runparams.local_font->language() : doc_language;
332 // The previous language that was in effect is either the language of
333 // the previous paragraph, if there is one, or else the outer language
334 // if there is no previous paragraph
335 Language const * const prev_language =
336 (pit != paragraphs.begin()) ?
337 boost::prior(pit)->getParLanguage(bparams) : outer_language;
339 if (par_language->babel() != prev_language->babel()
340 // check if we already put language command in TeXEnvironment()
341 && !(style->isEnvironment()
342 && (pit == paragraphs.begin() ||
343 (boost::prior(pit)->layout() != pit->layout() &&
344 boost::prior(pit)->getDepth() <= pit->getDepth())
345 || boost::prior(pit)->getDepth() < pit->getDepth())))
347 if (!lyxrc.language_command_end.empty() &&
348 prev_language->babel() != outer_language->babel() &&
349 !prev_language->babel().empty())
351 os << from_ascii(subst(lyxrc.language_command_end,
353 prev_language->babel()))
354 // the '%' is necessary to prevent unwanted whitespace
359 // We need to open a new language if we couldn't close the previous
360 // one (because there's no language_command_end); and even if we closed
361 // the previous one, if the current language is different than the
362 // outer_language (which is currently in effect once the previous one
364 if ((lyxrc.language_command_end.empty() ||
365 par_language->babel() != outer_language->babel()) &&
366 !par_language->babel().empty()) {
367 // If we're inside an inset, and that inset is within an \L or \R
368 // (or equivalents), then within the inset, too, any opposite
369 // language paragraph should appear within an \L or \R (in addition
370 // to, outside of, the normal language switch commands).
371 // This behavior is not correct for ArabTeX, though.
372 if ( // not for ArabTeX
373 (par_language->lang() != "arabic_arabtex" &&
374 outer_language->lang() != "arabic_arabtex") &&
375 // are we in an inset?
376 runparams.local_font != 0 &&
377 // is the inset within an \L or \R?
379 // FIXME: currently, we don't check this; this means that
380 // we'll have unnnecessary \L and \R commands, but that
381 // doesn't seem to hurt (though latex will complain)
383 // is this paragraph in the opposite direction?
384 runparams.local_font->isRightToLeft() !=
385 par_language->rightToLeft()
387 // FIXME: I don't have a working copy of the Arabi package, so
388 // I'm not sure if the farsi and arabic_arabi stuff is correct
390 if (par_language->lang() == "farsi")
392 else if (outer_language->lang() == "farsi")
394 else if (par_language->lang() == "arabic_arabi")
396 else if (outer_language->lang() == "arabic_arabi")
398 // remaining RTL languages currently is hebrew
399 else if (par_language->rightToLeft())
404 // With CJK, the CJK tag has to be closed first (see below)
405 if (runparams.encoding->package() != Encoding::CJK) {
406 os << from_ascii(subst(
407 lyxrc.language_command_begin,
409 par_language->babel()))
410 // the '%' is necessary to prevent unwanted whitespace
417 // Switch file encoding if necessary; no need to do this for "default"
418 // encoding, since this only affects the position of the outputted
419 // \inputencoding command; the encoding switch will occur when necessary
420 if (bparams.inputenc == "auto" &&
421 runparams.encoding->package() != Encoding::none) {
422 // Look ahead for future encoding changes.
423 // We try to output them at the beginning of the paragraph,
424 // since the \inputencoding command is not allowed e.g. in
426 for (pos_type i = 0; i < pit->size(); ++i) {
427 char_type const c = pit->getChar(i);
428 Encoding const * const encoding =
429 pit->getFontSettings(bparams, i).language()->encoding();
430 if (encoding->package() != Encoding::CJK &&
431 runparams.encoding->package() == Encoding::inputenc &&
436 // All characters before c are in the ASCII range, and
437 // c is non-ASCII (but no inset), so change the
438 // encoding to that required by the language of c.
439 // With CJK, only add switch if we have CJK content at the beginning
441 if (encoding->package() != Encoding::CJK || i == 0) {
442 OutputParams tmp_rp = runparams;
443 runparams.moving_arg = false;
444 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
447 // the following is necessary after a CJK environment in a multilingual
448 // context (nesting issue).
449 if (par_language->encoding()->package() == Encoding::CJK &&
450 open_encoding_ != CJK && !cjk_inherited_) {
451 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
453 open_encoding_ = CJK;
456 if (encoding->package() != Encoding::none && enc_switch.first) {
457 if (enc_switch.second > 0) {
458 // the '%' is necessary to prevent unwanted whitespace
462 // With CJK, the CJK tag had to be closed first (see above)
463 if (runparams.encoding->package() == Encoding::CJK) {
464 os << from_ascii(subst(
465 lyxrc.language_command_begin,
467 par_language->babel()))
468 // the '%' is necessary to prevent unwanted whitespace
472 runparams.encoding = encoding;
479 // In an inset with unlimited length (all in one row),
480 // don't allow any special options in the paragraph
481 bool const useSetSpace = bparams.getTextClass().provides("SetSpace");
482 if (!pit->forceDefaultParagraphs()) {
483 if (pit->params().startOfAppendix()) {
484 os << "\\appendix\n";
488 if (!pit->params().spacing().isDefault()
489 && (pit == paragraphs.begin()
490 || !boost::prior(pit)->hasSameLayout(*pit)))
492 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
497 if (style->isCommand()) {
503 switch (style->latextype) {
505 os << '\\' << from_ascii(style->latexname());
507 // Separate handling of optional argument inset.
508 if (style->optionalargs > 0) {
509 int ret = latexOptArgInsets(buf, *pit, os, runparams,
510 style->optionalargs);
517 os << from_ascii(style->latexparam());
519 case LATEX_ITEM_ENVIRONMENT:
520 case LATEX_LIST_ENVIRONMENT:
523 case LATEX_BIB_ENVIRONMENT:
524 // ignore this, the inset will write itself
530 Font const outerfont =
531 outerFont(distance(paragraphs.begin(), pit),
535 os << from_utf8(everypar);
536 bool need_par = pit->latex(buf, bparams, outerfont,
537 os, texrow, runparams);
539 // Make sure that \\par is done with the font of the last
540 // character if this has another size as the default.
541 // This is necessary because LaTeX (and LyX on the screen)
542 // calculates the space between the baselines according
543 // to this font. (Matthias)
545 // Is this really needed ? (Dekel)
546 // We do not need to use to change the font for the last paragraph
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 && boost::next(pit) != 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 (boost::next(pit) != paragraphs.end()
572 && (pit->params().depth() < boost::next(pit)->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 = boost::next(pit);
580 if (next != paragraphs.end()
581 && (next->layout() != pit->layout()
582 || next->params().depth() != pit->params().depth()))
586 // fall through possible
588 // we don't need it for the last paragraph!!!
589 if (boost::next(pit) != paragraphs.end())
590 pending_newline = true;
593 if (!pit->forceDefaultParagraphs()) {
594 if (!pit->params().spacing().isDefault()
595 && (boost::next(pit) == paragraphs.end()
596 || !boost::next(pit)->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 ((boost::next(pit) != paragraphs.end() &&
619 par_language->babel() !=
620 (boost::next(pit)->getParLanguage(bparams))->babel()) ||
621 (boost::next(pit) == paragraphs.end() &&
622 par_language->babel() != outer_language->babel()));
624 if (closing_rtl_ltr_environment || (boost::next(pit) == 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 (boost::next(pit) != paragraphs.end() && open_encoding_ == CJK &&
664 (boost::next(pit)->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
665 boost::next(pit)->layout()->isEnvironment() && boost::next(pit)->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 (boost::next(pit) == 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, the encoding should be set back to that local_font's
703 // encoding. We don't use switchEncoding(), because no explicit encoding
704 // switch command is needed, since latex will automatically revert to it
705 // when this inset closes.
706 // This switch is only necessary if we're using "auto" or "default"
708 if (boost::next(pit) == paragraphs.end() && runparams_in.local_font != 0) {
709 runparams_in.encoding = runparams_in.local_font->language()->encoding();
710 if (bparams.inputenc == "auto" || bparams.inputenc == "default")
711 os << setEncoding(runparams_in.encoding->iconvName());
714 // Otherwise, the current encoding should be set for the next paragraph.
716 runparams_in.encoding = runparams.encoding;
719 // we don't need it for the last paragraph!!!
720 // Note from JMarc: we will re-add a \n explicitely in
721 // TeXEnvironment, because it is needed in this case
722 if (boost::next(pit) != paragraphs.end()) {
727 if (boost::next(pit) != paragraphs.end())
728 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*boost::next(pit));
736 // LaTeX all paragraphs
737 void latexParagraphs(Buffer const & buf,
741 OutputParams const & runparams,
742 string const & everypar)
744 bool was_title = false;
745 bool already_title = false;
746 BufferParams const & bparams = buf.params();
747 TextClass const & tclass = bparams.getTextClass();
748 ParagraphList const & paragraphs = text.paragraphs();
749 ParagraphList::const_iterator par = paragraphs.begin();
750 ParagraphList::const_iterator endpar = paragraphs.end();
752 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
753 // if only part of the paragraphs will be outputed
754 if (runparams.par_begin != runparams.par_end) {
755 par = boost::next(paragraphs.begin(), runparams.par_begin);
756 endpar = boost::next(paragraphs.begin(), runparams.par_end);
757 // runparams will be passed to nested paragraphs, so
758 // we have to reset the range parameters.
759 const_cast<OutputParams&>(runparams).par_begin = 0;
760 const_cast<OutputParams&>(runparams).par_end = 0;
763 bool const maintext = text.isMainText(buf);
765 // Open a CJK environment at the beginning of the main buffer
766 // if the document's language is a CJK language
767 if (maintext && bparams.encoding().package() == Encoding::CJK) {
768 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
771 open_encoding_ = CJK;
773 // if "auto begin" is switched off, explicitely switch the
774 // language on at start
775 if (maintext && !lyxrc.language_auto_begin &&
776 !bparams.language->babel().empty()) {
778 os << from_utf8(subst(lyxrc.language_command_begin,
780 bparams.language->babel()))
785 ParagraphList::const_iterator lastpar;
787 while (par != endpar) {
789 // well we have to check if we are in an inset with unlimited
790 // length (all in one row) if that is true then we don't allow
791 // any special options in the paragraph and also we don't allow
792 // any environment other than the default layout of the
793 // text class to be valid!
794 if (!par->forceDefaultParagraphs()) {
795 LayoutPtr const & layout = par->layout();
797 if (layout->intitle) {
799 lyxerr << "Error in latexParagraphs: You"
800 " should not mix title layouts"
801 " with normal ones." << endl;
802 } else if (!was_title) {
804 if (tclass.titletype() == TITLE_ENVIRONMENT) {
806 << from_ascii(tclass.titlename())
811 } else if (was_title && !already_title) {
812 if (tclass.titletype() == TITLE_ENVIRONMENT) {
813 os << "\\end{" << from_ascii(tclass.titlename())
817 os << "\\" << from_ascii(tclass.titlename())
821 already_title = true;
825 if (layout->is_environment) {
826 par = TeXOnePar(buf, text, par, os, texrow,
827 runparams, everypar);
828 } else if (layout->isEnvironment() ||
829 !par->params().leftIndent().zero()) {
830 par = TeXEnvironment(buf, text, par, os,
833 par = TeXOnePar(buf, text, par, os, texrow,
834 runparams, everypar);
837 par = TeXOnePar(buf, text, par, os, texrow,
838 runparams, everypar);
840 if (distance(lastpar, par) >= distance(lastpar, endpar))
843 // It might be that we only have a title in this document
844 if (was_title && !already_title) {
845 if (tclass.titletype() == TITLE_ENVIRONMENT) {
846 os << "\\end{" << from_ascii(tclass.titlename())
850 os << "\\" << from_ascii(tclass.titlename())
855 // if "auto end" is switched off, explicitely close the language at the end
856 // but only if the last par is in a babel language
857 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
858 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
859 os << from_utf8(subst(lyxrc.language_command_end,
861 bparams.language->babel()))
865 // If the last paragraph is an environment, we'll have to close
866 // CJK at the very end to do proper nesting.
867 if (maintext && open_encoding_ == CJK) {
868 os << "\\end{CJK}\n";
870 open_encoding_ = none;
872 // reset inherited encoding
873 if (cjk_inherited_) {
874 open_encoding_ = CJK;
875 cjk_inherited_ = false;
880 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
881 OutputParams const & runparams, Encoding const & newEnc)
883 Encoding const oldEnc = *runparams.encoding;
884 bool moving_arg = runparams.moving_arg;
885 if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
887 return make_pair(false, 0);
889 // Do nothing if the encoding is unchanged.
890 if (oldEnc.name() == newEnc.name())
891 return make_pair(false, 0);
893 // FIXME We ignore encoding switches from/to encodings that do
894 // neither support the inputenc package nor the CJK package here.
895 // This does of course only work in special cases (e.g. switch from
896 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
897 // but it is the best we can do
898 if (oldEnc.package() == Encoding::none
899 || newEnc.package() == Encoding::none)
900 return make_pair(false, 0);
902 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
903 << oldEnc.name() << " to " << newEnc.name());
904 os << setEncoding(newEnc.iconvName());
905 if (bparams.inputenc == "default")
906 return make_pair(true, 0);
908 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
909 switch (newEnc.package()) {
911 // shouldn't ever reach here, see above
912 return make_pair(true, 0);
913 case Encoding::inputenc: {
914 int count = inputenc_arg.length();
915 if (oldEnc.package() == Encoding::CJK &&
916 open_encoding_ == CJK) {
918 open_encoding_ = none;
921 else if (oldEnc.package() == Encoding::inputenc &&
922 open_encoding_ == inputenc) {
924 open_encoding_ = none;
927 if (runparams.local_font != 0 && oldEnc.package() == Encoding::CJK) {
928 // within insets, \inputenc switches need to be
929 // embraced within \bgroup ... \egroup; else CJK fails.
932 open_encoding_ = inputenc;
934 os << "\\inputencoding{" << inputenc_arg << '}';
935 return make_pair(true, count + 16);
937 case Encoding::CJK: {
938 int count = inputenc_arg.length();
939 if (oldEnc.package() == Encoding::CJK &&
940 open_encoding_ == CJK) {
944 if (oldEnc.package() == Encoding::inputenc &&
945 open_encoding_ == inputenc) {
949 os << "\\begin{CJK}{" << inputenc_arg << "}{}";
950 open_encoding_ = CJK;
951 return make_pair(true, count + 15);
954 // Dead code to avoid a warning:
955 return make_pair(true, 0);