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;
56 static int open_encoding_ = none;
57 static int cjk_inherited_ = 0;
58 Language const * prev_env_language_ = 0;
61 string const getPolyglossiaEnvName(Language const * lang)
63 string result = lang->polyglossia();
64 if (result == "arabic")
65 // exceptional spelling; see polyglossia docs.
71 struct TeXEnvironmentData
75 Language const * par_language;
76 Encoding const * prev_encoding;
81 static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
83 ParagraphList::const_iterator pit,
85 OutputParams const & runparams)
87 TeXEnvironmentData data;
89 BufferParams const & bparams = buf.params();
91 // FIXME This test should not be necessary.
92 // We should perhaps issue an error if it is.
93 Layout const & style = text.inset().forcePlainLayout() ?
94 bparams.documentClass().plainLayout() : pit->layout();
96 ParagraphList const & paragraphs = text.paragraphs();
97 ParagraphList::const_iterator const priorpit =
98 pit == paragraphs.begin() ? pit : boost::prior(pit);
100 bool const use_prev_env_language = prev_env_language_ != 0
101 && priorpit->layout().isEnvironment()
102 && (priorpit->getDepth() > pit->getDepth()
103 || (priorpit->getDepth() == pit->getDepth()
104 && priorpit->layout() != pit->layout()));
106 data.prev_encoding = runparams.encoding;
107 data.par_language = pit->getParLanguage(bparams);
108 Language const * const doc_language = bparams.language;
109 Language const * const prev_par_language =
110 (pit != paragraphs.begin())
111 ? (use_prev_env_language ? prev_env_language_
112 : priorpit->getParLanguage(bparams))
115 bool const use_polyglossia = runparams.use_polyglossia;
116 string const par_lang = use_polyglossia ?
117 getPolyglossiaEnvName(data.par_language) : data.par_language->babel();
118 string const prev_par_lang = use_polyglossia ?
119 getPolyglossiaEnvName(prev_par_language) : prev_par_language->babel();
120 string const doc_lang = use_polyglossia ?
121 getPolyglossiaEnvName(doc_language) : doc_language->babel();
122 string const lang_begin_command = use_polyglossia ?
123 "\\begin{$$lang}" : lyxrc.language_command_begin;
124 string const lang_end_command = use_polyglossia ?
125 "\\end{$$lang}" : lyxrc.language_command_end;
127 if (par_lang != prev_par_lang) {
128 if (!lang_end_command.empty() &&
129 prev_par_lang != doc_lang &&
130 !prev_par_lang.empty()) {
131 os << from_ascii(subst(
135 // the '%' is necessary to prevent unwanted whitespace
139 if ((lang_end_command.empty() ||
140 par_lang != doc_lang) &&
142 os << from_ascii(subst(
147 && !data.par_language->polyglossiaOpts().empty())
149 << from_ascii(data.par_language->polyglossiaOpts())
151 // the '%' is necessary to prevent unwanted whitespace
156 data.leftindent_open = false;
157 if (!pit->params().leftIndent().zero()) {
158 os << "\\begin{LyXParagraphLeftIndent}{"
159 << from_ascii(pit->params().leftIndent().asLatexString())
161 data.leftindent_open = true;
164 if (style.isEnvironment()) {
165 os << "\\begin{" << from_ascii(style.latexname()) << '}';
166 if (!style.latexargs().empty()) {
167 OutputParams rp = runparams;
168 rp.local_font = &pit->getFirstFontSettings(bparams);
169 latexArgInsets(paragraphs, pit, os, rp, style.latexargs());
171 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
173 << pit->params().labelWidthString()
175 } else if (style.labeltype == LABEL_BIBLIO) {
176 if (pit->params().labelWidthString().empty())
177 os << '{' << bibitemWidest(buf, runparams) << "}\n";
180 << pit->params().labelWidthString()
183 os << from_ascii(style.latexparam()) << '\n';
187 // in multilingual environments, the CJK tags have to be nested properly
188 data.cjk_nested = false;
189 if (data.par_language->encoding()->package() == Encoding::CJK &&
190 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
191 if (prev_par_language->encoding()->package() == Encoding::CJK)
192 os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
193 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
194 open_encoding_ = CJK;
195 data.cjk_nested = true;
201 static void finishEnvironment(otexstream & os, OutputParams const & runparams,
202 TeXEnvironmentData const & data)
204 if (open_encoding_ == CJK && data.cjk_nested) {
205 // We need to close the encoding even if it does not change
206 // to do correct environment nesting
207 os << "\\end{CJK}\n";
208 open_encoding_ = none;
211 if (data.style->isEnvironment()) {
212 os << "\\end{" << from_ascii(data.style->latexname()) << "}\n";
213 prev_env_language_ = data.par_language;
214 if (runparams.encoding != data.prev_encoding) {
215 runparams.encoding = data.prev_encoding;
216 if (!runparams.isFullUnicode())
217 os << setEncoding(data.prev_encoding->iconvName());
221 if (data.leftindent_open) {
222 os << "\\end{LyXParagraphLeftIndent}\n";
223 prev_env_language_ = data.par_language;
224 if (runparams.encoding != data.prev_encoding) {
225 runparams.encoding = data.prev_encoding;
226 if (!runparams.isFullUnicode())
227 os << setEncoding(data.prev_encoding->iconvName());
233 void TeXEnvironment(Buffer const & buf, Text const & text,
234 OutputParams const & runparams,
235 pit_type & pit, otexstream & os)
237 ParagraphList const & paragraphs = text.paragraphs();
238 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
239 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
241 Layout const & current_layout = par->layout();
242 depth_type const current_depth = par->params().depth();
243 Length const & current_left_indent = par->params().leftIndent();
245 // This is for debugging purpose at the end.
246 pit_type const par_begin = pit;
247 for (; pit < runparams.par_end; ++pit) {
248 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
250 // check first if this is an higher depth paragraph.
251 bool go_out = (par->params().depth() < current_depth);
252 if (par->params().depth() == current_depth) {
253 // This environment is finished.
254 go_out |= (par->layout() != current_layout);
255 go_out |= (par->params().leftIndent() != current_left_indent);
258 // nothing to do here, restore pit and go out.
263 if (par->layout() == current_layout
264 && par->params().depth() == current_depth
265 && par->params().leftIndent() == current_left_indent) {
266 // We are still in the same environment so TeXOnePar and continue;
267 TeXOnePar(buf, text, pit, os, runparams);
271 // We are now in a deeper environment.
272 // Either par->layout() != current_layout
273 // Or par->params().depth() > current_depth
274 // Or par->params().leftIndent() != current_left_indent)
276 if (par->layout().isParagraph()) {
277 // FIXME (Lgb): How to handle this?
278 //&& !suffixIs(os, "\n\n")
280 // (ARRae) There should be at least one '\n' already but we need there to
281 // be two for Standard paragraphs that are depth-increment'ed to be
282 // output correctly. However, tables can also be paragraphs so
283 // don't adjust them.
285 // FIXME (Lgb): Will it ever harm to have one '\n' too
286 // many? i.e. that we sometimes will have
291 // FIXME This test should not be necessary.
292 // We should perhaps issue an error if it is.
293 bool const force_plain_layout = text.inset().forcePlainLayout();
294 Layout const & style = force_plain_layout
295 ? buf.params().documentClass().plainLayout()
298 if (!style.isEnvironment()) {
299 // This is a standard paragraph, no need to call TeXEnvironment.
300 TeXOnePar(buf, text, pit, os, runparams);
304 // This is a new environment.
305 TeXEnvironmentData const data =
306 prepareEnvironment(buf, text, par, os, runparams);
307 // Recursive call to TeXEnvironment!
308 TeXEnvironment(buf, text, runparams, pit, os);
309 finishEnvironment(os, runparams, data);
312 if (pit != runparams.par_end)
313 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << par_begin << " done.");
317 void getArgInsets(otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
318 map<int, lyx::InsetArgument const *> ilist, vector<string> required, string const & prefix)
320 unsigned int const argnr = latexargs.size();
324 for (unsigned int i = 1; i <= argnr; ++i) {
325 map<int, InsetArgument const *>::const_iterator lit = ilist.find(i);
326 bool inserted = false;
327 if (lit != ilist.end()) {
328 InsetArgument const * ins = (*lit).second;
330 Layout::LaTeXArgMap::const_iterator const lait =
331 latexargs.find(ins->name());
332 if (lait != latexargs.end()) {
333 Layout::latexarg arg = (*lait).second;
334 docstring ldelim = arg.mandatory ?
335 from_ascii("{") : from_ascii("[");
336 docstring rdelim = arg.mandatory ?
337 from_ascii("}") : from_ascii("]");
338 if (!arg.ldelim.empty())
340 if (!arg.rdelim.empty())
342 ins->latexArgument(os, runparams, ldelim, rdelim, arg.presetarg);
348 Layout::LaTeXArgMap::const_iterator lait = latexargs.begin();
349 Layout::LaTeXArgMap::const_iterator const laend = latexargs.end();
350 for (; lait != laend; ++lait) {
351 string const name = prefix + convert<string>(i);
352 if ((*lait).first == name) {
353 Layout::latexarg arg = (*lait).second;
354 docstring preset = arg.presetarg;
355 if (!arg.defaultarg.empty()) {
358 preset += arg.defaultarg;
361 docstring ldelim = arg.ldelim.empty() ?
362 from_ascii("{") : arg.ldelim;
363 docstring rdelim = arg.rdelim.empty() ?
364 from_ascii("}") : arg.rdelim;
365 os << ldelim << preset << rdelim;
366 } else if (!preset.empty()) {
367 docstring ldelim = arg.ldelim.empty() ?
368 from_ascii("[") : arg.ldelim;
369 docstring rdelim = arg.rdelim.empty() ?
370 from_ascii("]") : arg.rdelim;
371 os << ldelim << preset << rdelim;
372 } else if (find(required.begin(), required.end(),
373 (*lait).first) != required.end()) {
374 docstring ldelim = arg.ldelim.empty() ?
375 from_ascii("[") : arg.ldelim;
376 docstring rdelim = arg.rdelim.empty() ?
377 from_ascii("]") : arg.rdelim;
378 os << ldelim << rdelim;
391 void latexArgInsets(Paragraph const & par, otexstream & os,
392 OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs, string const & prefix)
394 map<int, InsetArgument const *> ilist;
395 vector<string> required;
397 InsetList::const_iterator it = par.insetList().begin();
398 InsetList::const_iterator end = par.insetList().end();
399 for (; it != end; ++it) {
400 if (it->inset->lyxCode() == ARG_CODE) {
401 InsetArgument const * ins =
402 static_cast<InsetArgument const *>(it->inset);
403 if (ins->name().empty())
404 LYXERR0("Error: Unnamed argument inset!");
406 string const name = prefix.empty() ? ins->name() : split(ins->name(), ':');
407 unsigned int const nr = convert<unsigned int>(name);
409 Layout::LaTeXArgMap::const_iterator const lit =
410 latexargs.find(ins->name());
411 if (lit != latexargs.end()) {
412 Layout::latexarg const & arg = (*lit).second;
413 if (!arg.requires.empty()) {
414 vector<string> req = getVectorFromString(arg.requires);
415 required.insert(required.end(), req.begin(), req.end());
421 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
425 void latexArgInsets(ParagraphList const & pars, ParagraphList::const_iterator pit,
426 otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
427 string const & prefix)
429 map<int, InsetArgument const *> ilist;
430 vector<string> required;
432 depth_type const current_depth = pit->params().depth();
433 Layout const current_layout = pit->layout();
435 // get the first paragraph in sequence with this layout and depth
438 if (boost::prior(pit, offset) == pars.begin())
440 ParagraphList::const_iterator priorpit = boost::prior(pit, offset + 1);
441 if (priorpit->layout() == current_layout
442 && priorpit->params().depth() == current_depth)
448 ParagraphList::const_iterator spit = boost::prior(pit, offset);
450 for (; spit != pars.end(); ++spit) {
451 if (spit->layout() != current_layout || spit->params().depth() < current_depth)
453 if (spit->params().depth() > current_depth)
455 InsetList::const_iterator it = spit->insetList().begin();
456 InsetList::const_iterator end = spit->insetList().end();
457 for (; it != end; ++it) {
458 if (it->inset->lyxCode() == ARG_CODE) {
459 InsetArgument const * ins =
460 static_cast<InsetArgument const *>(it->inset);
461 if (ins->name().empty())
462 LYXERR0("Error: Unnamed argument inset!");
464 string const name = prefix.empty() ? ins->name() : split(ins->name(), ':');
465 unsigned int const nr = convert<unsigned int>(name);
466 if (ilist.find(nr) == ilist.end())
468 Layout::LaTeXArgMap::const_iterator const lit =
469 latexargs.find(ins->name());
470 if (lit != latexargs.end()) {
471 Layout::latexarg const & arg = (*lit).second;
472 if (!arg.requires.empty()) {
473 vector<string> req = getVectorFromString(arg.requires);
474 required.insert(required.end(), req.begin(), req.end());
481 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
486 // output the proper paragraph start according to latextype.
487 void parStartCommand(Paragraph const & par, otexstream & os,
488 OutputParams const & runparams, Layout const & style)
490 switch (style.latextype) {
492 os << '\\' << from_ascii(style.latexname());
495 if (!style.latexargs().empty())
496 latexArgInsets(par, os, runparams, style.latexargs());
497 os << from_ascii(style.latexparam());
499 case LATEX_ITEM_ENVIRONMENT:
500 case LATEX_LIST_ENVIRONMENT:
501 os << "\\" + style.itemcommand();
503 if (!style.itemargs().empty())
504 latexArgInsets(par, os, runparams, style.itemargs(), "item:");
507 case LATEX_BIB_ENVIRONMENT:
508 // ignore this, the inset will write itself
517 // FIXME: this should be anonymous
518 void TeXOnePar(Buffer const & buf,
522 OutputParams const & runparams_in,
523 string const & everypar,
524 int start_pos, int end_pos)
526 BufferParams const & bparams = runparams_in.is_child
527 ? buf.masterBuffer()->params() : buf.params();
528 ParagraphList const & paragraphs = text.paragraphs();
529 Paragraph const & par = paragraphs.at(pit);
530 // FIXME This check should not really be needed.
531 // Perhaps we should issue an error if it is.
532 Layout const style = text.inset().forcePlainLayout() ?
533 bparams.documentClass().plainLayout() : par.layout();
535 if (style.inpreamble)
538 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
541 OutputParams runparams = runparams_in;
542 runparams.isLastPar = (pit == pit_type(paragraphs.size() - 1));
543 // We reinitialze par begin and end to be on the safe side
544 // with embedded inset as we don't know if they set those
546 runparams.par_begin = 0;
547 runparams.par_end = 0;
549 bool const maintext = text.isMainText();
550 // we are at the beginning of an inset and CJK is already open;
551 // we count inheritation levels to get the inset nesting right.
552 if (pit == 0 && !maintext
553 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
555 open_encoding_ = none;
558 if (text.inset().isPassThru()) {
559 Font const outerfont = text.outerFont(pit);
561 // No newline before first paragraph in this lyxtext
564 if (!text.inset().getLayout().parbreakIsNewline())
568 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
572 Paragraph const * nextpar = runparams.isLastPar
573 ? 0 : ¶graphs.at(pit + 1);
575 if (style.pass_thru) {
576 Font const outerfont = text.outerFont(pit);
577 parStartCommand(par, os, runparams, style);
579 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
581 // I did not create a parEndCommand for this minuscule
582 // task because in the other user of parStartCommand
583 // the code is different (JMarc)
584 if (style.isCommand())
588 if (!style.parbreak_is_newline) {
590 } else if (nextpar && !style.isEnvironment()) {
591 Layout const nextstyle = text.inset().forcePlainLayout()
592 ? bparams.documentClass().plainLayout()
594 if (nextstyle.name() != style.name())
601 // This paragraph's language
602 Language const * const par_language = par.getParLanguage(bparams);
603 // The document's language
604 Language const * const doc_language = bparams.language;
605 // The language that was in effect when the environment this paragraph is
606 // inside of was opened
607 Language const * const outer_language =
608 (runparams.local_font != 0) ?
609 runparams.local_font->language() : doc_language;
611 Paragraph const * priorpar = (pit == 0) ? 0 : ¶graphs.at(pit - 1);
613 // The previous language that was in effect is the language of the
614 // previous paragraph, unless the previous paragraph is inside an
615 // environment with nesting depth greater than (or equal to, but with
616 // a different layout) the current one. If there is no previous
617 // paragraph, the previous language is the outer language.
618 bool const use_prev_env_language = prev_env_language_ != 0
620 && priorpar->layout().isEnvironment()
621 && (priorpar->getDepth() > par.getDepth()
622 || (priorpar->getDepth() == par.getDepth()
623 && priorpar->layout() != par.layout()));
624 Language const * const prev_language =
626 ? (use_prev_env_language ? prev_env_language_
627 : priorpar->getParLanguage(bparams))
631 bool const use_polyglossia = runparams.use_polyglossia;
632 string const par_lang = use_polyglossia ?
633 getPolyglossiaEnvName(par_language): par_language->babel();
634 string const prev_lang = use_polyglossia ?
635 getPolyglossiaEnvName(prev_language) : prev_language->babel();
636 string const doc_lang = use_polyglossia ?
637 getPolyglossiaEnvName(doc_language) : doc_language->babel();
638 string const outer_lang = use_polyglossia ?
639 getPolyglossiaEnvName(outer_language) : outer_language->babel();
640 string lang_begin_command = use_polyglossia ?
641 "\\begin{$$lang}" : lyxrc.language_command_begin;
642 string lang_end_command = use_polyglossia ?
643 "\\end{$$lang}" : lyxrc.language_command_end;
644 // the '%' is necessary to prevent unwanted whitespace
645 string lang_command_termination = "%\n";
647 // In some insets (such as Arguments), we cannot use \selectlanguage
648 bool const localswitch = !use_polyglossia
649 && text.inset().getLayout().forcelocalfontswitch();
651 lang_begin_command = lyxrc.language_command_local;
652 lang_end_command = "}";
653 lang_command_termination.clear();
656 if (par_lang != prev_lang
657 // check if we already put language command in TeXEnvironment()
658 && !(style.isEnvironment()
659 && (pit == 0 || (priorpar->layout() != par.layout()
660 && priorpar->getDepth() <= par.getDepth())
661 || priorpar->getDepth() < par.getDepth())))
663 if (!lang_end_command.empty() &&
664 prev_lang != outer_lang &&
667 os << from_ascii(subst(lang_end_command,
670 << lang_command_termination;
673 // We need to open a new language if we couldn't close the previous
674 // one (because there's no language_command_end); and even if we closed
675 // the previous one, if the current language is different than the
676 // outer_language (which is currently in effect once the previous one
678 if ((lang_end_command.empty() || par_lang != outer_lang)
679 && !par_lang.empty()) {
680 // If we're inside an inset, and that inset is within an \L or \R
681 // (or equivalents), then within the inset, too, any opposite
682 // language paragraph should appear within an \L or \R (in addition
683 // to, outside of, the normal language switch commands).
684 // This behavior is not correct for ArabTeX, though.
687 && par_language->lang() != "arabic_arabtex"
688 && outer_language->lang() != "arabic_arabtex"
689 // are we in an inset?
690 && runparams.local_font != 0
691 // is the inset within an \L or \R?
693 // FIXME: currently, we don't check this; this means that
694 // we'll have unnnecessary \L and \R commands, but that
695 // doesn't seem to hurt (though latex will complain)
697 // is this paragraph in the opposite direction?
698 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()) {
699 // FIXME: I don't have a working copy of the Arabi package, so
700 // I'm not sure if the farsi and arabic_arabi stuff is correct
702 if (par_language->lang() == "farsi")
704 else if (outer_language->lang() == "farsi")
706 else if (par_language->lang() == "arabic_arabi")
708 else if (outer_language->lang() == "arabic_arabi")
710 // remaining RTL languages currently is hebrew
711 else if (par_language->rightToLeft())
716 // With CJK, the CJK tag has to be closed first (see below)
717 if (runparams.encoding->package() != Encoding::CJK
718 && !par_lang.empty()) {
719 os << from_ascii(subst(
724 && !par_language->polyglossiaOpts().empty())
726 << from_ascii(par_language->polyglossiaOpts())
728 os << lang_command_termination;
733 // Switch file encoding if necessary; no need to do this for "default"
734 // encoding, since this only affects the position of the outputted
735 // \inputencoding command; the encoding switch will occur when necessary
736 if (bparams.inputenc == "auto"
737 && runparams.encoding->package() != Encoding::none) {
738 // Look ahead for future encoding changes.
739 // We try to output them at the beginning of the paragraph,
740 // since the \inputencoding command is not allowed e.g. in
741 // sections. For this reason we only set runparams.moving_arg
742 // after checking for the encoding change, otherwise the
743 // change would be always avoided by switchEncoding().
744 for (pos_type i = 0; i < par.size(); ++i) {
745 char_type const c = par.getChar(i);
746 Encoding const * const encoding =
747 par.getFontSettings(bparams, i).language()->encoding();
748 if (encoding->package() != Encoding::CJK
749 && runparams.encoding->package() == Encoding::inputenc
754 // All characters before c are in the ASCII range, and
755 // c is non-ASCII (but no inset), so change the
756 // encoding to that required by the language of c.
757 // With CJK, only add switch if we have CJK content at the beginning
759 if (i != 0 && encoding->package() == Encoding::CJK)
762 pair<bool, int> enc_switch = switchEncoding(os.os(),
763 bparams, runparams, *encoding);
764 // the following is necessary after a CJK environment in a multilingual
765 // context (nesting issue).
766 if (par_language->encoding()->package() == Encoding::CJK
767 && open_encoding_ != CJK && cjk_inherited_ == 0) {
768 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
769 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
770 open_encoding_ = CJK;
772 if (encoding->package() != Encoding::none && enc_switch.first) {
773 if (enc_switch.second > 0) {
774 // the '%' is necessary to prevent unwanted whitespace
777 // With CJK, the CJK tag had to be closed first (see above)
778 if (runparams.encoding->package() == Encoding::CJK
779 && !par_lang.empty()) {
780 os << from_ascii(subst(
784 << lang_command_termination;
786 runparams.encoding = encoding;
792 runparams.moving_arg |= style.needprotect;
793 Encoding const * const prev_encoding = runparams.encoding;
795 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
796 if (par.allowParagraphCustomization()) {
797 if (par.params().startOfAppendix()) {
798 os << "\\appendix\n";
801 if (!par.params().spacing().isDefault()
802 && (pit == 0 || !priorpar->hasSameLayout(par)))
804 os << from_ascii(par.params().spacing().writeEnvirBegin(useSetSpace))
808 if (style.isCommand()) {
813 parStartCommand(par, os, runparams, style);
814 Font const outerfont = text.outerFont(pit);
817 os << from_utf8(everypar);
818 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
820 // Make sure that \\par is done with the font of the last
821 // character if this has another size as the default.
822 // This is necessary because LaTeX (and LyX on the screen)
823 // calculates the space between the baselines according
824 // to this font. (Matthias)
826 // Is this really needed ? (Dekel)
827 // We do not need to use to change the font for the last paragraph
830 Font const font = par.empty()
831 ? par.getLayoutFont(bparams, outerfont)
832 : par.getFont(bparams, par.size() - 1, outerfont);
834 bool const is_command = style.isCommand();
836 if (style.resfont.size() != font.fontInfo().size()
840 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
841 } else if (is_command) {
843 if (!style.postcommandargs().empty())
844 latexArgInsets(par, os, runparams, style.postcommandargs(), "post:");
845 if (runparams.encoding != prev_encoding) {
846 runparams.encoding = prev_encoding;
847 if (!runparams.isFullUnicode())
848 os << setEncoding(prev_encoding->iconvName());
852 bool pending_newline = false;
853 bool unskip_newline = false;
854 switch (style.latextype) {
855 case LATEX_ITEM_ENVIRONMENT:
856 case LATEX_LIST_ENVIRONMENT:
857 if (nextpar && (par.params().depth() < nextpar->params().depth()))
858 pending_newline = true;
860 case LATEX_ENVIRONMENT: {
861 // if its the last paragraph of the current environment
862 // skip it otherwise fall through
864 && (nextpar->layout() != par.layout()
865 || nextpar->params().depth() != par.params().depth()))
869 // fall through possible
871 // we don't need it for the last paragraph!!!
873 pending_newline = true;
876 if (par.allowParagraphCustomization()) {
877 if (!par.params().spacing().isDefault()
878 && (runparams.isLastPar || !nextpar->hasSameLayout(par))) {
881 os << from_ascii(par.params().spacing().writeEnvirEnd(useSetSpace));
882 pending_newline = true;
886 // Closing the language is needed for the last paragraph; it is also
887 // needed if we're within an \L or \R that we may have opened above (not
888 // necessarily in this paragraph) and are about to close.
889 bool closing_rtl_ltr_environment = !use_polyglossia
891 && (par_language->lang() != "arabic_arabtex"
892 && outer_language->lang() != "arabic_arabtex")
893 // have we opened an \L or \R environment?
894 && runparams.local_font != 0
895 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()
896 // are we about to close the language?
897 &&((nextpar && par_language->babel() != (nextpar->getParLanguage(bparams))->babel())
898 || (runparams.isLastPar && par_language->babel() != outer_language->babel()));
900 if (closing_rtl_ltr_environment
901 || (runparams.isLastPar
902 && ((!use_polyglossia && par_language->babel() != outer_language->babel())
903 || (use_polyglossia && par_language->polyglossia() != outer_language->polyglossia())))) {
904 // Since \selectlanguage write the language to the aux file,
905 // we need to reset the language at the end of footnote or
911 // when the paragraph uses CJK, the language has to be closed earlier
912 if (font.language()->encoding()->package() != Encoding::CJK) {
913 if (lang_end_command.empty()) {
914 // If this is a child, we should restore the
915 // master language after the last paragraph.
916 Language const * const current_language =
917 (runparams.isLastPar && runparams.master_language)
918 ? runparams.master_language
920 string const current_lang = use_polyglossia
921 ? getPolyglossiaEnvName(current_language)
922 : current_language->babel();
923 if (!current_lang.empty()) {
924 os << from_ascii(subst(
928 pending_newline = !localswitch;
929 unskip_newline = !localswitch;
931 } else if (!par_lang.empty()) {
932 os << from_ascii(subst(
936 pending_newline = !localswitch;
937 unskip_newline = !localswitch;
941 if (closing_rtl_ltr_environment)
944 if (pending_newline) {
946 // prevent unwanted whitespace
951 // if this is a CJK-paragraph and the next isn't, close CJK
952 // also if the next paragraph is a multilingual environment (because of nesting)
954 && open_encoding_ == CJK
955 && (nextpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK
956 || (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
957 // inbetween environments, CJK has to be closed later (nesting!)
958 && (!style.isEnvironment() || !nextpar->layout().isEnvironment())) {
959 os << "\\end{CJK}\n";
960 open_encoding_ = none;
963 // If this is the last paragraph, close the CJK environment
964 // if necessary. If it's an environment, we'll have to \end that first.
965 if (runparams.isLastPar && !style.isEnvironment()) {
966 switch (open_encoding_) {
968 // do nothing at the end of child documents
969 if (maintext && buf.masterBuffer() != &buf)
973 os << "\n\\end{CJK}\n";
977 open_encoding_ = none;
982 open_encoding_ = none;
992 // If this is the last paragraph, and a local_font was set upon entering
993 // the inset, and we're using "auto" or "default" encoding, the encoding
994 // should be set back to that local_font's encoding.
995 // However, do not change the encoding when a fully unicode aware backend
996 // such as XeTeX is used.
997 if (runparams.isLastPar && runparams_in.local_font != 0
998 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
999 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
1000 && (!runparams.isFullUnicode())) {
1001 runparams_in.encoding = runparams_in.local_font->language()->encoding();
1002 os << setEncoding(runparams_in.encoding->iconvName());
1004 // Otherwise, the current encoding should be set for the next paragraph.
1006 runparams_in.encoding = runparams.encoding;
1009 // we don't need a newline for the last paragraph!!!
1010 // Note from JMarc: we will re-add a \n explicitly in
1011 // TeXEnvironment, because it is needed in this case
1013 Layout const & next_layout = nextpar->layout();
1014 if (style == next_layout
1015 // no blank lines before environments!
1016 || !next_layout.isEnvironment()
1017 // unless there's a depth change
1018 // FIXME What we really want to do here is put every \begin and \end
1019 // tag on a new line (which was not the case with nested environments).
1020 // But in the present state of play, we don't have access to the
1021 // information whether the current TeX row is empty or not.
1022 // For some ideas about how to fix this, see this thread:
1023 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
1024 || nextpar->params().depth() != par.params().depth()) {
1029 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " done; ptr "
1030 << &par << " next " << nextpar);
1036 // LaTeX all paragraphs
1037 void latexParagraphs(Buffer const & buf,
1040 OutputParams const & runparams,
1041 string const & everypar)
1043 LASSERT(runparams.par_begin <= runparams.par_end,
1044 { os << "% LaTeX Output Error\n"; return; } );
1046 BufferParams const & bparams = buf.params();
1048 bool const maintext = text.isMainText();
1049 bool const is_child = buf.masterBuffer() != &buf;
1051 // Open a CJK environment at the beginning of the main buffer
1052 // if the document's language is a CJK language
1053 // (but not in child documents)
1054 if (maintext && !is_child
1055 && bparams.encoding().package() == Encoding::CJK) {
1056 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
1057 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
1058 open_encoding_ = CJK;
1060 // if "auto begin" is switched off, explicitly switch the
1061 // language on at start
1062 string const mainlang = runparams.use_polyglossia
1063 ? getPolyglossiaEnvName(bparams.language)
1064 : bparams.language->babel();
1065 string const lang_begin_command = runparams.use_polyglossia ?
1066 "\\begin{$$lang}" : lyxrc.language_command_begin;
1068 if (maintext && !lyxrc.language_auto_begin &&
1069 !mainlang.empty()) {
1071 os << from_utf8(subst(lang_begin_command,
1074 if (runparams.use_polyglossia
1075 && !bparams.language->polyglossiaOpts().empty())
1077 << from_ascii(bparams.language->polyglossiaOpts())
1082 ParagraphList const & paragraphs = text.paragraphs();
1084 if (runparams.par_begin == runparams.par_end) {
1085 // The full doc will be exported but it is easier to just rely on
1086 // runparams range parameters that will be passed TeXEnvironment.
1087 runparams.par_begin = 0;
1088 runparams.par_end = paragraphs.size();
1091 pit_type pit = runparams.par_begin;
1092 // lastpit is for the language check after the loop.
1093 pit_type lastpit = pit;
1094 // variables used in the loop:
1095 bool was_title = false;
1096 bool already_title = false;
1097 DocumentClass const & tclass = bparams.documentClass();
1099 for (; pit < runparams.par_end; ++pit) {
1101 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
1103 // FIXME This check should not be needed. We should
1104 // perhaps issue an error if it is.
1105 Layout const & layout = text.inset().forcePlainLayout() ?
1106 tclass.plainLayout() : par->layout();
1108 if (layout.intitle) {
1109 if (already_title) {
1110 LYXERR0("Error in latexParagraphs: You"
1111 " should not mix title layouts"
1112 " with normal ones.");
1113 } else if (!was_title) {
1115 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1117 << from_ascii(tclass.titlename())
1121 } else if (was_title && !already_title) {
1122 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1123 os << "\\end{" << from_ascii(tclass.titlename())
1127 os << "\\" << from_ascii(tclass.titlename())
1130 already_title = true;
1135 if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
1136 // This is a standard top level paragraph, TeX it and continue.
1137 TeXOnePar(buf, text, pit, os, runparams, everypar);
1141 TeXEnvironmentData const data =
1142 prepareEnvironment(buf, text, par, os, runparams);
1143 // pit can be changed in TeXEnvironment.
1144 TeXEnvironment(buf, text, runparams, pit, os);
1145 finishEnvironment(os, runparams, data);
1148 if (pit == runparams.par_end) {
1149 // Make sure that the last paragraph is
1150 // correctly terminated (because TeXOnePar does
1151 // not add a \n in this case)
1155 // It might be that we only have a title in this document
1156 if (was_title && !already_title) {
1157 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1158 os << "\\end{" << from_ascii(tclass.titlename())
1161 os << "\\" << from_ascii(tclass.titlename())
1166 // if "auto end" is switched off, explicitly close the language at the end
1167 // but only if the last par is in a babel language
1168 string const lang_end_command = runparams.use_polyglossia ?
1169 "\\end{$$lang}" : lyxrc.language_command_end;
1170 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1171 paragraphs.at(lastpit).getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1172 os << from_utf8(subst(lang_end_command,
1178 // If the last paragraph is an environment, we'll have to close
1179 // CJK at the very end to do proper nesting.
1180 if (maintext && !is_child && open_encoding_ == CJK) {
1181 os << "\\end{CJK}\n";
1182 open_encoding_ = none;
1185 // reset inherited encoding
1186 if (cjk_inherited_ > 0) {
1187 cjk_inherited_ -= 1;
1188 if (cjk_inherited_ == 0)
1189 open_encoding_ = CJK;
1194 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1195 OutputParams const & runparams, Encoding const & newEnc,
1198 Encoding const & oldEnc = *runparams.encoding;
1199 bool moving_arg = runparams.moving_arg;
1200 // If we switch from/to CJK, we need to switch anyway, despite custom inputenc
1201 bool const from_to_cjk =
1202 (oldEnc.package() == Encoding::CJK && newEnc.package() != Encoding::CJK)
1203 || (oldEnc.package() != Encoding::CJK && newEnc.package() == Encoding::CJK);
1204 if (!force && !from_to_cjk
1205 && ((bparams.inputenc != "auto" && bparams.inputenc != "default") || moving_arg))
1206 return make_pair(false, 0);
1208 // Do nothing if the encoding is unchanged.
1209 if (oldEnc.name() == newEnc.name())
1210 return make_pair(false, 0);
1212 // FIXME We ignore encoding switches from/to encodings that do
1213 // neither support the inputenc package nor the CJK package here.
1214 // This does of course only work in special cases (e.g. switch from
1215 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1216 // but it is the best we can do
1217 if (oldEnc.package() == Encoding::none
1218 || newEnc.package() == Encoding::none)
1219 return make_pair(false, 0);
1221 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1222 << oldEnc.name() << " to " << newEnc.name());
1223 os << setEncoding(newEnc.iconvName());
1224 if (bparams.inputenc == "default")
1225 return make_pair(true, 0);
1227 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1228 switch (newEnc.package()) {
1229 case Encoding::none:
1230 case Encoding::japanese:
1231 // shouldn't ever reach here, see above
1232 return make_pair(true, 0);
1233 case Encoding::inputenc: {
1234 int count = inputenc_arg.length();
1235 if (oldEnc.package() == Encoding::CJK &&
1236 open_encoding_ == CJK) {
1238 open_encoding_ = none;
1241 else if (oldEnc.package() == Encoding::inputenc &&
1242 open_encoding_ == inputenc) {
1244 open_encoding_ = none;
1247 if (runparams.local_font != 0
1248 && oldEnc.package() == Encoding::CJK) {
1249 // within insets, \inputenc switches need
1250 // to be embraced within \bgroup...\egroup;
1254 open_encoding_ = inputenc;
1256 // with the japanese option, inputenc is omitted.
1257 if (runparams.use_japanese)
1258 return make_pair(true, count);
1259 os << "\\inputencoding{" << inputenc_arg << '}';
1260 return make_pair(true, count + 16);
1262 case Encoding::CJK: {
1263 int count = inputenc_arg.length();
1264 if (oldEnc.package() == Encoding::CJK &&
1265 open_encoding_ == CJK) {
1269 if (oldEnc.package() == Encoding::inputenc &&
1270 open_encoding_ == inputenc) {
1274 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1275 << from_ascii(bparams.fonts_cjk) << "}";
1276 open_encoding_ = CJK;
1277 return make_pair(true, count + 15);
1280 // Dead code to avoid a warning:
1281 return make_pair(true, 0);