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"
36 #include "support/textutils.h"
39 #include <boost/next_prior.hpp>
43 using namespace lyx::support;
57 // There could easily be a conflict here, with the export process
58 // setting this one way, and a View>Source process (possbily for
59 // another Buffer) resetting it.
60 static int open_encoding_ = none;
61 static int cjk_inherited_ = 0;
62 Language const * prev_env_language_ = 0;
65 string const getPolyglossiaEnvName(Language const * lang)
67 string result = lang->polyglossia();
68 if (result == "arabic")
69 // exceptional spelling; see polyglossia docs.
75 struct TeXEnvironmentData
79 Language const * par_language;
80 Encoding const * prev_encoding;
85 static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
87 ParagraphList::const_iterator pit,
89 OutputParams const & runparams)
91 TeXEnvironmentData data;
93 BufferParams const & bparams = buf.params();
95 // FIXME This test should not be necessary.
96 // We should perhaps issue an error if it is.
97 Layout const & style = text.inset().forcePlainLayout() ?
98 bparams.documentClass().plainLayout() : pit->layout();
100 ParagraphList const & paragraphs = text.paragraphs();
101 ParagraphList::const_iterator const priorpit =
102 pit == paragraphs.begin() ? pit : boost::prior(pit);
104 bool const use_prev_env_language = prev_env_language_ != 0
105 && priorpit->layout().isEnvironment()
106 && (priorpit->getDepth() > pit->getDepth()
107 || (priorpit->getDepth() == pit->getDepth()
108 && priorpit->layout() != pit->layout()));
110 data.prev_encoding = runparams.encoding;
111 data.par_language = pit->getParLanguage(bparams);
112 Language const * const doc_language = bparams.language;
113 Language const * const prev_par_language =
114 (pit != paragraphs.begin())
115 ? (use_prev_env_language ? prev_env_language_
116 : priorpit->getParLanguage(bparams))
119 bool const use_polyglossia = runparams.use_polyglossia;
120 string const par_lang = use_polyglossia ?
121 getPolyglossiaEnvName(data.par_language) : data.par_language->babel();
122 string const prev_par_lang = use_polyglossia ?
123 getPolyglossiaEnvName(prev_par_language) : prev_par_language->babel();
124 string const doc_lang = use_polyglossia ?
125 getPolyglossiaEnvName(doc_language) : doc_language->babel();
126 string const lang_begin_command = use_polyglossia ?
127 "\\begin{$$lang}" : lyxrc.language_command_begin;
128 string const lang_end_command = use_polyglossia ?
129 "\\end{$$lang}" : lyxrc.language_command_end;
131 if (par_lang != prev_par_lang) {
132 if (!lang_end_command.empty() &&
133 prev_par_lang != doc_lang &&
134 !prev_par_lang.empty()) {
135 os << from_ascii(subst(
139 // the '%' is necessary to prevent unwanted whitespace
143 if ((lang_end_command.empty() ||
144 par_lang != doc_lang) &&
146 os << from_ascii(subst(
151 && !data.par_language->polyglossiaOpts().empty())
153 << from_ascii(data.par_language->polyglossiaOpts())
155 // the '%' is necessary to prevent unwanted whitespace
160 data.leftindent_open = false;
161 if (!pit->params().leftIndent().zero()) {
162 os << "\\begin{LyXParagraphLeftIndent}{"
163 << from_ascii(pit->params().leftIndent().asLatexString())
165 data.leftindent_open = true;
168 if (style.isEnvironment()) {
169 os << "\\begin{" << from_ascii(style.latexname()) << '}';
170 if (!style.latexargs().empty()) {
171 OutputParams rp = runparams;
172 rp.local_font = &pit->getFirstFontSettings(bparams);
173 latexArgInsets(paragraphs, pit, os, rp, style.latexargs());
175 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
177 << pit->params().labelWidthString()
179 } else if (style.labeltype == LABEL_BIBLIO) {
180 if (pit->params().labelWidthString().empty())
181 os << '{' << bibitemWidest(buf, runparams) << "}\n";
184 << pit->params().labelWidthString()
187 os << from_ascii(style.latexparam()) << '\n';
191 // in multilingual environments, the CJK tags have to be nested properly
192 data.cjk_nested = false;
193 if (data.par_language->encoding()->package() == Encoding::CJK &&
194 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
195 if (prev_par_language->encoding()->package() == Encoding::CJK)
196 os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
197 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
198 open_encoding_ = CJK;
199 data.cjk_nested = true;
205 static void finishEnvironment(otexstream & os, OutputParams const & runparams,
206 TeXEnvironmentData const & data)
208 if (open_encoding_ == CJK && data.cjk_nested) {
209 // We need to close the encoding even if it does not change
210 // to do correct environment nesting
211 os << "\\end{CJK}\n";
212 open_encoding_ = none;
215 if (data.style->isEnvironment()) {
216 os << "\\end{" << from_ascii(data.style->latexname()) << "}\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());
225 if (data.leftindent_open) {
226 os << "\\end{LyXParagraphLeftIndent}\n";
227 prev_env_language_ = data.par_language;
228 if (runparams.encoding != data.prev_encoding) {
229 runparams.encoding = data.prev_encoding;
230 if (!runparams.isFullUnicode())
231 os << setEncoding(data.prev_encoding->iconvName());
237 void TeXEnvironment(Buffer const & buf, Text const & text,
238 OutputParams const & runparams,
239 pit_type & pit, otexstream & os)
241 ParagraphList const & paragraphs = text.paragraphs();
242 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
243 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
245 Layout const & current_layout = par->layout();
246 depth_type const current_depth = par->params().depth();
247 Length const & current_left_indent = par->params().leftIndent();
249 // This is for debugging purpose at the end.
250 pit_type const par_begin = pit;
251 for (; pit < runparams.par_end; ++pit) {
252 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
254 // check first if this is an higher depth paragraph.
255 bool go_out = (par->params().depth() < current_depth);
256 if (par->params().depth() == current_depth) {
257 // This environment is finished.
258 go_out |= (par->layout() != current_layout);
259 go_out |= (par->params().leftIndent() != current_left_indent);
262 // nothing to do here, restore pit and go out.
267 if (par->layout() == current_layout
268 && par->params().depth() == current_depth
269 && par->params().leftIndent() == current_left_indent) {
270 // We are still in the same environment so TeXOnePar and continue;
271 TeXOnePar(buf, text, pit, os, runparams);
275 // We are now in a deeper environment.
276 // Either par->layout() != current_layout
277 // Or par->params().depth() > current_depth
278 // Or par->params().leftIndent() != current_left_indent)
280 if (par->layout().isParagraph()) {
281 // FIXME (Lgb): How to handle this?
282 //&& !suffixIs(os, "\n\n")
284 // (ARRae) There should be at least one '\n' already but we need there to
285 // be two for Standard paragraphs that are depth-increment'ed to be
286 // output correctly. However, tables can also be paragraphs so
287 // don't adjust them.
289 // FIXME (Lgb): Will it ever harm to have one '\n' too
290 // many? i.e. that we sometimes will have
295 // FIXME This test should not be necessary.
296 // We should perhaps issue an error if it is.
297 bool const force_plain_layout = text.inset().forcePlainLayout();
298 Layout const & style = force_plain_layout
299 ? buf.params().documentClass().plainLayout()
302 if (!style.isEnvironment()) {
303 // This is a standard paragraph, no need to call TeXEnvironment.
304 TeXOnePar(buf, text, pit, os, runparams);
308 // This is a new environment.
309 TeXEnvironmentData const data =
310 prepareEnvironment(buf, text, par, os, runparams);
311 // Recursive call to TeXEnvironment!
312 TeXEnvironment(buf, text, runparams, pit, os);
313 finishEnvironment(os, runparams, data);
316 if (pit != runparams.par_end)
317 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << par_begin << " done.");
321 void getArgInsets(otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
322 map<int, lyx::InsetArgument const *> ilist, vector<string> required, string const & prefix)
324 unsigned int const argnr = latexargs.size();
328 for (unsigned int i = 1; i <= argnr; ++i) {
329 map<int, InsetArgument const *>::const_iterator lit = ilist.find(i);
330 bool inserted = false;
331 if (lit != ilist.end()) {
332 InsetArgument const * ins = (*lit).second;
334 Layout::LaTeXArgMap::const_iterator const lait =
335 latexargs.find(ins->name());
336 if (lait != latexargs.end()) {
337 Layout::latexarg arg = (*lait).second;
338 docstring ldelim = arg.mandatory ?
339 from_ascii("{") : from_ascii("[");
340 docstring rdelim = arg.mandatory ?
341 from_ascii("}") : from_ascii("]");
342 if (!arg.ldelim.empty())
344 if (!arg.rdelim.empty())
346 ins->latexArgument(os, runparams, ldelim, rdelim, arg.presetarg);
352 Layout::LaTeXArgMap::const_iterator lait = latexargs.begin();
353 Layout::LaTeXArgMap::const_iterator const laend = latexargs.end();
354 for (; lait != laend; ++lait) {
355 string const name = prefix + convert<string>(i);
356 if ((*lait).first == name) {
357 Layout::latexarg arg = (*lait).second;
358 docstring preset = arg.presetarg;
359 if (!arg.defaultarg.empty()) {
362 preset += arg.defaultarg;
365 docstring ldelim = arg.ldelim.empty() ?
366 from_ascii("{") : arg.ldelim;
367 docstring rdelim = arg.rdelim.empty() ?
368 from_ascii("}") : arg.rdelim;
369 os << ldelim << preset << rdelim;
370 } else if (!preset.empty()) {
371 docstring ldelim = arg.ldelim.empty() ?
372 from_ascii("[") : arg.ldelim;
373 docstring rdelim = arg.rdelim.empty() ?
374 from_ascii("]") : arg.rdelim;
375 os << ldelim << preset << rdelim;
376 } else if (find(required.begin(), required.end(),
377 (*lait).first) != required.end()) {
378 docstring ldelim = arg.ldelim.empty() ?
379 from_ascii("[") : arg.ldelim;
380 docstring rdelim = arg.rdelim.empty() ?
381 from_ascii("]") : arg.rdelim;
382 os << ldelim << rdelim;
395 void latexArgInsets(Paragraph const & par, otexstream & os,
396 OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs, string const & prefix)
398 map<int, InsetArgument const *> ilist;
399 vector<string> required;
401 InsetList::const_iterator it = par.insetList().begin();
402 InsetList::const_iterator end = par.insetList().end();
403 for (; it != end; ++it) {
404 if (it->inset->lyxCode() == ARG_CODE) {
405 InsetArgument const * ins =
406 static_cast<InsetArgument const *>(it->inset);
407 if (ins->name().empty())
408 LYXERR0("Error: Unnamed argument inset!");
410 string const name = prefix.empty() ? ins->name() : split(ins->name(), ':');
411 unsigned int const nr = convert<unsigned int>(name);
413 Layout::LaTeXArgMap::const_iterator const lit =
414 latexargs.find(ins->name());
415 if (lit != latexargs.end()) {
416 Layout::latexarg const & arg = (*lit).second;
417 if (!arg.requires.empty()) {
418 vector<string> req = getVectorFromString(arg.requires);
419 required.insert(required.end(), req.begin(), req.end());
425 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
429 void latexArgInsets(ParagraphList const & pars, ParagraphList::const_iterator pit,
430 otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
431 string const & prefix)
433 map<int, InsetArgument const *> ilist;
434 vector<string> required;
436 depth_type const current_depth = pit->params().depth();
437 Layout const current_layout = pit->layout();
439 // get the first paragraph in sequence with this layout and depth
442 if (boost::prior(pit, offset) == pars.begin())
444 ParagraphList::const_iterator priorpit = boost::prior(pit, offset + 1);
445 if (priorpit->layout() == current_layout
446 && priorpit->params().depth() == current_depth)
452 ParagraphList::const_iterator spit = boost::prior(pit, offset);
454 for (; spit != pars.end(); ++spit) {
455 if (spit->layout() != current_layout || spit->params().depth() < current_depth)
457 if (spit->params().depth() > current_depth)
459 InsetList::const_iterator it = spit->insetList().begin();
460 InsetList::const_iterator end = spit->insetList().end();
461 for (; it != end; ++it) {
462 if (it->inset->lyxCode() == ARG_CODE) {
463 InsetArgument const * ins =
464 static_cast<InsetArgument const *>(it->inset);
465 if (ins->name().empty())
466 LYXERR0("Error: Unnamed argument inset!");
468 string const name = prefix.empty() ? ins->name() : split(ins->name(), ':');
469 unsigned int const nr = convert<unsigned int>(name);
470 if (ilist.find(nr) == ilist.end())
472 Layout::LaTeXArgMap::const_iterator const lit =
473 latexargs.find(ins->name());
474 if (lit != latexargs.end()) {
475 Layout::latexarg const & arg = (*lit).second;
476 if (!arg.requires.empty()) {
477 vector<string> req = getVectorFromString(arg.requires);
478 required.insert(required.end(), req.begin(), req.end());
485 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
490 // output the proper paragraph start according to latextype.
491 void parStartCommand(Paragraph const & par, otexstream & os,
492 OutputParams const & runparams, Layout const & style)
494 switch (style.latextype) {
496 os << '\\' << from_ascii(style.latexname());
499 if (!style.latexargs().empty())
500 latexArgInsets(par, os, runparams, style.latexargs());
501 os << from_ascii(style.latexparam());
503 case LATEX_ITEM_ENVIRONMENT:
504 case LATEX_LIST_ENVIRONMENT:
505 os << "\\" + style.itemcommand();
507 if (!style.itemargs().empty())
508 latexArgInsets(par, os, runparams, style.itemargs(), "item:");
511 case LATEX_BIB_ENVIRONMENT:
512 // ignore this, the inset will write itself
521 // FIXME: this should be anonymous
522 void TeXOnePar(Buffer const & buf,
526 OutputParams const & runparams_in,
527 string const & everypar,
528 int start_pos, int end_pos)
530 BufferParams const & bparams = runparams_in.is_child
531 ? buf.masterParams() : buf.params();
532 ParagraphList const & paragraphs = text.paragraphs();
533 Paragraph const & par = paragraphs.at(pit);
534 // FIXME This check should not really be needed.
535 // Perhaps we should issue an error if it is.
536 Layout const style = text.inset().forcePlainLayout() ?
537 bparams.documentClass().plainLayout() : par.layout();
539 if (style.inpreamble)
542 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
545 OutputParams runparams = runparams_in;
546 runparams.isLastPar = (pit == pit_type(paragraphs.size() - 1));
547 // We reinitialze par begin and end to be on the safe side
548 // with embedded inset as we don't know if they set those
550 runparams.par_begin = 0;
551 runparams.par_end = 0;
553 bool const maintext = text.isMainText();
554 // we are at the beginning of an inset and CJK is already open;
555 // we count inheritation levels to get the inset nesting right.
556 if (pit == 0 && !maintext
557 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
559 open_encoding_ = none;
562 if (text.inset().isPassThru()) {
563 Font const outerfont = text.outerFont(pit);
565 // No newline before first paragraph in this lyxtext
568 if (!text.inset().getLayout().parbreakIsNewline())
572 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
576 Paragraph const * nextpar = runparams.isLastPar
577 ? 0 : ¶graphs.at(pit + 1);
579 if (style.pass_thru) {
580 Font const outerfont = text.outerFont(pit);
581 parStartCommand(par, os, runparams, style);
583 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
585 // I did not create a parEndCommand for this minuscule
586 // task because in the other user of parStartCommand
587 // the code is different (JMarc)
588 if (style.isCommand())
592 if (!style.parbreak_is_newline) {
594 } else if (nextpar && !style.isEnvironment()) {
595 Layout const nextstyle = text.inset().forcePlainLayout()
596 ? bparams.documentClass().plainLayout()
598 if (nextstyle.name() != style.name())
605 // This paragraph's language
606 Language const * const par_language = par.getParLanguage(bparams);
607 // The document's language
608 Language const * const doc_language = bparams.language;
609 // The language that was in effect when the environment this paragraph is
610 // inside of was opened
611 Language const * const outer_language =
612 (runparams.local_font != 0) ?
613 runparams.local_font->language() : doc_language;
615 Paragraph const * priorpar = (pit == 0) ? 0 : ¶graphs.at(pit - 1);
617 // The previous language that was in effect is the language of the
618 // previous paragraph, unless the previous paragraph is inside an
619 // environment with nesting depth greater than (or equal to, but with
620 // a different layout) the current one. If there is no previous
621 // paragraph, the previous language is the outer language.
622 bool const use_prev_env_language = prev_env_language_ != 0
624 && priorpar->layout().isEnvironment()
625 && (priorpar->getDepth() > par.getDepth()
626 || (priorpar->getDepth() == par.getDepth()
627 && priorpar->layout() != par.layout()));
628 Language const * const prev_language =
630 ? (use_prev_env_language ? prev_env_language_
631 : priorpar->getParLanguage(bparams))
635 bool const use_polyglossia = runparams.use_polyglossia;
636 string const par_lang = use_polyglossia ?
637 getPolyglossiaEnvName(par_language): par_language->babel();
638 string const prev_lang = use_polyglossia ?
639 getPolyglossiaEnvName(prev_language) : prev_language->babel();
640 string const doc_lang = use_polyglossia ?
641 getPolyglossiaEnvName(doc_language) : doc_language->babel();
642 string const outer_lang = use_polyglossia ?
643 getPolyglossiaEnvName(outer_language) : outer_language->babel();
644 string lang_begin_command = use_polyglossia ?
645 "\\begin{$$lang}" : lyxrc.language_command_begin;
646 string lang_end_command = use_polyglossia ?
647 "\\end{$$lang}" : lyxrc.language_command_end;
648 // the '%' is necessary to prevent unwanted whitespace
649 string lang_command_termination = "%\n";
651 // In some insets (such as Arguments), we cannot use \selectlanguage
652 bool const localswitch = !use_polyglossia
653 && text.inset().getLayout().forcelocalfontswitch();
655 lang_begin_command = lyxrc.language_command_local;
656 lang_end_command = "}";
657 lang_command_termination.clear();
660 if (par_lang != prev_lang
661 // check if we already put language command in TeXEnvironment()
662 && !(style.isEnvironment()
663 && (pit == 0 || (priorpar->layout() != par.layout()
664 && priorpar->getDepth() <= par.getDepth())
665 || priorpar->getDepth() < par.getDepth())))
667 if (!lang_end_command.empty() &&
668 prev_lang != outer_lang &&
671 os << from_ascii(subst(lang_end_command,
674 << lang_command_termination;
677 // We need to open a new language if we couldn't close the previous
678 // one (because there's no language_command_end); and even if we closed
679 // the previous one, if the current language is different than the
680 // outer_language (which is currently in effect once the previous one
682 if ((lang_end_command.empty() || par_lang != outer_lang)
683 && !par_lang.empty()) {
684 // If we're inside an inset, and that inset is within an \L or \R
685 // (or equivalents), then within the inset, too, any opposite
686 // language paragraph should appear within an \L or \R (in addition
687 // to, outside of, the normal language switch commands).
688 // This behavior is not correct for ArabTeX, though.
691 && par_language->lang() != "arabic_arabtex"
692 && outer_language->lang() != "arabic_arabtex"
693 // are we in an inset?
694 && runparams.local_font != 0
695 // is the inset within an \L or \R?
697 // FIXME: currently, we don't check this; this means that
698 // we'll have unnnecessary \L and \R commands, but that
699 // doesn't seem to hurt (though latex will complain)
701 // is this paragraph in the opposite direction?
702 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()) {
703 // FIXME: I don't have a working copy of the Arabi package, so
704 // I'm not sure if the farsi and arabic_arabi stuff is correct
706 if (par_language->lang() == "farsi")
708 else if (outer_language->lang() == "farsi")
710 else if (par_language->lang() == "arabic_arabi")
712 else if (outer_language->lang() == "arabic_arabi")
714 // remaining RTL languages currently is hebrew
715 else if (par_language->rightToLeft())
720 // With CJK, the CJK tag has to be closed first (see below)
721 if (runparams.encoding->package() != Encoding::CJK
722 && !par_lang.empty()) {
723 os << from_ascii(subst(
728 && !par_language->polyglossiaOpts().empty())
730 << from_ascii(par_language->polyglossiaOpts())
732 os << lang_command_termination;
737 // Switch file encoding if necessary; no need to do this for "default"
738 // encoding, since this only affects the position of the outputted
739 // \inputencoding command; the encoding switch will occur when necessary
740 if (bparams.inputenc == "auto"
741 && runparams.encoding->package() != Encoding::none) {
742 // Look ahead for future encoding changes.
743 // We try to output them at the beginning of the paragraph,
744 // since the \inputencoding command is not allowed e.g. in
745 // sections. For this reason we only set runparams.moving_arg
746 // after checking for the encoding change, otherwise the
747 // change would be always avoided by switchEncoding().
748 for (pos_type i = 0; i < par.size(); ++i) {
749 char_type const c = par.getChar(i);
750 Encoding const * const encoding =
751 par.getFontSettings(bparams, i).language()->encoding();
752 if (encoding->package() != Encoding::CJK
753 && runparams.encoding->package() == Encoding::inputenc
758 // All characters before c are in the ASCII range, and
759 // c is non-ASCII (but no inset), so change the
760 // encoding to that required by the language of c.
761 // With CJK, only add switch if we have CJK content at the beginning
763 if (i != 0 && encoding->package() == Encoding::CJK)
766 pair<bool, int> enc_switch = switchEncoding(os.os(),
767 bparams, runparams, *encoding);
768 // the following is necessary after a CJK environment in a multilingual
769 // context (nesting issue).
770 if (par_language->encoding()->package() == Encoding::CJK
771 && open_encoding_ != CJK && cjk_inherited_ == 0) {
772 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
773 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
774 open_encoding_ = CJK;
776 if (encoding->package() != Encoding::none && enc_switch.first) {
777 if (enc_switch.second > 0) {
778 // the '%' is necessary to prevent unwanted whitespace
781 // With CJK, the CJK tag had to be closed first (see above)
782 if (runparams.encoding->package() == Encoding::CJK
783 && !par_lang.empty()) {
784 os << from_ascii(subst(
788 << lang_command_termination;
790 runparams.encoding = encoding;
796 runparams.moving_arg |= style.needprotect;
797 Encoding const * const prev_encoding = runparams.encoding;
799 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
800 if (par.allowParagraphCustomization()) {
801 if (par.params().startOfAppendix()) {
802 os << "\\appendix\n";
805 if (!par.params().spacing().isDefault()
806 && (pit == 0 || !priorpar->hasSameLayout(par)))
808 os << from_ascii(par.params().spacing().writeEnvirBegin(useSetSpace))
812 if (style.isCommand()) {
817 parStartCommand(par, os, runparams, style);
818 Font const outerfont = text.outerFont(pit);
821 os << from_utf8(everypar);
822 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
824 // Make sure that \\par is done with the font of the last
825 // character if this has another size as the default.
826 // This is necessary because LaTeX (and LyX on the screen)
827 // calculates the space between the baselines according
828 // to this font. (Matthias)
830 // Is this really needed ? (Dekel)
831 // We do not need to use to change the font for the last paragraph
834 Font const font = par.empty()
835 ? par.getLayoutFont(bparams, outerfont)
836 : par.getFont(bparams, par.size() - 1, outerfont);
838 bool const is_command = style.isCommand();
840 if (style.resfont.size() != font.fontInfo().size()
844 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
845 } else if (is_command) {
847 if (!style.postcommandargs().empty())
848 latexArgInsets(par, os, runparams, style.postcommandargs(), "post:");
849 if (runparams.encoding != prev_encoding) {
850 runparams.encoding = prev_encoding;
851 if (!runparams.isFullUnicode())
852 os << setEncoding(prev_encoding->iconvName());
856 bool pending_newline = false;
857 bool unskip_newline = false;
858 switch (style.latextype) {
859 case LATEX_ITEM_ENVIRONMENT:
860 case LATEX_LIST_ENVIRONMENT:
861 if (nextpar && (par.params().depth() < nextpar->params().depth()))
862 pending_newline = true;
864 case LATEX_ENVIRONMENT: {
865 // if its the last paragraph of the current environment
866 // skip it otherwise fall through
868 && (nextpar->layout() != par.layout()
869 || nextpar->params().depth() != par.params().depth()))
873 // fall through possible
875 // we don't need it for the last paragraph!!!
877 pending_newline = true;
880 if (par.allowParagraphCustomization()) {
881 if (!par.params().spacing().isDefault()
882 && (runparams.isLastPar || !nextpar->hasSameLayout(par))) {
885 os << from_ascii(par.params().spacing().writeEnvirEnd(useSetSpace));
886 pending_newline = true;
890 // Closing the language is needed for the last paragraph; it is also
891 // needed if we're within an \L or \R that we may have opened above (not
892 // necessarily in this paragraph) and are about to close.
893 bool closing_rtl_ltr_environment = !use_polyglossia
895 && (par_language->lang() != "arabic_arabtex"
896 && outer_language->lang() != "arabic_arabtex")
897 // have we opened an \L or \R environment?
898 && runparams.local_font != 0
899 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()
900 // are we about to close the language?
901 &&((nextpar && par_language->babel() != (nextpar->getParLanguage(bparams))->babel())
902 || (runparams.isLastPar && par_language->babel() != outer_language->babel()));
904 if (closing_rtl_ltr_environment
905 || (runparams.isLastPar
906 && ((!use_polyglossia && par_language->babel() != outer_language->babel())
907 || (use_polyglossia && par_language->polyglossia() != outer_language->polyglossia())))) {
908 // Since \selectlanguage write the language to the aux file,
909 // we need to reset the language at the end of footnote or
915 // when the paragraph uses CJK, the language has to be closed earlier
916 if (font.language()->encoding()->package() != Encoding::CJK) {
917 if (lang_end_command.empty()) {
918 // If this is a child, we should restore the
919 // master language after the last paragraph.
920 Language const * const current_language =
921 (runparams.isLastPar && runparams.master_language)
922 ? runparams.master_language
924 string const current_lang = use_polyglossia
925 ? getPolyglossiaEnvName(current_language)
926 : current_language->babel();
927 if (!current_lang.empty()) {
928 os << from_ascii(subst(
932 pending_newline = !localswitch;
933 unskip_newline = !localswitch;
935 } else if (!par_lang.empty()) {
936 os << from_ascii(subst(
940 pending_newline = !localswitch;
941 unskip_newline = !localswitch;
945 if (closing_rtl_ltr_environment)
948 if (pending_newline) {
950 // prevent unwanted whitespace
955 // if this is a CJK-paragraph and the next isn't, close CJK
956 // also if the next paragraph is a multilingual environment (because of nesting)
958 && open_encoding_ == CJK
959 && (nextpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK
960 || (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
961 // inbetween environments, CJK has to be closed later (nesting!)
962 && (!style.isEnvironment() || !nextpar->layout().isEnvironment())) {
963 os << "\\end{CJK}\n";
964 open_encoding_ = none;
967 // If this is the last paragraph, close the CJK environment
968 // if necessary. If it's an environment, we'll have to \end that first.
969 if (runparams.isLastPar && !style.isEnvironment()) {
970 switch (open_encoding_) {
972 // do nothing at the end of child documents
973 if (maintext && buf.masterBuffer() != &buf)
977 os << "\n\\end{CJK}\n";
981 open_encoding_ = none;
986 open_encoding_ = none;
996 // If this is the last paragraph, and a local_font was set upon entering
997 // the inset, and we're using "auto" or "default" encoding, the encoding
998 // should be set back to that local_font's encoding.
999 // However, do not change the encoding when a fully unicode aware backend
1000 // such as XeTeX is used.
1001 if (runparams.isLastPar && runparams_in.local_font != 0
1002 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
1003 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
1004 && (!runparams.isFullUnicode())) {
1005 runparams_in.encoding = runparams_in.local_font->language()->encoding();
1006 os << setEncoding(runparams_in.encoding->iconvName());
1008 // Otherwise, the current encoding should be set for the next paragraph.
1010 runparams_in.encoding = runparams.encoding;
1013 // we don't need a newline for the last paragraph!!!
1014 // Note from JMarc: we will re-add a \n explicitly in
1015 // TeXEnvironment, because it is needed in this case
1017 Layout const & next_layout = nextpar->layout();
1018 if (style == next_layout
1019 // no blank lines before environments!
1020 || !next_layout.isEnvironment()
1021 // unless there's a depth change
1022 // FIXME What we really want to do here is put every \begin and \end
1023 // tag on a new line (which was not the case with nested environments).
1024 // But in the present state of play, we don't have access to the
1025 // information whether the current TeX row is empty or not.
1026 // For some ideas about how to fix this, see this thread:
1027 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
1028 || nextpar->params().depth() != par.params().depth()) {
1033 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " done; ptr "
1034 << &par << " next " << nextpar);
1040 // LaTeX all paragraphs
1041 void latexParagraphs(Buffer const & buf,
1044 OutputParams const & runparams,
1045 string const & everypar)
1047 LASSERT(runparams.par_begin <= runparams.par_end,
1048 { os << "% LaTeX Output Error\n"; return; } );
1050 BufferParams const & bparams = buf.params();
1052 bool const maintext = text.isMainText();
1053 bool const is_child = buf.masterBuffer() != &buf;
1055 // Open a CJK environment at the beginning of the main buffer
1056 // if the document's language is a CJK language
1057 // (but not in child documents)
1058 if (maintext && !is_child
1059 && bparams.encoding().package() == Encoding::CJK) {
1060 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
1061 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
1062 open_encoding_ = CJK;
1064 // if "auto begin" is switched off, explicitly switch the
1065 // language on at start
1066 string const mainlang = runparams.use_polyglossia
1067 ? getPolyglossiaEnvName(bparams.language)
1068 : bparams.language->babel();
1069 string const lang_begin_command = runparams.use_polyglossia ?
1070 "\\begin{$$lang}" : lyxrc.language_command_begin;
1072 if (maintext && !lyxrc.language_auto_begin &&
1073 !mainlang.empty()) {
1075 os << from_utf8(subst(lang_begin_command,
1078 if (runparams.use_polyglossia
1079 && !bparams.language->polyglossiaOpts().empty())
1081 << from_ascii(bparams.language->polyglossiaOpts())
1086 ParagraphList const & paragraphs = text.paragraphs();
1088 if (runparams.par_begin == runparams.par_end) {
1089 // The full doc will be exported but it is easier to just rely on
1090 // runparams range parameters that will be passed TeXEnvironment.
1091 runparams.par_begin = 0;
1092 runparams.par_end = paragraphs.size();
1095 pit_type pit = runparams.par_begin;
1096 // lastpit is for the language check after the loop.
1097 pit_type lastpit = pit;
1098 // variables used in the loop:
1099 bool was_title = false;
1100 bool already_title = false;
1101 DocumentClass const & tclass = bparams.documentClass();
1103 for (; pit < runparams.par_end; ++pit) {
1105 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
1107 // FIXME This check should not be needed. We should
1108 // perhaps issue an error if it is.
1109 Layout const & layout = text.inset().forcePlainLayout() ?
1110 tclass.plainLayout() : par->layout();
1112 if (layout.intitle) {
1113 if (already_title) {
1114 LYXERR0("Error in latexParagraphs: You"
1115 " should not mix title layouts"
1116 " with normal ones.");
1117 } else if (!was_title) {
1119 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1121 << from_ascii(tclass.titlename())
1125 } else if (was_title && !already_title) {
1126 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1127 os << "\\end{" << from_ascii(tclass.titlename())
1131 os << "\\" << from_ascii(tclass.titlename())
1134 already_title = true;
1139 if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
1140 // This is a standard top level paragraph, TeX it and continue.
1141 TeXOnePar(buf, text, pit, os, runparams, everypar);
1145 TeXEnvironmentData const data =
1146 prepareEnvironment(buf, text, par, os, runparams);
1147 // pit can be changed in TeXEnvironment.
1148 TeXEnvironment(buf, text, runparams, pit, os);
1149 finishEnvironment(os, runparams, data);
1152 if (pit == runparams.par_end) {
1153 // Make sure that the last paragraph is
1154 // correctly terminated (because TeXOnePar does
1155 // not add a \n in this case)
1159 // It might be that we only have a title in this document
1160 if (was_title && !already_title) {
1161 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1162 os << "\\end{" << from_ascii(tclass.titlename())
1165 os << "\\" << from_ascii(tclass.titlename())
1170 // if "auto end" is switched off, explicitly close the language at the end
1171 // but only if the last par is in a babel language
1172 string const lang_end_command = runparams.use_polyglossia ?
1173 "\\end{$$lang}" : lyxrc.language_command_end;
1174 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1175 paragraphs.at(lastpit).getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1176 os << from_utf8(subst(lang_end_command,
1182 // If the last paragraph is an environment, we'll have to close
1183 // CJK at the very end to do proper nesting.
1184 if (maintext && !is_child && open_encoding_ == CJK) {
1185 os << "\\end{CJK}\n";
1186 open_encoding_ = none;
1189 // reset inherited encoding
1190 if (cjk_inherited_ > 0) {
1191 cjk_inherited_ -= 1;
1192 if (cjk_inherited_ == 0)
1193 open_encoding_ = CJK;
1198 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1199 OutputParams const & runparams, Encoding const & newEnc,
1202 Encoding const & oldEnc = *runparams.encoding;
1203 bool moving_arg = runparams.moving_arg;
1204 // If we switch from/to CJK, we need to switch anyway, despite custom inputenc
1205 bool const from_to_cjk =
1206 (oldEnc.package() == Encoding::CJK && newEnc.package() != Encoding::CJK)
1207 || (oldEnc.package() != Encoding::CJK && newEnc.package() == Encoding::CJK);
1208 if (!force && !from_to_cjk
1209 && ((bparams.inputenc != "auto" && bparams.inputenc != "default") || moving_arg))
1210 return make_pair(false, 0);
1212 // Do nothing if the encoding is unchanged.
1213 if (oldEnc.name() == newEnc.name())
1214 return make_pair(false, 0);
1216 // FIXME We ignore encoding switches from/to encodings that do
1217 // neither support the inputenc package nor the CJK package here.
1218 // This does of course only work in special cases (e.g. switch from
1219 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1220 // but it is the best we can do
1221 if (oldEnc.package() == Encoding::none
1222 || newEnc.package() == Encoding::none)
1223 return make_pair(false, 0);
1225 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1226 << oldEnc.name() << " to " << newEnc.name());
1227 os << setEncoding(newEnc.iconvName());
1228 if (bparams.inputenc == "default")
1229 return make_pair(true, 0);
1231 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1232 switch (newEnc.package()) {
1233 case Encoding::none:
1234 case Encoding::japanese:
1235 // shouldn't ever reach here, see above
1236 return make_pair(true, 0);
1237 case Encoding::inputenc: {
1238 int count = inputenc_arg.length();
1239 if (oldEnc.package() == Encoding::CJK &&
1240 open_encoding_ == CJK) {
1242 open_encoding_ = none;
1245 else if (oldEnc.package() == Encoding::inputenc &&
1246 open_encoding_ == inputenc) {
1248 open_encoding_ = none;
1251 if (runparams.local_font != 0
1252 && oldEnc.package() == Encoding::CJK) {
1253 // within insets, \inputenc switches need
1254 // to be embraced within \bgroup...\egroup;
1258 open_encoding_ = inputenc;
1260 // with the japanese option, inputenc is omitted.
1261 if (runparams.use_japanese)
1262 return make_pair(true, count);
1263 os << "\\inputencoding{" << inputenc_arg << '}';
1264 return make_pair(true, count + 16);
1266 case Encoding::CJK: {
1267 int count = inputenc_arg.length();
1268 if (oldEnc.package() == Encoding::CJK &&
1269 open_encoding_ == CJK) {
1273 if (oldEnc.package() == Encoding::inputenc &&
1274 open_encoding_ == inputenc) {
1278 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1279 << from_ascii(bparams.fonts_cjk) << "}";
1280 open_encoding_ = CJK;
1281 return make_pair(true, count + 15);
1284 // Dead code to avoid a warning:
1285 return make_pair(true, 0);