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;
55 Language const * prev_env_language_ = 0;
58 ParagraphList::const_iterator
59 TeXEnvironment(Buffer const & buf,
61 ParagraphList::const_iterator pit,
62 odocstream & os, TexRow & texrow,
63 OutputParams const & runparams);
66 ParagraphList::const_iterator
67 TeXDeeper(Buffer const & buf,
69 ParagraphList::const_iterator pit,
70 odocstream & os, TexRow & texrow,
71 OutputParams const & runparams)
73 LYXERR(Debug::LATEX, "TeXDeeper... " << &*pit);
74 ParagraphList::const_iterator par = pit;
76 ParagraphList const & paragraphs = text.paragraphs();
78 while (par != paragraphs.end() &&
79 par->params().depth() == pit->params().depth()) {
80 // FIXME This test should not be necessary.
81 // We should perhaps issue an error if it is.
82 Layout const & style = par->forcePlainLayout() ?
83 buf.params().documentClass().plainLayout() : par->layout();
84 if (style.isEnvironment()) {
85 par = TeXEnvironment(buf, text, par,
86 os, texrow, runparams);
88 par = TeXOnePar(buf, text, par,
89 os, texrow, runparams);
92 LYXERR(Debug::LATEX, "TeXDeeper...done ");
98 ParagraphList::const_iterator
99 TeXEnvironment(Buffer const & buf,
101 ParagraphList::const_iterator pit,
102 odocstream & os, TexRow & texrow,
103 OutputParams const & runparams)
105 LYXERR(Debug::LATEX, "TeXEnvironment... " << &*pit);
107 BufferParams const & bparams = buf.params();
109 // FIXME This test should not be necessary.
110 // We should perhaps issue an error if it is.
111 Layout const & style = pit->forcePlainLayout() ?
112 bparams.documentClass().plainLayout() : pit->layout();
114 ParagraphList const & paragraphs = text.paragraphs();
115 ParagraphList::const_iterator const priorpit =
116 pit == paragraphs.begin() ? pit : boost::prior(pit);
118 bool const use_prev_env_language = priorpit->layout().isEnvironment()
119 && (priorpit->getDepth() > pit->getDepth()
120 || (priorpit->getDepth() == pit->getDepth()
121 && priorpit->layout() != pit->layout()));
123 Language const * const par_language = pit->getParLanguage(bparams);
124 Language const * const doc_language = bparams.language;
125 Language const * const prev_par_language =
126 (pit != paragraphs.begin())
127 ? (use_prev_env_language ? prev_env_language_
128 : priorpit->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) {
181 if (pit->params().labelWidthString().empty())
182 os << '{' << bibitemWidest(buf) << "}\n";
185 << pit->params().labelWidthString()
188 os << from_ascii(style.latexparam()) << '\n';
192 // in multilingual environments, the CJK tags have to be nested properly
193 bool cjk_nested = false;
194 if (par_language->encoding()->package() == Encoding::CJK &&
195 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
196 if (prev_par_language->encoding()->package() == Encoding::CJK)
197 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
198 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
199 open_encoding_ = CJK;
204 ParagraphList::const_iterator par = pit;
206 par = TeXOnePar(buf, text, par, os, texrow, runparams);
208 if (par == paragraphs.end()) {
209 // Make sure that the last paragraph is
210 // correctly terminated (because TeXOnePar does
211 // not add a \n in this case)
214 } else if (par->params().depth() > pit->params().depth()) {
215 if (par->layout().isParagraph()) {
217 // How to handle this? (Lgb)
218 //&& !suffixIs(os, "\n\n")
220 // There should be at least one '\n' already
221 // but we need there to be two for Standard
222 // paragraphs that are depth-increment'ed to be
223 // output correctly. However, tables can
224 // also be paragraphs so don't adjust them.
227 // Will it ever harm to have one '\n' too
228 // many? i.e. that we sometimes will have
229 // three in a row. (Lgb)
233 par = TeXDeeper(buf, text, par, os, texrow,
236 } while (par != paragraphs.end()
237 && par->layout() == pit->layout()
238 && par->params().depth() == pit->params().depth()
239 && par->params().leftIndent() == pit->params().leftIndent());
241 if (open_encoding_ == CJK && cjk_nested) {
242 // We need to close the encoding even if it does not change
243 // to do correct environment nesting
244 os << "\\end{CJK}\n";
246 open_encoding_ = none;
249 if (style.isEnvironment()) {
250 os << "\\end{" << from_ascii(style.latexname()) << "}\n";
252 prev_env_language_ = par_language;
255 if (leftindent_open) {
256 os << "\\end{LyXParagraphLeftIndent}\n";
260 if (par != paragraphs.end())
261 LYXERR(Debug::LATEX, "TeXEnvironment...done " << &*par);
269 int latexOptArgInsets(Paragraph const & par, odocstream & os,
270 OutputParams const & runparams, int number)
274 InsetList::const_iterator it = par.insetList().begin();
275 InsetList::const_iterator end = par.insetList().end();
276 for (; it != end && number > 0 ; ++it) {
277 if (it->inset->lyxCode() == OPTARG_CODE) {
279 static_cast<InsetOptArg *>(it->inset);
280 lines += ins->latexOptional(os, runparams);
287 // FIXME: this should be anonymous
288 ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
290 ParagraphList::const_iterator const pit,
291 odocstream & os, TexRow & texrow,
292 OutputParams const & runparams_in,
293 string const & everypar,
294 int start_pos, int end_pos)
296 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
298 BufferParams const & bparams = buf.params();
299 ParagraphList const & paragraphs = text.paragraphs();
301 ParagraphList::const_iterator const priorpit =
302 pit == paragraphs.begin() ? pit : boost::prior(pit);
303 ParagraphList::const_iterator const nextpit =
304 pit == paragraphs.end() ? pit : boost::next(pit);
306 OutputParams runparams = runparams_in;
307 runparams.isLastPar = nextpit == paragraphs.end();
309 if (runparams.verbatim) {
310 int const dist = distance(paragraphs.begin(), pit);
311 Font const outerfont = outerFont(dist, paragraphs);
313 // No newline if only one paragraph in this lyxtext
319 /*bool need_par = */ pit->latex(bparams, outerfont,
320 os, texrow, runparams, start_pos, end_pos);
324 // FIXME This check should not really be needed.
325 // Perhaps we should issue an error if it is.
326 Layout const style = pit->forcePlainLayout() ?
327 bparams.documentClass().plainLayout() : pit->layout();
329 runparams.moving_arg |= style.needprotect;
331 bool const maintext = text.isMainText(buf);
332 // we are at the beginning of an inset and CJK is already open;
333 // we count inheritation levels to get the inset nesting right.
334 if (pit == paragraphs.begin() && !maintext
335 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
337 open_encoding_ = none;
340 // This paragraph's language
341 Language const * const par_language = pit->getParLanguage(bparams);
342 // The document's language
343 Language const * const doc_language = bparams.language;
344 // The language that was in effect when the environment this paragraph is
345 // inside of was opened
346 Language const * const outer_language =
347 (runparams.local_font != 0) ?
348 runparams.local_font->language() : doc_language;
350 // The previous language that was in effect is the language of the
351 // previous paragraph, unless the previous paragraph is inside an
352 // environment with nesting depth greater than (or equal to, but with
353 // a different layout) the current one. If there is no previous
354 // paragraph, the previous language is the outer language.
355 bool const use_prev_env_language = priorpit->layout().isEnvironment()
356 && (priorpit->getDepth() > pit->getDepth()
357 || (priorpit->getDepth() == pit->getDepth()
358 && priorpit->layout() != pit->layout()));
359 Language const * const prev_language =
360 (pit != paragraphs.begin())
361 ? (use_prev_env_language ? prev_env_language_
362 : priorpit->getParLanguage(bparams))
365 if (par_language->babel() != prev_language->babel()
366 // check if we already put language command in TeXEnvironment()
367 && !(style.isEnvironment()
368 && (pit == paragraphs.begin() ||
369 (priorpit->layout() != pit->layout() &&
370 priorpit->getDepth() <= pit->getDepth())
371 || priorpit->getDepth() < pit->getDepth())))
373 if (!lyxrc.language_command_end.empty() &&
374 prev_language->babel() != outer_language->babel() &&
375 !prev_language->babel().empty())
377 os << from_ascii(subst(lyxrc.language_command_end,
379 prev_language->babel()))
380 // the '%' is necessary to prevent unwanted whitespace
385 // We need to open a new language if we couldn't close the previous
386 // one (because there's no language_command_end); and even if we closed
387 // the previous one, if the current language is different than the
388 // outer_language (which is currently in effect once the previous one
390 if ((lyxrc.language_command_end.empty() ||
391 par_language->babel() != outer_language->babel()) &&
392 !par_language->babel().empty()) {
393 // If we're inside an inset, and that inset is within an \L or \R
394 // (or equivalents), then within the inset, too, any opposite
395 // language paragraph should appear within an \L or \R (in addition
396 // to, outside of, the normal language switch commands).
397 // This behavior is not correct for ArabTeX, though.
398 if ( // not for ArabTeX
399 (par_language->lang() != "arabic_arabtex" &&
400 outer_language->lang() != "arabic_arabtex") &&
401 // are we in an inset?
402 runparams.local_font != 0 &&
403 // is the inset within an \L or \R?
405 // FIXME: currently, we don't check this; this means that
406 // we'll have unnnecessary \L and \R commands, but that
407 // doesn't seem to hurt (though latex will complain)
409 // is this paragraph in the opposite direction?
410 runparams.local_font->isRightToLeft() !=
411 par_language->rightToLeft()
413 // FIXME: I don't have a working copy of the Arabi package, so
414 // I'm not sure if the farsi and arabic_arabi stuff is correct
416 if (par_language->lang() == "farsi")
418 else if (outer_language->lang() == "farsi")
420 else if (par_language->lang() == "arabic_arabi")
422 else if (outer_language->lang() == "arabic_arabi")
424 // remaining RTL languages currently is hebrew
425 else if (par_language->rightToLeft())
430 // With CJK, the CJK tag has to be closed first (see below)
431 if (runparams.encoding->package() != Encoding::CJK) {
432 os << from_ascii(subst(
433 lyxrc.language_command_begin,
435 par_language->babel()))
436 // the '%' is necessary to prevent unwanted whitespace
443 // Switch file encoding if necessary; no need to do this for "default"
444 // encoding, since this only affects the position of the outputted
445 // \inputencoding command; the encoding switch will occur when necessary
446 if (bparams.inputenc == "auto" &&
447 runparams.encoding->package() != Encoding::none) {
448 // Look ahead for future encoding changes.
449 // We try to output them at the beginning of the paragraph,
450 // since the \inputencoding command is not allowed e.g. in
452 for (pos_type i = 0; i < pit->size(); ++i) {
453 char_type const c = pit->getChar(i);
454 Encoding const * const encoding =
455 pit->getFontSettings(bparams, i).language()->encoding();
456 if (encoding->package() != Encoding::CJK &&
457 runparams.encoding->package() == Encoding::inputenc &&
462 // All characters before c are in the ASCII range, and
463 // c is non-ASCII (but no inset), so change the
464 // encoding to that required by the language of c.
465 // With CJK, only add switch if we have CJK content at the beginning
467 if (encoding->package() != Encoding::CJK || i == 0) {
468 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
470 // the following is necessary after a CJK environment in a multilingual
471 // context (nesting issue).
472 if (par_language->encoding()->package() == Encoding::CJK &&
473 open_encoding_ != CJK && cjk_inherited_ == 0) {
474 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
475 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
476 open_encoding_ = CJK;
479 if (encoding->package() != Encoding::none && enc_switch.first) {
480 if (enc_switch.second > 0) {
481 // the '%' is necessary to prevent unwanted whitespace
485 // With CJK, the CJK tag had to be closed first (see above)
486 if (runparams.encoding->package() == Encoding::CJK) {
487 os << from_ascii(subst(
488 lyxrc.language_command_begin,
490 par_language->babel()))
491 // the '%' is necessary to prevent unwanted whitespace
495 runparams.encoding = encoding;
502 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
503 if (pit->allowParagraphCustomization()) {
504 if (pit->params().startOfAppendix()) {
505 os << "\\appendix\n";
509 if (!pit->params().spacing().isDefault()
510 && (pit == paragraphs.begin()
511 || !priorpit->hasSameLayout(*pit)))
513 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
518 if (style.isCommand()) {
524 switch (style.latextype) {
526 os << '\\' << from_ascii(style.latexname());
528 // Separate handling of optional argument inset.
529 if (style.optionalargs > 0) {
530 int ret = latexOptArgInsets(*pit, os, runparams,
538 os << from_ascii(style.latexparam());
540 case LATEX_ITEM_ENVIRONMENT:
541 case LATEX_LIST_ENVIRONMENT:
544 case LATEX_BIB_ENVIRONMENT:
545 // ignore this, the inset will write itself
551 Font const outerfont = outerFont(distance(paragraphs.begin(), pit),
555 os << from_utf8(everypar);
556 bool need_par = pit->latex(bparams, outerfont,
557 os, texrow, runparams, start_pos, end_pos);
559 // Make sure that \\par is done with the font of the last
560 // character if this has another size as the default.
561 // This is necessary because LaTeX (and LyX on the screen)
562 // calculates the space between the baselines according
563 // to this font. (Matthias)
565 // Is this really needed ? (Dekel)
566 // We do not need to use to change the font for the last paragraph
569 Font const font = pit->empty()
570 ? pit->getLayoutFont(bparams, outerfont)
571 : pit->getFont(bparams, pit->size() - 1, outerfont);
573 bool const is_command = style.isCommand();
575 if (style.resfont.size() != font.fontInfo().size()
576 && nextpit != paragraphs.end()
580 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
581 } else if (need_par) {
583 } else if (is_command)
586 bool pending_newline = false;
587 switch (style.latextype) {
588 case LATEX_ITEM_ENVIRONMENT:
589 case LATEX_LIST_ENVIRONMENT:
590 if (nextpit != paragraphs.end()
591 && (pit->params().depth() < nextpit->params().depth()))
592 pending_newline = true;
594 case LATEX_ENVIRONMENT: {
595 // if its the last paragraph of the current environment
596 // skip it otherwise fall through
597 if (nextpit != paragraphs.end() &&
598 (nextpit->layout() != pit->layout()
599 || nextpit->params().depth() != pit->params().depth()))
603 // fall through possible
605 // we don't need it for the last paragraph!!!
606 if (nextpit != paragraphs.end())
607 pending_newline = true;
610 if (pit->allowParagraphCustomization()) {
611 if (!pit->params().spacing().isDefault()
612 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
614 if (pending_newline) {
618 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
619 pending_newline = true;
623 // Closing the language is needed for the last paragraph; it is also
624 // needed if we're within an \L or \R that we may have opened above (not
625 // necessarily in this paragraph) and are about to close.
626 bool closing_rtl_ltr_environment =
628 (par_language->lang() != "arabic_arabtex" &&
629 outer_language->lang() != "arabic_arabtex") &&
630 // have we opened and \L or \R environment?
631 runparams.local_font != 0 &&
632 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
633 // are we about to close the language?
634 ((nextpit != paragraphs.end() &&
635 par_language->babel() !=
636 (nextpit->getParLanguage(bparams))->babel()) ||
637 (nextpit == paragraphs.end() &&
638 par_language->babel() != outer_language->babel()));
640 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
641 && par_language->babel() != outer_language->babel())) {
642 // Since \selectlanguage write the language to the aux file,
643 // we need to reset the language at the end of footnote or
646 if (pending_newline) {
650 // when the paragraph uses CJK, the language has to be closed earlier
651 if (font.language()->encoding()->package() != Encoding::CJK) {
652 if (lyxrc.language_command_end.empty()) {
653 if (!prev_language->babel().empty()) {
654 os << from_ascii(subst(
655 lyxrc.language_command_begin,
657 prev_language->babel()));
658 pending_newline = true;
660 } else if (!par_language->babel().empty()) {
661 os << from_ascii(subst(
662 lyxrc.language_command_end,
664 par_language->babel()));
665 pending_newline = true;
669 if (closing_rtl_ltr_environment)
672 if (pending_newline) {
677 // if this is a CJK-paragraph and the next isn't, close CJK
678 // also if the next paragraph is a multilingual environment (because of nesting)
679 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
680 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
681 (nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams)))
682 // inbetween environments, CJK has to be closed later (nesting!)
683 && (!style.isEnvironment() || !nextpit->layout().isEnvironment())) {
684 os << "\\end{CJK}\n";
685 open_encoding_ = none;
688 // If this is the last paragraph, close the CJK environment
689 // if necessary. If it's an environment, we'll have to \end that first.
690 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
691 switch (open_encoding_) {
693 // do nothing at the end of child documents
694 if (maintext && buf.masterBuffer() != &buf)
700 os << "\\end{CJK}\n";
705 open_encoding_ = none;
710 open_encoding_ = none;
720 // If this is the last paragraph, and a local_font was set upon entering
721 // the inset, and we're using "auto" or "default" encoding, the encoding
722 // should be set back to that local_font's encoding.
723 // However, do not change the encoding when XeTeX is used.
724 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
725 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
726 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
727 && (!bparams.useXetex)) {
728 runparams_in.encoding = runparams_in.local_font->language()->encoding();
729 os << setEncoding(runparams_in.encoding->iconvName());
731 // Otherwise, the current encoding should be set for the next paragraph.
733 runparams_in.encoding = runparams.encoding;
736 // we don't need a newline for the last paragraph!!!
737 // Note from JMarc: we will re-add a \n explicitly in
738 // TeXEnvironment, because it is needed in this case
739 if (nextpit != paragraphs.end()) {
740 Layout const & next_layout = nextpit->layout();
741 if (style == next_layout
742 // no blank lines before environments!
743 || !next_layout.isEnvironment()
744 // unless there's a depth change
745 // FIXME What we really want to do here is put every \begin and \end
746 // tag on a new line (which was not the case with nested environments).
747 // But in the present state of play, we don't have access to the
748 // information whether the current TeX row is empty or not.
749 // For some ideas about how to fix this, see this thread:
750 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
751 || nextpit->params().depth() != pit->params().depth()) {
757 if (nextpit != paragraphs.end())
758 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*nextpit);
764 // LaTeX all paragraphs
765 void latexParagraphs(Buffer const & buf,
769 OutputParams const & runparams,
770 string const & everypar)
772 bool was_title = false;
773 bool already_title = false;
774 BufferParams const & bparams = buf.params();
775 DocumentClass const & tclass = bparams.documentClass();
776 ParagraphList const & paragraphs = text.paragraphs();
777 ParagraphList::const_iterator par = paragraphs.begin();
778 ParagraphList::const_iterator endpar = paragraphs.end();
780 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
781 // if only part of the paragraphs will be outputed
782 if (runparams.par_begin != runparams.par_end) {
783 par = boost::next(paragraphs.begin(), runparams.par_begin);
784 endpar = boost::next(paragraphs.begin(), runparams.par_end);
785 // runparams will be passed to nested paragraphs, so
786 // we have to reset the range parameters.
787 const_cast<OutputParams&>(runparams).par_begin = 0;
788 const_cast<OutputParams&>(runparams).par_end = 0;
791 bool const maintext = text.isMainText(buf);
792 bool const is_child = buf.masterBuffer() != &buf;
794 // Open a CJK environment at the beginning of the main buffer
795 // if the document's language is a CJK language
796 // (but not in child documents)
797 if (maintext && !is_child
798 && bparams.encoding().package() == Encoding::CJK) {
799 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
800 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
802 open_encoding_ = CJK;
804 // if "auto begin" is switched off, explicitely switch the
805 // language on at start
806 if (maintext && !lyxrc.language_auto_begin &&
807 !bparams.language->babel().empty()) {
809 os << from_utf8(subst(lyxrc.language_command_begin,
811 bparams.language->babel()))
816 ParagraphList::const_iterator lastpar;
818 while (par != endpar) {
820 // FIXME This check should not be needed. We should
821 // perhaps issue an error if it is.
822 Layout const & layout = par->forcePlainLayout() ?
823 tclass.plainLayout() : par->layout();
825 if (layout.intitle) {
827 lyxerr << "Error in latexParagraphs: You"
828 " should not mix title layouts"
829 " with normal ones." << endl;
830 } else if (!was_title) {
832 if (tclass.titletype() == TITLE_ENVIRONMENT) {
834 << from_ascii(tclass.titlename())
839 } else if (was_title && !already_title) {
840 if (tclass.titletype() == TITLE_ENVIRONMENT) {
841 os << "\\end{" << from_ascii(tclass.titlename())
845 os << "\\" << from_ascii(tclass.titlename())
849 already_title = true;
853 if (layout.isEnvironment() ||
854 !par->params().leftIndent().zero()) {
855 par = TeXEnvironment(buf, text, par, os,
858 par = TeXOnePar(buf, text, par, os, texrow,
859 runparams, everypar);
861 if (distance(lastpar, par) >= distance(lastpar, endpar))
865 // It might be that we only have a title in this document
866 if (was_title && !already_title) {
867 if (tclass.titletype() == TITLE_ENVIRONMENT) {
868 os << "\\end{" << from_ascii(tclass.titlename())
872 os << "\\" << from_ascii(tclass.titlename())
878 // if "auto end" is switched off, explicitely close the language at the end
879 // but only if the last par is in a babel language
880 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
881 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
882 os << from_utf8(subst(lyxrc.language_command_end,
884 bparams.language->babel()))
889 // If the last paragraph is an environment, we'll have to close
890 // CJK at the very end to do proper nesting.
891 if (maintext && !is_child && open_encoding_ == CJK) {
892 os << "\\end{CJK}\n";
894 open_encoding_ = none;
897 // reset inherited encoding
898 if (cjk_inherited_ > 0) {
900 if (cjk_inherited_ == 0)
901 open_encoding_ = CJK;
906 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
907 OutputParams const & runparams, Encoding const & newEnc,
910 Encoding const & oldEnc = *runparams.encoding;
911 bool moving_arg = runparams.moving_arg;
912 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
914 return make_pair(false, 0);
916 // Do nothing if the encoding is unchanged.
917 if (oldEnc.name() == newEnc.name())
918 return make_pair(false, 0);
920 // FIXME We ignore encoding switches from/to encodings that do
921 // neither support the inputenc package nor the CJK package here.
922 // This does of course only work in special cases (e.g. switch from
923 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
924 // but it is the best we can do
925 if (oldEnc.package() == Encoding::none
926 || newEnc.package() == Encoding::none)
927 return make_pair(false, 0);
929 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
930 << oldEnc.name() << " to " << newEnc.name());
931 os << setEncoding(newEnc.iconvName());
932 if (bparams.inputenc == "default")
933 return make_pair(true, 0);
935 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
936 switch (newEnc.package()) {
938 case Encoding::japanese:
939 // shouldn't ever reach here, see above
940 return make_pair(true, 0);
941 case Encoding::inputenc: {
942 int count = inputenc_arg.length();
943 if (oldEnc.package() == Encoding::CJK &&
944 open_encoding_ == CJK) {
946 open_encoding_ = none;
949 else if (oldEnc.package() == Encoding::inputenc &&
950 open_encoding_ == inputenc) {
952 open_encoding_ = none;
955 if (runparams.local_font != 0
956 && oldEnc.package() == Encoding::CJK) {
957 // within insets, \inputenc switches need
958 // to be embraced within \bgroup...\egroup;
962 open_encoding_ = inputenc;
964 // with the japanese option, inputenc is omitted.
965 if (runparams.use_japanese)
966 return make_pair(true, count);
967 os << "\\inputencoding{" << inputenc_arg << '}';
968 return make_pair(true, count + 16);
970 case Encoding::CJK: {
971 int count = inputenc_arg.length();
972 if (oldEnc.package() == Encoding::CJK &&
973 open_encoding_ == CJK) {
977 if (oldEnc.package() == Encoding::inputenc &&
978 open_encoding_ == inputenc) {
982 os << "\\begin{CJK}{" << inputenc_arg << "}{"
983 << from_ascii(bparams.fontsCJK) << "}";
984 open_encoding_ = CJK;
985 return make_pair(true, count + 15);
988 // Dead code to avoid a warning:
989 return make_pair(true, 0);