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) {
172 if (pit->params().labelWidthString().empty())
173 os << '{' << bibitemWidest(buf) << "}\n";
176 << pit->params().labelWidthString()
179 os << from_ascii(style.latexparam()) << '\n';
183 // in multilingual environments, the CJK tags have to be nested properly
184 bool cjk_nested = false;
185 if (par_language->encoding()->package() == Encoding::CJK &&
186 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
187 if (prev_par_language->encoding()->package() == Encoding::CJK)
188 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
189 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
190 open_encoding_ = CJK;
195 ParagraphList::const_iterator par = pit;
197 par = TeXOnePar(buf, text, par, os, texrow, runparams);
199 if (par == paragraphs.end()) {
200 // Make sure that the last paragraph is
201 // correctly terminated (because TeXOnePar does
202 // not add a \n in this case)
205 } else if (par->params().depth() > pit->params().depth()) {
206 if (par->layout().isParagraph()) {
208 // How to handle this? (Lgb)
209 //&& !suffixIs(os, "\n\n")
211 // There should be at least one '\n' already
212 // but we need there to be two for Standard
213 // paragraphs that are depth-increment'ed to be
214 // output correctly. However, tables can
215 // also be paragraphs so don't adjust them.
218 // Will it ever harm to have one '\n' too
219 // many? i.e. that we sometimes will have
220 // three in a row. (Lgb)
224 par = TeXDeeper(buf, text, par, os, texrow,
227 } while (par != paragraphs.end()
228 && par->layout() == pit->layout()
229 && par->params().depth() == pit->params().depth()
230 && par->params().leftIndent() == pit->params().leftIndent());
232 if (open_encoding_ == CJK && cjk_nested) {
233 // We need to close the encoding even if it does not change
234 // to do correct environment nesting
235 os << "\\end{CJK}\n";
237 open_encoding_ = none;
240 if (style.isEnvironment()) {
241 os << "\\end{" << from_ascii(style.latexname()) << "}\n";
245 if (leftindent_open) {
246 os << "\\end{LyXParagraphLeftIndent}\n";
250 if (par != paragraphs.end())
251 LYXERR(Debug::LATEX, "TeXEnvironment...done " << &*par);
259 int latexOptArgInsets(Paragraph const & par, odocstream & os,
260 OutputParams const & runparams, int number)
264 InsetList::const_iterator it = par.insetList().begin();
265 InsetList::const_iterator end = par.insetList().end();
266 for (; it != end && number > 0 ; ++it) {
267 if (it->inset->lyxCode() == OPTARG_CODE) {
269 static_cast<InsetOptArg *>(it->inset);
270 lines += ins->latexOptional(os, runparams);
277 // FIXME: this should be anonymous
278 ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
280 ParagraphList::const_iterator const pit,
281 odocstream & os, TexRow & texrow,
282 OutputParams const & runparams_in,
283 string const & everypar,
284 int start_pos, int end_pos)
286 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
288 BufferParams const & bparams = buf.params();
289 ParagraphList const & paragraphs = text.paragraphs();
291 ParagraphList::const_iterator const priorpit =
292 pit == paragraphs.begin() ? pit : boost::prior(pit);
293 ParagraphList::const_iterator const nextpit =
294 pit == paragraphs.end() ? pit : boost::next(pit);
296 OutputParams runparams = runparams_in;
297 runparams.isLastPar = nextpit == paragraphs.end();
299 if (runparams.verbatim) {
300 int const dist = distance(paragraphs.begin(), pit);
301 Font const outerfont = outerFont(dist, paragraphs);
303 // No newline if only one paragraph in this lyxtext
309 /*bool need_par = */ pit->latex(bparams, outerfont,
310 os, texrow, runparams, start_pos, end_pos);
314 // FIXME This check should not really be needed.
315 // Perhaps we should issue an error if it is.
316 Layout const style = pit->forcePlainLayout() ?
317 bparams.documentClass().plainLayout() : pit->layout();
319 runparams.moving_arg |= style.needprotect;
321 bool const maintext = text.isMainText(buf);
322 // we are at the beginning of an inset and CJK is already open;
323 // we count inheritation levels to get the inset nesting right.
324 if (pit == paragraphs.begin() && !maintext
325 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
327 open_encoding_ = none;
330 // This paragraph's language
331 Language const * const par_language = pit->getParLanguage(bparams);
332 // The document's language
333 Language const * const doc_language = bparams.language;
334 // The language that was in effect when the environment this paragraph is
335 // inside of was opened
336 Language const * const outer_language =
337 (runparams.local_font != 0) ?
338 runparams.local_font->language() : doc_language;
339 // The previous language that was in effect is either the language of
340 // the previous paragraph, if there is one, or else the outer language
341 // if there is no previous paragraph
342 Language const * prev_language =
343 (pit != paragraphs.begin()) ?
344 priorpit->getParLanguage(bparams) : outer_language;
346 // When the language is changed at the very start of a LyX environment,
347 // the language switch in the LaTeX code occurs just before switching to
348 // the corresponding LaTeX environment. So, when the environment ends,
349 // we have to restore the language that was in effect.
350 bool env_lang_switch = false;
351 if (!priorpit->hasSameLayout(*pit)) {
352 ParagraphList::const_iterator outpit = priorpit;
353 while (outpit != paragraphs.begin()
354 && (outpit->hasSameLayout(*priorpit)
355 || outpit->getDepth() > priorpit->getDepth()))
356 outpit = boost::prior(outpit);
357 ParagraphList::const_iterator const inpit = boost::next(outpit);
358 Language const * const outenv_language
359 = outpit->getParLanguage(bparams);
360 Language const * const inenv_language
361 = inpit->getParLanguage(bparams);
362 if (outenv_language->babel() != inenv_language->babel())
363 env_lang_switch = true;
364 if (outenv_language->babel() != par_language->babel())
365 prev_language = outenv_language;
368 if ((par_language->babel() != prev_language->babel() || env_lang_switch)
369 // check if we already put language command in TeXEnvironment()
370 && !(style.isEnvironment()
371 && (pit == paragraphs.begin() ||
372 (priorpit->layout() != pit->layout() &&
373 priorpit->getDepth() <= pit->getDepth())
374 || priorpit->getDepth() < pit->getDepth())))
376 if (!lyxrc.language_command_end.empty() &&
377 prev_language->babel() != outer_language->babel() &&
378 !prev_language->babel().empty())
380 os << from_ascii(subst(lyxrc.language_command_end,
382 prev_language->babel()))
383 // the '%' is necessary to prevent unwanted whitespace
388 // We need to open a new language if we couldn't close the previous
389 // one (because there's no language_command_end); and even if we closed
390 // the previous one, if the current language is different than the
391 // outer_language (which is currently in effect once the previous one
393 if ((lyxrc.language_command_end.empty() ||
394 par_language->babel() != outer_language->babel()) &&
395 !par_language->babel().empty()) {
396 // If we're inside an inset, and that inset is within an \L or \R
397 // (or equivalents), then within the inset, too, any opposite
398 // language paragraph should appear within an \L or \R (in addition
399 // to, outside of, the normal language switch commands).
400 // This behavior is not correct for ArabTeX, though.
401 if ( // not for ArabTeX
402 (par_language->lang() != "arabic_arabtex" &&
403 outer_language->lang() != "arabic_arabtex") &&
404 // are we in an inset?
405 runparams.local_font != 0 &&
406 // is the inset within an \L or \R?
408 // FIXME: currently, we don't check this; this means that
409 // we'll have unnnecessary \L and \R commands, but that
410 // doesn't seem to hurt (though latex will complain)
412 // is this paragraph in the opposite direction?
413 runparams.local_font->isRightToLeft() !=
414 par_language->rightToLeft()
416 // FIXME: I don't have a working copy of the Arabi package, so
417 // I'm not sure if the farsi and arabic_arabi stuff is correct
419 if (par_language->lang() == "farsi")
421 else if (outer_language->lang() == "farsi")
423 else if (par_language->lang() == "arabic_arabi")
425 else if (outer_language->lang() == "arabic_arabi")
427 // remaining RTL languages currently is hebrew
428 else if (par_language->rightToLeft())
433 // With CJK, the CJK tag has to be closed first (see below)
434 if (runparams.encoding->package() != Encoding::CJK) {
435 os << from_ascii(subst(
436 lyxrc.language_command_begin,
438 par_language->babel()))
439 // the '%' is necessary to prevent unwanted whitespace
446 // Switch file encoding if necessary; no need to do this for "default"
447 // encoding, since this only affects the position of the outputted
448 // \inputencoding command; the encoding switch will occur when necessary
449 if (bparams.inputenc == "auto" &&
450 runparams.encoding->package() != Encoding::none) {
451 // Look ahead for future encoding changes.
452 // We try to output them at the beginning of the paragraph,
453 // since the \inputencoding command is not allowed e.g. in
455 for (pos_type i = 0; i < pit->size(); ++i) {
456 char_type const c = pit->getChar(i);
457 Encoding const * const encoding =
458 pit->getFontSettings(bparams, i).language()->encoding();
459 if (encoding->package() != Encoding::CJK &&
460 runparams.encoding->package() == Encoding::inputenc &&
465 // All characters before c are in the ASCII range, and
466 // c is non-ASCII (but no inset), so change the
467 // encoding to that required by the language of c.
468 // With CJK, only add switch if we have CJK content at the beginning
470 if (encoding->package() != Encoding::CJK || i == 0) {
471 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
473 // the following is necessary after a CJK environment in a multilingual
474 // context (nesting issue).
475 if (par_language->encoding()->package() == Encoding::CJK &&
476 open_encoding_ != CJK && cjk_inherited_ == 0) {
477 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
478 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
479 open_encoding_ = CJK;
482 if (encoding->package() != Encoding::none && enc_switch.first) {
483 if (enc_switch.second > 0) {
484 // the '%' is necessary to prevent unwanted whitespace
488 // With CJK, the CJK tag had to be closed first (see above)
489 if (runparams.encoding->package() == Encoding::CJK) {
490 os << from_ascii(subst(
491 lyxrc.language_command_begin,
493 par_language->babel()))
494 // the '%' is necessary to prevent unwanted whitespace
498 runparams.encoding = encoding;
505 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
506 if (pit->allowParagraphCustomization()) {
507 if (pit->params().startOfAppendix()) {
508 os << "\\appendix\n";
512 if (!pit->params().spacing().isDefault()
513 && (pit == paragraphs.begin()
514 || !priorpit->hasSameLayout(*pit)))
516 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
521 if (style.isCommand()) {
527 switch (style.latextype) {
529 os << '\\' << from_ascii(style.latexname());
531 // Separate handling of optional argument inset.
532 if (style.optionalargs > 0) {
533 int ret = latexOptArgInsets(*pit, os, runparams,
541 os << from_ascii(style.latexparam());
543 case LATEX_ITEM_ENVIRONMENT:
544 case LATEX_LIST_ENVIRONMENT:
547 case LATEX_BIB_ENVIRONMENT:
548 // ignore this, the inset will write itself
554 Font const outerfont = outerFont(distance(paragraphs.begin(), pit),
558 os << from_utf8(everypar);
559 bool need_par = pit->latex(bparams, outerfont,
560 os, texrow, runparams, start_pos, end_pos);
562 // Make sure that \\par is done with the font of the last
563 // character if this has another size as the default.
564 // This is necessary because LaTeX (and LyX on the screen)
565 // calculates the space between the baselines according
566 // to this font. (Matthias)
568 // Is this really needed ? (Dekel)
569 // We do not need to use to change the font for the last paragraph
572 Font const font = pit->empty()
573 ? pit->getLayoutFont(bparams, outerfont)
574 : pit->getFont(bparams, pit->size() - 1, outerfont);
576 bool const is_command = style.isCommand();
578 if (style.resfont.size() != font.fontInfo().size()
579 && nextpit != paragraphs.end()
583 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
584 } else if (need_par) {
586 } else if (is_command)
589 bool pending_newline = false;
590 switch (style.latextype) {
591 case LATEX_ITEM_ENVIRONMENT:
592 case LATEX_LIST_ENVIRONMENT:
593 if (nextpit != paragraphs.end()
594 && (pit->params().depth() < nextpit->params().depth()))
595 pending_newline = true;
597 case LATEX_ENVIRONMENT: {
598 // if its the last paragraph of the current environment
599 // skip it otherwise fall through
600 if (nextpit != paragraphs.end() &&
601 (nextpit->layout() != pit->layout()
602 || nextpit->params().depth() != pit->params().depth()))
606 // fall through possible
608 // we don't need it for the last paragraph!!!
609 if (nextpit != paragraphs.end())
610 pending_newline = true;
613 if (pit->allowParagraphCustomization()) {
614 if (!pit->params().spacing().isDefault()
615 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
617 if (pending_newline) {
621 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
622 pending_newline = true;
626 // Closing the language is needed for the last paragraph; it is also
627 // needed if we're within an \L or \R that we may have opened above (not
628 // necessarily in this paragraph) and are about to close.
629 bool closing_rtl_ltr_environment =
631 (par_language->lang() != "arabic_arabtex" &&
632 outer_language->lang() != "arabic_arabtex") &&
633 // have we opened and \L or \R environment?
634 runparams.local_font != 0 &&
635 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
636 // are we about to close the language?
637 ((nextpit != paragraphs.end() &&
638 par_language->babel() !=
639 (nextpit->getParLanguage(bparams))->babel()) ||
640 (nextpit == paragraphs.end() &&
641 par_language->babel() != outer_language->babel()));
643 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
644 && par_language->babel() != outer_language->babel())) {
645 // Since \selectlanguage write the language to the aux file,
646 // we need to reset the language at the end of footnote or
649 if (pending_newline) {
653 // when the paragraph uses CJK, the language has to be closed earlier
654 if (font.language()->encoding()->package() != Encoding::CJK) {
655 if (lyxrc.language_command_end.empty()) {
656 if (!prev_language->babel().empty()) {
657 os << from_ascii(subst(
658 lyxrc.language_command_begin,
660 prev_language->babel()));
661 pending_newline = true;
663 } else if (!par_language->babel().empty()) {
664 os << from_ascii(subst(
665 lyxrc.language_command_end,
667 par_language->babel()));
668 pending_newline = true;
672 if (closing_rtl_ltr_environment)
675 if (pending_newline) {
680 // if this is a CJK-paragraph and the next isn't, close CJK
681 // also if the next paragraph is a multilingual environment (because of nesting)
682 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
683 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
684 (nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams)))
685 // inbetween environments, CJK has to be closed later (nesting!)
686 && (!style.isEnvironment() || !nextpit->layout().isEnvironment())) {
687 os << "\\end{CJK}\n";
688 open_encoding_ = none;
691 // If this is the last paragraph, close the CJK environment
692 // if necessary. If it's an environment, we'll have to \end that first.
693 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
694 switch (open_encoding_) {
696 // do nothing at the end of child documents
697 if (maintext && buf.masterBuffer() != &buf)
703 os << "\\end{CJK}\n";
708 open_encoding_ = none;
713 open_encoding_ = none;
723 // If this is the last paragraph, and a local_font was set upon entering
724 // the inset, and we're using "auto" or "default" encoding, the encoding
725 // should be set back to that local_font's encoding.
726 // However, do not change the encoding when XeTeX is used.
727 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
728 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
729 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
730 && (!bparams.useXetex)) {
731 runparams_in.encoding = runparams_in.local_font->language()->encoding();
732 os << setEncoding(runparams_in.encoding->iconvName());
734 // Otherwise, the current encoding should be set for the next paragraph.
736 runparams_in.encoding = runparams.encoding;
739 // we don't need a newline for the last paragraph!!!
740 // Note from JMarc: we will re-add a \n explicitly in
741 // TeXEnvironment, because it is needed in this case
742 if (nextpit != paragraphs.end()) {
743 Layout const & next_layout = nextpit->layout();
744 if (style == next_layout
745 // no blank lines before environments!
746 || !next_layout.isEnvironment()
747 // unless there's a depth change
748 // FIXME What we really want to do here is put every \begin and \end
749 // tag on a new line (which was not the case with nested environments).
750 // But in the present state of play, we don't have access to the
751 // information whether the current TeX row is empty or not.
752 // For some ideas about how to fix this, see this thread:
753 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
754 || nextpit->params().depth() != pit->params().depth()) {
760 if (nextpit != paragraphs.end())
761 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*nextpit);
767 // LaTeX all paragraphs
768 void latexParagraphs(Buffer const & buf,
772 OutputParams const & runparams,
773 string const & everypar)
775 bool was_title = false;
776 bool already_title = false;
777 BufferParams const & bparams = buf.params();
778 DocumentClass const & tclass = bparams.documentClass();
779 ParagraphList const & paragraphs = text.paragraphs();
780 ParagraphList::const_iterator par = paragraphs.begin();
781 ParagraphList::const_iterator endpar = paragraphs.end();
783 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
784 // if only part of the paragraphs will be outputed
785 if (runparams.par_begin != runparams.par_end) {
786 par = boost::next(paragraphs.begin(), runparams.par_begin);
787 endpar = boost::next(paragraphs.begin(), runparams.par_end);
788 // runparams will be passed to nested paragraphs, so
789 // we have to reset the range parameters.
790 const_cast<OutputParams&>(runparams).par_begin = 0;
791 const_cast<OutputParams&>(runparams).par_end = 0;
794 bool const maintext = text.isMainText(buf);
795 bool const is_child = buf.masterBuffer() != &buf;
797 // Open a CJK environment at the beginning of the main buffer
798 // if the document's language is a CJK language
799 // (but not in child documents)
800 if (maintext && !is_child
801 && bparams.encoding().package() == Encoding::CJK) {
802 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
803 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
805 open_encoding_ = CJK;
807 // if "auto begin" is switched off, explicitely switch the
808 // language on at start
809 if (maintext && !lyxrc.language_auto_begin &&
810 !bparams.language->babel().empty()) {
812 os << from_utf8(subst(lyxrc.language_command_begin,
814 bparams.language->babel()))
819 ParagraphList::const_iterator lastpar;
821 while (par != endpar) {
823 // FIXME This check should not be needed. We should
824 // perhaps issue an error if it is.
825 Layout const & layout = par->forcePlainLayout() ?
826 tclass.plainLayout() : par->layout();
828 if (layout.intitle) {
830 lyxerr << "Error in latexParagraphs: You"
831 " should not mix title layouts"
832 " with normal ones." << endl;
833 } else if (!was_title) {
835 if (tclass.titletype() == TITLE_ENVIRONMENT) {
837 << from_ascii(tclass.titlename())
842 } else if (was_title && !already_title) {
843 if (tclass.titletype() == TITLE_ENVIRONMENT) {
844 os << "\\end{" << from_ascii(tclass.titlename())
848 os << "\\" << from_ascii(tclass.titlename())
852 already_title = true;
856 if (layout.isEnvironment() ||
857 !par->params().leftIndent().zero()) {
858 par = TeXEnvironment(buf, text, par, os,
861 par = TeXOnePar(buf, text, par, os, texrow,
862 runparams, everypar);
864 if (distance(lastpar, par) >= distance(lastpar, endpar))
868 // It might be that we only have a title in this document
869 if (was_title && !already_title) {
870 if (tclass.titletype() == TITLE_ENVIRONMENT) {
871 os << "\\end{" << from_ascii(tclass.titlename())
875 os << "\\" << from_ascii(tclass.titlename())
881 // if "auto end" is switched off, explicitely close the language at the end
882 // but only if the last par is in a babel language
883 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
884 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
885 os << from_utf8(subst(lyxrc.language_command_end,
887 bparams.language->babel()))
892 // If the last paragraph is an environment, we'll have to close
893 // CJK at the very end to do proper nesting.
894 if (maintext && !is_child && open_encoding_ == CJK) {
895 os << "\\end{CJK}\n";
897 open_encoding_ = none;
900 // reset inherited encoding
901 if (cjk_inherited_ > 0) {
903 if (cjk_inherited_ == 0)
904 open_encoding_ = CJK;
909 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
910 OutputParams const & runparams, Encoding const & newEnc,
913 Encoding const & oldEnc = *runparams.encoding;
914 bool moving_arg = runparams.moving_arg;
915 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
917 return make_pair(false, 0);
919 // Do nothing if the encoding is unchanged.
920 if (oldEnc.name() == newEnc.name())
921 return make_pair(false, 0);
923 // FIXME We ignore encoding switches from/to encodings that do
924 // neither support the inputenc package nor the CJK package here.
925 // This does of course only work in special cases (e.g. switch from
926 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
927 // but it is the best we can do
928 if (oldEnc.package() == Encoding::none
929 || newEnc.package() == Encoding::none)
930 return make_pair(false, 0);
932 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
933 << oldEnc.name() << " to " << newEnc.name());
934 os << setEncoding(newEnc.iconvName());
935 if (bparams.inputenc == "default")
936 return make_pair(true, 0);
938 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
939 switch (newEnc.package()) {
941 case Encoding::japanese:
942 // shouldn't ever reach here, see above
943 return make_pair(true, 0);
944 case Encoding::inputenc: {
945 int count = inputenc_arg.length();
946 if (oldEnc.package() == Encoding::CJK &&
947 open_encoding_ == CJK) {
949 open_encoding_ = none;
952 else if (oldEnc.package() == Encoding::inputenc &&
953 open_encoding_ == inputenc) {
955 open_encoding_ = none;
958 if (runparams.local_font != 0
959 && oldEnc.package() == Encoding::CJK) {
960 // within insets, \inputenc switches need
961 // to be embraced within \bgroup...\egroup;
965 open_encoding_ = inputenc;
967 // with the japanese option, inputenc is omitted.
968 if (runparams.use_japanese)
969 return make_pair(true, count);
970 os << "\\inputencoding{" << inputenc_arg << '}';
971 return make_pair(true, count + 16);
973 case Encoding::CJK: {
974 int count = inputenc_arg.length();
975 if (oldEnc.package() == Encoding::CJK &&
976 open_encoding_ == CJK) {
980 if (oldEnc.package() == Encoding::inputenc &&
981 open_encoding_ == inputenc) {
985 os << "\\begin{CJK}{" << inputenc_arg << "}{"
986 << from_ascii(bparams.fontsCJK) << "}";
987 open_encoding_ = CJK;
988 return make_pair(true, count + 15);
991 // Dead code to avoid a warning:
992 return make_pair(true, 0);