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);
65 ParagraphList::const_iterator
66 TeXDeeper(Buffer const & buf,
68 ParagraphList::const_iterator pit,
69 odocstream & os, TexRow & texrow,
70 OutputParams const & runparams)
72 LYXERR(Debug::LATEX, "TeXDeeper... " << &*pit);
73 ParagraphList::const_iterator par = pit;
75 ParagraphList const & paragraphs = text.paragraphs();
77 while (par != paragraphs.end() &&
78 par->params().depth() == pit->params().depth()) {
79 // FIXME This test should not be necessary.
80 // We should perhaps issue an error if it is.
81 Layout const & style = par->forcePlainLayout() ?
82 buf.params().documentClass().plainLayout() : par->layout();
83 if (style.isEnvironment()) {
84 par = TeXEnvironment(buf, text, par,
85 os, texrow, runparams);
87 par = TeXOnePar(buf, text, par,
88 os, texrow, runparams);
91 LYXERR(Debug::LATEX, "TeXDeeper...done ");
97 ParagraphList::const_iterator
98 TeXEnvironment(Buffer const & buf,
100 ParagraphList::const_iterator pit,
101 odocstream & os, TexRow & texrow,
102 OutputParams const & runparams)
104 LYXERR(Debug::LATEX, "TeXEnvironment... " << &*pit);
106 BufferParams const & bparams = buf.params();
108 // FIXME This test should not be necessary.
109 // We should perhaps issue an error if it is.
110 Layout const & style = pit->forcePlainLayout() ?
111 bparams.documentClass().plainLayout() : 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(*pit, os, runparams,
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 if (prev_par_language->encoding()->package() == Encoding::CJK)
184 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
185 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
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")
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(Paragraph const & par, odocstream & os,
256 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(os, runparams);
273 // FIXME: this should be anonymous
274 ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
276 ParagraphList::const_iterator const pit,
277 odocstream & os, TexRow & texrow,
278 OutputParams const & runparams_in,
279 string const & everypar,
280 int start_pos, int end_pos)
282 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
284 BufferParams const & bparams = buf.params();
285 ParagraphList const & paragraphs = text.paragraphs();
287 ParagraphList::const_iterator const priorpit =
288 pit == paragraphs.begin() ? pit : boost::prior(pit);
289 ParagraphList::const_iterator const nextpit =
290 pit == paragraphs.end() ? pit : boost::next(pit);
292 OutputParams runparams = runparams_in;
293 runparams.isLastPar = nextpit == paragraphs.end();
295 if (runparams.verbatim) {
296 int const dist = distance(paragraphs.begin(), pit);
297 Font const outerfont = outerFont(dist, paragraphs);
299 // No newline if only one paragraph in this lyxtext
305 /*bool need_par = */ pit->latex(bparams, outerfont,
306 os, texrow, runparams, start_pos, end_pos);
310 // FIXME This check should not really be needed.
311 // Perhaps we should issue an error if it is.
312 Layout const style = pit->forcePlainLayout() ?
313 bparams.documentClass().plainLayout() : pit->layout();
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 // we count inheritation levels to get the inset nesting right.
320 if (pit == paragraphs.begin() && !maintext
321 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
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 priorpit->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 (priorpit->layout() != pit->layout() &&
347 priorpit->getDepth() <= pit->getDepth())
348 || priorpit->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_ == 0) {
454 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
455 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
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.documentClass().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 || !priorpit->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(*pit, os, runparams,
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 = outerFont(distance(paragraphs.begin(), pit),
535 os << from_utf8(everypar);
536 bool need_par = pit->latex(bparams, outerfont,
537 os, texrow, runparams, start_pos, end_pos);
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
549 Font const font = pit->empty()
550 ? pit->getLayoutFont(bparams, outerfont)
551 : pit->getFont(bparams, pit->size() - 1, outerfont);
553 bool const is_command = style.isCommand();
555 if (style.resfont.size() != font.fontInfo().size()
556 && nextpit != paragraphs.end()
560 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
561 } else if (need_par) {
563 } else if (is_command)
566 bool pending_newline = false;
567 switch (style.latextype) {
568 case LATEX_ITEM_ENVIRONMENT:
569 case LATEX_LIST_ENVIRONMENT:
570 if (nextpit != paragraphs.end()
571 && (pit->params().depth() < nextpit->params().depth()))
572 pending_newline = true;
574 case LATEX_ENVIRONMENT: {
575 // if its the last paragraph of the current environment
576 // skip it otherwise fall through
577 if (nextpit != paragraphs.end() &&
578 (nextpit->layout() != pit->layout()
579 || nextpit->params().depth() != pit->params().depth()))
583 // fall through possible
585 // we don't need it for the last paragraph!!!
586 if (nextpit != paragraphs.end())
587 pending_newline = true;
590 if (pit->allowParagraphCustomization()) {
591 if (!pit->params().spacing().isDefault()
592 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
594 if (pending_newline) {
598 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
599 pending_newline = true;
603 // Closing the language is needed for the last paragraph; it is also
604 // needed if we're within an \L or \R that we may have opened above (not
605 // necessarily in this paragraph) and are about to close.
606 bool closing_rtl_ltr_environment =
608 (par_language->lang() != "arabic_arabtex" &&
609 outer_language->lang() != "arabic_arabtex") &&
610 // have we opened and \L or \R environment?
611 runparams.local_font != 0 &&
612 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
613 // are we about to close the language?
614 ((nextpit != paragraphs.end() &&
615 par_language->babel() !=
616 (nextpit->getParLanguage(bparams))->babel()) ||
617 (nextpit == paragraphs.end() &&
618 par_language->babel() != outer_language->babel()));
620 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
621 && par_language->babel() != outer_language->babel())) {
622 // Since \selectlanguage write the language to the aux file,
623 // we need to reset the language at the end of footnote or
626 if (pending_newline) {
630 // when the paragraph uses CJK, the language has to be closed earlier
631 if (font.language()->encoding()->package() != Encoding::CJK) {
632 if (lyxrc.language_command_end.empty()) {
633 if (!prev_language->babel().empty()) {
634 os << from_ascii(subst(
635 lyxrc.language_command_begin,
637 prev_language->babel()));
638 pending_newline = true;
640 } else if (!par_language->babel().empty()) {
641 os << from_ascii(subst(
642 lyxrc.language_command_end,
644 par_language->babel()));
645 pending_newline = true;
649 if (closing_rtl_ltr_environment)
652 if (pending_newline) {
657 // if this is a CJK-paragraph and the next isn't, close CJK
658 // also if the next paragraph is a multilingual environment (because of nesting)
659 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
660 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
661 (nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams)))
662 // inbetween environments, CJK has to be closed later (nesting!)
663 && (!style.isEnvironment() || !nextpit->layout().isEnvironment())) {
664 os << "\\end{CJK}\n";
665 open_encoding_ = none;
668 // If this is the last paragraph, close the CJK environment
669 // if necessary. If it's an environment, we'll have to \end that first.
670 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
671 switch (open_encoding_) {
673 // do nothing at the end of child documents
674 if (maintext && buf.masterBuffer() != &buf)
680 os << "\\end{CJK}\n";
685 open_encoding_ = none;
690 open_encoding_ = none;
700 // If this is the last paragraph, and a local_font was set upon entering
701 // the inset, and we're using "auto" or "default" encoding, the encoding
702 // should be set back to that local_font's encoding.
703 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
704 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
705 && (bparams.inputenc == "auto" || bparams.inputenc == "default")) {
706 runparams_in.encoding = runparams_in.local_font->language()->encoding();
707 os << setEncoding(runparams_in.encoding->iconvName());
709 // Otherwise, the current encoding should be set for the next paragraph.
711 runparams_in.encoding = runparams.encoding;
714 // we don't need a newline for the last paragraph!!!
715 // Note from JMarc: we will re-add a \n explicitly in
716 // TeXEnvironment, because it is needed in this case
717 if (nextpit != paragraphs.end()) {
718 Layout const & next_layout = nextpit->layout();
719 if (style == next_layout
720 // no blank lines before environments!
721 || !next_layout.isEnvironment()
722 // unless there's a depth change
723 // FIXME What we really want to do here is put every \begin and \end
724 // tag on a new line (which was not the case with nested environments).
725 // But in the present state of play, we don't have access to the
726 // information whether the current TeX row is empty or not.
727 // For some ideas about how to fix this, see this thread:
728 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
729 || nextpit->params().depth() != pit->params().depth()) {
735 if (nextpit != paragraphs.end())
736 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*nextpit);
742 // LaTeX all paragraphs
743 void latexParagraphs(Buffer const & buf,
747 OutputParams const & runparams,
748 string const & everypar)
750 bool was_title = false;
751 bool already_title = false;
752 BufferParams const & bparams = buf.params();
753 DocumentClass const & tclass = bparams.documentClass();
754 ParagraphList const & paragraphs = text.paragraphs();
755 ParagraphList::const_iterator par = paragraphs.begin();
756 ParagraphList::const_iterator endpar = paragraphs.end();
758 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
759 // if only part of the paragraphs will be outputed
760 if (runparams.par_begin != runparams.par_end) {
761 par = boost::next(paragraphs.begin(), runparams.par_begin);
762 endpar = boost::next(paragraphs.begin(), runparams.par_end);
763 // runparams will be passed to nested paragraphs, so
764 // we have to reset the range parameters.
765 const_cast<OutputParams&>(runparams).par_begin = 0;
766 const_cast<OutputParams&>(runparams).par_end = 0;
769 bool const maintext = text.isMainText(buf);
770 bool const is_child = buf.masterBuffer() != &buf;
772 // Open a CJK environment at the beginning of the main buffer
773 // if the document's language is a CJK language
774 // (but not in child documents)
775 if (maintext && !is_child
776 && bparams.encoding().package() == Encoding::CJK) {
777 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
778 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
780 open_encoding_ = CJK;
782 // if "auto begin" is switched off, explicitely switch the
783 // language on at start
784 if (maintext && !lyxrc.language_auto_begin &&
785 !bparams.language->babel().empty()) {
787 os << from_utf8(subst(lyxrc.language_command_begin,
789 bparams.language->babel()))
794 ParagraphList::const_iterator lastpar;
796 while (par != endpar) {
798 // FIXME This check should not be needed. We should
799 // perhaps issue an error if it is.
800 Layout const & layout = par->forcePlainLayout() ?
801 tclass.plainLayout() :
804 if (layout.intitle) {
806 lyxerr << "Error in latexParagraphs: You"
807 " should not mix title layouts"
808 " with normal ones." << endl;
809 } else if (!was_title) {
811 if (tclass.titletype() == TITLE_ENVIRONMENT) {
813 << from_ascii(tclass.titlename())
818 } else if (was_title && !already_title) {
819 if (tclass.titletype() == TITLE_ENVIRONMENT) {
820 os << "\\end{" << from_ascii(tclass.titlename())
824 os << "\\" << from_ascii(tclass.titlename())
828 already_title = true;
832 if (layout.isEnvironment() ||
833 !par->params().leftIndent().zero()) {
834 par = TeXEnvironment(buf, text, par, os,
837 par = TeXOnePar(buf, text, par, os, texrow,
838 runparams, everypar);
840 if (distance(lastpar, par) >= distance(lastpar, endpar))
844 // It might be that we only have a title in this document
845 if (was_title && !already_title) {
846 if (tclass.titletype() == TITLE_ENVIRONMENT) {
847 os << "\\end{" << from_ascii(tclass.titlename())
851 os << "\\" << from_ascii(tclass.titlename())
857 // if "auto end" is switched off, explicitely close the language at the end
858 // but only if the last par is in a babel language
859 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
860 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
861 os << from_utf8(subst(lyxrc.language_command_end,
863 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 && !is_child && open_encoding_ == CJK) {
871 os << "\\end{CJK}\n";
873 open_encoding_ = none;
876 // reset inherited encoding
877 if (cjk_inherited_ > 0) {
879 if (cjk_inherited_ == 0)
880 open_encoding_ = CJK;
885 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
886 OutputParams const & runparams, Encoding const & newEnc,
889 Encoding const & oldEnc = *runparams.encoding;
890 bool moving_arg = runparams.moving_arg;
891 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
893 return make_pair(false, 0);
895 // Do nothing if the encoding is unchanged.
896 if (oldEnc.name() == newEnc.name())
897 return make_pair(false, 0);
899 // FIXME We ignore encoding switches from/to encodings that do
900 // neither support the inputenc package nor the CJK package here.
901 // This does of course only work in special cases (e.g. switch from
902 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
903 // but it is the best we can do
904 if (oldEnc.package() == Encoding::none
905 || newEnc.package() == Encoding::none)
906 return make_pair(false, 0);
908 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
909 << oldEnc.name() << " to " << newEnc.name());
910 os << setEncoding(newEnc.iconvName());
911 if (bparams.inputenc == "default")
912 return make_pair(true, 0);
914 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
915 switch (newEnc.package()) {
917 case Encoding::japanese:
918 // shouldn't ever reach here, see above
919 return make_pair(true, 0);
920 case Encoding::inputenc: {
921 int count = inputenc_arg.length();
922 if (oldEnc.package() == Encoding::CJK &&
923 open_encoding_ == CJK) {
925 open_encoding_ = none;
928 else if (oldEnc.package() == Encoding::inputenc &&
929 open_encoding_ == inputenc) {
931 open_encoding_ = none;
934 if (runparams.local_font != 0
935 && oldEnc.package() == Encoding::CJK) {
936 // within insets, \inputenc switches need
937 // to be embraced within \bgroup...\egroup;
941 open_encoding_ = inputenc;
943 // with the japanese option, inputenc is omitted.
944 if (runparams.use_japanese)
945 return make_pair(true, count);
946 os << "\\inputencoding{" << inputenc_arg << '}';
947 return make_pair(true, count + 16);
949 case Encoding::CJK: {
950 int count = inputenc_arg.length();
951 if (oldEnc.package() == Encoding::CJK &&
952 open_encoding_ == CJK) {
956 if (oldEnc.package() == Encoding::inputenc &&
957 open_encoding_ == inputenc) {
961 os << "\\begin{CJK}{" << inputenc_arg << "}{"
962 << from_ascii(bparams.fontsCJK) << "}";
963 open_encoding_ = CJK;
964 return make_pair(true, count + 15);
967 // Dead code to avoid a warning:
968 return make_pair(true, 0);