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 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
447 // the following is necessary after a CJK environment in a multilingual
448 // context (nesting issue).
449 if (par_language->encoding()->package() == Encoding::CJK &&
450 open_encoding_ != CJK && cjk_inherited_ == 0) {
451 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
452 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
453 open_encoding_ = CJK;
456 if (encoding->package() != Encoding::none && enc_switch.first) {
457 if (enc_switch.second > 0) {
458 // the '%' is necessary to prevent unwanted whitespace
462 // With CJK, the CJK tag had to be closed first (see above)
463 if (runparams.encoding->package() == Encoding::CJK) {
464 os << from_ascii(subst(
465 lyxrc.language_command_begin,
467 par_language->babel()))
468 // the '%' is necessary to prevent unwanted whitespace
472 runparams.encoding = encoding;
479 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
480 if (pit->allowParagraphCustomization()) {
481 if (pit->params().startOfAppendix()) {
482 os << "\\appendix\n";
486 if (!pit->params().spacing().isDefault()
487 && (pit == paragraphs.begin()
488 || !priorpit->hasSameLayout(*pit)))
490 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
495 if (style.isCommand()) {
501 switch (style.latextype) {
503 os << '\\' << from_ascii(style.latexname());
505 // Separate handling of optional argument inset.
506 if (style.optionalargs > 0) {
507 int ret = latexOptArgInsets(*pit, os, runparams,
515 os << from_ascii(style.latexparam());
517 case LATEX_ITEM_ENVIRONMENT:
518 case LATEX_LIST_ENVIRONMENT:
521 case LATEX_BIB_ENVIRONMENT:
522 // ignore this, the inset will write itself
528 Font const outerfont = outerFont(distance(paragraphs.begin(), pit),
532 os << from_utf8(everypar);
533 bool need_par = pit->latex(bparams, outerfont,
534 os, texrow, runparams, start_pos, end_pos);
536 // Make sure that \\par is done with the font of the last
537 // character if this has another size as the default.
538 // This is necessary because LaTeX (and LyX on the screen)
539 // calculates the space between the baselines according
540 // to this font. (Matthias)
542 // Is this really needed ? (Dekel)
543 // We do not need to use to change the font for the last paragraph
546 Font const font = pit->empty()
547 ? pit->getLayoutFont(bparams, outerfont)
548 : pit->getFont(bparams, pit->size() - 1, outerfont);
550 bool const is_command = style.isCommand();
552 if (style.resfont.size() != font.fontInfo().size()
553 && nextpit != paragraphs.end()
557 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
558 } else if (need_par) {
560 } else if (is_command)
563 bool pending_newline = false;
564 switch (style.latextype) {
565 case LATEX_ITEM_ENVIRONMENT:
566 case LATEX_LIST_ENVIRONMENT:
567 if (nextpit != paragraphs.end()
568 && (pit->params().depth() < nextpit->params().depth()))
569 pending_newline = true;
571 case LATEX_ENVIRONMENT: {
572 // if its the last paragraph of the current environment
573 // skip it otherwise fall through
574 if (nextpit != paragraphs.end() &&
575 (nextpit->layout() != pit->layout()
576 || nextpit->params().depth() != pit->params().depth()))
580 // fall through possible
582 // we don't need it for the last paragraph!!!
583 if (nextpit != paragraphs.end())
584 pending_newline = true;
587 if (pit->allowParagraphCustomization()) {
588 if (!pit->params().spacing().isDefault()
589 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
591 if (pending_newline) {
595 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
596 pending_newline = true;
600 // Closing the language is needed for the last paragraph; it is also
601 // needed if we're within an \L or \R that we may have opened above (not
602 // necessarily in this paragraph) and are about to close.
603 bool closing_rtl_ltr_environment =
605 (par_language->lang() != "arabic_arabtex" &&
606 outer_language->lang() != "arabic_arabtex") &&
607 // have we opened and \L or \R environment?
608 runparams.local_font != 0 &&
609 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
610 // are we about to close the language?
611 ((nextpit != paragraphs.end() &&
612 par_language->babel() !=
613 (nextpit->getParLanguage(bparams))->babel()) ||
614 (nextpit == paragraphs.end() &&
615 par_language->babel() != outer_language->babel()));
617 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
618 && par_language->babel() != outer_language->babel())) {
619 // Since \selectlanguage write the language to the aux file,
620 // we need to reset the language at the end of footnote or
623 if (pending_newline) {
627 // when the paragraph uses CJK, the language has to be closed earlier
628 if (font.language()->encoding()->package() != Encoding::CJK) {
629 if (lyxrc.language_command_end.empty()) {
630 if (!prev_language->babel().empty()) {
631 os << from_ascii(subst(
632 lyxrc.language_command_begin,
634 prev_language->babel()));
635 pending_newline = true;
637 } else if (!par_language->babel().empty()) {
638 os << from_ascii(subst(
639 lyxrc.language_command_end,
641 par_language->babel()));
642 pending_newline = true;
646 if (closing_rtl_ltr_environment)
649 if (pending_newline) {
654 // if this is a CJK-paragraph and the next isn't, close CJK
655 // also if the next paragraph is a multilingual environment (because of nesting)
656 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
657 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
658 (nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams)))
659 // inbetween environments, CJK has to be closed later (nesting!)
660 && (!style.isEnvironment() || !nextpit->layout().isEnvironment())) {
661 os << "\\end{CJK}\n";
662 open_encoding_ = none;
665 // If this is the last paragraph, close the CJK environment
666 // if necessary. If it's an environment, we'll have to \end that first.
667 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
668 switch (open_encoding_) {
670 // do nothing at the end of child documents
671 if (maintext && buf.masterBuffer() != &buf)
677 os << "\\end{CJK}\n";
682 open_encoding_ = none;
687 open_encoding_ = none;
697 // If this is the last paragraph, and a local_font was set upon entering
698 // the inset, and we're using "auto" or "default" encoding, the encoding
699 // should be set back to that local_font's encoding.
700 // However, do not change the encoding when XeTeX is used.
701 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
702 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
703 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
704 && (!bparams.useXetex)) {
705 runparams_in.encoding = runparams_in.local_font->language()->encoding();
706 os << setEncoding(runparams_in.encoding->iconvName());
708 // Otherwise, the current encoding should be set for the next paragraph.
710 runparams_in.encoding = runparams.encoding;
713 // we don't need a newline for the last paragraph!!!
714 // Note from JMarc: we will re-add a \n explicitly in
715 // TeXEnvironment, because it is needed in this case
716 if (nextpit != paragraphs.end()) {
717 Layout const & next_layout = nextpit->layout();
718 if (style == next_layout
719 // no blank lines before environments!
720 || !next_layout.isEnvironment()
721 // unless there's a depth change
722 // FIXME What we really want to do here is put every \begin and \end
723 // tag on a new line (which was not the case with nested environments).
724 // But in the present state of play, we don't have access to the
725 // information whether the current TeX row is empty or not.
726 // For some ideas about how to fix this, see this thread:
727 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
728 || nextpit->params().depth() != pit->params().depth()) {
734 if (nextpit != paragraphs.end())
735 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*nextpit);
741 // LaTeX all paragraphs
742 void latexParagraphs(Buffer const & buf,
746 OutputParams const & runparams,
747 string const & everypar)
749 bool was_title = false;
750 bool already_title = false;
751 BufferParams const & bparams = buf.params();
752 DocumentClass const & tclass = bparams.documentClass();
753 ParagraphList const & paragraphs = text.paragraphs();
754 ParagraphList::const_iterator par = paragraphs.begin();
755 ParagraphList::const_iterator endpar = paragraphs.end();
757 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
758 // if only part of the paragraphs will be outputed
759 if (runparams.par_begin != runparams.par_end) {
760 par = boost::next(paragraphs.begin(), runparams.par_begin);
761 endpar = boost::next(paragraphs.begin(), runparams.par_end);
762 // runparams will be passed to nested paragraphs, so
763 // we have to reset the range parameters.
764 const_cast<OutputParams&>(runparams).par_begin = 0;
765 const_cast<OutputParams&>(runparams).par_end = 0;
768 bool const maintext = text.isMainText(buf);
769 bool const is_child = buf.masterBuffer() != &buf;
771 // Open a CJK environment at the beginning of the main buffer
772 // if the document's language is a CJK language
773 // (but not in child documents)
774 if (maintext && !is_child
775 && bparams.encoding().package() == Encoding::CJK) {
776 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
777 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
779 open_encoding_ = CJK;
781 // if "auto begin" is switched off, explicitely switch the
782 // language on at start
783 if (maintext && !lyxrc.language_auto_begin &&
784 !bparams.language->babel().empty()) {
786 os << from_utf8(subst(lyxrc.language_command_begin,
788 bparams.language->babel()))
793 ParagraphList::const_iterator lastpar;
795 while (par != endpar) {
797 // FIXME This check should not be needed. We should
798 // perhaps issue an error if it is.
799 Layout const & layout = par->forcePlainLayout() ?
800 tclass.plainLayout() : par->layout();
802 if (layout.intitle) {
804 lyxerr << "Error in latexParagraphs: You"
805 " should not mix title layouts"
806 " with normal ones." << endl;
807 } else if (!was_title) {
809 if (tclass.titletype() == TITLE_ENVIRONMENT) {
811 << from_ascii(tclass.titlename())
816 } else if (was_title && !already_title) {
817 if (tclass.titletype() == TITLE_ENVIRONMENT) {
818 os << "\\end{" << from_ascii(tclass.titlename())
822 os << "\\" << from_ascii(tclass.titlename())
826 already_title = true;
830 if (layout.isEnvironment() ||
831 !par->params().leftIndent().zero()) {
832 par = TeXEnvironment(buf, text, par, os,
835 par = TeXOnePar(buf, text, par, os, texrow,
836 runparams, everypar);
838 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())
855 // if "auto end" is switched off, explicitely close the language at the end
856 // but only if the last par is in a babel language
857 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
858 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
859 os << from_utf8(subst(lyxrc.language_command_end,
861 bparams.language->babel()))
866 // If the last paragraph is an environment, we'll have to close
867 // CJK at the very end to do proper nesting.
868 if (maintext && !is_child && open_encoding_ == CJK) {
869 os << "\\end{CJK}\n";
871 open_encoding_ = none;
874 // reset inherited encoding
875 if (cjk_inherited_ > 0) {
877 if (cjk_inherited_ == 0)
878 open_encoding_ = CJK;
883 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
884 OutputParams const & runparams, Encoding const & newEnc,
887 Encoding const & oldEnc = *runparams.encoding;
888 bool moving_arg = runparams.moving_arg;
889 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
891 return make_pair(false, 0);
893 // Do nothing if the encoding is unchanged.
894 if (oldEnc.name() == newEnc.name())
895 return make_pair(false, 0);
897 // FIXME We ignore encoding switches from/to encodings that do
898 // neither support the inputenc package nor the CJK package here.
899 // This does of course only work in special cases (e.g. switch from
900 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
901 // but it is the best we can do
902 if (oldEnc.package() == Encoding::none
903 || newEnc.package() == Encoding::none)
904 return make_pair(false, 0);
906 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
907 << oldEnc.name() << " to " << newEnc.name());
908 os << setEncoding(newEnc.iconvName());
909 if (bparams.inputenc == "default")
910 return make_pair(true, 0);
912 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
913 switch (newEnc.package()) {
915 case Encoding::japanese:
916 // shouldn't ever reach here, see above
917 return make_pair(true, 0);
918 case Encoding::inputenc: {
919 int count = inputenc_arg.length();
920 if (oldEnc.package() == Encoding::CJK &&
921 open_encoding_ == CJK) {
923 open_encoding_ = none;
926 else if (oldEnc.package() == Encoding::inputenc &&
927 open_encoding_ == inputenc) {
929 open_encoding_ = none;
932 if (runparams.local_font != 0
933 && oldEnc.package() == Encoding::CJK) {
934 // within insets, \inputenc switches need
935 // to be embraced within \bgroup...\egroup;
939 open_encoding_ = inputenc;
941 // with the japanese option, inputenc is omitted.
942 if (runparams.use_japanese)
943 return make_pair(true, count);
944 os << "\\inputencoding{" << inputenc_arg << '}';
945 return make_pair(true, count + 16);
947 case Encoding::CJK: {
948 int count = inputenc_arg.length();
949 if (oldEnc.package() == Encoding::CJK &&
950 open_encoding_ == CJK) {
954 if (oldEnc.package() == Encoding::inputenc &&
955 open_encoding_ == inputenc) {
959 os << "\\begin{CJK}{" << inputenc_arg << "}{"
960 << from_ascii(bparams.fontsCJK) << "}";
961 open_encoding_ = CJK;
962 return make_pair(true, count + 15);
965 // Dead code to avoid a warning:
966 return make_pair(true, 0);