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/convert.h"
34 #include "support/debug.h"
35 #include "support/lstrings.h"
38 #include <boost/next_prior.hpp>
42 using namespace lyx::support;
55 static int open_encoding_ = none;
56 static int cjk_inherited_ = 0;
57 Language const * prev_env_language_ = 0;
60 string const getPolyglossiaEnvName(Language const * lang)
62 string result = lang->polyglossia();
63 if (result == "arabic")
64 // exceptional spelling; see polyglossia docs.
70 struct TeXEnvironmentData
74 Language const * par_language;
75 Encoding const * prev_encoding;
80 static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
82 ParagraphList::const_iterator pit,
84 OutputParams const & runparams)
86 TeXEnvironmentData data;
88 BufferParams const & bparams = buf.params();
90 // FIXME This test should not be necessary.
91 // We should perhaps issue an error if it is.
92 Layout const & style = text.inset().forcePlainLayout() ?
93 bparams.documentClass().plainLayout() : pit->layout();
95 ParagraphList const & paragraphs = text.paragraphs();
96 ParagraphList::const_iterator const priorpit =
97 pit == paragraphs.begin() ? pit : boost::prior(pit);
99 bool const use_prev_env_language = prev_env_language_ != 0
100 && priorpit->layout().isEnvironment()
101 && (priorpit->getDepth() > pit->getDepth()
102 || (priorpit->getDepth() == pit->getDepth()
103 && priorpit->layout() != pit->layout()));
105 data.prev_encoding = runparams.encoding;
106 data.par_language = pit->getParLanguage(bparams);
107 Language const * const doc_language = bparams.language;
108 Language const * const prev_par_language =
109 (pit != paragraphs.begin())
110 ? (use_prev_env_language ? prev_env_language_
111 : priorpit->getParLanguage(bparams))
114 bool const use_polyglossia = runparams.use_polyglossia;
115 string const par_lang = use_polyglossia ?
116 getPolyglossiaEnvName(data.par_language) : data.par_language->babel();
117 string const prev_par_lang = use_polyglossia ?
118 getPolyglossiaEnvName(prev_par_language) : prev_par_language->babel();
119 string const doc_lang = use_polyglossia ?
120 getPolyglossiaEnvName(doc_language) : doc_language->babel();
121 string const lang_begin_command = use_polyglossia ?
122 "\\begin{$$lang}" : lyxrc.language_command_begin;
123 string const lang_end_command = use_polyglossia ?
124 "\\end{$$lang}" : lyxrc.language_command_end;
126 if (par_lang != prev_par_lang) {
127 if (!lang_end_command.empty() &&
128 prev_par_lang != doc_lang &&
129 !prev_par_lang.empty()) {
130 os << from_ascii(subst(
134 // the '%' is necessary to prevent unwanted whitespace
138 if ((lang_end_command.empty() ||
139 par_lang != doc_lang) &&
141 os << from_ascii(subst(
146 && !data.par_language->polyglossiaOpts().empty())
148 << from_ascii(data.par_language->polyglossiaOpts())
150 // the '%' is necessary to prevent unwanted whitespace
155 data.leftindent_open = false;
156 if (!pit->params().leftIndent().zero()) {
157 os << "\\begin{LyXParagraphLeftIndent}{"
158 << from_ascii(pit->params().leftIndent().asLatexString())
160 data.leftindent_open = true;
163 if (style.isEnvironment()) {
164 os << "\\begin{" << from_ascii(style.latexname()) << '}';
165 if (style.latexargs().size() != 0)
166 latexArgInsets(*pit, os, runparams, style.latexargs());
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, runparams) << "}\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 data.cjk_nested = false;
185 if (data.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(data.par_language->encoding()->latexName())
189 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
190 open_encoding_ = CJK;
191 data.cjk_nested = true;
197 static void finishEnvironment(otexstream & os, OutputParams const & runparams,
198 TeXEnvironmentData const & data)
200 if (open_encoding_ == CJK && data.cjk_nested) {
201 // We need to close the encoding even if it does not change
202 // to do correct environment nesting
203 os << "\\end{CJK}\n";
204 open_encoding_ = none;
207 if (data.style->isEnvironment()) {
208 os << "\\end{" << from_ascii(data.style->latexname()) << "}\n";
209 prev_env_language_ = data.par_language;
210 if (runparams.encoding != data.prev_encoding) {
211 runparams.encoding = data.prev_encoding;
212 if (!runparams.isFullUnicode())
213 os << setEncoding(data.prev_encoding->iconvName());
217 if (data.leftindent_open) {
218 os << "\\end{LyXParagraphLeftIndent}\n";
219 prev_env_language_ = data.par_language;
220 if (runparams.encoding != data.prev_encoding) {
221 runparams.encoding = data.prev_encoding;
222 if (!runparams.isFullUnicode())
223 os << setEncoding(data.prev_encoding->iconvName());
229 void TeXEnvironment(Buffer const & buf, Text const & text,
230 OutputParams const & runparams,
231 pit_type & pit, otexstream & os)
233 ParagraphList const & paragraphs = text.paragraphs();
234 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
235 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
237 Layout const & current_layout = par->layout();
238 depth_type const current_depth = par->params().depth();
239 Length const & current_left_indent = par->params().leftIndent();
241 // This is for debugging purpose at the end.
242 pit_type const par_begin = pit;
243 for (; pit < runparams.par_end; ++pit) {
244 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
246 // check first if this is an higher depth paragraph.
247 bool go_out = (par->params().depth() < current_depth);
248 if (par->params().depth() == current_depth) {
249 // This environment is finished.
250 go_out |= (par->layout() != current_layout);
251 go_out |= (par->params().leftIndent() != current_left_indent);
254 // nothing to do here, restore pit and go out.
259 if (par->layout() == current_layout
260 && par->params().depth() == current_depth
261 && par->params().leftIndent() == current_left_indent) {
262 // We are still in the same environment so TeXOnePar and continue;
263 TeXOnePar(buf, text, pit, os, runparams);
267 // We are now in a deeper environment.
268 // Either par->layout() != current_layout
269 // Or par->params().depth() > current_depth
270 // Or par->params().leftIndent() != current_left_indent)
272 if (par->layout().isParagraph()) {
273 // FIXME (Lgb): How to handle this?
274 //&& !suffixIs(os, "\n\n")
276 // (ARRae) There should be at least one '\n' already but we need there to
277 // be two for Standard paragraphs that are depth-increment'ed to be
278 // output correctly. However, tables can also be paragraphs so
279 // don't adjust them.
281 // FIXME (Lgb): Will it ever harm to have one '\n' too
282 // many? i.e. that we sometimes will have
287 // FIXME This test should not be necessary.
288 // We should perhaps issue an error if it is.
289 bool const force_plain_layout = text.inset().forcePlainLayout();
290 Layout const & style = force_plain_layout
291 ? buf.params().documentClass().plainLayout()
294 if (!style.isEnvironment()) {
295 // This is a standard paragraph, no need to call TeXEnvironment.
296 TeXOnePar(buf, text, pit, os, runparams);
300 // This is a new environment.
301 TeXEnvironmentData const data =
302 prepareEnvironment(buf, text, par, os, runparams);
303 // Recursive call to TeXEnvironment!
304 TeXEnvironment(buf, text, runparams, pit, os);
305 finishEnvironment(os, runparams, data);
308 if (pit != runparams.par_end)
309 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << par_begin << " done.");
315 void latexArgInsets(Paragraph const & par, otexstream & os,
316 OutputParams const & runparams, Layout::LaTeXArgMap latexargs)
318 map<int, InsetArgument const *> ilist;
319 vector<string> required;
321 InsetList::const_iterator it = par.insetList().begin();
322 InsetList::const_iterator end = par.insetList().end();
323 for (; it != end; ++it) {
324 if (it->inset->lyxCode() == ARG_CODE) {
325 InsetArgument const * ins =
326 static_cast<InsetArgument const *>(it->inset);
327 if (ins->name().empty())
328 LYXERR0("Error: Unnamed argument inset!");
330 unsigned int const nr = convert<unsigned int>(ins->name());
332 Layout::LaTeXArgMap::const_iterator const lit =
334 if (lit != latexargs.end()) {
335 Layout::latexarg arg = (*lit).second;
336 if (!arg.requires.empty()) {
337 vector<string> req = getVectorFromString(arg.requires);
338 required.insert(required.end(), req.begin(), req.end());
345 unsigned int const argnr = latexargs.size();
349 for (unsigned int i = 1; i <= argnr; ++i) {
350 map<int, InsetArgument const *>::const_iterator lit = ilist.find(i);
351 bool inserted = false;
352 if (lit != ilist.end()) {
353 InsetArgument const * ins = (*lit).second;
355 Layout::LaTeXArgMap::const_iterator const lait =
356 latexargs.find(convert<unsigned int>(ins->name()));
357 if (lait != latexargs.end()) {
358 Layout::latexarg arg = (*lait).second;
359 docstring ldelim = arg.mandatory ?
360 from_ascii("{") : from_ascii("[");
361 docstring rdelim = arg.mandatory ?
362 from_ascii("}") : from_ascii("]");
363 if (!arg.ldelim.empty())
365 if (!arg.rdelim.empty())
367 ins->latexArgument(os, runparams, ldelim, rdelim);
373 Layout::LaTeXArgMap::const_iterator lait = latexargs.begin();
374 Layout::LaTeXArgMap::const_iterator const laend = latexargs.end();
375 for (; lait != laend; ++lait) {
376 if ((*lait).first == i) {
377 Layout::latexarg arg = (*lait).second;
379 docstring ldelim = arg.ldelim.empty() ?
380 from_ascii("{") : arg.ldelim;
381 docstring rdelim = arg.rdelim.empty() ?
382 from_ascii("}") : arg.rdelim;
383 os << ldelim << rdelim;
384 } else if (find(required.begin(), required.end(),
385 convert<string>((*lait).first)) != required.end()) {
386 docstring ldelim = arg.ldelim.empty() ?
387 from_ascii("[") : arg.ldelim;
388 docstring rdelim = arg.rdelim.empty() ?
389 from_ascii("]") : arg.rdelim;
390 os << ldelim << rdelim;
401 // output the proper paragraph start according to latextype.
402 void parStartCommand(Paragraph const & par, otexstream & os,
403 OutputParams const & runparams, Layout const & style)
405 switch (style.latextype) {
407 os << '\\' << from_ascii(style.latexname());
409 // Separate handling of optional argument inset.
410 if (style.latexargs().size() != 0)
411 latexArgInsets(par, os, runparams, style.latexargs());
413 os << from_ascii(style.latexparam());
415 case LATEX_ITEM_ENVIRONMENT:
416 case LATEX_LIST_ENVIRONMENT:
419 case LATEX_BIB_ENVIRONMENT:
420 // ignore this, the inset will write itself
429 // FIXME: this should be anonymous
430 void TeXOnePar(Buffer const & buf,
434 OutputParams const & runparams_in,
435 string const & everypar,
436 int start_pos, int end_pos)
438 BufferParams const & bparams = buf.params();
439 ParagraphList const & paragraphs = text.paragraphs();
440 Paragraph const & par = paragraphs.at(pit);
441 // FIXME This check should not really be needed.
442 // Perhaps we should issue an error if it is.
443 Layout const style = text.inset().forcePlainLayout() ?
444 bparams.documentClass().plainLayout() : par.layout();
446 if (style.inpreamble)
449 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
452 OutputParams runparams = runparams_in;
453 runparams.isLastPar = (pit == pit_type(paragraphs.size() - 1));
454 // We reinitialze par begin and end to be on the safe side
455 // with embedded inset as we don't know if they set those
457 runparams.par_begin = 0;
458 runparams.par_end = 0;
460 bool const maintext = text.isMainText();
461 // we are at the beginning of an inset and CJK is already open;
462 // we count inheritation levels to get the inset nesting right.
463 if (pit == 0 && !maintext
464 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
466 open_encoding_ = none;
469 if (text.inset().getLayout().isPassThru()) {
470 Font const outerfont = text.outerFont(pit);
472 // No newline before first paragraph in this lyxtext
475 if (!text.inset().getLayout().parbreakIsNewline())
479 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
483 Paragraph const * nextpar = runparams.isLastPar
484 ? 0 : ¶graphs.at(pit + 1);
486 if (style.pass_thru) {
487 Font const outerfont = text.outerFont(pit);
488 parStartCommand(par, os, runparams, style);
490 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
492 // I did not create a parEndCommand for this minuscule
493 // task because in the other user of parStartCommand
494 // the code is different (JMarc)
495 if (style.isCommand())
499 if (!style.parbreak_is_newline) {
501 } else if (nextpar) {
502 Layout const nextstyle = text.inset().forcePlainLayout()
503 ? bparams.documentClass().plainLayout()
505 if (nextstyle.name() != style.name())
512 // This paragraph's language
513 Language const * const par_language = par.getParLanguage(bparams);
514 // The document's language
515 Language const * const doc_language = bparams.language;
516 // The language that was in effect when the environment this paragraph is
517 // inside of was opened
518 Language const * const outer_language =
519 (runparams.local_font != 0) ?
520 runparams.local_font->language() : doc_language;
522 Paragraph const * priorpar = (pit == 0) ? 0 : ¶graphs.at(pit - 1);
524 // The previous language that was in effect is the language of the
525 // previous paragraph, unless the previous paragraph is inside an
526 // environment with nesting depth greater than (or equal to, but with
527 // a different layout) the current one. If there is no previous
528 // paragraph, the previous language is the outer language.
529 bool const use_prev_env_language = prev_env_language_ != 0
531 && priorpar->layout().isEnvironment()
532 && (priorpar->getDepth() > par.getDepth()
533 || (priorpar->getDepth() == par.getDepth()
534 && priorpar->layout() != par.layout()));
535 Language const * const prev_language =
537 ? (use_prev_env_language ? prev_env_language_
538 : priorpar->getParLanguage(bparams))
542 bool const use_polyglossia = runparams.use_polyglossia;
543 string const par_lang = use_polyglossia ?
544 getPolyglossiaEnvName(par_language): par_language->babel();
545 string const prev_lang = use_polyglossia ?
546 getPolyglossiaEnvName(prev_language) : prev_language->babel();
547 string const doc_lang = use_polyglossia ?
548 getPolyglossiaEnvName(doc_language) : doc_language->babel();
549 string const outer_lang = use_polyglossia ?
550 getPolyglossiaEnvName(outer_language) : outer_language->babel();
551 string const lang_begin_command = use_polyglossia ?
552 "\\begin{$$lang}" : lyxrc.language_command_begin;
553 string const lang_end_command = use_polyglossia ?
554 "\\end{$$lang}" : lyxrc.language_command_end;
556 if (par_lang != prev_lang
557 // check if we already put language command in TeXEnvironment()
558 && !(style.isEnvironment()
559 && (pit == 0 || (priorpar->layout() != par.layout()
560 && priorpar->getDepth() <= par.getDepth())
561 || priorpar->getDepth() < par.getDepth())))
563 if (!lang_end_command.empty() &&
564 prev_lang != outer_lang &&
567 os << from_ascii(subst(lang_end_command,
570 // the '%' is necessary to prevent unwanted whitespace
574 // We need to open a new language if we couldn't close the previous
575 // one (because there's no language_command_end); and even if we closed
576 // the previous one, if the current language is different than the
577 // outer_language (which is currently in effect once the previous one
579 if ((lang_end_command.empty() || par_lang != outer_lang)
580 && !par_lang.empty()) {
581 // If we're inside an inset, and that inset is within an \L or \R
582 // (or equivalents), then within the inset, too, any opposite
583 // language paragraph should appear within an \L or \R (in addition
584 // to, outside of, the normal language switch commands).
585 // This behavior is not correct for ArabTeX, though.
588 && par_language->lang() != "arabic_arabtex"
589 && outer_language->lang() != "arabic_arabtex"
590 // are we in an inset?
591 && runparams.local_font != 0
592 // is the inset within an \L or \R?
594 // FIXME: currently, we don't check this; this means that
595 // we'll have unnnecessary \L and \R commands, but that
596 // doesn't seem to hurt (though latex will complain)
598 // is this paragraph in the opposite direction?
599 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()) {
600 // FIXME: I don't have a working copy of the Arabi package, so
601 // I'm not sure if the farsi and arabic_arabi stuff is correct
603 if (par_language->lang() == "farsi")
605 else if (outer_language->lang() == "farsi")
607 else if (par_language->lang() == "arabic_arabi")
609 else if (outer_language->lang() == "arabic_arabi")
611 // remaining RTL languages currently is hebrew
612 else if (par_language->rightToLeft())
617 // With CJK, the CJK tag has to be closed first (see below)
618 if (runparams.encoding->package() != Encoding::CJK
619 && !par_lang.empty()) {
620 os << from_ascii(subst(
625 && !par_language->polyglossiaOpts().empty())
627 << from_ascii(par_language->polyglossiaOpts())
629 // the '%' is necessary to prevent unwanted whitespace
635 // Switch file encoding if necessary; no need to do this for "default"
636 // encoding, since this only affects the position of the outputted
637 // \inputencoding command; the encoding switch will occur when necessary
638 if (bparams.inputenc == "auto"
639 && runparams.encoding->package() != Encoding::none) {
640 // Look ahead for future encoding changes.
641 // We try to output them at the beginning of the paragraph,
642 // since the \inputencoding command is not allowed e.g. in
643 // sections. For this reason we only set runparams.moving_arg
644 // after checking for the encoding change, otherwise the
645 // change would be always avoided by switchEncoding().
646 for (pos_type i = 0; i < par.size(); ++i) {
647 char_type const c = par.getChar(i);
648 Encoding const * const encoding =
649 par.getFontSettings(bparams, i).language()->encoding();
650 if (encoding->package() != Encoding::CJK
651 && runparams.encoding->package() == Encoding::inputenc
656 // All characters before c are in the ASCII range, and
657 // c is non-ASCII (but no inset), so change the
658 // encoding to that required by the language of c.
659 // With CJK, only add switch if we have CJK content at the beginning
661 if (i != 0 && encoding->package() == Encoding::CJK)
664 pair<bool, int> enc_switch = switchEncoding(os.os(),
665 bparams, runparams, *encoding);
666 // the following is necessary after a CJK environment in a multilingual
667 // context (nesting issue).
668 if (par_language->encoding()->package() == Encoding::CJK
669 && open_encoding_ != CJK && cjk_inherited_ == 0) {
670 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
671 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
672 open_encoding_ = CJK;
674 if (encoding->package() != Encoding::none && enc_switch.first) {
675 if (enc_switch.second > 0) {
676 // the '%' is necessary to prevent unwanted whitespace
679 // With CJK, the CJK tag had to be closed first (see above)
680 if (runparams.encoding->package() == Encoding::CJK
681 && !par_lang.empty()) {
682 os << from_ascii(subst(
686 // the '%' is necessary to prevent unwanted whitespace
689 runparams.encoding = encoding;
695 runparams.moving_arg |= style.needprotect;
696 Encoding const * const prev_encoding = runparams.encoding;
698 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
699 if (par.allowParagraphCustomization()) {
700 if (par.params().startOfAppendix()) {
701 os << "\\appendix\n";
704 if (!par.params().spacing().isDefault()
705 && (pit == 0 || !priorpar->hasSameLayout(par)))
707 os << from_ascii(par.params().spacing().writeEnvirBegin(useSetSpace))
711 if (style.isCommand()) {
716 parStartCommand(par, os, runparams, style);
718 Font const outerfont = text.outerFont(pit);
721 os << from_utf8(everypar);
722 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
724 // Make sure that \\par is done with the font of the last
725 // character if this has another size as the default.
726 // This is necessary because LaTeX (and LyX on the screen)
727 // calculates the space between the baselines according
728 // to this font. (Matthias)
730 // Is this really needed ? (Dekel)
731 // We do not need to use to change the font for the last paragraph
734 Font const font = par.empty()
735 ? par.getLayoutFont(bparams, outerfont)
736 : par.getFont(bparams, par.size() - 1, outerfont);
738 bool const is_command = style.isCommand();
740 if (style.resfont.size() != font.fontInfo().size()
744 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
745 } else if (is_command) {
747 if (runparams.encoding != prev_encoding) {
748 runparams.encoding = prev_encoding;
749 if (!runparams.isFullUnicode())
750 os << setEncoding(prev_encoding->iconvName());
754 bool pending_newline = false;
755 bool unskip_newline = false;
756 switch (style.latextype) {
757 case LATEX_ITEM_ENVIRONMENT:
758 case LATEX_LIST_ENVIRONMENT:
759 if (nextpar && (par.params().depth() < nextpar->params().depth()))
760 pending_newline = true;
762 case LATEX_ENVIRONMENT: {
763 // if its the last paragraph of the current environment
764 // skip it otherwise fall through
766 && (nextpar->layout() != par.layout()
767 || nextpar->params().depth() != par.params().depth()))
771 // fall through possible
773 // we don't need it for the last paragraph!!!
775 pending_newline = true;
778 if (par.allowParagraphCustomization()) {
779 if (!par.params().spacing().isDefault()
780 && (runparams.isLastPar || !nextpar->hasSameLayout(par))) {
783 os << from_ascii(par.params().spacing().writeEnvirEnd(useSetSpace));
784 pending_newline = true;
788 // Closing the language is needed for the last paragraph; it is also
789 // needed if we're within an \L or \R that we may have opened above (not
790 // necessarily in this paragraph) and are about to close.
791 bool closing_rtl_ltr_environment = !use_polyglossia
793 && (par_language->lang() != "arabic_arabtex"
794 && outer_language->lang() != "arabic_arabtex")
795 // have we opened and \L or \R environment?
796 && runparams.local_font != 0
797 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()
798 // are we about to close the language?
799 &&((nextpar && par_language->babel() != (nextpar->getParLanguage(bparams))->babel())
800 || (runparams.isLastPar && par_language->babel() != outer_language->babel()));
802 if (closing_rtl_ltr_environment
803 || (runparams.isLastPar
804 && ((!use_polyglossia && par_language->babel() != outer_language->babel())
805 || (use_polyglossia && par_language->polyglossia() != outer_language->polyglossia())))) {
806 // Since \selectlanguage write the language to the aux file,
807 // we need to reset the language at the end of footnote or
813 // when the paragraph uses CJK, the language has to be closed earlier
814 if (font.language()->encoding()->package() != Encoding::CJK) {
815 if (lang_end_command.empty()) {
816 // If this is a child, we should restore the
817 // master language after the last paragraph.
818 Language const * const current_language =
819 (runparams.isLastPar && runparams.master_language)
820 ? runparams.master_language
822 string const current_lang = use_polyglossia
823 ? getPolyglossiaEnvName(current_language)
824 : current_language->babel();
825 if (!current_lang.empty()) {
826 os << from_ascii(subst(
830 pending_newline = true;
831 unskip_newline = true;
833 } else if (!par_lang.empty()) {
834 os << from_ascii(subst(
838 pending_newline = true;
839 unskip_newline = true;
843 if (closing_rtl_ltr_environment)
846 if (pending_newline) {
848 // prevent unwanted whitespace
853 // if this is a CJK-paragraph and the next isn't, close CJK
854 // also if the next paragraph is a multilingual environment (because of nesting)
856 && open_encoding_ == CJK
857 && (nextpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK
858 || (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
859 // inbetween environments, CJK has to be closed later (nesting!)
860 && (!style.isEnvironment() || !nextpar->layout().isEnvironment())) {
861 os << "\\end{CJK}\n";
862 open_encoding_ = none;
865 // If this is the last paragraph, close the CJK environment
866 // if necessary. If it's an environment, we'll have to \end that first.
867 if (runparams.isLastPar && !style.isEnvironment()) {
868 switch (open_encoding_) {
870 // do nothing at the end of child documents
871 if (maintext && buf.masterBuffer() != &buf)
875 os << "\n\\end{CJK}\n";
879 open_encoding_ = none;
884 open_encoding_ = none;
894 // If this is the last paragraph, and a local_font was set upon entering
895 // the inset, and we're using "auto" or "default" encoding, the encoding
896 // should be set back to that local_font's encoding.
897 // However, do not change the encoding when a fully unicode aware backend
898 // such as XeTeX is used.
899 if (runparams.isLastPar && runparams_in.local_font != 0
900 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
901 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
902 && (!runparams.isFullUnicode())) {
903 runparams_in.encoding = runparams_in.local_font->language()->encoding();
904 os << setEncoding(runparams_in.encoding->iconvName());
906 // Otherwise, the current encoding should be set for the next paragraph.
908 runparams_in.encoding = runparams.encoding;
911 // we don't need a newline for the last paragraph!!!
912 // Note from JMarc: we will re-add a \n explicitly in
913 // TeXEnvironment, because it is needed in this case
915 Layout const & next_layout = nextpar->layout();
916 if (style == next_layout
917 // no blank lines before environments!
918 || !next_layout.isEnvironment()
919 // unless there's a depth change
920 // FIXME What we really want to do here is put every \begin and \end
921 // tag on a new line (which was not the case with nested environments).
922 // But in the present state of play, we don't have access to the
923 // information whether the current TeX row is empty or not.
924 // For some ideas about how to fix this, see this thread:
925 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
926 || nextpar->params().depth() != par.params().depth()) {
931 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " done; ptr "
932 << &par << " next " << nextpar);
938 // LaTeX all paragraphs
939 void latexParagraphs(Buffer const & buf,
942 OutputParams const & runparams,
943 string const & everypar)
945 BufferParams const & bparams = buf.params();
947 bool const maintext = text.isMainText();
948 bool const is_child = buf.masterBuffer() != &buf;
950 // Open a CJK environment at the beginning of the main buffer
951 // if the document's language is a CJK language
952 // (but not in child documents)
953 if (maintext && !is_child
954 && bparams.encoding().package() == Encoding::CJK) {
955 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
956 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
957 open_encoding_ = CJK;
959 // if "auto begin" is switched off, explicitly switch the
960 // language on at start
961 string const mainlang = runparams.use_polyglossia
962 ? getPolyglossiaEnvName(bparams.language)
963 : bparams.language->babel();
964 string const lang_begin_command = runparams.use_polyglossia ?
965 "\\begin{$$lang}" : lyxrc.language_command_begin;
967 if (maintext && !lyxrc.language_auto_begin &&
970 os << from_utf8(subst(lang_begin_command,
973 if (runparams.use_polyglossia
974 && !bparams.language->polyglossiaOpts().empty())
976 << from_ascii(bparams.language->polyglossiaOpts())
981 ParagraphList const & paragraphs = text.paragraphs();
982 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
984 if (runparams.par_begin == runparams.par_end) {
985 // The full doc will be exported but it is easier to just rely on
986 // runparams range parameters that will be passed TeXEnvironment.
987 runparams.par_begin = 0;
988 runparams.par_end = paragraphs.size();
991 pit_type pit = runparams.par_begin;
992 // lastpit is for the language check after the loop.
993 pit_type lastpit = pit;
994 // variables used in the loop:
995 bool was_title = false;
996 bool already_title = false;
997 DocumentClass const & tclass = bparams.documentClass();
999 for (; pit < runparams.par_end; ++pit) {
1001 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
1003 // FIXME This check should not be needed. We should
1004 // perhaps issue an error if it is.
1005 Layout const & layout = text.inset().forcePlainLayout() ?
1006 tclass.plainLayout() : par->layout();
1008 if (layout.intitle) {
1009 if (already_title) {
1010 LYXERR0("Error in latexParagraphs: You"
1011 " should not mix title layouts"
1012 " with normal ones.");
1013 } else if (!was_title) {
1015 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1017 << from_ascii(tclass.titlename())
1021 } else if (was_title && !already_title) {
1022 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1023 os << "\\end{" << from_ascii(tclass.titlename())
1027 os << "\\" << from_ascii(tclass.titlename())
1030 already_title = true;
1035 if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
1036 // This is a standard top level paragraph, TeX it and continue.
1037 TeXOnePar(buf, text, pit, os, runparams, everypar);
1041 TeXEnvironmentData const data =
1042 prepareEnvironment(buf, text, par, os, runparams);
1043 // pit can be changed in TeXEnvironment.
1044 TeXEnvironment(buf, text, runparams, pit, os);
1045 finishEnvironment(os, runparams, data);
1048 if (pit == runparams.par_end) {
1049 // Make sure that the last paragraph is
1050 // correctly terminated (because TeXOnePar does
1051 // not add a \n in this case)
1055 // It might be that we only have a title in this document
1056 if (was_title && !already_title) {
1057 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1058 os << "\\end{" << from_ascii(tclass.titlename())
1061 os << "\\" << from_ascii(tclass.titlename())
1066 // if "auto end" is switched off, explicitly close the language at the end
1067 // but only if the last par is in a babel language
1068 string const lang_end_command = runparams.use_polyglossia ?
1069 "\\end{$$lang}" : lyxrc.language_command_end;
1070 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1071 paragraphs.at(lastpit).getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1072 os << from_utf8(subst(lang_end_command,
1078 // If the last paragraph is an environment, we'll have to close
1079 // CJK at the very end to do proper nesting.
1080 if (maintext && !is_child && open_encoding_ == CJK) {
1081 os << "\\end{CJK}\n";
1082 open_encoding_ = none;
1085 // reset inherited encoding
1086 if (cjk_inherited_ > 0) {
1087 cjk_inherited_ -= 1;
1088 if (cjk_inherited_ == 0)
1089 open_encoding_ = CJK;
1094 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1095 OutputParams const & runparams, Encoding const & newEnc,
1098 Encoding const & oldEnc = *runparams.encoding;
1099 bool moving_arg = runparams.moving_arg;
1100 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
1102 return make_pair(false, 0);
1104 // Do nothing if the encoding is unchanged.
1105 if (oldEnc.name() == newEnc.name())
1106 return make_pair(false, 0);
1108 // FIXME We ignore encoding switches from/to encodings that do
1109 // neither support the inputenc package nor the CJK package here.
1110 // This does of course only work in special cases (e.g. switch from
1111 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1112 // but it is the best we can do
1113 if (oldEnc.package() == Encoding::none
1114 || newEnc.package() == Encoding::none)
1115 return make_pair(false, 0);
1117 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1118 << oldEnc.name() << " to " << newEnc.name());
1119 os << setEncoding(newEnc.iconvName());
1120 if (bparams.inputenc == "default")
1121 return make_pair(true, 0);
1123 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1124 switch (newEnc.package()) {
1125 case Encoding::none:
1126 case Encoding::japanese:
1127 // shouldn't ever reach here, see above
1128 return make_pair(true, 0);
1129 case Encoding::inputenc: {
1130 int count = inputenc_arg.length();
1131 if (oldEnc.package() == Encoding::CJK &&
1132 open_encoding_ == CJK) {
1134 open_encoding_ = none;
1137 else if (oldEnc.package() == Encoding::inputenc &&
1138 open_encoding_ == inputenc) {
1140 open_encoding_ = none;
1143 if (runparams.local_font != 0
1144 && oldEnc.package() == Encoding::CJK) {
1145 // within insets, \inputenc switches need
1146 // to be embraced within \bgroup...\egroup;
1150 open_encoding_ = inputenc;
1152 // with the japanese option, inputenc is omitted.
1153 if (runparams.use_japanese)
1154 return make_pair(true, count);
1155 os << "\\inputencoding{" << inputenc_arg << '}';
1156 return make_pair(true, count + 16);
1158 case Encoding::CJK: {
1159 int count = inputenc_arg.length();
1160 if (oldEnc.package() == Encoding::CJK &&
1161 open_encoding_ == CJK) {
1165 if (oldEnc.package() == Encoding::inputenc &&
1166 open_encoding_ == inputenc) {
1170 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1171 << from_ascii(bparams.fonts_cjk) << "}";
1172 open_encoding_ = CJK;
1173 return make_pair(true, count + 15);
1176 // Dead code to avoid a warning:
1177 return make_pair(true, 0);