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"
19 #include "InsetList.h"
23 #include "OutputParams.h"
24 #include "Paragraph.h"
25 #include "ParagraphParameters.h"
26 #include "TextClass.h"
29 #include "insets/InsetBibitem.h"
30 #include "insets/InsetArgument.h"
32 #include "support/lassert.h"
33 #include "support/debug.h"
34 #include "support/lstrings.h"
36 #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 string const getPolyglossiaEnvName(Language const * lang)
60 string result = lang->polyglossia();
61 if (result == "arabic")
62 // exceptional spelling; see polyglossia docs.
68 struct TeXEnvironmentData
72 Language const * par_language;
73 Encoding const * prev_encoding;
78 static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
80 ParagraphList::const_iterator pit,
82 OutputParams const & runparams)
84 TeXEnvironmentData data;
86 BufferParams const & bparams = buf.params();
88 // FIXME This test should not be necessary.
89 // We should perhaps issue an error if it is.
90 Layout const & style = text.inset().forcePlainLayout() ?
91 bparams.documentClass().plainLayout() : pit->layout();
93 ParagraphList const & paragraphs = text.paragraphs();
94 ParagraphList::const_iterator const priorpit =
95 pit == paragraphs.begin() ? pit : boost::prior(pit);
97 bool const use_prev_env_language = prev_env_language_ != 0
98 && priorpit->layout().isEnvironment()
99 && (priorpit->getDepth() > pit->getDepth()
100 || (priorpit->getDepth() == pit->getDepth()
101 && priorpit->layout() != pit->layout()));
103 data.prev_encoding = runparams.encoding;
104 data.par_language = pit->getParLanguage(bparams);
105 Language const * const doc_language = bparams.language;
106 Language const * const prev_par_language =
107 (pit != paragraphs.begin())
108 ? (use_prev_env_language ? prev_env_language_
109 : priorpit->getParLanguage(bparams))
112 bool const use_pg = runparams.use_polyglossia;
113 string const par_lang = use_pg ?
114 getPolyglossiaEnvName(data.par_language) : data.par_language->babel();
115 string const prev_par_lang = use_pg ?
116 getPolyglossiaEnvName(prev_par_language) : prev_par_language->babel();
117 string const doc_lang = use_pg ?
118 getPolyglossiaEnvName(doc_language) : doc_language->babel();
119 string const lang_begin_command = use_pg ?
120 "\\begin{$$lang}" : lyxrc.language_command_begin;
121 string const lang_end_command = use_pg ?
122 "\\end{$$lang}" : lyxrc.language_command_end;
124 if (par_lang != prev_par_lang) {
125 if (!lang_end_command.empty() &&
126 prev_par_lang != doc_lang &&
127 !prev_par_lang.empty()) {
128 os << from_ascii(subst(
132 // the '%' is necessary to prevent unwanted whitespace
136 if ((lang_end_command.empty() ||
137 par_lang != doc_lang) &&
139 os << from_ascii(subst(
143 if (runparams.use_polyglossia
144 && !data.par_language->polyglossiaOpts().empty())
146 << from_ascii(data.par_language->polyglossiaOpts())
148 // the '%' is necessary to prevent unwanted whitespace
153 data.leftindent_open = false;
154 if (!pit->params().leftIndent().zero()) {
155 os << "\\begin{LyXParagraphLeftIndent}{"
156 << from_ascii(pit->params().leftIndent().asLatexString())
158 data.leftindent_open = true;
161 if (style.isEnvironment()) {
162 os << "\\begin{" << from_ascii(style.latexname()) << '}';
163 if (style.optargs != 0 || style.reqargs != 0)
164 latexArgInsets(*pit, os, runparams, style.reqargs, style.optargs);
165 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
167 << pit->params().labelWidthString()
169 } else if (style.labeltype == LABEL_BIBLIO) {
170 if (pit->params().labelWidthString().empty())
171 os << '{' << bibitemWidest(buf, runparams) << "}\n";
174 << pit->params().labelWidthString()
177 os << from_ascii(style.latexparam()) << '\n';
181 // in multilingual environments, the CJK tags have to be nested properly
182 data.cjk_nested = false;
183 if (data.par_language->encoding()->package() == Encoding::CJK &&
184 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
185 if (prev_par_language->encoding()->package() == Encoding::CJK)
186 os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
187 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
188 open_encoding_ = CJK;
189 data.cjk_nested = true;
195 static void finishEnvironment(otexstream & os, OutputParams const & runparams,
196 TeXEnvironmentData const & data)
198 if (open_encoding_ == CJK && data.cjk_nested) {
199 // We need to close the encoding even if it does not change
200 // to do correct environment nesting
201 os << "\\end{CJK}\n";
202 open_encoding_ = none;
205 if (data.style->isEnvironment()) {
206 os << "\\end{" << from_ascii(data.style->latexname()) << "}\n";
207 prev_env_language_ = data.par_language;
208 if (runparams.encoding != data.prev_encoding) {
209 runparams.encoding = data.prev_encoding;
210 if (!runparams.isFullUnicode())
211 os << setEncoding(data.prev_encoding->iconvName());
215 if (data.leftindent_open) {
216 os << "\\end{LyXParagraphLeftIndent}\n";
217 prev_env_language_ = data.par_language;
218 if (runparams.encoding != data.prev_encoding) {
219 runparams.encoding = data.prev_encoding;
220 if (!runparams.isFullUnicode())
221 os << setEncoding(data.prev_encoding->iconvName());
227 void TeXEnvironment(Buffer const & buf, Text const & text,
228 OutputParams const & runparams,
229 pit_type & pit, otexstream & os)
231 ParagraphList const & paragraphs = text.paragraphs();
232 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
233 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
235 Layout const & current_layout = par->layout();
236 depth_type const current_depth = par->params().depth();
237 Length const & current_left_indent = par->params().leftIndent();
239 // This is for debugging purpose at the end.
240 pit_type const par_begin = pit;
241 for (; pit < runparams.par_end; ++pit) {
242 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
244 // check first if this is an higher depth paragraph.
245 bool go_out = (par->params().depth() < current_depth);
246 if (par->params().depth() == current_depth) {
247 // This environment is finished.
248 go_out |= (par->layout() != current_layout);
249 go_out |= (par->params().leftIndent() != current_left_indent);
252 // nothing to do here, restore pit and go out.
257 if (par->layout() == current_layout
258 && par->params().depth() == current_depth
259 && par->params().leftIndent() == current_left_indent) {
260 // We are still in the same environment so TeXOnePar and continue;
261 TeXOnePar(buf, text, pit, os, runparams);
265 // We are now in a deeper environment.
266 // Either par->layout() != current_layout
267 // Or par->params().depth() > current_depth
268 // Or par->params().leftIndent() != current_left_indent)
270 if (par->layout().isParagraph()) {
271 // FIXME (Lgb): How to handle this?
272 //&& !suffixIs(os, "\n\n")
274 // (ARRae) There should be at least one '\n' already but we need there to
275 // be two for Standard paragraphs that are depth-increment'ed to be
276 // output correctly. However, tables can also be paragraphs so
277 // don't adjust them.
279 // FIXME (Lgb): Will it ever harm to have one '\n' too
280 // many? i.e. that we sometimes will have
285 // FIXME This test should not be necessary.
286 // We should perhaps issue an error if it is.
287 bool const force_plain_layout = text.inset().forcePlainLayout();
288 Layout const & style = force_plain_layout
289 ? buf.params().documentClass().plainLayout()
292 if (!style.isEnvironment()) {
293 // This is a standard paragraph, no need to call TeXEnvironment.
294 TeXOnePar(buf, text, pit, os, runparams);
298 // This is a new environment.
299 TeXEnvironmentData const data =
300 prepareEnvironment(buf, text, par, os, runparams);
301 // Recursive call to TeXEnvironment!
302 TeXEnvironment(buf, text, runparams, pit, os);
303 finishEnvironment(os, runparams, data);
306 if (pit != runparams.par_end)
307 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << par_begin << " done.");
313 void latexArgInsets(Paragraph const & par, otexstream & os,
314 OutputParams const & runparams, unsigned int reqargs,
315 unsigned int optargs)
317 unsigned int totalargs = reqargs + optargs;
318 list<InsetArgument const *> ilist;
320 InsetList::const_iterator it = par.insetList().begin();
321 InsetList::const_iterator end = par.insetList().end();
322 for (; it != end; ++it) {
323 if (it->inset->lyxCode() == ARG_CODE) {
324 if (ilist.size() >= totalargs) {
325 LYXERR0("WARNING: Found extra argument inset.");
328 InsetArgument const * ins =
329 static_cast<InsetArgument const *>(it->inset);
330 ilist.push_back(ins);
334 if (!reqargs && ilist.empty())
337 bool const have_optional_args = ilist.size() > reqargs;
338 if (have_optional_args) {
339 unsigned int todo = ilist.size() - reqargs;
340 for (unsigned int i = 0; i < todo; ++i) {
341 InsetArgument const * ins = ilist.front();
343 ins->latexArgument(os, runparams, true);
347 // we should now have no more insets than there are required
349 LASSERT(ilist.size() <= reqargs, /* */);
353 for (unsigned int i = 0; i < reqargs; ++i) {
355 // a required argument wasn't given, so we output {}
358 InsetArgument const * ins = ilist.front();
360 ins->latexArgument(os, runparams, false);
367 // output the proper paragraph start according to latextype.
368 void parStartCommand(Paragraph const & par, otexstream & os,
369 OutputParams const & runparams, Layout const & style)
371 switch (style.latextype) {
373 os << '\\' << from_ascii(style.latexname());
375 // Separate handling of optional argument inset.
376 if (style.optargs != 0 || style.reqargs != 0)
377 latexArgInsets(par, os, runparams, style.reqargs, style.optargs);
379 os << from_ascii(style.latexparam());
381 case LATEX_ITEM_ENVIRONMENT:
382 case LATEX_LIST_ENVIRONMENT:
385 case LATEX_BIB_ENVIRONMENT:
386 // ignore this, the inset will write itself
395 // FIXME: this should be anonymous
396 void TeXOnePar(Buffer const & buf,
400 OutputParams const & runparams_in,
401 string const & everypar,
402 int start_pos, int end_pos)
404 BufferParams const & bparams = buf.params();
405 ParagraphList const & paragraphs = text.paragraphs();
406 Paragraph const & par = paragraphs.at(pit);
407 // FIXME This check should not really be needed.
408 // Perhaps we should issue an error if it is.
409 Layout const style = text.inset().forcePlainLayout() ?
410 bparams.documentClass().plainLayout() : par.layout();
412 if (style.inpreamble)
415 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
418 OutputParams runparams = runparams_in;
419 runparams.isLastPar = (pit == pit_type(paragraphs.size() - 1));
420 // We reinitialze par begin and end to be on the safe side
421 // with embedded inset as we don't know if they set those
423 runparams.par_begin = 0;
424 runparams.par_end = 0;
426 bool const maintext = text.isMainText();
427 // we are at the beginning of an inset and CJK is already open;
428 // we count inheritation levels to get the inset nesting right.
429 if (pit == 0 && !maintext
430 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
432 open_encoding_ = none;
435 if (text.inset().getLayout().isPassThru()) {
436 Font const outerfont = text.outerFont(pit);
438 // No newline before first paragraph in this lyxtext
441 if (!text.inset().getLayout().parbreakIsNewline())
445 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
449 Paragraph const * nextpar = runparams.isLastPar
450 ? 0 : ¶graphs.at(pit + 1);
452 if (style.pass_thru) {
453 Font const outerfont = text.outerFont(pit);
454 parStartCommand(par, os, runparams, style);
456 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
458 // I did not create a parEndCommand for this minuscule
459 // task because in the other user of parStartCommand
460 // the code is different (JMarc)
461 if (style.isCommand())
465 if (!style.parbreak_is_newline) {
467 } else if (nextpar) {
468 Layout const nextstyle = text.inset().forcePlainLayout()
469 ? bparams.documentClass().plainLayout()
471 if (nextstyle.name() != style.name())
478 // This paragraph's language
479 Language const * const par_language = par.getParLanguage(bparams);
480 // The document's language
481 Language const * const doc_language = bparams.language;
482 // The language that was in effect when the environment this paragraph is
483 // inside of was opened
484 Language const * const outer_language =
485 (runparams.local_font != 0) ?
486 runparams.local_font->language() : doc_language;
488 Paragraph const * priorpar = (pit == 0) ? 0 : ¶graphs.at(pit - 1);
490 // The previous language that was in effect is the language of the
491 // previous paragraph, unless the previous paragraph is inside an
492 // environment with nesting depth greater than (or equal to, but with
493 // a different layout) the current one. If there is no previous
494 // paragraph, the previous language is the outer language.
495 bool const use_prev_env_language = prev_env_language_ != 0
497 && priorpar->layout().isEnvironment()
498 && (priorpar->getDepth() > par.getDepth()
499 || (priorpar->getDepth() == par.getDepth()
500 && priorpar->layout() != par.layout()));
501 Language const * const prev_language =
503 ? (use_prev_env_language ? prev_env_language_
504 : priorpar->getParLanguage(bparams))
508 bool const use_pg = runparams.use_polyglossia;
509 string const par_lang = use_pg ?
510 getPolyglossiaEnvName(par_language): par_language->babel();
511 string const prev_lang = use_pg ?
512 getPolyglossiaEnvName(prev_language) : prev_language->babel();
513 string const doc_lang = use_pg ?
514 getPolyglossiaEnvName(doc_language) : doc_language->babel();
515 string const outer_lang = use_pg ?
516 getPolyglossiaEnvName(outer_language) : outer_language->babel();
517 string const lang_begin_command = use_pg ?
518 "\\begin{$$lang}" : lyxrc.language_command_begin;
519 string const lang_end_command = use_pg ?
520 "\\end{$$lang}" : lyxrc.language_command_end;
522 if (par_lang != prev_lang
523 // check if we already put language command in TeXEnvironment()
524 && !(style.isEnvironment()
525 && (pit == 0 || (priorpar->layout() != par.layout()
526 && priorpar->getDepth() <= par.getDepth())
527 || priorpar->getDepth() < par.getDepth())))
529 if (!lang_end_command.empty() &&
530 prev_lang != outer_lang &&
533 os << from_ascii(subst(lang_end_command,
536 // the '%' is necessary to prevent unwanted whitespace
540 // We need to open a new language if we couldn't close the previous
541 // one (because there's no language_command_end); and even if we closed
542 // the previous one, if the current language is different than the
543 // outer_language (which is currently in effect once the previous one
545 if ((lang_end_command.empty() || par_lang != outer_lang)
546 && !par_lang.empty()) {
547 // If we're inside an inset, and that inset is within an \L or \R
548 // (or equivalents), then within the inset, too, any opposite
549 // language paragraph should appear within an \L or \R (in addition
550 // to, outside of, the normal language switch commands).
551 // This behavior is not correct for ArabTeX, though.
552 if (!runparams.use_polyglossia
554 && par_language->lang() != "arabic_arabtex"
555 && outer_language->lang() != "arabic_arabtex"
556 // are we in an inset?
557 && runparams.local_font != 0
558 // is the inset within an \L or \R?
560 // FIXME: currently, we don't check this; this means that
561 // we'll have unnnecessary \L and \R commands, but that
562 // doesn't seem to hurt (though latex will complain)
564 // is this paragraph in the opposite direction?
565 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()) {
566 // FIXME: I don't have a working copy of the Arabi package, so
567 // I'm not sure if the farsi and arabic_arabi stuff is correct
569 if (par_language->lang() == "farsi")
571 else if (outer_language->lang() == "farsi")
573 else if (par_language->lang() == "arabic_arabi")
575 else if (outer_language->lang() == "arabic_arabi")
577 // remaining RTL languages currently is hebrew
578 else if (par_language->rightToLeft())
583 // With CJK, the CJK tag has to be closed first (see below)
584 if (runparams.encoding->package() != Encoding::CJK) {
585 os << from_ascii(subst(
589 if (runparams.use_polyglossia
590 && !par_language->polyglossiaOpts().empty())
592 << from_ascii(par_language->polyglossiaOpts())
594 // the '%' is necessary to prevent unwanted whitespace
600 // Switch file encoding if necessary; no need to do this for "default"
601 // encoding, since this only affects the position of the outputted
602 // \inputencoding command; the encoding switch will occur when necessary
603 if (bparams.inputenc == "auto"
604 && runparams.encoding->package() != Encoding::none) {
605 // Look ahead for future encoding changes.
606 // We try to output them at the beginning of the paragraph,
607 // since the \inputencoding command is not allowed e.g. in
608 // sections. For this reason we only set runparams.moving_arg
609 // after checking for the encoding change, otherwise the
610 // change would be always avoided by switchEncoding().
611 for (pos_type i = 0; i < par.size(); ++i) {
612 char_type const c = par.getChar(i);
613 Encoding const * const encoding =
614 par.getFontSettings(bparams, i).language()->encoding();
615 if (encoding->package() != Encoding::CJK
616 && runparams.encoding->package() == Encoding::inputenc
621 // All characters before c are in the ASCII range, and
622 // c is non-ASCII (but no inset), so change the
623 // encoding to that required by the language of c.
624 // With CJK, only add switch if we have CJK content at the beginning
626 if (i != 0 && encoding->package() == Encoding::CJK)
629 pair<bool, int> enc_switch = switchEncoding(os.os(),
630 bparams, runparams, *encoding);
631 // the following is necessary after a CJK environment in a multilingual
632 // context (nesting issue).
633 if (par_language->encoding()->package() == Encoding::CJK
634 && open_encoding_ != CJK && cjk_inherited_ == 0) {
635 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
636 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
637 open_encoding_ = CJK;
639 if (encoding->package() != Encoding::none && enc_switch.first) {
640 if (enc_switch.second > 0) {
641 // the '%' is necessary to prevent unwanted whitespace
644 // With CJK, the CJK tag had to be closed first (see above)
645 if (runparams.encoding->package() == Encoding::CJK) {
646 os << from_ascii(subst(
650 // the '%' is necessary to prevent unwanted whitespace
653 runparams.encoding = encoding;
659 runparams.moving_arg |= style.needprotect;
660 Encoding const * const prev_encoding = runparams.encoding;
662 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
663 if (par.allowParagraphCustomization()) {
664 if (par.params().startOfAppendix()) {
665 os << "\\appendix\n";
668 if (!par.params().spacing().isDefault()
669 && (pit == 0 || !priorpar->hasSameLayout(par)))
671 os << from_ascii(par.params().spacing().writeEnvirBegin(useSetSpace))
675 if (style.isCommand()) {
680 parStartCommand(par, os, runparams, style);
682 Font const outerfont = text.outerFont(pit);
685 os << from_utf8(everypar);
686 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
688 // Make sure that \\par is done with the font of the last
689 // character if this has another size as the default.
690 // This is necessary because LaTeX (and LyX on the screen)
691 // calculates the space between the baselines according
692 // to this font. (Matthias)
694 // Is this really needed ? (Dekel)
695 // We do not need to use to change the font for the last paragraph
698 Font const font = par.empty()
699 ? par.getLayoutFont(bparams, outerfont)
700 : par.getFont(bparams, par.size() - 1, outerfont);
702 bool const is_command = style.isCommand();
704 if (style.resfont.size() != font.fontInfo().size()
708 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
709 } else if (is_command) {
711 if (runparams.encoding != prev_encoding) {
712 runparams.encoding = prev_encoding;
713 if (!runparams.isFullUnicode())
714 os << setEncoding(prev_encoding->iconvName());
718 bool pending_newline = false;
719 bool unskip_newline = false;
720 switch (style.latextype) {
721 case LATEX_ITEM_ENVIRONMENT:
722 case LATEX_LIST_ENVIRONMENT:
723 if (nextpar && (par.params().depth() < nextpar->params().depth()))
724 pending_newline = true;
726 case LATEX_ENVIRONMENT: {
727 // if its the last paragraph of the current environment
728 // skip it otherwise fall through
730 && (nextpar->layout() != par.layout()
731 || nextpar->params().depth() != par.params().depth()))
735 // fall through possible
737 // we don't need it for the last paragraph!!!
739 pending_newline = true;
742 if (par.allowParagraphCustomization()) {
743 if (!par.params().spacing().isDefault()
744 && (runparams.isLastPar || !nextpar->hasSameLayout(par))) {
747 os << from_ascii(par.params().spacing().writeEnvirEnd(useSetSpace));
748 pending_newline = true;
752 // Closing the language is needed for the last paragraph; it is also
753 // needed if we're within an \L or \R that we may have opened above (not
754 // necessarily in this paragraph) and are about to close.
755 bool closing_rtl_ltr_environment = !runparams.use_polyglossia
757 && (par_language->lang() != "arabic_arabtex"
758 && outer_language->lang() != "arabic_arabtex")
759 // have we opened and \L or \R environment?
760 && runparams.local_font != 0
761 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()
762 // are we about to close the language?
763 &&((nextpar && par_language->babel() != (nextpar->getParLanguage(bparams))->babel())
764 || (runparams.isLastPar && par_language->babel() != outer_language->babel()));
766 if (closing_rtl_ltr_environment
767 || (runparams.isLastPar && par_language->babel() != outer_language->babel())) {
768 // Since \selectlanguage write the language to the aux file,
769 // we need to reset the language at the end of footnote or
775 // when the paragraph uses CJK, the language has to be closed earlier
776 if (font.language()->encoding()->package() != Encoding::CJK) {
777 if (lang_end_command.empty()) {
778 // If this is a child, we should restore the
779 // master language after the last paragraph.
780 Language const * const current_language =
781 (runparams.isLastPar && runparams.master_language)
782 ? runparams.master_language
784 string const current_lang = runparams.use_polyglossia
785 ? getPolyglossiaEnvName(current_language)
786 : current_language->babel();
787 if (!current_lang.empty()) {
788 os << from_ascii(subst(
792 pending_newline = true;
793 unskip_newline = true;
795 } else if (!par_lang.empty()) {
796 os << from_ascii(subst(
800 pending_newline = true;
801 unskip_newline = true;
805 if (closing_rtl_ltr_environment)
808 if (pending_newline) {
810 // prevent unwanted whitespace
815 // if this is a CJK-paragraph and the next isn't, close CJK
816 // also if the next paragraph is a multilingual environment (because of nesting)
818 && open_encoding_ == CJK
819 && (nextpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK
820 || (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
821 // inbetween environments, CJK has to be closed later (nesting!)
822 && (!style.isEnvironment() || !nextpar->layout().isEnvironment())) {
823 os << "\\end{CJK}\n";
824 open_encoding_ = none;
827 // If this is the last paragraph, close the CJK environment
828 // if necessary. If it's an environment, we'll have to \end that first.
829 if (runparams.isLastPar && !style.isEnvironment()) {
830 switch (open_encoding_) {
832 // do nothing at the end of child documents
833 if (maintext && buf.masterBuffer() != &buf)
837 os << "\n\\end{CJK}\n";
841 open_encoding_ = none;
846 open_encoding_ = none;
856 // If this is the last paragraph, and a local_font was set upon entering
857 // the inset, and we're using "auto" or "default" encoding, the encoding
858 // should be set back to that local_font's encoding.
859 // However, do not change the encoding when a fully unicode aware backend
860 // such as XeTeX is used.
861 if (runparams.isLastPar && runparams_in.local_font != 0
862 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
863 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
864 && (!runparams.isFullUnicode())) {
865 runparams_in.encoding = runparams_in.local_font->language()->encoding();
866 os << setEncoding(runparams_in.encoding->iconvName());
868 // Otherwise, the current encoding should be set for the next paragraph.
870 runparams_in.encoding = runparams.encoding;
873 // we don't need a newline for the last paragraph!!!
874 // Note from JMarc: we will re-add a \n explicitly in
875 // TeXEnvironment, because it is needed in this case
877 Layout const & next_layout = nextpar->layout();
878 if (style == next_layout
879 // no blank lines before environments!
880 || !next_layout.isEnvironment()
881 // unless there's a depth change
882 // FIXME What we really want to do here is put every \begin and \end
883 // tag on a new line (which was not the case with nested environments).
884 // But in the present state of play, we don't have access to the
885 // information whether the current TeX row is empty or not.
886 // For some ideas about how to fix this, see this thread:
887 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
888 || nextpar->params().depth() != par.params().depth()) {
893 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " done; ptr "
894 << &par << " next " << nextpar);
900 // LaTeX all paragraphs
901 void latexParagraphs(Buffer const & buf,
904 OutputParams const & runparams,
905 string const & everypar)
907 BufferParams const & bparams = buf.params();
909 bool const maintext = text.isMainText();
910 bool const is_child = buf.masterBuffer() != &buf;
912 // Open a CJK environment at the beginning of the main buffer
913 // if the document's language is a CJK language
914 // (but not in child documents)
915 if (maintext && !is_child
916 && bparams.encoding().package() == Encoding::CJK) {
917 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
918 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
919 open_encoding_ = CJK;
921 // if "auto begin" is switched off, explicitly switch the
922 // language on at start
923 string const mainlang = runparams.use_polyglossia
924 ? getPolyglossiaEnvName(bparams.language)
925 : bparams.language->babel();
926 string const lang_begin_command = runparams.use_polyglossia ?
927 "\\begin{$$lang}" : lyxrc.language_command_begin;
929 if (maintext && !lyxrc.language_auto_begin &&
932 os << from_utf8(subst(lang_begin_command,
935 if (runparams.use_polyglossia
936 && !bparams.language->polyglossiaOpts().empty())
938 << from_ascii(bparams.language->polyglossiaOpts())
943 ParagraphList const & paragraphs = text.paragraphs();
944 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
946 if (runparams.par_begin == runparams.par_end) {
947 // The full doc will be exported but it is easier to just rely on
948 // runparams range parameters that will be passed TeXEnvironment.
949 runparams.par_begin = 0;
950 runparams.par_end = paragraphs.size();
953 pit_type pit = runparams.par_begin;
954 // lastpit is for the language check after the loop.
955 pit_type lastpit = pit;
956 // variables used in the loop:
957 bool was_title = false;
958 bool already_title = false;
959 DocumentClass const & tclass = bparams.documentClass();
961 for (; pit < runparams.par_end; ++pit) {
963 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
965 // FIXME This check should not be needed. We should
966 // perhaps issue an error if it is.
967 Layout const & layout = text.inset().forcePlainLayout() ?
968 tclass.plainLayout() : par->layout();
970 if (layout.intitle) {
972 LYXERR0("Error in latexParagraphs: You"
973 " should not mix title layouts"
974 " with normal ones.");
975 } else if (!was_title) {
977 if (tclass.titletype() == TITLE_ENVIRONMENT) {
979 << from_ascii(tclass.titlename())
983 } else if (was_title && !already_title) {
984 if (tclass.titletype() == TITLE_ENVIRONMENT) {
985 os << "\\end{" << from_ascii(tclass.titlename())
989 os << "\\" << from_ascii(tclass.titlename())
992 already_title = true;
997 if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
998 // This is a standard top level paragraph, TeX it and continue.
999 TeXOnePar(buf, text, pit, os, runparams, everypar);
1003 TeXEnvironmentData const data =
1004 prepareEnvironment(buf, text, par, os, runparams);
1005 // pit can be changed in TeXEnvironment.
1006 TeXEnvironment(buf, text, runparams, pit, os);
1007 finishEnvironment(os, runparams, data);
1010 if (pit == runparams.par_end) {
1011 // Make sure that the last paragraph is
1012 // correctly terminated (because TeXOnePar does
1013 // not add a \n in this case)
1017 // It might be that we only have a title in this document
1018 if (was_title && !already_title) {
1019 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1020 os << "\\end{" << from_ascii(tclass.titlename())
1023 os << "\\" << from_ascii(tclass.titlename())
1028 // if "auto end" is switched off, explicitly close the language at the end
1029 // but only if the last par is in a babel language
1030 string const lang_end_command = runparams.use_polyglossia ?
1031 "\\end{$$lang}" : lyxrc.language_command_end;
1032 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1033 paragraphs.at(lastpit).getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1034 os << from_utf8(subst(lang_end_command,
1040 // If the last paragraph is an environment, we'll have to close
1041 // CJK at the very end to do proper nesting.
1042 if (maintext && !is_child && open_encoding_ == CJK) {
1043 os << "\\end{CJK}\n";
1044 open_encoding_ = none;
1047 // reset inherited encoding
1048 if (cjk_inherited_ > 0) {
1049 cjk_inherited_ -= 1;
1050 if (cjk_inherited_ == 0)
1051 open_encoding_ = CJK;
1056 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1057 OutputParams const & runparams, Encoding const & newEnc,
1060 Encoding const & oldEnc = *runparams.encoding;
1061 bool moving_arg = runparams.moving_arg;
1062 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
1064 return make_pair(false, 0);
1066 // Do nothing if the encoding is unchanged.
1067 if (oldEnc.name() == newEnc.name())
1068 return make_pair(false, 0);
1070 // FIXME We ignore encoding switches from/to encodings that do
1071 // neither support the inputenc package nor the CJK package here.
1072 // This does of course only work in special cases (e.g. switch from
1073 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1074 // but it is the best we can do
1075 if (oldEnc.package() == Encoding::none
1076 || newEnc.package() == Encoding::none)
1077 return make_pair(false, 0);
1079 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1080 << oldEnc.name() << " to " << newEnc.name());
1081 os << setEncoding(newEnc.iconvName());
1082 if (bparams.inputenc == "default")
1083 return make_pair(true, 0);
1085 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1086 switch (newEnc.package()) {
1087 case Encoding::none:
1088 case Encoding::japanese:
1089 // shouldn't ever reach here, see above
1090 return make_pair(true, 0);
1091 case Encoding::inputenc: {
1092 int count = inputenc_arg.length();
1093 if (oldEnc.package() == Encoding::CJK &&
1094 open_encoding_ == CJK) {
1096 open_encoding_ = none;
1099 else if (oldEnc.package() == Encoding::inputenc &&
1100 open_encoding_ == inputenc) {
1102 open_encoding_ = none;
1105 if (runparams.local_font != 0
1106 && oldEnc.package() == Encoding::CJK) {
1107 // within insets, \inputenc switches need
1108 // to be embraced within \bgroup...\egroup;
1112 open_encoding_ = inputenc;
1114 // with the japanese option, inputenc is omitted.
1115 if (runparams.use_japanese)
1116 return make_pair(true, count);
1117 os << "\\inputencoding{" << inputenc_arg << '}';
1118 return make_pair(true, count + 16);
1120 case Encoding::CJK: {
1121 int count = inputenc_arg.length();
1122 if (oldEnc.package() == Encoding::CJK &&
1123 open_encoding_ == CJK) {
1127 if (oldEnc.package() == Encoding::inputenc &&
1128 open_encoding_ == inputenc) {
1132 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1133 << from_ascii(bparams.fonts_cjk) << "}";
1134 open_encoding_ = CJK;
1135 return make_pair(true, count + 15);
1138 // Dead code to avoid a warning:
1139 return make_pair(true, 0);