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 LayoutPtr 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,
162 style->optionalargs);
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")
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 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 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(bparams, outerfont,
301 os, texrow, runparams_in);
306 // FIXME This comment doesn't make sense. What's the
307 // length got to do with forceEmptyLayout()? I.e., what
308 // was forceDefaultParagraphs()?
309 // In an inset with unlimited length (all in one row),
310 // force layout to default
311 LayoutPtr const style = pit->forceEmptyLayout() ?
312 bparams.documentClass().emptyLayout() : pit->layout();
314 OutputParams runparams = runparams_in;
315 runparams.moving_arg |= style->needprotect;
317 bool const maintext = text.isMainText(buf);
318 // we are at the beginning of an inset and CJK is already open.
319 if (pit == paragraphs.begin() && !maintext && open_encoding_ == CJK) {
320 cjk_inherited_ = true;
321 open_encoding_ = none;
324 // This paragraph's language
325 Language const * const par_language = pit->getParLanguage(bparams);
326 // The document's language
327 Language const * const doc_language = bparams.language;
328 // The language that was in effect when the environment this paragraph is
329 // inside of was opened
330 Language const * const outer_language =
331 (runparams.local_font != 0) ?
332 runparams.local_font->language() : doc_language;
333 // The previous language that was in effect is either the language of
334 // the previous paragraph, if there is one, or else the outer language
335 // if there is no previous paragraph
336 Language const * const prev_language =
337 (pit != paragraphs.begin()) ?
338 boost::prior(pit)->getParLanguage(bparams) : outer_language;
340 if (par_language->babel() != prev_language->babel()
341 // check if we already put language command in TeXEnvironment()
342 && !(style->isEnvironment()
343 && (pit == paragraphs.begin() ||
344 (boost::prior(pit)->layout() != pit->layout() &&
345 boost::prior(pit)->getDepth() <= pit->getDepth())
346 || boost::prior(pit)->getDepth() < pit->getDepth())))
348 if (!lyxrc.language_command_end.empty() &&
349 prev_language->babel() != outer_language->babel() &&
350 !prev_language->babel().empty())
352 os << from_ascii(subst(lyxrc.language_command_end,
354 prev_language->babel()))
355 // the '%' is necessary to prevent unwanted whitespace
360 // We need to open a new language if we couldn't close the previous
361 // one (because there's no language_command_end); and even if we closed
362 // the previous one, if the current language is different than the
363 // outer_language (which is currently in effect once the previous one
365 if ((lyxrc.language_command_end.empty() ||
366 par_language->babel() != outer_language->babel()) &&
367 !par_language->babel().empty()) {
368 // If we're inside an inset, and that inset is within an \L or \R
369 // (or equivalents), then within the inset, too, any opposite
370 // language paragraph should appear within an \L or \R (in addition
371 // to, outside of, the normal language switch commands).
372 // This behavior is not correct for ArabTeX, though.
373 if ( // not for ArabTeX
374 (par_language->lang() != "arabic_arabtex" &&
375 outer_language->lang() != "arabic_arabtex") &&
376 // are we in an inset?
377 runparams.local_font != 0 &&
378 // is the inset within an \L or \R?
380 // FIXME: currently, we don't check this; this means that
381 // we'll have unnnecessary \L and \R commands, but that
382 // doesn't seem to hurt (though latex will complain)
384 // is this paragraph in the opposite direction?
385 runparams.local_font->isRightToLeft() !=
386 par_language->rightToLeft()
388 // FIXME: I don't have a working copy of the Arabi package, so
389 // I'm not sure if the farsi and arabic_arabi stuff is correct
391 if (par_language->lang() == "farsi")
393 else if (outer_language->lang() == "farsi")
395 else if (par_language->lang() == "arabic_arabi")
397 else if (outer_language->lang() == "arabic_arabi")
399 // remaining RTL languages currently is hebrew
400 else if (par_language->rightToLeft())
405 // With CJK, the CJK tag has to be closed first (see below)
406 if (runparams.encoding->package() != Encoding::CJK) {
407 os << from_ascii(subst(
408 lyxrc.language_command_begin,
410 par_language->babel()))
411 // the '%' is necessary to prevent unwanted whitespace
418 // Switch file encoding if necessary; no need to do this for "default"
419 // encoding, since this only affects the position of the outputted
420 // \inputencoding command; the encoding switch will occur when necessary
421 if (bparams.inputenc == "auto" &&
422 runparams.encoding->package() != Encoding::none) {
423 // Look ahead for future encoding changes.
424 // We try to output them at the beginning of the paragraph,
425 // since the \inputencoding command is not allowed e.g. in
427 for (pos_type i = 0; i < pit->size(); ++i) {
428 char_type const c = pit->getChar(i);
429 Encoding const * const encoding =
430 pit->getFontSettings(bparams, i).language()->encoding();
431 if (encoding->package() != Encoding::CJK &&
432 runparams.encoding->package() == Encoding::inputenc &&
437 // All characters before c are in the ASCII range, and
438 // c is non-ASCII (but no inset), so change the
439 // encoding to that required by the language of c.
440 // With CJK, only add switch if we have CJK content at the beginning
442 if (encoding->package() != Encoding::CJK || i == 0) {
443 OutputParams tmp_rp = runparams;
444 runparams.moving_arg = false;
445 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
448 // the following is necessary after a CJK environment in a multilingual
449 // context (nesting issue).
450 if (par_language->encoding()->package() == Encoding::CJK &&
451 open_encoding_ != CJK && !cjk_inherited_) {
452 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
454 open_encoding_ = CJK;
457 if (encoding->package() != Encoding::none && enc_switch.first) {
458 if (enc_switch.second > 0) {
459 // the '%' is necessary to prevent unwanted whitespace
463 // With CJK, the CJK tag had to be closed first (see above)
464 if (runparams.encoding->package() == Encoding::CJK) {
465 os << from_ascii(subst(
466 lyxrc.language_command_begin,
468 par_language->babel()))
469 // the '%' is necessary to prevent unwanted whitespace
473 runparams.encoding = encoding;
480 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
481 if (pit->allowParagraphCustomization()) {
482 if (pit->params().startOfAppendix()) {
483 os << "\\appendix\n";
487 if (!pit->params().spacing().isDefault()
488 && (pit == paragraphs.begin()
489 || !boost::prior(pit)->hasSameLayout(*pit)))
491 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
496 if (style->isCommand()) {
502 switch (style->latextype) {
504 os << '\\' << from_ascii(style->latexname());
506 // Separate handling of optional argument inset.
507 if (style->optionalargs > 0) {
508 int ret = latexOptArgInsets(*pit, os, runparams,
509 style->optionalargs);
516 os << from_ascii(style->latexparam());
518 case LATEX_ITEM_ENVIRONMENT:
519 case LATEX_LIST_ENVIRONMENT:
522 case LATEX_BIB_ENVIRONMENT:
523 // ignore this, the inset will write itself
529 Font const outerfont = outerFont(distance(paragraphs.begin(), pit),
533 os << from_utf8(everypar);
534 bool need_par = pit->latex(bparams, outerfont,
535 os, texrow, runparams);
537 // Make sure that \\par is done with the font of the last
538 // character if this has another size as the default.
539 // This is necessary because LaTeX (and LyX on the screen)
540 // calculates the space between the baselines according
541 // to this font. (Matthias)
543 // Is this really needed ? (Dekel)
544 // We do not need to use to change the font for the last paragraph
547 Font const font = pit->empty()
548 ? pit->getLayoutFont(bparams, outerfont)
549 : pit->getFont(bparams, pit->size() - 1, outerfont);
551 bool is_command = style->isCommand();
553 if (style->resfont.size() != font.fontInfo().size()
554 && boost::next(pit) != paragraphs.end()
558 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
559 } else if (need_par) {
561 } else if (is_command)
564 bool pending_newline = false;
565 switch (style->latextype) {
566 case LATEX_ITEM_ENVIRONMENT:
567 case LATEX_LIST_ENVIRONMENT:
568 if (boost::next(pit) != paragraphs.end()
569 && (pit->params().depth() < boost::next(pit)->params().depth()))
570 pending_newline = true;
572 case LATEX_ENVIRONMENT: {
573 // if its the last paragraph of the current environment
574 // skip it otherwise fall through
575 ParagraphList::const_iterator next = boost::next(pit);
577 if (next != paragraphs.end()
578 && (next->layout() != pit->layout()
579 || next->params().depth() != pit->params().depth()))
583 // fall through possible
585 // we don't need it for the last paragraph!!!
586 if (boost::next(pit) != paragraphs.end())
587 pending_newline = true;
590 if (pit->allowParagraphCustomization()) {
591 if (!pit->params().spacing().isDefault()
592 && (boost::next(pit) == paragraphs.end()
593 || !boost::next(pit)->hasSameLayout(*pit)))
595 if (pending_newline) {
599 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
600 pending_newline = true;
604 // Closing the language is needed for the last paragraph; it is also
605 // needed if we're within an \L or \R that we may have opened above (not
606 // necessarily in this paragraph) and are about to close.
607 bool closing_rtl_ltr_environment =
609 (par_language->lang() != "arabic_arabtex" &&
610 outer_language->lang() != "arabic_arabtex") &&
611 // have we opened and \L or \R environment?
612 runparams.local_font != 0 &&
613 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
614 // are we about to close the language?
615 ((boost::next(pit) != paragraphs.end() &&
616 par_language->babel() !=
617 (boost::next(pit)->getParLanguage(bparams))->babel()) ||
618 (boost::next(pit) == paragraphs.end() &&
619 par_language->babel() != outer_language->babel()));
621 if (closing_rtl_ltr_environment || (boost::next(pit) == paragraphs.end()
622 && par_language->babel() != outer_language->babel())) {
623 // Since \selectlanguage write the language to the aux file,
624 // we need to reset the language at the end of footnote or
627 if (pending_newline) {
631 // when the paragraph uses CJK, the language has to be closed earlier
632 if (font.language()->encoding()->package() != Encoding::CJK) {
633 if (lyxrc.language_command_end.empty()) {
634 if (!prev_language->babel().empty()) {
635 os << from_ascii(subst(
636 lyxrc.language_command_begin,
638 prev_language->babel()));
639 pending_newline = true;
641 } else if (!par_language->babel().empty()) {
642 os << from_ascii(subst(
643 lyxrc.language_command_end,
645 par_language->babel()));
646 pending_newline = true;
650 if (closing_rtl_ltr_environment)
653 if (pending_newline) {
658 // if this is a CJK-paragraph and the next isn't, close CJK
659 // also if the next paragraph is a multilingual environment (because of nesting)
660 if (boost::next(pit) != paragraphs.end() && open_encoding_ == CJK &&
661 (boost::next(pit)->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
662 boost::next(pit)->layout()->isEnvironment() && boost::next(pit)->isMultiLingual(bparams))
663 // in environments, CJK has to be closed later (nesting!)
664 && !style->isEnvironment()) {
665 os << "\\end{CJK}\n";
666 open_encoding_ = none;
669 // If this is the last paragraph, close the CJK environment
670 // if necessary. If it's an environment, we'll have to \end that first.
671 if (boost::next(pit) == paragraphs.end() && !style->isEnvironment()) {
672 switch (open_encoding_) {
678 os << "\\end{CJK}\n";
683 open_encoding_ = none;
688 open_encoding_ = none;
698 // If this is the last paragraph, and a local_font was set upon entering
699 // the inset, the encoding should be set back to that local_font's
700 // encoding. We don't use switchEncoding(), because no explicit encoding
701 // switch command is needed, since latex will automatically revert to it
702 // when this inset closes.
703 // This switch is only necessary if we're using "auto" or "default"
705 if (boost::next(pit) == paragraphs.end() && runparams_in.local_font != 0) {
706 runparams_in.encoding = runparams_in.local_font->language()->encoding();
707 if (bparams.inputenc == "auto" || bparams.inputenc == "default")
708 os << setEncoding(runparams_in.encoding->iconvName());
711 // Otherwise, the current encoding should be set for the next paragraph.
713 runparams_in.encoding = runparams.encoding;
716 // we don't need it for the last paragraph!!!
717 // Note from JMarc: we will re-add a \n explicitely in
718 // TeXEnvironment, because it is needed in this case
719 if (boost::next(pit) != paragraphs.end()) {
724 if (boost::next(pit) != paragraphs.end())
725 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*boost::next(pit));
733 // LaTeX all paragraphs
734 void latexParagraphs(Buffer const & buf,
738 OutputParams const & runparams,
739 string const & everypar)
741 bool was_title = false;
742 bool already_title = false;
743 BufferParams const & bparams = buf.params();
744 DocumentClass const & tclass = bparams.documentClass();
745 ParagraphList const & paragraphs = text.paragraphs();
746 ParagraphList::const_iterator par = paragraphs.begin();
747 ParagraphList::const_iterator endpar = paragraphs.end();
749 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
750 // if only part of the paragraphs will be outputed
751 if (runparams.par_begin != runparams.par_end) {
752 par = boost::next(paragraphs.begin(), runparams.par_begin);
753 endpar = boost::next(paragraphs.begin(), runparams.par_end);
754 // runparams will be passed to nested paragraphs, so
755 // we have to reset the range parameters.
756 const_cast<OutputParams&>(runparams).par_begin = 0;
757 const_cast<OutputParams&>(runparams).par_end = 0;
760 bool const maintext = text.isMainText(buf);
762 // Open a CJK environment at the beginning of the main buffer
763 // if the document's language is a CJK language
764 if (maintext && bparams.encoding().package() == Encoding::CJK) {
765 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
768 open_encoding_ = CJK;
770 // if "auto begin" is switched off, explicitely switch the
771 // language on at start
772 if (maintext && !lyxrc.language_auto_begin &&
773 !bparams.language->babel().empty()) {
775 os << from_utf8(subst(lyxrc.language_command_begin,
777 bparams.language->babel()))
782 ParagraphList::const_iterator lastpar;
784 while (par != endpar) {
786 // well we have to check if we are in an inset with unlimited
787 // length (all in one row) if that is true then we don't allow
788 // any special options in the paragraph and also we don't allow
789 // any environment other than the default layout of the
790 // text class to be valid!
791 if (par->allowParagraphCustomization()) {
792 LayoutPtr const & layout = par->forceEmptyLayout() ?
793 tclass.emptyLayout() :
796 if (layout->intitle) {
798 lyxerr << "Error in latexParagraphs: You"
799 " should not mix title layouts"
800 " with normal ones." << endl;
801 } else if (!was_title) {
803 if (tclass.titletype() == TITLE_ENVIRONMENT) {
805 << from_ascii(tclass.titlename())
810 } else if (was_title && !already_title) {
811 if (tclass.titletype() == TITLE_ENVIRONMENT) {
812 os << "\\end{" << from_ascii(tclass.titlename())
816 os << "\\" << from_ascii(tclass.titlename())
820 already_title = true;
824 if (layout->is_environment) {
825 par = TeXOnePar(buf, text, par, os, texrow,
826 runparams, everypar);
827 } else if (layout->isEnvironment() ||
828 !par->params().leftIndent().zero()) {
829 par = TeXEnvironment(buf, text, par, os,
832 par = TeXOnePar(buf, text, par, os, texrow,
833 runparams, everypar);
836 par = TeXOnePar(buf, text, par, os, texrow,
837 runparams, everypar);
839 if (distance(lastpar, par) >= distance(lastpar, endpar))
842 // It might be that we only have a title in this document
843 if (was_title && !already_title) {
844 if (tclass.titletype() == TITLE_ENVIRONMENT) {
845 os << "\\end{" << from_ascii(tclass.titlename())
849 os << "\\" << from_ascii(tclass.titlename())
854 // if "auto end" is switched off, explicitely close the language at the end
855 // but only if the last par is in a babel language
856 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
857 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
858 os << from_utf8(subst(lyxrc.language_command_end,
860 bparams.language->babel()))
864 // If the last paragraph is an environment, we'll have to close
865 // CJK at the very end to do proper nesting.
866 if (maintext && open_encoding_ == CJK) {
867 os << "\\end{CJK}\n";
869 open_encoding_ = none;
871 // reset inherited encoding
872 if (cjk_inherited_) {
873 open_encoding_ = CJK;
874 cjk_inherited_ = false;
879 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
880 OutputParams const & runparams, Encoding const & newEnc)
882 Encoding const oldEnc = *runparams.encoding;
883 bool moving_arg = runparams.moving_arg;
884 if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
886 return make_pair(false, 0);
888 // Do nothing if the encoding is unchanged.
889 if (oldEnc.name() == newEnc.name())
890 return make_pair(false, 0);
892 // FIXME We ignore encoding switches from/to encodings that do
893 // neither support the inputenc package nor the CJK package here.
894 // This does of course only work in special cases (e.g. switch from
895 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
896 // but it is the best we can do
897 if (oldEnc.package() == Encoding::none
898 || newEnc.package() == Encoding::none)
899 return make_pair(false, 0);
901 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
902 << oldEnc.name() << " to " << newEnc.name());
903 os << setEncoding(newEnc.iconvName());
904 if (bparams.inputenc == "default")
905 return make_pair(true, 0);
907 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
908 switch (newEnc.package()) {
910 // shouldn't ever reach here, see above
911 return make_pair(true, 0);
912 case Encoding::inputenc: {
913 int count = inputenc_arg.length();
914 if (oldEnc.package() == Encoding::CJK &&
915 open_encoding_ == CJK) {
917 open_encoding_ = none;
920 else if (oldEnc.package() == Encoding::inputenc &&
921 open_encoding_ == inputenc) {
923 open_encoding_ = none;
926 if (runparams.local_font != 0 && oldEnc.package() == Encoding::CJK) {
927 // within insets, \inputenc switches need to be
928 // embraced within \bgroup ... \egroup; else CJK fails.
931 open_encoding_ = inputenc;
933 os << "\\inputencoding{" << inputenc_arg << '}';
934 return make_pair(true, count + 16);
936 case Encoding::CJK: {
937 int count = inputenc_arg.length();
938 if (oldEnc.package() == Encoding::CJK &&
939 open_encoding_ == CJK) {
943 if (oldEnc.package() == Encoding::inputenc &&
944 open_encoding_ == inputenc) {
948 os << "\\begin{CJK}{" << inputenc_arg << "}{}";
949 open_encoding_ = CJK;
950 return make_pair(true, count + 15);
953 // Dead code to avoid a warning:
954 return make_pair(true, 0);