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 // FIXME This test should not be necessary.
86 // We should perhaps issue an error if it is.
87 Layout const & style = par->forcePlainLayout() ?
88 buf.params().documentClass().plainLayout() : par->layout();
90 while (par != paragraphs.end() &&
91 par->params().depth() == pit->params().depth()) {
92 if (style.isEnvironment()) {
93 par = TeXEnvironment(buf, text, par,
94 os, texrow, runparams);
96 par = TeXOnePar(buf, text, par,
97 os, texrow, runparams);
100 LYXERR(Debug::LATEX, "TeXDeeper...done ");
106 ParagraphList::const_iterator
107 TeXEnvironment(Buffer const & buf,
109 ParagraphList::const_iterator pit,
110 odocstream & os, TexRow & texrow,
111 OutputParams const & runparams)
113 LYXERR(Debug::LATEX, "TeXEnvironment... " << &*pit);
115 BufferParams const & bparams = buf.params();
117 // FIXME This test should not be necessary.
118 // We should perhaps issue an error if it is.
119 Layout const & style = pit->forcePlainLayout() ?
120 bparams.documentClass().plainLayout() : pit->layout();
122 ParagraphList const & paragraphs = text.paragraphs();
124 Language const * const par_language = pit->getParLanguage(bparams);
125 Language const * const doc_language = bparams.language;
126 Language const * const prev_par_language =
127 (pit != paragraphs.begin())
128 ? boost::prior(pit)->getParLanguage(bparams)
130 if (par_language->babel() != prev_par_language->babel()) {
132 if (!lyxrc.language_command_end.empty() &&
133 prev_par_language->babel() != doc_language->babel() &&
134 !prev_par_language->babel().empty()) {
135 os << from_ascii(subst(
136 lyxrc.language_command_end,
138 prev_par_language->babel()))
139 // the '%' is necessary to prevent unwanted whitespace
144 if ((lyxrc.language_command_end.empty() ||
145 par_language->babel() != doc_language->babel()) &&
146 !par_language->babel().empty()) {
147 os << from_ascii(subst(
148 lyxrc.language_command_begin,
150 par_language->babel()))
151 // the '%' is necessary to prevent unwanted whitespace
157 bool leftindent_open = false;
158 if (!pit->params().leftIndent().zero()) {
159 os << "\\begin{LyXParagraphLeftIndent}{"
160 << from_ascii(pit->params().leftIndent().asLatexString())
163 leftindent_open = true;
166 if (style.isEnvironment()) {
167 os << "\\begin{" << from_ascii(style.latexname()) << '}';
168 if (style.optionalargs > 0) {
169 int ret = latexOptArgInsets(*pit, os, runparams,
176 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
178 << pit->params().labelWidthString()
180 } else if (style.labeltype == LABEL_BIBLIO) {
182 os << '{' << bibitemWidest(buf) << "}\n";
184 os << from_ascii(style.latexparam()) << '\n';
188 // in multilingual environments, the CJK tags have to be nested properly
189 bool cjk_nested = false;
190 if (par_language->encoding()->package() == Encoding::CJK &&
191 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
192 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
193 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
194 open_encoding_ = CJK;
199 ParagraphList::const_iterator par = pit;
201 par = TeXOnePar(buf, text, par, os, texrow, runparams);
203 if (par == paragraphs.end()) {
204 // Make sure that the last paragraph is
205 // correctly terminated (because TeXOnePar does
206 // not add a \n in this case)
209 } else if (par->params().depth() > pit->params().depth()) {
210 if (par->layout().isParagraph()) {
212 // How to handle this? (Lgb)
213 //&& !suffixIs(os, "\n\n")
215 // There should be at least one '\n' already
216 // but we need there to be two for Standard
217 // paragraphs that are depth-increment'ed to be
218 // output correctly. However, tables can
219 // also be paragraphs so don't adjust them.
222 // Will it ever harm to have one '\n' too
223 // many? i.e. that we sometimes will have
224 // three in a row. (Lgb)
228 par = TeXDeeper(buf, text, par, os, texrow,
231 } while (par != paragraphs.end()
232 && par->layout() == pit->layout()
233 && par->params().depth() == pit->params().depth()
234 && par->params().leftIndent() == pit->params().leftIndent());
236 if (open_encoding_ == CJK && cjk_nested) {
237 // We need to close the encoding even if it does not change
238 // to do correct environment nesting
239 os << "\\end{CJK}\n";
241 open_encoding_ = none;
244 if (style.isEnvironment()) {
245 os << "\\end{" << from_ascii(style.latexname()) << "}\n";
249 if (leftindent_open) {
250 os << "\\end{LyXParagraphLeftIndent}\n";
254 if (par != paragraphs.end())
255 LYXERR(Debug::LATEX, "TeXEnvironment...done " << &*par);
263 int latexOptArgInsets(Paragraph const & par, odocstream & os,
264 OutputParams const & runparams, int number)
268 InsetList::const_iterator it = par.insetList().begin();
269 InsetList::const_iterator end = par.insetList().end();
270 for (; it != end && number > 0 ; ++it) {
271 if (it->inset->lyxCode() == OPTARG_CODE) {
273 static_cast<InsetOptArg *>(it->inset);
274 lines += ins->latexOptional(os, runparams);
284 ParagraphList::const_iterator
285 TeXOnePar(Buffer const & buf,
287 ParagraphList::const_iterator const pit,
288 odocstream & os, TexRow & texrow,
289 OutputParams const & runparams_in,
290 string const & everypar)
292 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
294 BufferParams const & bparams = buf.params();
295 ParagraphList const & paragraphs = text.paragraphs();
297 ParagraphList::const_iterator priorpit = pit;
298 if (priorpit != paragraphs.begin())
300 ParagraphList::const_iterator nextpit = pit;
301 if (nextpit != paragraphs.end())
304 OutputParams runparams = runparams_in;
305 runparams.isLastPar = nextpit == paragraphs.end();
307 if (runparams.verbatim) {
308 int const dist = distance(paragraphs.begin(), pit);
309 Font const outerfont = outerFont(dist, paragraphs);
311 // No newline if only one paragraph in this lyxtext
317 /*bool need_par = */ pit->latex(bparams, outerfont,
318 os, texrow, runparams);
322 // FIXME This check should not really be needed.
323 // Perhaps we should issue an error if it is.
324 Layout const style = pit->forcePlainLayout() ?
325 bparams.documentClass().plainLayout() : pit->layout();
327 runparams.moving_arg |= style.needprotect;
329 bool const maintext = text.isMainText(buf);
330 // we are at the beginning of an inset and CJK is already open;
331 // we count inheritation levels to get the inset nesting right.
332 if (pit == paragraphs.begin() && !maintext
333 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
335 open_encoding_ = none;
338 // This paragraph's language
339 Language const * const par_language = pit->getParLanguage(bparams);
340 // The document's language
341 Language const * const doc_language = bparams.language;
342 // The language that was in effect when the environment this paragraph is
343 // inside of was opened
344 Language const * const outer_language =
345 (runparams.local_font != 0) ?
346 runparams.local_font->language() : doc_language;
347 // The previous language that was in effect is either the language of
348 // the previous paragraph, if there is one, or else the outer language
349 // if there is no previous paragraph
350 Language const * const prev_language =
351 (pit != paragraphs.begin()) ?
352 priorpit->getParLanguage(bparams) : outer_language;
354 if (par_language->babel() != prev_language->babel()
355 // check if we already put language command in TeXEnvironment()
356 && !(style.isEnvironment()
357 && (pit == paragraphs.begin() ||
358 (priorpit->layout() != pit->layout() &&
359 priorpit->getDepth() <= pit->getDepth())
360 || priorpit->getDepth() < pit->getDepth())))
362 if (!lyxrc.language_command_end.empty() &&
363 prev_language->babel() != outer_language->babel() &&
364 !prev_language->babel().empty())
366 os << from_ascii(subst(lyxrc.language_command_end,
368 prev_language->babel()))
369 // the '%' is necessary to prevent unwanted whitespace
374 // We need to open a new language if we couldn't close the previous
375 // one (because there's no language_command_end); and even if we closed
376 // the previous one, if the current language is different than the
377 // outer_language (which is currently in effect once the previous one
379 if ((lyxrc.language_command_end.empty() ||
380 par_language->babel() != outer_language->babel()) &&
381 !par_language->babel().empty()) {
382 // If we're inside an inset, and that inset is within an \L or \R
383 // (or equivalents), then within the inset, too, any opposite
384 // language paragraph should appear within an \L or \R (in addition
385 // to, outside of, the normal language switch commands).
386 // This behavior is not correct for ArabTeX, though.
387 if ( // not for ArabTeX
388 (par_language->lang() != "arabic_arabtex" &&
389 outer_language->lang() != "arabic_arabtex") &&
390 // are we in an inset?
391 runparams.local_font != 0 &&
392 // is the inset within an \L or \R?
394 // FIXME: currently, we don't check this; this means that
395 // we'll have unnnecessary \L and \R commands, but that
396 // doesn't seem to hurt (though latex will complain)
398 // is this paragraph in the opposite direction?
399 runparams.local_font->isRightToLeft() !=
400 par_language->rightToLeft()
402 // FIXME: I don't have a working copy of the Arabi package, so
403 // I'm not sure if the farsi and arabic_arabi stuff is correct
405 if (par_language->lang() == "farsi")
407 else if (outer_language->lang() == "farsi")
409 else if (par_language->lang() == "arabic_arabi")
411 else if (outer_language->lang() == "arabic_arabi")
413 // remaining RTL languages currently is hebrew
414 else if (par_language->rightToLeft())
419 // With CJK, the CJK tag has to be closed first (see below)
420 if (runparams.encoding->package() != Encoding::CJK) {
421 os << from_ascii(subst(
422 lyxrc.language_command_begin,
424 par_language->babel()))
425 // the '%' is necessary to prevent unwanted whitespace
432 // Switch file encoding if necessary; no need to do this for "default"
433 // encoding, since this only affects the position of the outputted
434 // \inputencoding command; the encoding switch will occur when necessary
435 if (bparams.inputenc == "auto" &&
436 runparams.encoding->package() != Encoding::none) {
437 // Look ahead for future encoding changes.
438 // We try to output them at the beginning of the paragraph,
439 // since the \inputencoding command is not allowed e.g. in
441 for (pos_type i = 0; i < pit->size(); ++i) {
442 char_type const c = pit->getChar(i);
443 Encoding const * const encoding =
444 pit->getFontSettings(bparams, i).language()->encoding();
445 if (encoding->package() != Encoding::CJK &&
446 runparams.encoding->package() == Encoding::inputenc &&
451 // All characters before c are in the ASCII range, and
452 // c is non-ASCII (but no inset), so change the
453 // encoding to that required by the language of c.
454 // With CJK, only add switch if we have CJK content at the beginning
456 if (encoding->package() != Encoding::CJK || i == 0) {
457 OutputParams tmp_rp = runparams;
458 runparams.moving_arg = false;
459 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
462 // the following is necessary after a CJK environment in a multilingual
463 // context (nesting issue).
464 if (par_language->encoding()->package() == Encoding::CJK &&
465 open_encoding_ != CJK && cjk_inherited_ == 0) {
466 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
467 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
468 open_encoding_ = CJK;
471 if (encoding->package() != Encoding::none && enc_switch.first) {
472 if (enc_switch.second > 0) {
473 // the '%' is necessary to prevent unwanted whitespace
477 // With CJK, the CJK tag had to be closed first (see above)
478 if (runparams.encoding->package() == Encoding::CJK) {
479 os << from_ascii(subst(
480 lyxrc.language_command_begin,
482 par_language->babel()))
483 // the '%' is necessary to prevent unwanted whitespace
487 runparams.encoding = encoding;
494 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
495 if (pit->allowParagraphCustomization()) {
496 if (pit->params().startOfAppendix()) {
497 os << "\\appendix\n";
501 if (!pit->params().spacing().isDefault()
502 && (pit == paragraphs.begin()
503 || !priorpit->hasSameLayout(*pit)))
505 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
510 if (style.isCommand()) {
516 switch (style.latextype) {
518 os << '\\' << from_ascii(style.latexname());
520 // Separate handling of optional argument inset.
521 if (style.optionalargs > 0) {
522 int ret = latexOptArgInsets(*pit, os, runparams,
530 os << from_ascii(style.latexparam());
532 case LATEX_ITEM_ENVIRONMENT:
533 case LATEX_LIST_ENVIRONMENT:
536 case LATEX_BIB_ENVIRONMENT:
537 // ignore this, the inset will write itself
543 Font const outerfont = outerFont(distance(paragraphs.begin(), pit),
547 os << from_utf8(everypar);
548 bool need_par = pit->latex(bparams, outerfont,
549 os, texrow, runparams);
551 // Make sure that \\par is done with the font of the last
552 // character if this has another size as the default.
553 // This is necessary because LaTeX (and LyX on the screen)
554 // calculates the space between the baselines according
555 // to this font. (Matthias)
557 // Is this really needed ? (Dekel)
558 // We do not need to use to change the font for the last paragraph
561 Font const font = pit->empty()
562 ? pit->getLayoutFont(bparams, outerfont)
563 : pit->getFont(bparams, pit->size() - 1, outerfont);
565 bool const is_command = style.isCommand();
567 if (style.resfont.size() != font.fontInfo().size()
568 && nextpit != paragraphs.end()
572 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
573 } else if (need_par) {
575 } else if (is_command)
578 bool pending_newline = false;
579 switch (style.latextype) {
580 case LATEX_ITEM_ENVIRONMENT:
581 case LATEX_LIST_ENVIRONMENT:
582 if (nextpit != paragraphs.end()
583 && (pit->params().depth() < nextpit->params().depth()))
584 pending_newline = true;
586 case LATEX_ENVIRONMENT: {
587 // if its the last paragraph of the current environment
588 // skip it otherwise fall through
589 ParagraphList::const_iterator next = nextpit;
591 if (next != paragraphs.end() && (next->layout() != pit->layout()
592 || next->params().depth() != pit->params().depth()))
596 // fall through possible
598 // we don't need it for the last paragraph!!!
599 if (nextpit != paragraphs.end())
600 pending_newline = true;
603 if (pit->allowParagraphCustomization()) {
604 if (!pit->params().spacing().isDefault()
605 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
607 if (pending_newline) {
611 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
612 pending_newline = true;
616 // Closing the language is needed for the last paragraph; it is also
617 // needed if we're within an \L or \R that we may have opened above (not
618 // necessarily in this paragraph) and are about to close.
619 bool closing_rtl_ltr_environment =
621 (par_language->lang() != "arabic_arabtex" &&
622 outer_language->lang() != "arabic_arabtex") &&
623 // have we opened and \L or \R environment?
624 runparams.local_font != 0 &&
625 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
626 // are we about to close the language?
627 ((nextpit != paragraphs.end() &&
628 par_language->babel() !=
629 (nextpit->getParLanguage(bparams))->babel()) ||
630 (nextpit == paragraphs.end() &&
631 par_language->babel() != outer_language->babel()));
633 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
634 && par_language->babel() != outer_language->babel())) {
635 // Since \selectlanguage write the language to the aux file,
636 // we need to reset the language at the end of footnote or
639 if (pending_newline) {
643 // when the paragraph uses CJK, the language has to be closed earlier
644 if (font.language()->encoding()->package() != Encoding::CJK) {
645 if (lyxrc.language_command_end.empty()) {
646 if (!prev_language->babel().empty()) {
647 os << from_ascii(subst(
648 lyxrc.language_command_begin,
650 prev_language->babel()));
651 pending_newline = true;
653 } else if (!par_language->babel().empty()) {
654 os << from_ascii(subst(
655 lyxrc.language_command_end,
657 par_language->babel()));
658 pending_newline = true;
662 if (closing_rtl_ltr_environment)
665 if (pending_newline) {
670 // if this is a CJK-paragraph and the next isn't, close CJK
671 // also if the next paragraph is a multilingual environment (because of nesting)
672 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
673 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
674 nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams))
675 // in environments, CJK has to be closed later (nesting!)
676 && !style.isEnvironment()) {
677 os << "\\end{CJK}\n";
678 open_encoding_ = none;
681 // If this is the last paragraph, close the CJK environment
682 // if necessary. If it's an environment, we'll have to \end that first.
683 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
684 switch (open_encoding_) {
686 // do nothing at the end of child documents
687 if (maintext && buf.masterBuffer() != &buf)
693 os << "\\end{CJK}\n";
698 open_encoding_ = none;
703 open_encoding_ = none;
713 // If this is the last paragraph, and a local_font was set upon entering
714 // the inset, and we're using "auto" or "default" encoding, the encoding
715 // should be set back to that local_font's encoding.
716 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
717 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
718 && (bparams.inputenc == "auto" || bparams.inputenc == "default")) {
719 runparams_in.encoding = runparams_in.local_font->language()->encoding();
720 os << setEncoding(runparams_in.encoding->iconvName());
722 // Otherwise, the current encoding should be set for the next paragraph.
724 runparams_in.encoding = runparams.encoding;
727 // we don't need it for the last paragraph!!!
728 // Note from JMarc: we will re-add a \n explicitely in
729 // TeXEnvironment, because it is needed in this case
730 if (nextpit != paragraphs.end()) {
731 Layout const & next_layout = nextpit->layout();
732 // no blank lines before environments!
733 if (!next_layout.isEnvironment() || style == next_layout) {
739 if (nextpit != paragraphs.end())
740 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*nextpit);
748 // LaTeX all paragraphs
749 void latexParagraphs(Buffer const & buf,
753 OutputParams const & runparams,
754 string const & everypar)
756 bool was_title = false;
757 bool already_title = false;
758 BufferParams const & bparams = buf.params();
759 DocumentClass const & tclass = bparams.documentClass();
760 ParagraphList const & paragraphs = text.paragraphs();
761 ParagraphList::const_iterator par = paragraphs.begin();
762 ParagraphList::const_iterator endpar = paragraphs.end();
764 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
765 // if only part of the paragraphs will be outputed
766 if (runparams.par_begin != runparams.par_end) {
767 par = boost::next(paragraphs.begin(), runparams.par_begin);
768 endpar = boost::next(paragraphs.begin(), runparams.par_end);
769 // runparams will be passed to nested paragraphs, so
770 // we have to reset the range parameters.
771 const_cast<OutputParams&>(runparams).par_begin = 0;
772 const_cast<OutputParams&>(runparams).par_end = 0;
775 bool const maintext = text.isMainText(buf);
776 bool const is_child = buf.masterBuffer() != &buf;
778 // Open a CJK environment at the beginning of the main buffer
779 // if the document's language is a CJK language
780 // (but not in child documents)
781 if (maintext && !is_child
782 && bparams.encoding().package() == Encoding::CJK) {
783 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
784 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
786 open_encoding_ = CJK;
788 // if "auto begin" is switched off, explicitely switch the
789 // language on at start
790 if (maintext && !lyxrc.language_auto_begin &&
791 !bparams.language->babel().empty()) {
793 os << from_utf8(subst(lyxrc.language_command_begin,
795 bparams.language->babel()))
800 ParagraphList::const_iterator lastpar;
802 while (par != endpar) {
804 // FIXME This check should not be needed. We should
805 // perhaps issue an error if it is.
806 Layout const & layout = par->forcePlainLayout() ?
807 tclass.plainLayout() :
810 if (layout.intitle) {
812 lyxerr << "Error in latexParagraphs: You"
813 " should not mix title layouts"
814 " with normal ones." << endl;
815 } else if (!was_title) {
817 if (tclass.titletype() == TITLE_ENVIRONMENT) {
819 << from_ascii(tclass.titlename())
824 } else if (was_title && !already_title) {
825 if (tclass.titletype() == TITLE_ENVIRONMENT) {
826 os << "\\end{" << from_ascii(tclass.titlename())
830 os << "\\" << from_ascii(tclass.titlename())
834 already_title = true;
838 if (layout.isEnvironment() ||
839 !par->params().leftIndent().zero()) {
840 par = TeXEnvironment(buf, text, par, os,
843 par = TeXOnePar(buf, text, par, os, texrow,
844 runparams, everypar);
846 if (distance(lastpar, par) >= distance(lastpar, endpar))
850 // It might be that we only have a title in this document
851 if (was_title && !already_title) {
852 if (tclass.titletype() == TITLE_ENVIRONMENT) {
853 os << "\\end{" << from_ascii(tclass.titlename())
857 os << "\\" << from_ascii(tclass.titlename())
863 // if "auto end" is switched off, explicitely close the language at the end
864 // but only if the last par is in a babel language
865 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
866 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
867 os << from_utf8(subst(lyxrc.language_command_end,
869 bparams.language->babel()))
874 // If the last paragraph is an environment, we'll have to close
875 // CJK at the very end to do proper nesting.
876 if (maintext && !is_child && open_encoding_ == CJK) {
877 os << "\\end{CJK}\n";
879 open_encoding_ = none;
882 // reset inherited encoding
883 if (cjk_inherited_ > 0) {
885 if (cjk_inherited_ == 0)
886 open_encoding_ = CJK;
891 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
892 OutputParams const & runparams, Encoding const & newEnc,
895 Encoding const & oldEnc = *runparams.encoding;
896 bool moving_arg = runparams.moving_arg;
897 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
899 return make_pair(false, 0);
901 // Do nothing if the encoding is unchanged.
902 if (oldEnc.name() == newEnc.name())
903 return make_pair(false, 0);
905 // FIXME We ignore encoding switches from/to encodings that do
906 // neither support the inputenc package nor the CJK package here.
907 // This does of course only work in special cases (e.g. switch from
908 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
909 // but it is the best we can do
910 if (oldEnc.package() == Encoding::none
911 || newEnc.package() == Encoding::none)
912 return make_pair(false, 0);
914 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
915 << oldEnc.name() << " to " << newEnc.name());
916 os << setEncoding(newEnc.iconvName());
917 if (bparams.inputenc == "default")
918 return make_pair(true, 0);
920 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
921 switch (newEnc.package()) {
923 case Encoding::japanese:
924 // shouldn't ever reach here, see above
925 return make_pair(true, 0);
926 case Encoding::inputenc: {
927 int count = inputenc_arg.length();
928 if (oldEnc.package() == Encoding::CJK &&
929 open_encoding_ == CJK) {
931 open_encoding_ = none;
934 else if (oldEnc.package() == Encoding::inputenc &&
935 open_encoding_ == inputenc) {
937 open_encoding_ = none;
940 if (runparams.local_font != 0
941 && oldEnc.package() == Encoding::CJK) {
942 // within insets, \inputenc switches need
943 // to be embraced within \bgroup...\egroup;
947 open_encoding_ = inputenc;
949 // with the japanese option, inputenc is omitted.
950 if (runparams.use_japanese)
951 return make_pair(true, count);
952 os << "\\inputencoding{" << inputenc_arg << '}';
953 return make_pair(true, count + 16);
955 case Encoding::CJK: {
956 int count = inputenc_arg.length();
957 if (oldEnc.package() == Encoding::CJK &&
958 open_encoding_ == CJK) {
962 if (oldEnc.package() == Encoding::inputenc &&
963 open_encoding_ == inputenc) {
967 os << "\\begin{CJK}{" << inputenc_arg << "}{"
968 << from_ascii(bparams.fontsCJK) << "}";
969 open_encoding_ = CJK;
970 return make_pair(true, count + 15);
973 // Dead code to avoid a warning:
974 return make_pair(true, 0);