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->forceEmptyLayout() ?
112 bparams.getTextClass().emptyLayout() :
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(buf, *pit, os, runparams,
163 style->optionalargs);
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())
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")
209 // There should be at least one '\n' already
210 // but we need there to be two for Standard
211 // paragraphs that are depth-increment'ed to be
212 // output correctly. However, tables can
213 // also be paragraphs so don't adjust them.
216 // Will it ever harm to have one '\n' too
217 // many? i.e. that we sometimes will have
218 // three in a row. (Lgb)
222 par = TeXDeeper(buf, text, par, os, texrow,
225 } while (par != paragraphs.end()
226 && par->layout() == pit->layout()
227 && par->params().depth() == pit->params().depth()
228 && par->params().leftIndent() == pit->params().leftIndent());
230 if (open_encoding_ == CJK && cjk_nested) {
231 // We need to close the encoding even if it does not change
232 // to do correct environment nesting
233 os << "\\end{CJK}\n";
235 open_encoding_ = none;
238 if (style->isEnvironment()) {
239 os << "\\end{" << from_ascii(style->latexname()) << "}\n";
243 if (leftindent_open) {
244 os << "\\end{LyXParagraphLeftIndent}\n";
248 if (par != paragraphs.end())
249 LYXERR(Debug::LATEX, "TeXEnvironment...done " << &*par);
257 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
258 odocstream & os, OutputParams const & runparams, int number)
262 InsetList::const_iterator it = par.insetList().begin();
263 InsetList::const_iterator end = par.insetList().end();
264 for (; it != end && number > 0 ; ++it) {
265 if (it->inset->lyxCode() == OPTARG_CODE) {
267 static_cast<InsetOptArg *>(it->inset);
268 lines += ins->latexOptional(buf, os, runparams);
278 ParagraphList::const_iterator
279 TeXOnePar(Buffer const & buf,
281 ParagraphList::const_iterator pit,
282 odocstream & os, TexRow & texrow,
283 OutputParams const & runparams_in,
284 string const & everypar)
286 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
288 BufferParams const & bparams = buf.params();
289 ParagraphList const & paragraphs = text.paragraphs();
291 if (runparams_in.verbatim) {
292 int const dist = distance(paragraphs.begin(), pit);
293 Font const outerfont = outerFont(dist, paragraphs);
295 // No newline if only one paragraph in this lyxtext
301 /*bool need_par = */ pit->latex(buf, bparams, outerfont,
302 os, texrow, runparams_in);
307 // FIXME This comment doesn't make sense. What's the
308 // length got to do with forceEmptyLayout()? I.e., what
309 // was forceDefaultParagraphs()?
310 // In an inset with unlimited length (all in one row),
311 // force layout to default
312 LayoutPtr const style = pit->forceEmptyLayout() ?
313 bparams.getTextClass().emptyLayout() :
316 OutputParams runparams = runparams_in;
317 runparams.moving_arg |= style->needprotect;
319 bool const maintext = text.isMainText(buf);
320 // we are at the beginning of an inset and CJK is already open.
321 if (pit == paragraphs.begin() && !maintext && open_encoding_ == CJK) {
322 cjk_inherited_ = true;
323 open_encoding_ = none;
326 // This paragraph's language
327 Language const * const par_language = pit->getParLanguage(bparams);
328 // The document's language
329 Language const * const doc_language = bparams.language;
330 // The language that was in effect when the environment this paragraph is
331 // inside of was opened
332 Language const * const outer_language =
333 (runparams.local_font != 0) ?
334 runparams.local_font->language() : doc_language;
335 // The previous language that was in effect is either the language of
336 // the previous paragraph, if there is one, or else the outer language
337 // if there is no previous paragraph
338 Language const * const prev_language =
339 (pit != paragraphs.begin()) ?
340 boost::prior(pit)->getParLanguage(bparams) : outer_language;
342 if (par_language->babel() != prev_language->babel()
343 // check if we already put language command in TeXEnvironment()
344 && !(style->isEnvironment()
345 && (pit == paragraphs.begin() ||
346 (boost::prior(pit)->layout() != pit->layout() &&
347 boost::prior(pit)->getDepth() <= pit->getDepth())
348 || boost::prior(pit)->getDepth() < pit->getDepth())))
350 if (!lyxrc.language_command_end.empty() &&
351 prev_language->babel() != outer_language->babel() &&
352 !prev_language->babel().empty())
354 os << from_ascii(subst(lyxrc.language_command_end,
356 prev_language->babel()))
357 // the '%' is necessary to prevent unwanted whitespace
362 // We need to open a new language if we couldn't close the previous
363 // one (because there's no language_command_end); and even if we closed
364 // the previous one, if the current language is different than the
365 // outer_language (which is currently in effect once the previous one
367 if ((lyxrc.language_command_end.empty() ||
368 par_language->babel() != outer_language->babel()) &&
369 !par_language->babel().empty()) {
370 // If we're inside an inset, and that inset is within an \L or \R
371 // (or equivalents), then within the inset, too, any opposite
372 // language paragraph should appear within an \L or \R (in addition
373 // to, outside of, the normal language switch commands).
374 // This behavior is not correct for ArabTeX, though.
375 if ( // not for ArabTeX
376 (par_language->lang() != "arabic_arabtex" &&
377 outer_language->lang() != "arabic_arabtex") &&
378 // are we in an inset?
379 runparams.local_font != 0 &&
380 // is the inset within an \L or \R?
382 // FIXME: currently, we don't check this; this means that
383 // we'll have unnnecessary \L and \R commands, but that
384 // doesn't seem to hurt (though latex will complain)
386 // is this paragraph in the opposite direction?
387 runparams.local_font->isRightToLeft() !=
388 par_language->rightToLeft()
390 // FIXME: I don't have a working copy of the Arabi package, so
391 // I'm not sure if the farsi and arabic_arabi stuff is correct
393 if (par_language->lang() == "farsi")
395 else if (outer_language->lang() == "farsi")
397 else if (par_language->lang() == "arabic_arabi")
399 else if (outer_language->lang() == "arabic_arabi")
401 // remaining RTL languages currently is hebrew
402 else if (par_language->rightToLeft())
407 // With CJK, the CJK tag has to be closed first (see below)
408 if (runparams.encoding->package() != Encoding::CJK) {
409 os << from_ascii(subst(
410 lyxrc.language_command_begin,
412 par_language->babel()))
413 // the '%' is necessary to prevent unwanted whitespace
420 // Switch file encoding if necessary; no need to do this for "default"
421 // encoding, since this only affects the position of the outputted
422 // \inputencoding command; the encoding switch will occur when necessary
423 if (bparams.inputenc == "auto" &&
424 runparams.encoding->package() != Encoding::none) {
425 // Look ahead for future encoding changes.
426 // We try to output them at the beginning of the paragraph,
427 // since the \inputencoding command is not allowed e.g. in
429 for (pos_type i = 0; i < pit->size(); ++i) {
430 char_type const c = pit->getChar(i);
431 Encoding const * const encoding =
432 pit->getFontSettings(bparams, i).language()->encoding();
433 if (encoding->package() != Encoding::CJK &&
434 runparams.encoding->package() == Encoding::inputenc &&
439 // All characters before c are in the ASCII range, and
440 // c is non-ASCII (but no inset), so change the
441 // encoding to that required by the language of c.
442 // With CJK, only add switch if we have CJK content at the beginning
444 if (encoding->package() != Encoding::CJK || i == 0) {
445 OutputParams tmp_rp = runparams;
446 runparams.moving_arg = false;
447 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
450 // the following is necessary after a CJK environment in a multilingual
451 // context (nesting issue).
452 if (par_language->encoding()->package() == Encoding::CJK &&
453 open_encoding_ != CJK && !cjk_inherited_) {
454 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
456 open_encoding_ = CJK;
459 if (encoding->package() != Encoding::none && enc_switch.first) {
460 if (enc_switch.second > 0) {
461 // the '%' is necessary to prevent unwanted whitespace
465 // With CJK, the CJK tag had to be closed first (see above)
466 if (runparams.encoding->package() == Encoding::CJK) {
467 os << from_ascii(subst(
468 lyxrc.language_command_begin,
470 par_language->babel()))
471 // the '%' is necessary to prevent unwanted whitespace
475 runparams.encoding = encoding;
482 bool const useSetSpace = bparams.getTextClass().provides("SetSpace");
483 if (pit->allowParagraphCustomization()) {
484 if (pit->params().startOfAppendix()) {
485 os << "\\appendix\n";
489 if (!pit->params().spacing().isDefault()
490 && (pit == paragraphs.begin()
491 || !boost::prior(pit)->hasSameLayout(*pit)))
493 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
498 if (style->isCommand()) {
504 switch (style->latextype) {
506 os << '\\' << from_ascii(style->latexname());
508 // Separate handling of optional argument inset.
509 if (style->optionalargs > 0) {
510 int ret = latexOptArgInsets(buf, *pit, os, runparams,
511 style->optionalargs);
518 os << from_ascii(style->latexparam());
520 case LATEX_ITEM_ENVIRONMENT:
521 case LATEX_LIST_ENVIRONMENT:
524 case LATEX_BIB_ENVIRONMENT:
525 // ignore this, the inset will write itself
531 Font const outerfont =
532 outerFont(distance(paragraphs.begin(), pit),
536 os << from_utf8(everypar);
537 bool need_par = pit->latex(buf, 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
552 ? pit->getLayoutFont(bparams, outerfont)
553 : pit->getFont(bparams, pit->size() - 1, outerfont));
555 bool is_command = style->isCommand();
557 if (style->resfont.size() != font.fontInfo().size()
558 && boost::next(pit) != paragraphs.end()
562 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
563 } else if (need_par) {
565 } else if (is_command)
568 bool pending_newline = false;
569 switch (style->latextype) {
570 case LATEX_ITEM_ENVIRONMENT:
571 case LATEX_LIST_ENVIRONMENT:
572 if (boost::next(pit) != paragraphs.end()
573 && (pit->params().depth() < boost::next(pit)->params().depth()))
574 pending_newline = true;
576 case LATEX_ENVIRONMENT: {
577 // if its the last paragraph of the current environment
578 // skip it otherwise fall through
579 ParagraphList::const_iterator next = boost::next(pit);
581 if (next != paragraphs.end()
582 && (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 (boost::next(pit) != paragraphs.end())
591 pending_newline = true;
594 if (pit->allowParagraphCustomization()) {
595 if (!pit->params().spacing().isDefault()
596 && (boost::next(pit) == paragraphs.end()
597 || !boost::next(pit)->hasSameLayout(*pit)))
599 if (pending_newline) {
603 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
604 pending_newline = true;
608 // Closing the language is needed for the last paragraph; it is also
609 // needed if we're within an \L or \R that we may have opened above (not
610 // necessarily in this paragraph) and are about to close.
611 bool closing_rtl_ltr_environment =
613 (par_language->lang() != "arabic_arabtex" &&
614 outer_language->lang() != "arabic_arabtex") &&
615 // have we opened and \L or \R environment?
616 runparams.local_font != 0 &&
617 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
618 // are we about to close the language?
619 ((boost::next(pit) != paragraphs.end() &&
620 par_language->babel() !=
621 (boost::next(pit)->getParLanguage(bparams))->babel()) ||
622 (boost::next(pit) == paragraphs.end() &&
623 par_language->babel() != outer_language->babel()));
625 if (closing_rtl_ltr_environment || (boost::next(pit) == paragraphs.end()
626 && par_language->babel() != outer_language->babel())) {
627 // Since \selectlanguage write the language to the aux file,
628 // we need to reset the language at the end of footnote or
631 if (pending_newline) {
635 // when the paragraph uses CJK, the language has to be closed earlier
636 if (font.language()->encoding()->package() != Encoding::CJK) {
637 if (lyxrc.language_command_end.empty()) {
638 if (!prev_language->babel().empty()) {
639 os << from_ascii(subst(
640 lyxrc.language_command_begin,
642 prev_language->babel()));
643 pending_newline = true;
645 } else if (!par_language->babel().empty()) {
646 os << from_ascii(subst(
647 lyxrc.language_command_end,
649 par_language->babel()));
650 pending_newline = true;
654 if (closing_rtl_ltr_environment)
657 if (pending_newline) {
662 // if this is a CJK-paragraph and the next isn't, close CJK
663 // also if the next paragraph is a multilingual environment (because of nesting)
664 if (boost::next(pit) != paragraphs.end() && open_encoding_ == CJK &&
665 (boost::next(pit)->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
666 boost::next(pit)->layout()->isEnvironment() && boost::next(pit)->isMultiLingual(bparams))
667 // in environments, CJK has to be closed later (nesting!)
668 && !style->isEnvironment()) {
669 os << "\\end{CJK}\n";
670 open_encoding_ = none;
673 // If this is the last paragraph, close the CJK environment
674 // if necessary. If it's an environment, we'll have to \end that first.
675 if (boost::next(pit) == paragraphs.end() && !style->isEnvironment()) {
676 switch (open_encoding_) {
682 os << "\\end{CJK}\n";
687 open_encoding_ = none;
692 open_encoding_ = none;
702 // If this is the last paragraph, and a local_font was set upon entering
703 // the inset, the encoding should be set back to that local_font's
704 // encoding. We don't use switchEncoding(), because no explicit encoding
705 // switch command is needed, since latex will automatically revert to it
706 // when this inset closes.
707 // This switch is only necessary if we're using "auto" or "default"
709 if (boost::next(pit) == paragraphs.end() && runparams_in.local_font != 0) {
710 runparams_in.encoding = runparams_in.local_font->language()->encoding();
711 if (bparams.inputenc == "auto" || bparams.inputenc == "default")
712 os << setEncoding(runparams_in.encoding->iconvName());
715 // Otherwise, the current encoding should be set for the next paragraph.
717 runparams_in.encoding = runparams.encoding;
720 // we don't need it for the last paragraph!!!
721 // Note from JMarc: we will re-add a \n explicitely in
722 // TeXEnvironment, because it is needed in this case
723 if (boost::next(pit) != paragraphs.end()) {
728 if (boost::next(pit) != paragraphs.end())
729 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*boost::next(pit));
737 // LaTeX all paragraphs
738 void latexParagraphs(Buffer const & buf,
742 OutputParams const & runparams,
743 string const & everypar)
745 bool was_title = false;
746 bool already_title = false;
747 BufferParams const & bparams = buf.params();
748 TextClass const & tclass = bparams.getTextClass();
749 ParagraphList const & paragraphs = text.paragraphs();
750 ParagraphList::const_iterator par = paragraphs.begin();
751 ParagraphList::const_iterator endpar = paragraphs.end();
753 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
754 // if only part of the paragraphs will be outputed
755 if (runparams.par_begin != runparams.par_end) {
756 par = boost::next(paragraphs.begin(), runparams.par_begin);
757 endpar = boost::next(paragraphs.begin(), runparams.par_end);
758 // runparams will be passed to nested paragraphs, so
759 // we have to reset the range parameters.
760 const_cast<OutputParams&>(runparams).par_begin = 0;
761 const_cast<OutputParams&>(runparams).par_end = 0;
764 bool const maintext = text.isMainText(buf);
766 // Open a CJK environment at the beginning of the main buffer
767 // if the document's language is a CJK language
768 if (maintext && bparams.encoding().package() == Encoding::CJK) {
769 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
772 open_encoding_ = CJK;
774 // if "auto begin" is switched off, explicitely switch the
775 // language on at start
776 if (maintext && !lyxrc.language_auto_begin &&
777 !bparams.language->babel().empty()) {
779 os << from_utf8(subst(lyxrc.language_command_begin,
781 bparams.language->babel()))
786 ParagraphList::const_iterator lastpar;
788 while (par != endpar) {
790 // well we have to check if we are in an inset with unlimited
791 // length (all in one row) if that is true then we don't allow
792 // any special options in the paragraph and also we don't allow
793 // any environment other than the default layout of the
794 // text class to be valid!
795 if (par->allowParagraphCustomization()) {
796 LayoutPtr const & layout = par->forceEmptyLayout() ?
797 tclass.emptyLayout() :
800 if (layout->intitle) {
802 lyxerr << "Error in latexParagraphs: You"
803 " should not mix title layouts"
804 " with normal ones." << endl;
805 } else if (!was_title) {
807 if (tclass.titletype() == TITLE_ENVIRONMENT) {
809 << from_ascii(tclass.titlename())
814 } else if (was_title && !already_title) {
815 if (tclass.titletype() == TITLE_ENVIRONMENT) {
816 os << "\\end{" << from_ascii(tclass.titlename())
820 os << "\\" << from_ascii(tclass.titlename())
824 already_title = true;
828 if (layout->is_environment) {
829 par = TeXOnePar(buf, text, par, os, texrow,
830 runparams, everypar);
831 } else if (layout->isEnvironment() ||
832 !par->params().leftIndent().zero()) {
833 par = TeXEnvironment(buf, text, par, os,
836 par = TeXOnePar(buf, text, par, os, texrow,
837 runparams, everypar);
840 par = TeXOnePar(buf, text, par, os, texrow,
841 runparams, everypar);
843 if (distance(lastpar, par) >= distance(lastpar, endpar))
846 // It might be that we only have a title in this document
847 if (was_title && !already_title) {
848 if (tclass.titletype() == TITLE_ENVIRONMENT) {
849 os << "\\end{" << from_ascii(tclass.titlename())
853 os << "\\" << from_ascii(tclass.titlename())
858 // if "auto end" is switched off, explicitely close the language at the end
859 // but only if the last par is in a babel language
860 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
861 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
862 os << from_utf8(subst(lyxrc.language_command_end,
864 bparams.language->babel()))
868 // If the last paragraph is an environment, we'll have to close
869 // CJK at the very end to do proper nesting.
870 if (maintext && open_encoding_ == CJK) {
871 os << "\\end{CJK}\n";
873 open_encoding_ = none;
875 // reset inherited encoding
876 if (cjk_inherited_) {
877 open_encoding_ = CJK;
878 cjk_inherited_ = false;
883 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
884 OutputParams const & runparams, Encoding const & newEnc)
886 Encoding const oldEnc = *runparams.encoding;
887 bool moving_arg = runparams.moving_arg;
888 if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
890 return make_pair(false, 0);
892 // Do nothing if the encoding is unchanged.
893 if (oldEnc.name() == newEnc.name())
894 return make_pair(false, 0);
896 // FIXME We ignore encoding switches from/to encodings that do
897 // neither support the inputenc package nor the CJK package here.
898 // This does of course only work in special cases (e.g. switch from
899 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
900 // but it is the best we can do
901 if (oldEnc.package() == Encoding::none
902 || newEnc.package() == Encoding::none)
903 return make_pair(false, 0);
905 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
906 << oldEnc.name() << " to " << newEnc.name());
907 os << setEncoding(newEnc.iconvName());
908 if (bparams.inputenc == "default")
909 return make_pair(true, 0);
911 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
912 switch (newEnc.package()) {
914 // shouldn't ever reach here, see above
915 return make_pair(true, 0);
916 case Encoding::inputenc: {
917 int count = inputenc_arg.length();
918 if (oldEnc.package() == Encoding::CJK &&
919 open_encoding_ == CJK) {
921 open_encoding_ = none;
924 else if (oldEnc.package() == Encoding::inputenc &&
925 open_encoding_ == inputenc) {
927 open_encoding_ = none;
930 if (runparams.local_font != 0 && oldEnc.package() == Encoding::CJK) {
931 // within insets, \inputenc switches need to be
932 // embraced within \bgroup ... \egroup; else CJK fails.
935 open_encoding_ = inputenc;
937 os << "\\inputencoding{" << inputenc_arg << '}';
938 return make_pair(true, count + 16);
940 case Encoding::CJK: {
941 int count = inputenc_arg.length();
942 if (oldEnc.package() == Encoding::CJK &&
943 open_encoding_ == CJK) {
947 if (oldEnc.package() == Encoding::inputenc &&
948 open_encoding_ == inputenc) {
952 os << "\\begin{CJK}{" << inputenc_arg << "}{}";
953 open_encoding_ = CJK;
954 return make_pair(true, count + 15);
957 // Dead code to avoid a warning:
958 return make_pair(true, 0);