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 = prev_env_language_ != 0
119 && priorpit->layout().isEnvironment()
120 && (priorpit->getDepth() > pit->getDepth()
121 || (priorpit->getDepth() == pit->getDepth()
122 && priorpit->layout() != pit->layout()));
124 Language const * const par_language = pit->getParLanguage(bparams);
125 Language const * const doc_language = bparams.language;
126 Language const * const prev_par_language =
127 (pit != paragraphs.begin())
128 ? (use_prev_env_language ? prev_env_language_
129 : priorpit->getParLanguage(bparams))
131 if (par_language->babel() != prev_par_language->babel()) {
133 if (!lyxrc.language_command_end.empty() &&
134 prev_par_language->babel() != doc_language->babel() &&
135 !prev_par_language->babel().empty()) {
136 os << from_ascii(subst(
137 lyxrc.language_command_end,
139 prev_par_language->babel()))
140 // the '%' is necessary to prevent unwanted whitespace
145 if ((lyxrc.language_command_end.empty() ||
146 par_language->babel() != doc_language->babel()) &&
147 !par_language->babel().empty()) {
148 os << from_ascii(subst(
149 lyxrc.language_command_begin,
151 par_language->babel()))
152 // the '%' is necessary to prevent unwanted whitespace
158 bool leftindent_open = false;
159 if (!pit->params().leftIndent().zero()) {
160 os << "\\begin{LyXParagraphLeftIndent}{"
161 << from_ascii(pit->params().leftIndent().asLatexString())
164 leftindent_open = true;
167 if (style.isEnvironment()) {
168 os << "\\begin{" << from_ascii(style.latexname()) << '}';
169 if (style.optionalargs > 0) {
170 int ret = latexOptArgInsets(*pit, os, runparams,
177 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
179 << pit->params().labelWidthString()
181 } else if (style.labeltype == LABEL_BIBLIO) {
182 if (pit->params().labelWidthString().empty())
183 os << '{' << bibitemWidest(buf) << "}\n";
186 << pit->params().labelWidthString()
189 os << from_ascii(style.latexparam()) << '\n';
193 // in multilingual environments, the CJK tags have to be nested properly
194 bool cjk_nested = false;
195 if (par_language->encoding()->package() == Encoding::CJK &&
196 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
197 if (prev_par_language->encoding()->package() == Encoding::CJK)
198 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
199 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
200 open_encoding_ = CJK;
205 ParagraphList::const_iterator par = pit;
207 par = TeXOnePar(buf, text, par, os, texrow, runparams);
209 if (par == paragraphs.end()) {
210 // Make sure that the last paragraph is
211 // correctly terminated (because TeXOnePar does
212 // not add a \n in this case)
215 } else if (par->params().depth() > pit->params().depth()) {
216 if (par->layout().isParagraph()) {
218 // How to handle this? (Lgb)
219 //&& !suffixIs(os, "\n\n")
221 // There should be at least one '\n' already
222 // but we need there to be two for Standard
223 // paragraphs that are depth-increment'ed to be
224 // output correctly. However, tables can
225 // also be paragraphs so don't adjust them.
228 // Will it ever harm to have one '\n' too
229 // many? i.e. that we sometimes will have
230 // three in a row. (Lgb)
234 par = TeXDeeper(buf, text, par, os, texrow,
237 } while (par != paragraphs.end()
238 && par->layout() == pit->layout()
239 && par->params().depth() == pit->params().depth()
240 && par->params().leftIndent() == pit->params().leftIndent());
242 if (open_encoding_ == CJK && cjk_nested) {
243 // We need to close the encoding even if it does not change
244 // to do correct environment nesting
245 os << "\\end{CJK}\n";
247 open_encoding_ = none;
250 if (style.isEnvironment()) {
251 os << "\\end{" << from_ascii(style.latexname()) << "}\n";
253 prev_env_language_ = par_language;
256 if (leftindent_open) {
257 os << "\\end{LyXParagraphLeftIndent}\n";
261 if (par != paragraphs.end())
262 LYXERR(Debug::LATEX, "TeXEnvironment...done " << &*par);
270 int latexOptArgInsets(Paragraph const & par, odocstream & os,
271 OutputParams const & runparams, int number)
275 InsetList::const_iterator it = par.insetList().begin();
276 InsetList::const_iterator end = par.insetList().end();
277 for (; it != end && number > 0 ; ++it) {
278 if (it->inset->lyxCode() == OPTARG_CODE) {
280 static_cast<InsetOptArg *>(it->inset);
281 lines += ins->latexOptional(os, runparams);
288 // FIXME: this should be anonymous
289 ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
291 ParagraphList::const_iterator const pit,
292 odocstream & os, TexRow & texrow,
293 OutputParams const & runparams_in,
294 string const & everypar,
295 int start_pos, int end_pos)
297 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
299 BufferParams const & bparams = buf.params();
300 ParagraphList const & paragraphs = text.paragraphs();
302 ParagraphList::const_iterator const priorpit =
303 pit == paragraphs.begin() ? pit : boost::prior(pit);
304 ParagraphList::const_iterator const nextpit =
305 pit == paragraphs.end() ? pit : boost::next(pit);
307 OutputParams runparams = runparams_in;
308 runparams.isLastPar = nextpit == paragraphs.end();
310 if (runparams.verbatim) {
311 int const dist = distance(paragraphs.begin(), pit);
312 Font const outerfont = outerFont(dist, paragraphs);
314 // No newline if only one paragraph in this lyxtext
320 /*bool need_par = */ pit->latex(bparams, outerfont,
321 os, texrow, runparams, start_pos, end_pos);
325 // FIXME This check should not really be needed.
326 // Perhaps we should issue an error if it is.
327 Layout const style = pit->forcePlainLayout() ?
328 bparams.documentClass().plainLayout() : pit->layout();
330 runparams.moving_arg |= style.needprotect;
332 bool const maintext = text.isMainText(buf);
333 // we are at the beginning of an inset and CJK is already open;
334 // we count inheritation levels to get the inset nesting right.
335 if (pit == paragraphs.begin() && !maintext
336 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
338 open_encoding_ = none;
341 // This paragraph's language
342 Language const * const par_language = pit->getParLanguage(bparams);
343 // The document's language
344 Language const * const doc_language = bparams.language;
345 // The language that was in effect when the environment this paragraph is
346 // inside of was opened
347 Language const * const outer_language =
348 (runparams.local_font != 0) ?
349 runparams.local_font->language() : doc_language;
351 // The previous language that was in effect is the language of the
352 // previous paragraph, unless the previous paragraph is inside an
353 // environment with nesting depth greater than (or equal to, but with
354 // a different layout) the current one. If there is no previous
355 // paragraph, the previous language is the outer language.
356 bool const use_prev_env_language = prev_env_language_ != 0
357 && priorpit->layout().isEnvironment()
358 && (priorpit->getDepth() > pit->getDepth()
359 || (priorpit->getDepth() == pit->getDepth()
360 && priorpit->layout() != pit->layout()));
361 Language const * const prev_language =
362 (pit != paragraphs.begin())
363 ? (use_prev_env_language ? prev_env_language_
364 : priorpit->getParLanguage(bparams))
367 if (par_language->babel() != prev_language->babel()
368 // check if we already put language command in TeXEnvironment()
369 && !(style.isEnvironment()
370 && (pit == paragraphs.begin() ||
371 (priorpit->layout() != pit->layout() &&
372 priorpit->getDepth() <= pit->getDepth())
373 || priorpit->getDepth() < pit->getDepth())))
375 if (!lyxrc.language_command_end.empty() &&
376 prev_language->babel() != outer_language->babel() &&
377 !prev_language->babel().empty())
379 os << from_ascii(subst(lyxrc.language_command_end,
381 prev_language->babel()))
382 // the '%' is necessary to prevent unwanted whitespace
387 // We need to open a new language if we couldn't close the previous
388 // one (because there's no language_command_end); and even if we closed
389 // the previous one, if the current language is different than the
390 // outer_language (which is currently in effect once the previous one
392 if ((lyxrc.language_command_end.empty() ||
393 par_language->babel() != outer_language->babel()) &&
394 !par_language->babel().empty()) {
395 // If we're inside an inset, and that inset is within an \L or \R
396 // (or equivalents), then within the inset, too, any opposite
397 // language paragraph should appear within an \L or \R (in addition
398 // to, outside of, the normal language switch commands).
399 // This behavior is not correct for ArabTeX, though.
400 if ( // not for ArabTeX
401 (par_language->lang() != "arabic_arabtex" &&
402 outer_language->lang() != "arabic_arabtex") &&
403 // are we in an inset?
404 runparams.local_font != 0 &&
405 // is the inset within an \L or \R?
407 // FIXME: currently, we don't check this; this means that
408 // we'll have unnnecessary \L and \R commands, but that
409 // doesn't seem to hurt (though latex will complain)
411 // is this paragraph in the opposite direction?
412 runparams.local_font->isRightToLeft() !=
413 par_language->rightToLeft()
415 // FIXME: I don't have a working copy of the Arabi package, so
416 // I'm not sure if the farsi and arabic_arabi stuff is correct
418 if (par_language->lang() == "farsi")
420 else if (outer_language->lang() == "farsi")
422 else if (par_language->lang() == "arabic_arabi")
424 else if (outer_language->lang() == "arabic_arabi")
426 // remaining RTL languages currently is hebrew
427 else if (par_language->rightToLeft())
432 // With CJK, the CJK tag has to be closed first (see below)
433 if (runparams.encoding->package() != Encoding::CJK) {
434 os << from_ascii(subst(
435 lyxrc.language_command_begin,
437 par_language->babel()))
438 // the '%' is necessary to prevent unwanted whitespace
445 // Switch file encoding if necessary; no need to do this for "default"
446 // encoding, since this only affects the position of the outputted
447 // \inputencoding command; the encoding switch will occur when necessary
448 if (bparams.inputenc == "auto" &&
449 runparams.encoding->package() != Encoding::none) {
450 // Look ahead for future encoding changes.
451 // We try to output them at the beginning of the paragraph,
452 // since the \inputencoding command is not allowed e.g. in
454 for (pos_type i = 0; i < pit->size(); ++i) {
455 char_type const c = pit->getChar(i);
456 Encoding const * const encoding =
457 pit->getFontSettings(bparams, i).language()->encoding();
458 if (encoding->package() != Encoding::CJK &&
459 runparams.encoding->package() == Encoding::inputenc &&
464 // All characters before c are in the ASCII range, and
465 // c is non-ASCII (but no inset), so change the
466 // encoding to that required by the language of c.
467 // With CJK, only add switch if we have CJK content at the beginning
469 if (encoding->package() != Encoding::CJK || i == 0) {
470 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
472 // the following is necessary after a CJK environment in a multilingual
473 // context (nesting issue).
474 if (par_language->encoding()->package() == Encoding::CJK &&
475 open_encoding_ != CJK && cjk_inherited_ == 0) {
476 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
477 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
478 open_encoding_ = CJK;
481 if (encoding->package() != Encoding::none && enc_switch.first) {
482 if (enc_switch.second > 0) {
483 // the '%' is necessary to prevent unwanted whitespace
487 // With CJK, the CJK tag had to be closed first (see above)
488 if (runparams.encoding->package() == Encoding::CJK) {
489 os << from_ascii(subst(
490 lyxrc.language_command_begin,
492 par_language->babel()))
493 // the '%' is necessary to prevent unwanted whitespace
497 runparams.encoding = encoding;
504 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
505 if (pit->allowParagraphCustomization()) {
506 if (pit->params().startOfAppendix()) {
507 os << "\\appendix\n";
511 if (!pit->params().spacing().isDefault()
512 && (pit == paragraphs.begin()
513 || !priorpit->hasSameLayout(*pit)))
515 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
520 if (style.isCommand()) {
526 switch (style.latextype) {
528 os << '\\' << from_ascii(style.latexname());
530 // Separate handling of optional argument inset.
531 if (style.optionalargs > 0) {
532 int ret = latexOptArgInsets(*pit, os, runparams,
540 os << from_ascii(style.latexparam());
542 case LATEX_ITEM_ENVIRONMENT:
543 case LATEX_LIST_ENVIRONMENT:
546 case LATEX_BIB_ENVIRONMENT:
547 // ignore this, the inset will write itself
553 Font const outerfont = outerFont(distance(paragraphs.begin(), pit),
557 os << from_utf8(everypar);
558 bool need_par = pit->latex(bparams, outerfont,
559 os, texrow, runparams, start_pos, end_pos);
561 // Make sure that \\par is done with the font of the last
562 // character if this has another size as the default.
563 // This is necessary because LaTeX (and LyX on the screen)
564 // calculates the space between the baselines according
565 // to this font. (Matthias)
567 // Is this really needed ? (Dekel)
568 // We do not need to use to change the font for the last paragraph
571 Font const font = pit->empty()
572 ? pit->getLayoutFont(bparams, outerfont)
573 : pit->getFont(bparams, pit->size() - 1, outerfont);
575 bool const is_command = style.isCommand();
577 if (style.resfont.size() != font.fontInfo().size()
578 && nextpit != paragraphs.end()
582 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
583 } else if (need_par) {
585 } else if (is_command)
588 bool pending_newline = false;
589 switch (style.latextype) {
590 case LATEX_ITEM_ENVIRONMENT:
591 case LATEX_LIST_ENVIRONMENT:
592 if (nextpit != paragraphs.end()
593 && (pit->params().depth() < nextpit->params().depth()))
594 pending_newline = true;
596 case LATEX_ENVIRONMENT: {
597 // if its the last paragraph of the current environment
598 // skip it otherwise fall through
599 if (nextpit != paragraphs.end() &&
600 (nextpit->layout() != pit->layout()
601 || nextpit->params().depth() != pit->params().depth()))
605 // fall through possible
607 // we don't need it for the last paragraph!!!
608 if (nextpit != paragraphs.end())
609 pending_newline = true;
612 if (pit->allowParagraphCustomization()) {
613 if (!pit->params().spacing().isDefault()
614 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
616 if (pending_newline) {
620 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
621 pending_newline = true;
625 // Closing the language is needed for the last paragraph; it is also
626 // needed if we're within an \L or \R that we may have opened above (not
627 // necessarily in this paragraph) and are about to close.
628 bool closing_rtl_ltr_environment =
630 (par_language->lang() != "arabic_arabtex" &&
631 outer_language->lang() != "arabic_arabtex") &&
632 // have we opened and \L or \R environment?
633 runparams.local_font != 0 &&
634 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
635 // are we about to close the language?
636 ((nextpit != paragraphs.end() &&
637 par_language->babel() !=
638 (nextpit->getParLanguage(bparams))->babel()) ||
639 (nextpit == paragraphs.end() &&
640 par_language->babel() != outer_language->babel()));
642 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
643 && par_language->babel() != outer_language->babel())) {
644 // Since \selectlanguage write the language to the aux file,
645 // we need to reset the language at the end of footnote or
648 if (pending_newline) {
652 // when the paragraph uses CJK, the language has to be closed earlier
653 if (font.language()->encoding()->package() != Encoding::CJK) {
654 if (lyxrc.language_command_end.empty()) {
655 if (!prev_language->babel().empty()) {
656 os << from_ascii(subst(
657 lyxrc.language_command_begin,
659 prev_language->babel()));
660 pending_newline = true;
662 } else if (!par_language->babel().empty()) {
663 os << from_ascii(subst(
664 lyxrc.language_command_end,
666 par_language->babel()));
667 pending_newline = true;
671 if (closing_rtl_ltr_environment)
674 if (pending_newline) {
679 // if this is a CJK-paragraph and the next isn't, close CJK
680 // also if the next paragraph is a multilingual environment (because of nesting)
681 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
682 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
683 (nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams)))
684 // inbetween environments, CJK has to be closed later (nesting!)
685 && (!style.isEnvironment() || !nextpit->layout().isEnvironment())) {
686 os << "\\end{CJK}\n";
687 open_encoding_ = none;
690 // If this is the last paragraph, close the CJK environment
691 // if necessary. If it's an environment, we'll have to \end that first.
692 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
693 switch (open_encoding_) {
695 // do nothing at the end of child documents
696 if (maintext && buf.masterBuffer() != &buf)
702 os << "\\end{CJK}\n";
707 open_encoding_ = none;
712 open_encoding_ = none;
722 // If this is the last paragraph, and a local_font was set upon entering
723 // the inset, and we're using "auto" or "default" encoding, the encoding
724 // should be set back to that local_font's encoding.
725 // However, do not change the encoding when XeTeX is used.
726 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
727 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
728 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
729 && (!bparams.useXetex)) {
730 runparams_in.encoding = runparams_in.local_font->language()->encoding();
731 os << setEncoding(runparams_in.encoding->iconvName());
733 // Otherwise, the current encoding should be set for the next paragraph.
735 runparams_in.encoding = runparams.encoding;
738 // we don't need a newline for the last paragraph!!!
739 // Note from JMarc: we will re-add a \n explicitly in
740 // TeXEnvironment, because it is needed in this case
741 if (nextpit != paragraphs.end()) {
742 Layout const & next_layout = nextpit->layout();
743 if (style == next_layout
744 // no blank lines before environments!
745 || !next_layout.isEnvironment()
746 // unless there's a depth change
747 // FIXME What we really want to do here is put every \begin and \end
748 // tag on a new line (which was not the case with nested environments).
749 // But in the present state of play, we don't have access to the
750 // information whether the current TeX row is empty or not.
751 // For some ideas about how to fix this, see this thread:
752 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
753 || nextpit->params().depth() != pit->params().depth()) {
759 if (nextpit != paragraphs.end())
760 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*nextpit);
766 // LaTeX all paragraphs
767 void latexParagraphs(Buffer const & buf,
771 OutputParams const & runparams,
772 string const & everypar)
774 bool was_title = false;
775 bool already_title = false;
776 BufferParams const & bparams = buf.params();
777 DocumentClass const & tclass = bparams.documentClass();
778 ParagraphList const & paragraphs = text.paragraphs();
779 ParagraphList::const_iterator par = paragraphs.begin();
780 ParagraphList::const_iterator endpar = paragraphs.end();
782 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
783 // if only part of the paragraphs will be outputed
784 if (runparams.par_begin != runparams.par_end) {
785 par = boost::next(paragraphs.begin(), runparams.par_begin);
786 endpar = boost::next(paragraphs.begin(), runparams.par_end);
787 // runparams will be passed to nested paragraphs, so
788 // we have to reset the range parameters.
789 const_cast<OutputParams&>(runparams).par_begin = 0;
790 const_cast<OutputParams&>(runparams).par_end = 0;
793 bool const maintext = text.isMainText(buf);
794 bool const is_child = buf.masterBuffer() != &buf;
796 // Open a CJK environment at the beginning of the main buffer
797 // if the document's language is a CJK language
798 // (but not in child documents)
799 if (maintext && !is_child
800 && bparams.encoding().package() == Encoding::CJK) {
801 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
802 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
804 open_encoding_ = CJK;
806 // if "auto begin" is switched off, explicitely switch the
807 // language on at start
808 if (maintext && !lyxrc.language_auto_begin &&
809 !bparams.language->babel().empty()) {
811 os << from_utf8(subst(lyxrc.language_command_begin,
813 bparams.language->babel()))
818 ParagraphList::const_iterator lastpar;
820 while (par != endpar) {
822 // FIXME This check should not be needed. We should
823 // perhaps issue an error if it is.
824 Layout const & layout = par->forcePlainLayout() ?
825 tclass.plainLayout() : par->layout();
827 if (layout.intitle) {
829 lyxerr << "Error in latexParagraphs: You"
830 " should not mix title layouts"
831 " with normal ones." << endl;
832 } else if (!was_title) {
834 if (tclass.titletype() == TITLE_ENVIRONMENT) {
836 << from_ascii(tclass.titlename())
841 } else if (was_title && !already_title) {
842 if (tclass.titletype() == TITLE_ENVIRONMENT) {
843 os << "\\end{" << from_ascii(tclass.titlename())
847 os << "\\" << from_ascii(tclass.titlename())
851 already_title = true;
855 if (layout.isEnvironment() ||
856 !par->params().leftIndent().zero()) {
857 par = TeXEnvironment(buf, text, par, os,
860 par = TeXOnePar(buf, text, par, os, texrow,
861 runparams, everypar);
863 if (distance(lastpar, par) >= distance(lastpar, endpar))
867 // It might be that we only have a title in this document
868 if (was_title && !already_title) {
869 if (tclass.titletype() == TITLE_ENVIRONMENT) {
870 os << "\\end{" << from_ascii(tclass.titlename())
874 os << "\\" << from_ascii(tclass.titlename())
880 // if "auto end" is switched off, explicitely close the language at the end
881 // but only if the last par is in a babel language
882 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
883 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
884 os << from_utf8(subst(lyxrc.language_command_end,
886 bparams.language->babel()))
891 // If the last paragraph is an environment, we'll have to close
892 // CJK at the very end to do proper nesting.
893 if (maintext && !is_child && open_encoding_ == CJK) {
894 os << "\\end{CJK}\n";
896 open_encoding_ = none;
899 // reset inherited encoding
900 if (cjk_inherited_ > 0) {
902 if (cjk_inherited_ == 0)
903 open_encoding_ = CJK;
908 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
909 OutputParams const & runparams, Encoding const & newEnc,
912 Encoding const & oldEnc = *runparams.encoding;
913 bool moving_arg = runparams.moving_arg;
914 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
916 return make_pair(false, 0);
918 // Do nothing if the encoding is unchanged.
919 if (oldEnc.name() == newEnc.name())
920 return make_pair(false, 0);
922 // FIXME We ignore encoding switches from/to encodings that do
923 // neither support the inputenc package nor the CJK package here.
924 // This does of course only work in special cases (e.g. switch from
925 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
926 // but it is the best we can do
927 if (oldEnc.package() == Encoding::none
928 || newEnc.package() == Encoding::none)
929 return make_pair(false, 0);
931 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
932 << oldEnc.name() << " to " << newEnc.name());
933 os << setEncoding(newEnc.iconvName());
934 if (bparams.inputenc == "default")
935 return make_pair(true, 0);
937 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
938 switch (newEnc.package()) {
940 case Encoding::japanese:
941 // shouldn't ever reach here, see above
942 return make_pair(true, 0);
943 case Encoding::inputenc: {
944 int count = inputenc_arg.length();
945 if (oldEnc.package() == Encoding::CJK &&
946 open_encoding_ == CJK) {
948 open_encoding_ = none;
951 else if (oldEnc.package() == Encoding::inputenc &&
952 open_encoding_ == inputenc) {
954 open_encoding_ = none;
957 if (runparams.local_font != 0
958 && oldEnc.package() == Encoding::CJK) {
959 // within insets, \inputenc switches need
960 // to be embraced within \bgroup...\egroup;
964 open_encoding_ = inputenc;
966 // with the japanese option, inputenc is omitted.
967 if (runparams.use_japanese)
968 return make_pair(true, count);
969 os << "\\inputencoding{" << inputenc_arg << '}';
970 return make_pair(true, count + 16);
972 case Encoding::CJK: {
973 int count = inputenc_arg.length();
974 if (oldEnc.package() == Encoding::CJK &&
975 open_encoding_ == CJK) {
979 if (oldEnc.package() == Encoding::inputenc &&
980 open_encoding_ == inputenc) {
984 os << "\\begin{CJK}{" << inputenc_arg << "}{"
985 << from_ascii(bparams.fontsCJK) << "}";
986 open_encoding_ = CJK;
987 return make_pair(true, count + 15);
990 // Dead code to avoid a warning:
991 return make_pair(true, 0);