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 "ParagraphParameters.h"
25 #include "TextClass.h"
29 #include "insets/InsetBibitem.h"
30 #include "insets/InsetOptArg.h"
32 #include "support/lassert.h"
33 #include "support/debug.h"
34 #include "support/lstrings.h"
36 #include <boost/next_prior.hpp>
39 using namespace lyx::support;
52 static int open_encoding_ = none;
53 static int cjk_inherited_ = 0;
54 Language const * prev_env_language_ = 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 bool const force_plain_layout = text.inset().forcePlainLayout();
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 = force_plain_layout
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 = text.inset().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 Encoding const * const prev_encoding = runparams.encoding;
125 Language const * const par_language = pit->getParLanguage(bparams);
126 Language const * const doc_language = bparams.language;
127 Language const * const prev_par_language =
128 (pit != paragraphs.begin())
129 ? (use_prev_env_language ? prev_env_language_
130 : priorpit->getParLanguage(bparams))
132 if (par_language->babel() != prev_par_language->babel()) {
134 if (!lyxrc.language_command_end.empty() &&
135 prev_par_language->babel() != doc_language->babel() &&
136 !prev_par_language->babel().empty()) {
137 os << from_ascii(subst(
138 lyxrc.language_command_end,
140 prev_par_language->babel()))
141 // the '%' is necessary to prevent unwanted whitespace
146 if ((lyxrc.language_command_end.empty() ||
147 par_language->babel() != doc_language->babel()) &&
148 !par_language->babel().empty()) {
149 os << from_ascii(subst(
150 lyxrc.language_command_begin,
152 par_language->babel()))
153 // the '%' is necessary to prevent unwanted whitespace
159 bool leftindent_open = false;
160 if (!pit->params().leftIndent().zero()) {
161 os << "\\begin{LyXParagraphLeftIndent}{"
162 << from_ascii(pit->params().leftIndent().asLatexString())
165 leftindent_open = true;
168 if (style.isEnvironment()) {
169 os << "\\begin{" << from_ascii(style.latexname()) << '}';
170 if (style.optionalargs > 0) {
171 int ret = latexOptArgInsets(*pit, os, runparams,
178 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
180 << pit->params().labelWidthString()
182 } else if (style.labeltype == LABEL_BIBLIO) {
183 if (pit->params().labelWidthString().empty())
184 os << '{' << bibitemWidest(buf) << "}\n";
187 << pit->params().labelWidthString()
190 os << from_ascii(style.latexparam()) << '\n';
194 // in multilingual environments, the CJK tags have to be nested properly
195 bool cjk_nested = false;
196 if (par_language->encoding()->package() == Encoding::CJK &&
197 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
198 if (prev_par_language->encoding()->package() == Encoding::CJK)
199 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
200 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
201 open_encoding_ = CJK;
206 ParagraphList::const_iterator par = pit;
208 par = TeXOnePar(buf, text, par, os, texrow, runparams);
210 if (par == paragraphs.end()) {
211 // Make sure that the last paragraph is
212 // correctly terminated (because TeXOnePar does
213 // not add a \n in this case)
216 } else if (par->params().depth() > pit->params().depth()) {
217 if (par->layout().isParagraph()) {
219 // How to handle this? (Lgb)
220 //&& !suffixIs(os, "\n\n")
222 // There should be at least one '\n' already
223 // but we need there to be two for Standard
224 // paragraphs that are depth-increment'ed to be
225 // output correctly. However, tables can
226 // also be paragraphs so don't adjust them.
229 // Will it ever harm to have one '\n' too
230 // many? i.e. that we sometimes will have
231 // three in a row. (Lgb)
235 par = TeXDeeper(buf, text, par, os, texrow,
238 } while (par != paragraphs.end()
239 && par->layout() == pit->layout()
240 && par->params().depth() == pit->params().depth()
241 && par->params().leftIndent() == pit->params().leftIndent());
243 if (open_encoding_ == CJK && cjk_nested) {
244 // We need to close the encoding even if it does not change
245 // to do correct environment nesting
246 os << "\\end{CJK}\n";
248 open_encoding_ = none;
251 if (style.isEnvironment()) {
252 os << "\\end{" << from_ascii(style.latexname()) << "}\n";
254 prev_env_language_ = par_language;
255 runparams.encoding = prev_encoding;
258 if (leftindent_open) {
259 os << "\\end{LyXParagraphLeftIndent}\n";
261 prev_env_language_ = par_language;
262 runparams.encoding = prev_encoding;
265 if (par != paragraphs.end())
266 LYXERR(Debug::LATEX, "TeXEnvironment...done " << &*par);
274 int latexOptArgInsets(Paragraph const & par, odocstream & os,
275 OutputParams const & runparams, int number)
279 InsetList::const_iterator it = par.insetList().begin();
280 InsetList::const_iterator end = par.insetList().end();
281 for (; it != end && number > 0 ; ++it) {
282 if (it->inset->lyxCode() == OPTARG_CODE) {
284 static_cast<InsetOptArg *>(it->inset);
285 lines += ins->latexOptional(os, runparams);
292 // FIXME: this should be anonymous
293 ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
295 ParagraphList::const_iterator const pit,
296 odocstream & os, TexRow & texrow,
297 OutputParams const & runparams_in,
298 string const & everypar,
299 int start_pos, int end_pos)
301 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
303 BufferParams const & bparams = buf.params();
304 ParagraphList const & paragraphs = text.paragraphs();
306 ParagraphList::const_iterator const priorpit =
307 pit == paragraphs.begin() ? pit : boost::prior(pit);
308 ParagraphList::const_iterator const nextpit =
309 pit == paragraphs.end() ? pit : boost::next(pit);
311 OutputParams runparams = runparams_in;
312 runparams.isLastPar = nextpit == paragraphs.end();
314 if (runparams.verbatim) {
315 int const dist = distance(paragraphs.begin(), pit);
316 Font const outerfont = text.outerFont(dist);
318 // No newline if only one paragraph in this lyxtext
324 /*bool need_par = */ pit->latex(bparams, outerfont,
325 os, texrow, runparams, start_pos, end_pos);
329 // FIXME This check should not really be needed.
330 // Perhaps we should issue an error if it is.
331 Layout const style = text.inset().forcePlainLayout() ?
332 bparams.documentClass().plainLayout() : pit->layout();
334 runparams.moving_arg |= style.needprotect;
336 bool const maintext = text.isMainText();
337 // we are at the beginning of an inset and CJK is already open;
338 // we count inheritation levels to get the inset nesting right.
339 if (pit == paragraphs.begin() && !maintext
340 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
342 open_encoding_ = none;
345 // This paragraph's language
346 Language const * const par_language = pit->getParLanguage(bparams);
347 // The document's language
348 Language const * const doc_language = bparams.language;
349 // The language that was in effect when the environment this paragraph is
350 // inside of was opened
351 Language const * const outer_language =
352 (runparams.local_font != 0) ?
353 runparams.local_font->language() : doc_language;
355 // The previous language that was in effect is the language of the
356 // previous paragraph, unless the previous paragraph is inside an
357 // environment with nesting depth greater than (or equal to, but with
358 // a different layout) the current one. If there is no previous
359 // paragraph, the previous language is the outer language.
360 bool const use_prev_env_language = prev_env_language_ != 0
361 && priorpit->layout().isEnvironment()
362 && (priorpit->getDepth() > pit->getDepth()
363 || (priorpit->getDepth() == pit->getDepth()
364 && priorpit->layout() != pit->layout()));
365 Language const * const prev_language =
366 (pit != paragraphs.begin())
367 ? (use_prev_env_language ? prev_env_language_
368 : priorpit->getParLanguage(bparams))
371 if (par_language->babel() != prev_language->babel()
372 // check if we already put language command in TeXEnvironment()
373 && !(style.isEnvironment()
374 && (pit == paragraphs.begin() ||
375 (priorpit->layout() != pit->layout() &&
376 priorpit->getDepth() <= pit->getDepth())
377 || priorpit->getDepth() < pit->getDepth())))
379 if (!lyxrc.language_command_end.empty() &&
380 prev_language->babel() != outer_language->babel() &&
381 !prev_language->babel().empty())
383 os << from_ascii(subst(lyxrc.language_command_end,
385 prev_language->babel()))
386 // the '%' is necessary to prevent unwanted whitespace
391 // We need to open a new language if we couldn't close the previous
392 // one (because there's no language_command_end); and even if we closed
393 // the previous one, if the current language is different than the
394 // outer_language (which is currently in effect once the previous one
396 if ((lyxrc.language_command_end.empty() ||
397 par_language->babel() != outer_language->babel()) &&
398 !par_language->babel().empty()) {
399 // If we're inside an inset, and that inset is within an \L or \R
400 // (or equivalents), then within the inset, too, any opposite
401 // language paragraph should appear within an \L or \R (in addition
402 // to, outside of, the normal language switch commands).
403 // This behavior is not correct for ArabTeX, though.
404 if ( // not for ArabTeX
405 (par_language->lang() != "arabic_arabtex" &&
406 outer_language->lang() != "arabic_arabtex") &&
407 // are we in an inset?
408 runparams.local_font != 0 &&
409 // is the inset within an \L or \R?
411 // FIXME: currently, we don't check this; this means that
412 // we'll have unnnecessary \L and \R commands, but that
413 // doesn't seem to hurt (though latex will complain)
415 // is this paragraph in the opposite direction?
416 runparams.local_font->isRightToLeft() !=
417 par_language->rightToLeft()
419 // FIXME: I don't have a working copy of the Arabi package, so
420 // I'm not sure if the farsi and arabic_arabi stuff is correct
422 if (par_language->lang() == "farsi")
424 else if (outer_language->lang() == "farsi")
426 else if (par_language->lang() == "arabic_arabi")
428 else if (outer_language->lang() == "arabic_arabi")
430 // remaining RTL languages currently is hebrew
431 else if (par_language->rightToLeft())
436 // With CJK, the CJK tag has to be closed first (see below)
437 if (runparams.encoding->package() != Encoding::CJK) {
438 os << from_ascii(subst(
439 lyxrc.language_command_begin,
441 par_language->babel()))
442 // the '%' is necessary to prevent unwanted whitespace
449 // Switch file encoding if necessary; no need to do this for "default"
450 // encoding, since this only affects the position of the outputted
451 // \inputencoding command; the encoding switch will occur when necessary
452 if (bparams.inputenc == "auto" &&
453 runparams.encoding->package() != Encoding::none) {
454 // Look ahead for future encoding changes.
455 // We try to output them at the beginning of the paragraph,
456 // since the \inputencoding command is not allowed e.g. in
458 for (pos_type i = 0; i < pit->size(); ++i) {
459 char_type const c = pit->getChar(i);
460 Encoding const * const encoding =
461 pit->getFontSettings(bparams, i).language()->encoding();
462 if (encoding->package() != Encoding::CJK &&
463 runparams.encoding->package() == Encoding::inputenc &&
468 // All characters before c are in the ASCII range, and
469 // c is non-ASCII (but no inset), so change the
470 // encoding to that required by the language of c.
471 // With CJK, only add switch if we have CJK content at the beginning
473 if (encoding->package() != Encoding::CJK || i == 0) {
474 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
476 // the following is necessary after a CJK environment in a multilingual
477 // context (nesting issue).
478 if (par_language->encoding()->package() == Encoding::CJK &&
479 open_encoding_ != CJK && cjk_inherited_ == 0) {
480 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
481 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
482 open_encoding_ = CJK;
485 if (encoding->package() != Encoding::none && enc_switch.first) {
486 if (enc_switch.second > 0) {
487 // the '%' is necessary to prevent unwanted whitespace
491 // With CJK, the CJK tag had to be closed first (see above)
492 if (runparams.encoding->package() == Encoding::CJK) {
493 os << from_ascii(subst(
494 lyxrc.language_command_begin,
496 par_language->babel()))
497 // the '%' is necessary to prevent unwanted whitespace
501 runparams.encoding = encoding;
508 Encoding const * const prev_encoding = runparams.encoding;
510 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
511 if (pit->allowParagraphCustomization()) {
512 if (pit->params().startOfAppendix()) {
513 os << "\\appendix\n";
517 if (!pit->params().spacing().isDefault()
518 && (pit == paragraphs.begin()
519 || !priorpit->hasSameLayout(*pit)))
521 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
526 if (style.isCommand()) {
532 switch (style.latextype) {
534 os << '\\' << from_ascii(style.latexname());
536 // Separate handling of optional argument inset.
537 if (style.optionalargs > 0) {
538 int ret = latexOptArgInsets(*pit, os, runparams,
546 os << from_ascii(style.latexparam());
548 case LATEX_ITEM_ENVIRONMENT:
549 case LATEX_LIST_ENVIRONMENT:
552 case LATEX_BIB_ENVIRONMENT:
553 // ignore this, the inset will write itself
559 Font const outerfont = text.outerFont(distance(paragraphs.begin(), pit));
562 os << from_utf8(everypar);
563 bool need_par = pit->latex(bparams, outerfont,
564 os, texrow, runparams, start_pos, end_pos);
566 // Make sure that \\par is done with the font of the last
567 // character if this has another size as the default.
568 // This is necessary because LaTeX (and LyX on the screen)
569 // calculates the space between the baselines according
570 // to this font. (Matthias)
572 // Is this really needed ? (Dekel)
573 // We do not need to use to change the font for the last paragraph
576 Font const font = pit->empty()
577 ? pit->getLayoutFont(bparams, outerfont)
578 : pit->getFont(bparams, pit->size() - 1, outerfont);
580 bool const is_command = style.isCommand();
582 if (style.resfont.size() != font.fontInfo().size()
583 && nextpit != paragraphs.end()
587 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
588 } else if (need_par) {
590 } else if (is_command) {
592 runparams.encoding = prev_encoding;
595 bool pending_newline = false;
596 switch (style.latextype) {
597 case LATEX_ITEM_ENVIRONMENT:
598 case LATEX_LIST_ENVIRONMENT:
599 if (nextpit != paragraphs.end()
600 && (pit->params().depth() < nextpit->params().depth()))
601 pending_newline = true;
603 case LATEX_ENVIRONMENT: {
604 // if its the last paragraph of the current environment
605 // skip it otherwise fall through
606 if (nextpit != paragraphs.end() &&
607 (nextpit->layout() != pit->layout()
608 || nextpit->params().depth() != pit->params().depth()))
612 // fall through possible
614 // we don't need it for the last paragraph!!!
615 if (nextpit != paragraphs.end())
616 pending_newline = true;
619 if (pit->allowParagraphCustomization()) {
620 if (!pit->params().spacing().isDefault()
621 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
623 if (pending_newline) {
627 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
628 pending_newline = true;
632 // Closing the language is needed for the last paragraph; it is also
633 // needed if we're within an \L or \R that we may have opened above (not
634 // necessarily in this paragraph) and are about to close.
635 bool closing_rtl_ltr_environment =
637 (par_language->lang() != "arabic_arabtex" &&
638 outer_language->lang() != "arabic_arabtex") &&
639 // have we opened and \L or \R environment?
640 runparams.local_font != 0 &&
641 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
642 // are we about to close the language?
643 ((nextpit != paragraphs.end() &&
644 par_language->babel() !=
645 (nextpit->getParLanguage(bparams))->babel()) ||
646 (nextpit == paragraphs.end() &&
647 par_language->babel() != outer_language->babel()));
649 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
650 && par_language->babel() != outer_language->babel())) {
651 // Since \selectlanguage write the language to the aux file,
652 // we need to reset the language at the end of footnote or
655 if (pending_newline) {
659 // when the paragraph uses CJK, the language has to be closed earlier
660 if (font.language()->encoding()->package() != Encoding::CJK) {
661 if (lyxrc.language_command_end.empty()) {
662 if (!prev_language->babel().empty()) {
663 os << from_ascii(subst(
664 lyxrc.language_command_begin,
666 prev_language->babel()));
667 pending_newline = true;
669 } else if (!par_language->babel().empty()) {
670 os << from_ascii(subst(
671 lyxrc.language_command_end,
673 par_language->babel()));
674 pending_newline = true;
678 if (closing_rtl_ltr_environment)
681 if (pending_newline) {
686 // if this is a CJK-paragraph and the next isn't, close CJK
687 // also if the next paragraph is a multilingual environment (because of nesting)
688 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
689 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
690 (nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams)))
691 // inbetween environments, CJK has to be closed later (nesting!)
692 && (!style.isEnvironment() || !nextpit->layout().isEnvironment())) {
693 os << "\\end{CJK}\n";
694 open_encoding_ = none;
697 // If this is the last paragraph, close the CJK environment
698 // if necessary. If it's an environment, we'll have to \end that first.
699 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
700 switch (open_encoding_) {
702 // do nothing at the end of child documents
703 if (maintext && buf.masterBuffer() != &buf)
709 os << "\\end{CJK}\n";
714 open_encoding_ = none;
719 open_encoding_ = none;
729 // If this is the last paragraph, and a local_font was set upon entering
730 // the inset, and we're using "auto" or "default" encoding, the encoding
731 // should be set back to that local_font's encoding.
732 // However, do not change the encoding when XeTeX is used.
733 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
734 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
735 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
736 && (!bparams.useXetex)) {
737 runparams_in.encoding = runparams_in.local_font->language()->encoding();
738 os << setEncoding(runparams_in.encoding->iconvName());
740 // Otherwise, the current encoding should be set for the next paragraph.
742 runparams_in.encoding = runparams.encoding;
745 // we don't need a newline for the last paragraph!!!
746 // Note from JMarc: we will re-add a \n explicitly in
747 // TeXEnvironment, because it is needed in this case
748 if (nextpit != paragraphs.end()) {
749 Layout const & next_layout = nextpit->layout();
750 if (style == next_layout
751 // no blank lines before environments!
752 || !next_layout.isEnvironment()
753 // unless there's a depth change
754 // FIXME What we really want to do here is put every \begin and \end
755 // tag on a new line (which was not the case with nested environments).
756 // But in the present state of play, we don't have access to the
757 // information whether the current TeX row is empty or not.
758 // For some ideas about how to fix this, see this thread:
759 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
760 || nextpit->params().depth() != pit->params().depth()) {
766 if (nextpit != paragraphs.end())
767 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*nextpit);
773 // LaTeX all paragraphs
774 void latexParagraphs(Buffer const & buf,
778 OutputParams const & runparams,
779 string const & everypar)
781 bool was_title = false;
782 bool already_title = false;
783 BufferParams const & bparams = buf.params();
784 DocumentClass const & tclass = bparams.documentClass();
785 ParagraphList const & paragraphs = text.paragraphs();
786 ParagraphList::const_iterator par = paragraphs.begin();
787 ParagraphList::const_iterator endpar = paragraphs.end();
789 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
790 // if only part of the paragraphs will be outputed
791 if (runparams.par_begin != runparams.par_end) {
792 par = boost::next(paragraphs.begin(), runparams.par_begin);
793 endpar = boost::next(paragraphs.begin(), runparams.par_end);
794 // runparams will be passed to nested paragraphs, so
795 // we have to reset the range parameters.
796 const_cast<OutputParams&>(runparams).par_begin = 0;
797 const_cast<OutputParams&>(runparams).par_end = 0;
800 bool const maintext = text.isMainText();
801 bool const is_child = buf.masterBuffer() != &buf;
803 // Open a CJK environment at the beginning of the main buffer
804 // if the document's language is a CJK language
805 // (but not in child documents)
806 if (maintext && !is_child
807 && bparams.encoding().package() == Encoding::CJK) {
808 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
809 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
811 open_encoding_ = CJK;
813 // if "auto begin" is switched off, explicitely switch the
814 // language on at start
815 if (maintext && !lyxrc.language_auto_begin &&
816 !bparams.language->babel().empty()) {
818 os << from_utf8(subst(lyxrc.language_command_begin,
820 bparams.language->babel()))
825 ParagraphList::const_iterator lastpar;
827 while (par != endpar) {
829 // FIXME This check should not be needed. We should
830 // perhaps issue an error if it is.
831 Layout const & layout = text.inset().forcePlainLayout() ?
832 tclass.plainLayout() : par->layout();
834 if (layout.intitle) {
836 lyxerr << "Error in latexParagraphs: You"
837 " should not mix title layouts"
838 " with normal ones." << endl;
839 } else if (!was_title) {
841 if (tclass.titletype() == TITLE_ENVIRONMENT) {
843 << from_ascii(tclass.titlename())
848 } else if (was_title && !already_title) {
849 if (tclass.titletype() == TITLE_ENVIRONMENT) {
850 os << "\\end{" << from_ascii(tclass.titlename())
854 os << "\\" << from_ascii(tclass.titlename())
858 already_title = true;
862 if (layout.isEnvironment() ||
863 !par->params().leftIndent().zero()) {
864 par = TeXEnvironment(buf, text, par, os,
867 par = TeXOnePar(buf, text, par, os, texrow,
868 runparams, everypar);
870 if (distance(lastpar, par) >= distance(lastpar, endpar))
874 // It might be that we only have a title in this document
875 if (was_title && !already_title) {
876 if (tclass.titletype() == TITLE_ENVIRONMENT) {
877 os << "\\end{" << from_ascii(tclass.titlename())
881 os << "\\" << from_ascii(tclass.titlename())
887 // if "auto end" is switched off, explicitely close the language at the end
888 // but only if the last par is in a babel language
889 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
890 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
891 os << from_utf8(subst(lyxrc.language_command_end,
893 bparams.language->babel()))
898 // If the last paragraph is an environment, we'll have to close
899 // CJK at the very end to do proper nesting.
900 if (maintext && !is_child && open_encoding_ == CJK) {
901 os << "\\end{CJK}\n";
903 open_encoding_ = none;
906 // reset inherited encoding
907 if (cjk_inherited_ > 0) {
909 if (cjk_inherited_ == 0)
910 open_encoding_ = CJK;
915 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
916 OutputParams const & runparams, Encoding const & newEnc,
919 Encoding const & oldEnc = *runparams.encoding;
920 bool moving_arg = runparams.moving_arg;
921 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
923 return make_pair(false, 0);
925 // Do nothing if the encoding is unchanged.
926 if (oldEnc.name() == newEnc.name())
927 return make_pair(false, 0);
929 // FIXME We ignore encoding switches from/to encodings that do
930 // neither support the inputenc package nor the CJK package here.
931 // This does of course only work in special cases (e.g. switch from
932 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
933 // but it is the best we can do
934 if (oldEnc.package() == Encoding::none
935 || newEnc.package() == Encoding::none)
936 return make_pair(false, 0);
938 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
939 << oldEnc.name() << " to " << newEnc.name());
940 os << setEncoding(newEnc.iconvName());
941 if (bparams.inputenc == "default")
942 return make_pair(true, 0);
944 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
945 switch (newEnc.package()) {
947 case Encoding::japanese:
948 // shouldn't ever reach here, see above
949 return make_pair(true, 0);
950 case Encoding::inputenc: {
951 int count = inputenc_arg.length();
952 if (oldEnc.package() == Encoding::CJK &&
953 open_encoding_ == CJK) {
955 open_encoding_ = none;
958 else if (oldEnc.package() == Encoding::inputenc &&
959 open_encoding_ == inputenc) {
961 open_encoding_ = none;
964 if (runparams.local_font != 0
965 && oldEnc.package() == Encoding::CJK) {
966 // within insets, \inputenc switches need
967 // to be embraced within \bgroup...\egroup;
971 open_encoding_ = inputenc;
973 // with the japanese option, inputenc is omitted.
974 if (runparams.use_japanese)
975 return make_pair(true, count);
976 os << "\\inputencoding{" << inputenc_arg << '}';
977 return make_pair(true, count + 16);
979 case Encoding::CJK: {
980 int count = inputenc_arg.length();
981 if (oldEnc.package() == Encoding::CJK &&
982 open_encoding_ == CJK) {
986 if (oldEnc.package() == Encoding::inputenc &&
987 open_encoding_ == inputenc) {
991 os << "\\begin{CJK}{" << inputenc_arg << "}{"
992 << from_ascii(bparams.fontsCJK) << "}";
993 open_encoding_ = CJK;
994 return make_pair(true, count + 15);
997 // Dead code to avoid a warning:
998 return make_pair(true, 0);