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 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
184 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
185 open_encoding_ = CJK;
190 ParagraphList::const_iterator par = pit;
192 par = TeXOnePar(buf, text, par, os, texrow, runparams);
194 if (par == paragraphs.end()) {
195 // Make sure that the last paragraph is
196 // correctly terminated (because TeXOnePar does
197 // not add a \n in this case)
200 } else if (par->params().depth() > pit->params().depth()) {
201 if (par->layout().isParagraph()) {
203 // How to handle this? (Lgb)
204 //&& !suffixIs(os, "\n\n")
206 // There should be at least one '\n' already
207 // but we need there to be two for Standard
208 // paragraphs that are depth-increment'ed to be
209 // output correctly. However, tables can
210 // also be paragraphs so don't adjust them.
213 // Will it ever harm to have one '\n' too
214 // many? i.e. that we sometimes will have
215 // three in a row. (Lgb)
219 par = TeXDeeper(buf, text, par, os, texrow,
222 } while (par != paragraphs.end()
223 && par->layout() == pit->layout()
224 && par->params().depth() == pit->params().depth()
225 && par->params().leftIndent() == pit->params().leftIndent());
227 if (open_encoding_ == CJK && cjk_nested) {
228 // We need to close the encoding even if it does not change
229 // to do correct environment nesting
230 os << "\\end{CJK}\n";
232 open_encoding_ = none;
235 if (style.isEnvironment()) {
236 os << "\\end{" << from_ascii(style.latexname()) << "}\n";
240 if (leftindent_open) {
241 os << "\\end{LyXParagraphLeftIndent}\n";
245 if (par != paragraphs.end())
246 LYXERR(Debug::LATEX, "TeXEnvironment...done " << &*par);
254 int latexOptArgInsets(Paragraph const & par, odocstream & os,
255 OutputParams const & runparams, int number)
259 InsetList::const_iterator it = par.insetList().begin();
260 InsetList::const_iterator end = par.insetList().end();
261 for (; it != end && number > 0 ; ++it) {
262 if (it->inset->lyxCode() == OPTARG_CODE) {
264 static_cast<InsetOptArg *>(it->inset);
265 lines += ins->latexOptional(os, runparams);
272 // FIXME: this should be anonymous
273 ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
275 ParagraphList::const_iterator const pit,
276 odocstream & os, TexRow & texrow,
277 OutputParams const & runparams_in,
278 string const & everypar,
279 int start_pos, int end_pos)
281 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
283 BufferParams const & bparams = buf.params();
284 ParagraphList const & paragraphs = text.paragraphs();
286 ParagraphList::const_iterator const priorpit =
287 pit == paragraphs.begin() ? pit : boost::prior(pit);
288 ParagraphList::const_iterator const nextpit =
289 pit == paragraphs.end() ? pit : boost::next(pit);
291 OutputParams runparams = runparams_in;
292 runparams.isLastPar = nextpit == paragraphs.end();
294 if (runparams.verbatim) {
295 int const dist = distance(paragraphs.begin(), pit);
296 Font const outerfont = outerFont(dist, paragraphs);
298 // No newline if only one paragraph in this lyxtext
304 /*bool need_par = */ pit->latex(bparams, outerfont,
305 os, texrow, runparams, start_pos, end_pos);
309 // FIXME This check should not really be needed.
310 // Perhaps we should issue an error if it is.
311 Layout const style = pit->forcePlainLayout() ?
312 bparams.documentClass().plainLayout() : pit->layout();
314 runparams.moving_arg |= style.needprotect;
316 bool const maintext = text.isMainText(buf);
317 // we are at the beginning of an inset and CJK is already open;
318 // we count inheritation levels to get the inset nesting right.
319 if (pit == paragraphs.begin() && !maintext
320 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
322 open_encoding_ = none;
325 // This paragraph's language
326 Language const * const par_language = pit->getParLanguage(bparams);
327 // The document's language
328 Language const * const doc_language = bparams.language;
329 // The language that was in effect when the environment this paragraph is
330 // inside of was opened
331 Language const * const outer_language =
332 (runparams.local_font != 0) ?
333 runparams.local_font->language() : doc_language;
334 // The previous language that was in effect is either the language of
335 // the previous paragraph, if there is one, or else the outer language
336 // if there is no previous paragraph
337 Language const * const prev_language =
338 (pit != paragraphs.begin()) ?
339 priorpit->getParLanguage(bparams) : outer_language;
341 if (par_language->babel() != prev_language->babel()
342 // check if we already put language command in TeXEnvironment()
343 && !(style.isEnvironment()
344 && (pit == paragraphs.begin() ||
345 (priorpit->layout() != pit->layout() &&
346 priorpit->getDepth() <= pit->getDepth())
347 || priorpit->getDepth() < pit->getDepth())))
349 if (!lyxrc.language_command_end.empty() &&
350 prev_language->babel() != outer_language->babel() &&
351 !prev_language->babel().empty())
353 os << from_ascii(subst(lyxrc.language_command_end,
355 prev_language->babel()))
356 // the '%' is necessary to prevent unwanted whitespace
361 // We need to open a new language if we couldn't close the previous
362 // one (because there's no language_command_end); and even if we closed
363 // the previous one, if the current language is different than the
364 // outer_language (which is currently in effect once the previous one
366 if ((lyxrc.language_command_end.empty() ||
367 par_language->babel() != outer_language->babel()) &&
368 !par_language->babel().empty()) {
369 // If we're inside an inset, and that inset is within an \L or \R
370 // (or equivalents), then within the inset, too, any opposite
371 // language paragraph should appear within an \L or \R (in addition
372 // to, outside of, the normal language switch commands).
373 // This behavior is not correct for ArabTeX, though.
374 if ( // not for ArabTeX
375 (par_language->lang() != "arabic_arabtex" &&
376 outer_language->lang() != "arabic_arabtex") &&
377 // are we in an inset?
378 runparams.local_font != 0 &&
379 // is the inset within an \L or \R?
381 // FIXME: currently, we don't check this; this means that
382 // we'll have unnnecessary \L and \R commands, but that
383 // doesn't seem to hurt (though latex will complain)
385 // is this paragraph in the opposite direction?
386 runparams.local_font->isRightToLeft() !=
387 par_language->rightToLeft()
389 // FIXME: I don't have a working copy of the Arabi package, so
390 // I'm not sure if the farsi and arabic_arabi stuff is correct
392 if (par_language->lang() == "farsi")
394 else if (outer_language->lang() == "farsi")
396 else if (par_language->lang() == "arabic_arabi")
398 else if (outer_language->lang() == "arabic_arabi")
400 // remaining RTL languages currently is hebrew
401 else if (par_language->rightToLeft())
406 // With CJK, the CJK tag has to be closed first (see below)
407 if (runparams.encoding->package() != Encoding::CJK) {
408 os << from_ascii(subst(
409 lyxrc.language_command_begin,
411 par_language->babel()))
412 // the '%' is necessary to prevent unwanted whitespace
419 // Switch file encoding if necessary; no need to do this for "default"
420 // encoding, since this only affects the position of the outputted
421 // \inputencoding command; the encoding switch will occur when necessary
422 if (bparams.inputenc == "auto" &&
423 runparams.encoding->package() != Encoding::none) {
424 // Look ahead for future encoding changes.
425 // We try to output them at the beginning of the paragraph,
426 // since the \inputencoding command is not allowed e.g. in
428 for (pos_type i = 0; i < pit->size(); ++i) {
429 char_type const c = pit->getChar(i);
430 Encoding const * const encoding =
431 pit->getFontSettings(bparams, i).language()->encoding();
432 if (encoding->package() != Encoding::CJK &&
433 runparams.encoding->package() == Encoding::inputenc &&
438 // All characters before c are in the ASCII range, and
439 // c is non-ASCII (but no inset), so change the
440 // encoding to that required by the language of c.
441 // With CJK, only add switch if we have CJK content at the beginning
443 if (encoding->package() != Encoding::CJK || i == 0) {
444 OutputParams tmp_rp = runparams;
445 runparams.moving_arg = false;
446 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
449 // the following is necessary after a CJK environment in a multilingual
450 // context (nesting issue).
451 if (par_language->encoding()->package() == Encoding::CJK &&
452 open_encoding_ != CJK && cjk_inherited_ == 0) {
453 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
454 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
455 open_encoding_ = CJK;
458 if (encoding->package() != Encoding::none && enc_switch.first) {
459 if (enc_switch.second > 0) {
460 // the '%' is necessary to prevent unwanted whitespace
464 // With CJK, the CJK tag had to be closed first (see above)
465 if (runparams.encoding->package() == Encoding::CJK) {
466 os << from_ascii(subst(
467 lyxrc.language_command_begin,
469 par_language->babel()))
470 // the '%' is necessary to prevent unwanted whitespace
474 runparams.encoding = encoding;
481 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
482 if (pit->allowParagraphCustomization()) {
483 if (pit->params().startOfAppendix()) {
484 os << "\\appendix\n";
488 if (!pit->params().spacing().isDefault()
489 && (pit == paragraphs.begin()
490 || !priorpit->hasSameLayout(*pit)))
492 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
497 if (style.isCommand()) {
503 switch (style.latextype) {
505 os << '\\' << from_ascii(style.latexname());
507 // Separate handling of optional argument inset.
508 if (style.optionalargs > 0) {
509 int ret = latexOptArgInsets(*pit, os, runparams,
517 os << from_ascii(style.latexparam());
519 case LATEX_ITEM_ENVIRONMENT:
520 case LATEX_LIST_ENVIRONMENT:
523 case LATEX_BIB_ENVIRONMENT:
524 // ignore this, the inset will write itself
530 Font const outerfont = outerFont(distance(paragraphs.begin(), pit),
534 os << from_utf8(everypar);
535 bool need_par = pit->latex(bparams, outerfont,
536 os, texrow, runparams, start_pos, end_pos);
538 // Make sure that \\par is done with the font of the last
539 // character if this has another size as the default.
540 // This is necessary because LaTeX (and LyX on the screen)
541 // calculates the space between the baselines according
542 // to this font. (Matthias)
544 // Is this really needed ? (Dekel)
545 // We do not need to use to change the font for the last paragraph
548 Font const font = pit->empty()
549 ? pit->getLayoutFont(bparams, outerfont)
550 : pit->getFont(bparams, pit->size() - 1, outerfont);
552 bool const is_command = style.isCommand();
554 if (style.resfont.size() != font.fontInfo().size()
555 && nextpit != paragraphs.end()
559 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
560 } else if (need_par) {
562 } else if (is_command)
565 bool pending_newline = false;
566 switch (style.latextype) {
567 case LATEX_ITEM_ENVIRONMENT:
568 case LATEX_LIST_ENVIRONMENT:
569 if (nextpit != paragraphs.end()
570 && (pit->params().depth() < nextpit->params().depth()))
571 pending_newline = true;
573 case LATEX_ENVIRONMENT: {
574 // if its the last paragraph of the current environment
575 // skip it otherwise fall through
576 if (nextpit != paragraphs.end() &&
577 (nextpit->layout() != pit->layout()
578 || nextpit->params().depth() != pit->params().depth()))
582 // fall through possible
584 // we don't need it for the last paragraph!!!
585 if (nextpit != paragraphs.end())
586 pending_newline = true;
589 if (pit->allowParagraphCustomization()) {
590 if (!pit->params().spacing().isDefault()
591 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
593 if (pending_newline) {
597 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
598 pending_newline = true;
602 // Closing the language is needed for the last paragraph; it is also
603 // needed if we're within an \L or \R that we may have opened above (not
604 // necessarily in this paragraph) and are about to close.
605 bool closing_rtl_ltr_environment =
607 (par_language->lang() != "arabic_arabtex" &&
608 outer_language->lang() != "arabic_arabtex") &&
609 // have we opened and \L or \R environment?
610 runparams.local_font != 0 &&
611 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
612 // are we about to close the language?
613 ((nextpit != paragraphs.end() &&
614 par_language->babel() !=
615 (nextpit->getParLanguage(bparams))->babel()) ||
616 (nextpit == paragraphs.end() &&
617 par_language->babel() != outer_language->babel()));
619 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
620 && par_language->babel() != outer_language->babel())) {
621 // Since \selectlanguage write the language to the aux file,
622 // we need to reset the language at the end of footnote or
625 if (pending_newline) {
629 // when the paragraph uses CJK, the language has to be closed earlier
630 if (font.language()->encoding()->package() != Encoding::CJK) {
631 if (lyxrc.language_command_end.empty()) {
632 if (!prev_language->babel().empty()) {
633 os << from_ascii(subst(
634 lyxrc.language_command_begin,
636 prev_language->babel()));
637 pending_newline = true;
639 } else if (!par_language->babel().empty()) {
640 os << from_ascii(subst(
641 lyxrc.language_command_end,
643 par_language->babel()));
644 pending_newline = true;
648 if (closing_rtl_ltr_environment)
651 if (pending_newline) {
656 // if this is a CJK-paragraph and the next isn't, close CJK
657 // also if the next paragraph is a multilingual environment (because of nesting)
658 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
659 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
660 nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams))
661 // in environments, CJK has to be closed later (nesting!)
662 && !style.isEnvironment()) {
663 os << "\\end{CJK}\n";
664 open_encoding_ = none;
667 // If this is the last paragraph, close the CJK environment
668 // if necessary. If it's an environment, we'll have to \end that first.
669 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
670 switch (open_encoding_) {
672 // do nothing at the end of child documents
673 if (maintext && buf.masterBuffer() != &buf)
679 os << "\\end{CJK}\n";
684 open_encoding_ = none;
689 open_encoding_ = none;
699 // If this is the last paragraph, and a local_font was set upon entering
700 // the inset, and we're using "auto" or "default" encoding, the encoding
701 // should be set back to that local_font's encoding.
702 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
703 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
704 && (bparams.inputenc == "auto" || bparams.inputenc == "default")) {
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() :
803 if (layout.intitle) {
805 lyxerr << "Error in latexParagraphs: You"
806 " should not mix title layouts"
807 " with normal ones." << endl;
808 } else if (!was_title) {
810 if (tclass.titletype() == TITLE_ENVIRONMENT) {
812 << from_ascii(tclass.titlename())
817 } else if (was_title && !already_title) {
818 if (tclass.titletype() == TITLE_ENVIRONMENT) {
819 os << "\\end{" << from_ascii(tclass.titlename())
823 os << "\\" << from_ascii(tclass.titlename())
827 already_title = true;
831 if (layout.isEnvironment() ||
832 !par->params().leftIndent().zero()) {
833 par = TeXEnvironment(buf, text, par, os,
836 par = TeXOnePar(buf, text, par, os, texrow,
837 runparams, everypar);
839 if (distance(lastpar, par) >= distance(lastpar, endpar))
843 // It might be that we only have a title in this document
844 if (was_title && !already_title) {
845 if (tclass.titletype() == TITLE_ENVIRONMENT) {
846 os << "\\end{" << from_ascii(tclass.titlename())
850 os << "\\" << from_ascii(tclass.titlename())
856 // if "auto end" is switched off, explicitely close the language at the end
857 // but only if the last par is in a babel language
858 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
859 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
860 os << from_utf8(subst(lyxrc.language_command_end,
862 bparams.language->babel()))
867 // If the last paragraph is an environment, we'll have to close
868 // CJK at the very end to do proper nesting.
869 if (maintext && !is_child && open_encoding_ == CJK) {
870 os << "\\end{CJK}\n";
872 open_encoding_ = none;
875 // reset inherited encoding
876 if (cjk_inherited_ > 0) {
878 if (cjk_inherited_ == 0)
879 open_encoding_ = CJK;
884 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
885 OutputParams const & runparams, Encoding const & newEnc,
888 Encoding const & oldEnc = *runparams.encoding;
889 bool moving_arg = runparams.moving_arg;
890 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
892 return make_pair(false, 0);
894 // Do nothing if the encoding is unchanged.
895 if (oldEnc.name() == newEnc.name())
896 return make_pair(false, 0);
898 // FIXME We ignore encoding switches from/to encodings that do
899 // neither support the inputenc package nor the CJK package here.
900 // This does of course only work in special cases (e.g. switch from
901 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
902 // but it is the best we can do
903 if (oldEnc.package() == Encoding::none
904 || newEnc.package() == Encoding::none)
905 return make_pair(false, 0);
907 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
908 << oldEnc.name() << " to " << newEnc.name());
909 os << setEncoding(newEnc.iconvName());
910 if (bparams.inputenc == "default")
911 return make_pair(true, 0);
913 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
914 switch (newEnc.package()) {
916 case Encoding::japanese:
917 // shouldn't ever reach here, see above
918 return make_pair(true, 0);
919 case Encoding::inputenc: {
920 int count = inputenc_arg.length();
921 if (oldEnc.package() == Encoding::CJK &&
922 open_encoding_ == CJK) {
924 open_encoding_ = none;
927 else if (oldEnc.package() == Encoding::inputenc &&
928 open_encoding_ == inputenc) {
930 open_encoding_ = none;
933 if (runparams.local_font != 0
934 && oldEnc.package() == Encoding::CJK) {
935 // within insets, \inputenc switches need
936 // to be embraced within \bgroup...\egroup;
940 open_encoding_ = inputenc;
942 // with the japanese option, inputenc is omitted.
943 if (runparams.use_japanese)
944 return make_pair(true, count);
945 os << "\\inputencoding{" << inputenc_arg << '}';
946 return make_pair(true, count + 16);
948 case Encoding::CJK: {
949 int count = inputenc_arg.length();
950 if (oldEnc.package() == Encoding::CJK &&
951 open_encoding_ == CJK) {
955 if (oldEnc.package() == Encoding::inputenc &&
956 open_encoding_ == inputenc) {
960 os << "\\begin{CJK}{" << inputenc_arg << "}{"
961 << from_ascii(bparams.fontsCJK) << "}";
962 open_encoding_ = CJK;
963 return make_pair(true, count + 15);
966 // Dead code to avoid a warning:
967 return make_pair(true, 0);