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().empty()) {
166 OutputParams rp = runparams;
167 rp.local_font = &pit->getFirstFontSettings(bparams);
168 latexArgInsets(paragraphs, pit, os, rp, style.latexargs());
170 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
172 << pit->params().labelWidthString()
174 } else if (style.labeltype == LABEL_BIBLIO) {
175 if (pit->params().labelWidthString().empty())
176 os << '{' << bibitemWidest(buf, runparams) << "}\n";
179 << pit->params().labelWidthString()
182 os << from_ascii(style.latexparam()) << '\n';
186 // in multilingual environments, the CJK tags have to be nested properly
187 data.cjk_nested = false;
188 if (data.par_language->encoding()->package() == Encoding::CJK &&
189 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
190 if (prev_par_language->encoding()->package() == Encoding::CJK)
191 os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
192 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
193 open_encoding_ = CJK;
194 data.cjk_nested = true;
200 static void finishEnvironment(otexstream & os, OutputParams const & runparams,
201 TeXEnvironmentData const & data)
203 if (open_encoding_ == CJK && data.cjk_nested) {
204 // We need to close the encoding even if it does not change
205 // to do correct environment nesting
206 os << "\\end{CJK}\n";
207 open_encoding_ = none;
210 if (data.style->isEnvironment()) {
211 os << "\\end{" << from_ascii(data.style->latexname()) << "}\n";
212 prev_env_language_ = data.par_language;
213 if (runparams.encoding != data.prev_encoding) {
214 runparams.encoding = data.prev_encoding;
215 if (!runparams.isFullUnicode())
216 os << setEncoding(data.prev_encoding->iconvName());
220 if (data.leftindent_open) {
221 os << "\\end{LyXParagraphLeftIndent}\n";
222 prev_env_language_ = data.par_language;
223 if (runparams.encoding != data.prev_encoding) {
224 runparams.encoding = data.prev_encoding;
225 if (!runparams.isFullUnicode())
226 os << setEncoding(data.prev_encoding->iconvName());
232 void TeXEnvironment(Buffer const & buf, Text const & text,
233 OutputParams const & runparams,
234 pit_type & pit, otexstream & os)
236 ParagraphList const & paragraphs = text.paragraphs();
237 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
238 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
240 Layout const & current_layout = par->layout();
241 depth_type const current_depth = par->params().depth();
242 Length const & current_left_indent = par->params().leftIndent();
244 // This is for debugging purpose at the end.
245 pit_type const par_begin = pit;
246 for (; pit < runparams.par_end; ++pit) {
247 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
249 // check first if this is an higher depth paragraph.
250 bool go_out = (par->params().depth() < current_depth);
251 if (par->params().depth() == current_depth) {
252 // This environment is finished.
253 go_out |= (par->layout() != current_layout);
254 go_out |= (par->params().leftIndent() != current_left_indent);
257 // nothing to do here, restore pit and go out.
262 if (par->layout() == current_layout
263 && par->params().depth() == current_depth
264 && par->params().leftIndent() == current_left_indent) {
265 // We are still in the same environment so TeXOnePar and continue;
266 TeXOnePar(buf, text, pit, os, runparams);
270 // We are now in a deeper environment.
271 // Either par->layout() != current_layout
272 // Or par->params().depth() > current_depth
273 // Or par->params().leftIndent() != current_left_indent)
275 if (par->layout().isParagraph()) {
276 // FIXME (Lgb): How to handle this?
277 //&& !suffixIs(os, "\n\n")
279 // (ARRae) There should be at least one '\n' already but we need there to
280 // be two for Standard paragraphs that are depth-increment'ed to be
281 // output correctly. However, tables can also be paragraphs so
282 // don't adjust them.
284 // FIXME (Lgb): Will it ever harm to have one '\n' too
285 // many? i.e. that we sometimes will have
290 // FIXME This test should not be necessary.
291 // We should perhaps issue an error if it is.
292 bool const force_plain_layout = text.inset().forcePlainLayout();
293 Layout const & style = force_plain_layout
294 ? buf.params().documentClass().plainLayout()
297 if (!style.isEnvironment()) {
298 // This is a standard paragraph, no need to call TeXEnvironment.
299 TeXOnePar(buf, text, pit, os, runparams);
303 // This is a new environment.
304 TeXEnvironmentData const data =
305 prepareEnvironment(buf, text, par, os, runparams);
306 // Recursive call to TeXEnvironment!
307 TeXEnvironment(buf, text, runparams, pit, os);
308 finishEnvironment(os, runparams, data);
311 if (pit != runparams.par_end)
312 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << par_begin << " done.");
316 void getArgInsets(otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
317 map<int, lyx::InsetArgument const *> ilist, vector<string> required, bool item)
319 unsigned int const argnr = latexargs.size();
323 for (unsigned int i = 1; i <= argnr; ++i) {
324 map<int, InsetArgument const *>::const_iterator lit = ilist.find(i);
325 bool inserted = false;
326 if (lit != ilist.end()) {
327 InsetArgument const * ins = (*lit).second;
329 Layout::LaTeXArgMap::const_iterator const lait =
330 latexargs.find(ins->name());
331 if (lait != latexargs.end()) {
332 Layout::latexarg arg = (*lait).second;
333 docstring ldelim = arg.mandatory ?
334 from_ascii("{") : from_ascii("[");
335 docstring rdelim = arg.mandatory ?
336 from_ascii("}") : from_ascii("]");
337 if (!arg.ldelim.empty())
339 if (!arg.rdelim.empty())
341 ins->latexArgument(os, runparams, ldelim, rdelim, arg.presetarg);
347 Layout::LaTeXArgMap::const_iterator lait = latexargs.begin();
348 Layout::LaTeXArgMap::const_iterator const laend = latexargs.end();
349 for (; lait != laend; ++lait) {
350 string const name = item ? "item:" + convert<string>(i) : convert<string>(i);
351 if ((*lait).first == name) {
352 Layout::latexarg arg = (*lait).second;
354 docstring ldelim = arg.ldelim.empty() ?
355 from_ascii("{") : arg.ldelim;
356 docstring rdelim = arg.rdelim.empty() ?
357 from_ascii("}") : arg.rdelim;
358 os << ldelim << arg.presetarg << rdelim;
359 } else if (!arg.presetarg.empty()) {
360 docstring ldelim = arg.mandatory ?
361 from_ascii("{") : from_ascii("[");
362 docstring rdelim = arg.mandatory ?
363 from_ascii("}") : from_ascii("]");
364 if (!arg.ldelim.empty())
366 if (!arg.rdelim.empty())
368 os << ldelim << arg.presetarg << rdelim;
369 } else if (find(required.begin(), required.end(),
370 (*lait).first) != required.end()) {
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 << rdelim;
388 void latexArgInsets(Paragraph const & par, otexstream & os,
389 OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs, bool item)
391 map<int, InsetArgument const *> ilist;
392 vector<string> required;
394 InsetList::const_iterator it = par.insetList().begin();
395 InsetList::const_iterator end = par.insetList().end();
396 for (; it != end; ++it) {
397 if (it->inset->lyxCode() == ARG_CODE) {
398 InsetArgument const * ins =
399 static_cast<InsetArgument const *>(it->inset);
400 if (ins->name().empty())
401 LYXERR0("Error: Unnamed argument inset!");
403 string const name = item ? split(ins->name(), ':') : ins->name();
404 unsigned int const nr = convert<unsigned int>(name);
406 Layout::LaTeXArgMap::const_iterator const lit =
407 latexargs.find(ins->name());
408 if (lit != latexargs.end()) {
409 Layout::latexarg const & arg = (*lit).second;
410 if (!arg.requires.empty()) {
411 vector<string> req = getVectorFromString(arg.requires);
412 required.insert(required.end(), req.begin(), req.end());
418 getArgInsets(os, runparams, latexargs, ilist, required, item);
422 void latexArgInsets(ParagraphList const & pars, ParagraphList::const_iterator pit,
423 otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs)
425 map<int, InsetArgument const *> ilist;
426 vector<string> required;
428 depth_type const current_depth = pit->params().depth();
429 Layout const current_layout = pit->layout();
431 // get the first paragraph in sequence with this layout and depth
434 if (boost::prior(pit, offset) == pars.begin())
436 ParagraphList::const_iterator priorpit = boost::prior(pit, offset + 1);
437 if (priorpit->layout() == current_layout
438 && priorpit->params().depth() == current_depth)
444 ParagraphList::const_iterator spit = boost::prior(pit, offset);
446 for (; spit != pars.end(); ++spit) {
447 if (spit->layout() != current_layout || spit->params().depth() < current_depth)
449 if (spit->params().depth() > current_depth)
451 InsetList::const_iterator it = spit->insetList().begin();
452 InsetList::const_iterator end = spit->insetList().end();
453 for (; it != end; ++it) {
454 if (it->inset->lyxCode() == ARG_CODE) {
455 InsetArgument const * ins =
456 static_cast<InsetArgument const *>(it->inset);
457 if (ins->name().empty())
458 LYXERR0("Error: Unnamed argument inset!");
460 string const name = ins->name();
461 unsigned int const nr = convert<unsigned int>(name);
462 if (ilist.find(nr) == ilist.end())
464 Layout::LaTeXArgMap::const_iterator const lit =
465 latexargs.find(ins->name());
466 if (lit != latexargs.end()) {
467 Layout::latexarg const & arg = (*lit).second;
468 if (!arg.requires.empty()) {
469 vector<string> req = getVectorFromString(arg.requires);
470 required.insert(required.end(), req.begin(), req.end());
477 getArgInsets(os, runparams, latexargs, ilist, required, false);
482 // output the proper paragraph start according to latextype.
483 void parStartCommand(Paragraph const & par, otexstream & os,
484 OutputParams const & runparams, Layout const & style)
486 switch (style.latextype) {
488 os << '\\' << from_ascii(style.latexname());
491 if (!style.latexargs().empty())
492 latexArgInsets(par, os, runparams, style.latexargs());
493 os << from_ascii(style.latexparam());
495 case LATEX_ITEM_ENVIRONMENT:
496 case LATEX_LIST_ENVIRONMENT:
497 os << "\\" + style.itemcommand();
499 if (!style.itemargs().empty())
500 latexArgInsets(par, os, runparams, style.itemargs(), true);
503 case LATEX_BIB_ENVIRONMENT:
504 // ignore this, the inset will write itself
513 // FIXME: this should be anonymous
514 void TeXOnePar(Buffer const & buf,
518 OutputParams const & runparams_in,
519 string const & everypar,
520 int start_pos, int end_pos)
522 BufferParams const & bparams = buf.params();
523 ParagraphList const & paragraphs = text.paragraphs();
524 Paragraph const & par = paragraphs.at(pit);
525 // FIXME This check should not really be needed.
526 // Perhaps we should issue an error if it is.
527 Layout const style = text.inset().forcePlainLayout() ?
528 bparams.documentClass().plainLayout() : par.layout();
530 if (style.inpreamble)
533 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
536 OutputParams runparams = runparams_in;
537 runparams.isLastPar = (pit == pit_type(paragraphs.size() - 1));
538 // We reinitialze par begin and end to be on the safe side
539 // with embedded inset as we don't know if they set those
541 runparams.par_begin = 0;
542 runparams.par_end = 0;
544 bool const maintext = text.isMainText();
545 // we are at the beginning of an inset and CJK is already open;
546 // we count inheritation levels to get the inset nesting right.
547 if (pit == 0 && !maintext
548 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
550 open_encoding_ = none;
553 if (text.inset().isPassThru()) {
554 Font const outerfont = text.outerFont(pit);
556 // No newline before first paragraph in this lyxtext
559 if (!text.inset().getLayout().parbreakIsNewline())
563 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
567 Paragraph const * nextpar = runparams.isLastPar
568 ? 0 : ¶graphs.at(pit + 1);
570 if (style.pass_thru) {
571 Font const outerfont = text.outerFont(pit);
572 runparams.local_font = &par.getFirstFontSettings(bparams);
573 parStartCommand(par, os, runparams, style);
575 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
577 // I did not create a parEndCommand for this minuscule
578 // task because in the other user of parStartCommand
579 // the code is different (JMarc)
580 if (style.isCommand())
584 if (!style.parbreak_is_newline) {
586 } else if (nextpar) {
587 Layout const nextstyle = text.inset().forcePlainLayout()
588 ? bparams.documentClass().plainLayout()
590 if (nextstyle.name() != style.name())
597 // This paragraph's language
598 Language const * const par_language = par.getParLanguage(bparams);
599 // The document's language
600 Language const * const doc_language = bparams.language;
601 // The language that was in effect when the environment this paragraph is
602 // inside of was opened
603 Language const * const outer_language =
604 (runparams.local_font != 0) ?
605 runparams.local_font->language() : doc_language;
607 Paragraph const * priorpar = (pit == 0) ? 0 : ¶graphs.at(pit - 1);
609 // The previous language that was in effect is the language of the
610 // previous paragraph, unless the previous paragraph is inside an
611 // environment with nesting depth greater than (or equal to, but with
612 // a different layout) the current one. If there is no previous
613 // paragraph, the previous language is the outer language.
614 bool const use_prev_env_language = prev_env_language_ != 0
616 && priorpar->layout().isEnvironment()
617 && (priorpar->getDepth() > par.getDepth()
618 || (priorpar->getDepth() == par.getDepth()
619 && priorpar->layout() != par.layout()));
620 Language const * const prev_language =
622 ? (use_prev_env_language ? prev_env_language_
623 : priorpar->getParLanguage(bparams))
627 bool const use_polyglossia = runparams.use_polyglossia;
628 string const par_lang = use_polyglossia ?
629 getPolyglossiaEnvName(par_language): par_language->babel();
630 string const prev_lang = use_polyglossia ?
631 getPolyglossiaEnvName(prev_language) : prev_language->babel();
632 string const doc_lang = use_polyglossia ?
633 getPolyglossiaEnvName(doc_language) : doc_language->babel();
634 string const outer_lang = use_polyglossia ?
635 getPolyglossiaEnvName(outer_language) : outer_language->babel();
636 string lang_begin_command = use_polyglossia ?
637 "\\begin{$$lang}" : lyxrc.language_command_begin;
638 string lang_end_command = use_polyglossia ?
639 "\\end{$$lang}" : lyxrc.language_command_end;
640 // the '%' is necessary to prevent unwanted whitespace
641 string lang_command_termination = "%\n";
643 // In some insets (such as Arguments), we cannot use \selectlanguage
644 bool const localswitch = !use_polyglossia
645 && text.inset().getLayout().forcelocalfontswitch();
647 lang_begin_command = lyxrc.language_command_local;
648 lang_end_command = "}";
649 lang_command_termination.clear();
652 if (par_lang != prev_lang
653 // check if we already put language command in TeXEnvironment()
654 && !(style.isEnvironment()
655 && (pit == 0 || (priorpar->layout() != par.layout()
656 && priorpar->getDepth() <= par.getDepth())
657 || priorpar->getDepth() < par.getDepth())))
659 if (!lang_end_command.empty() &&
660 prev_lang != outer_lang &&
663 os << from_ascii(subst(lang_end_command,
666 << lang_command_termination;
669 // We need to open a new language if we couldn't close the previous
670 // one (because there's no language_command_end); and even if we closed
671 // the previous one, if the current language is different than the
672 // outer_language (which is currently in effect once the previous one
674 if ((lang_end_command.empty() || par_lang != outer_lang)
675 && !par_lang.empty()) {
676 // If we're inside an inset, and that inset is within an \L or \R
677 // (or equivalents), then within the inset, too, any opposite
678 // language paragraph should appear within an \L or \R (in addition
679 // to, outside of, the normal language switch commands).
680 // This behavior is not correct for ArabTeX, though.
683 && par_language->lang() != "arabic_arabtex"
684 && outer_language->lang() != "arabic_arabtex"
685 // are we in an inset?
686 && runparams.local_font != 0
687 // is the inset within an \L or \R?
689 // FIXME: currently, we don't check this; this means that
690 // we'll have unnnecessary \L and \R commands, but that
691 // doesn't seem to hurt (though latex will complain)
693 // is this paragraph in the opposite direction?
694 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()) {
695 // FIXME: I don't have a working copy of the Arabi package, so
696 // I'm not sure if the farsi and arabic_arabi stuff is correct
698 if (par_language->lang() == "farsi")
700 else if (outer_language->lang() == "farsi")
702 else if (par_language->lang() == "arabic_arabi")
704 else if (outer_language->lang() == "arabic_arabi")
706 // remaining RTL languages currently is hebrew
707 else if (par_language->rightToLeft())
712 // With CJK, the CJK tag has to be closed first (see below)
713 if (runparams.encoding->package() != Encoding::CJK
714 && !par_lang.empty()) {
715 os << from_ascii(subst(
720 && !par_language->polyglossiaOpts().empty())
722 << from_ascii(par_language->polyglossiaOpts())
724 os << lang_command_termination;
729 // Switch file encoding if necessary; no need to do this for "default"
730 // encoding, since this only affects the position of the outputted
731 // \inputencoding command; the encoding switch will occur when necessary
732 if (bparams.inputenc == "auto"
733 && runparams.encoding->package() != Encoding::none) {
734 // Look ahead for future encoding changes.
735 // We try to output them at the beginning of the paragraph,
736 // since the \inputencoding command is not allowed e.g. in
737 // sections. For this reason we only set runparams.moving_arg
738 // after checking for the encoding change, otherwise the
739 // change would be always avoided by switchEncoding().
740 for (pos_type i = 0; i < par.size(); ++i) {
741 char_type const c = par.getChar(i);
742 Encoding const * const encoding =
743 par.getFontSettings(bparams, i).language()->encoding();
744 if (encoding->package() != Encoding::CJK
745 && runparams.encoding->package() == Encoding::inputenc
750 // All characters before c are in the ASCII range, and
751 // c is non-ASCII (but no inset), so change the
752 // encoding to that required by the language of c.
753 // With CJK, only add switch if we have CJK content at the beginning
755 if (i != 0 && encoding->package() == Encoding::CJK)
758 pair<bool, int> enc_switch = switchEncoding(os.os(),
759 bparams, runparams, *encoding);
760 // the following is necessary after a CJK environment in a multilingual
761 // context (nesting issue).
762 if (par_language->encoding()->package() == Encoding::CJK
763 && open_encoding_ != CJK && cjk_inherited_ == 0) {
764 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
765 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
766 open_encoding_ = CJK;
768 if (encoding->package() != Encoding::none && enc_switch.first) {
769 if (enc_switch.second > 0) {
770 // the '%' is necessary to prevent unwanted whitespace
773 // With CJK, the CJK tag had to be closed first (see above)
774 if (runparams.encoding->package() == Encoding::CJK
775 && !par_lang.empty()) {
776 os << from_ascii(subst(
780 << lang_command_termination;
782 runparams.encoding = encoding;
788 runparams.moving_arg |= style.needprotect;
789 Encoding const * const prev_encoding = runparams.encoding;
791 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
792 if (par.allowParagraphCustomization()) {
793 if (par.params().startOfAppendix()) {
794 os << "\\appendix\n";
797 if (!par.params().spacing().isDefault()
798 && (pit == 0 || !priorpar->hasSameLayout(par)))
800 os << from_ascii(par.params().spacing().writeEnvirBegin(useSetSpace))
804 if (style.isCommand()) {
809 runparams.local_font = &par.getFirstFontSettings(bparams);
810 parStartCommand(par, os, runparams, style);
811 Font const outerfont = text.outerFont(pit);
814 os << from_utf8(everypar);
815 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
817 // Make sure that \\par is done with the font of the last
818 // character if this has another size as the default.
819 // This is necessary because LaTeX (and LyX on the screen)
820 // calculates the space between the baselines according
821 // to this font. (Matthias)
823 // Is this really needed ? (Dekel)
824 // We do not need to use to change the font for the last paragraph
827 Font const font = par.empty()
828 ? par.getLayoutFont(bparams, outerfont)
829 : par.getFont(bparams, par.size() - 1, outerfont);
831 bool const is_command = style.isCommand();
833 if (style.resfont.size() != font.fontInfo().size()
837 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
838 } else if (is_command) {
840 if (runparams.encoding != prev_encoding) {
841 runparams.encoding = prev_encoding;
842 if (!runparams.isFullUnicode())
843 os << setEncoding(prev_encoding->iconvName());
847 bool pending_newline = false;
848 bool unskip_newline = false;
849 switch (style.latextype) {
850 case LATEX_ITEM_ENVIRONMENT:
851 case LATEX_LIST_ENVIRONMENT:
852 if (nextpar && (par.params().depth() < nextpar->params().depth()))
853 pending_newline = true;
855 case LATEX_ENVIRONMENT: {
856 // if its the last paragraph of the current environment
857 // skip it otherwise fall through
859 && (nextpar->layout() != par.layout()
860 || nextpar->params().depth() != par.params().depth()))
864 // fall through possible
866 // we don't need it for the last paragraph!!!
868 pending_newline = true;
871 if (par.allowParagraphCustomization()) {
872 if (!par.params().spacing().isDefault()
873 && (runparams.isLastPar || !nextpar->hasSameLayout(par))) {
876 os << from_ascii(par.params().spacing().writeEnvirEnd(useSetSpace));
877 pending_newline = true;
881 // Closing the language is needed for the last paragraph; it is also
882 // needed if we're within an \L or \R that we may have opened above (not
883 // necessarily in this paragraph) and are about to close.
884 bool closing_rtl_ltr_environment = !use_polyglossia
886 && (par_language->lang() != "arabic_arabtex"
887 && outer_language->lang() != "arabic_arabtex")
888 // have we opened and \L or \R environment?
889 && runparams.local_font != 0
890 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()
891 // are we about to close the language?
892 &&((nextpar && par_language->babel() != (nextpar->getParLanguage(bparams))->babel())
893 || (runparams.isLastPar && par_language->babel() != outer_language->babel()));
895 if (closing_rtl_ltr_environment
896 || (runparams.isLastPar
897 && ((!use_polyglossia && par_language->babel() != outer_language->babel())
898 || (use_polyglossia && par_language->polyglossia() != outer_language->polyglossia())))) {
899 // Since \selectlanguage write the language to the aux file,
900 // we need to reset the language at the end of footnote or
906 // when the paragraph uses CJK, the language has to be closed earlier
907 if (font.language()->encoding()->package() != Encoding::CJK) {
908 if (lang_end_command.empty()) {
909 // If this is a child, we should restore the
910 // master language after the last paragraph.
911 Language const * const current_language =
912 (runparams.isLastPar && runparams.master_language)
913 ? runparams.master_language
915 string const current_lang = use_polyglossia
916 ? getPolyglossiaEnvName(current_language)
917 : current_language->babel();
918 if (!current_lang.empty()) {
919 os << from_ascii(subst(
923 pending_newline = !localswitch;
924 unskip_newline = !localswitch;
926 } else if (!par_lang.empty()) {
927 os << from_ascii(subst(
931 pending_newline = !localswitch;
932 unskip_newline = !localswitch;
936 if (closing_rtl_ltr_environment)
939 if (pending_newline) {
941 // prevent unwanted whitespace
946 // if this is a CJK-paragraph and the next isn't, close CJK
947 // also if the next paragraph is a multilingual environment (because of nesting)
949 && open_encoding_ == CJK
950 && (nextpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK
951 || (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
952 // inbetween environments, CJK has to be closed later (nesting!)
953 && (!style.isEnvironment() || !nextpar->layout().isEnvironment())) {
954 os << "\\end{CJK}\n";
955 open_encoding_ = none;
958 // If this is the last paragraph, close the CJK environment
959 // if necessary. If it's an environment, we'll have to \end that first.
960 if (runparams.isLastPar && !style.isEnvironment()) {
961 switch (open_encoding_) {
963 // do nothing at the end of child documents
964 if (maintext && buf.masterBuffer() != &buf)
968 os << "\n\\end{CJK}\n";
972 open_encoding_ = none;
977 open_encoding_ = none;
987 // If this is the last paragraph, and a local_font was set upon entering
988 // the inset, and we're using "auto" or "default" encoding, the encoding
989 // should be set back to that local_font's encoding.
990 // However, do not change the encoding when a fully unicode aware backend
991 // such as XeTeX is used.
992 if (runparams.isLastPar && runparams_in.local_font != 0
993 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
994 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
995 && (!runparams.isFullUnicode())) {
996 runparams_in.encoding = runparams_in.local_font->language()->encoding();
997 os << setEncoding(runparams_in.encoding->iconvName());
999 // Otherwise, the current encoding should be set for the next paragraph.
1001 runparams_in.encoding = runparams.encoding;
1004 // we don't need a newline for the last paragraph!!!
1005 // Note from JMarc: we will re-add a \n explicitly in
1006 // TeXEnvironment, because it is needed in this case
1008 Layout const & next_layout = nextpar->layout();
1009 if (style == next_layout
1010 // no blank lines before environments!
1011 || !next_layout.isEnvironment()
1012 // unless there's a depth change
1013 // FIXME What we really want to do here is put every \begin and \end
1014 // tag on a new line (which was not the case with nested environments).
1015 // But in the present state of play, we don't have access to the
1016 // information whether the current TeX row is empty or not.
1017 // For some ideas about how to fix this, see this thread:
1018 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
1019 || nextpar->params().depth() != par.params().depth()) {
1024 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " done; ptr "
1025 << &par << " next " << nextpar);
1031 // LaTeX all paragraphs
1032 void latexParagraphs(Buffer const & buf,
1035 OutputParams const & runparams,
1036 string const & everypar)
1038 BufferParams const & bparams = buf.params();
1040 bool const maintext = text.isMainText();
1041 bool const is_child = buf.masterBuffer() != &buf;
1043 // Open a CJK environment at the beginning of the main buffer
1044 // if the document's language is a CJK language
1045 // (but not in child documents)
1046 if (maintext && !is_child
1047 && bparams.encoding().package() == Encoding::CJK) {
1048 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
1049 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
1050 open_encoding_ = CJK;
1052 // if "auto begin" is switched off, explicitly switch the
1053 // language on at start
1054 string const mainlang = runparams.use_polyglossia
1055 ? getPolyglossiaEnvName(bparams.language)
1056 : bparams.language->babel();
1057 string const lang_begin_command = runparams.use_polyglossia ?
1058 "\\begin{$$lang}" : lyxrc.language_command_begin;
1060 if (maintext && !lyxrc.language_auto_begin &&
1061 !mainlang.empty()) {
1063 os << from_utf8(subst(lang_begin_command,
1066 if (runparams.use_polyglossia
1067 && !bparams.language->polyglossiaOpts().empty())
1069 << from_ascii(bparams.language->polyglossiaOpts())
1074 ParagraphList const & paragraphs = text.paragraphs();
1075 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
1077 if (runparams.par_begin == runparams.par_end) {
1078 // The full doc will be exported but it is easier to just rely on
1079 // runparams range parameters that will be passed TeXEnvironment.
1080 runparams.par_begin = 0;
1081 runparams.par_end = paragraphs.size();
1084 pit_type pit = runparams.par_begin;
1085 // lastpit is for the language check after the loop.
1086 pit_type lastpit = pit;
1087 // variables used in the loop:
1088 bool was_title = false;
1089 bool already_title = false;
1090 DocumentClass const & tclass = bparams.documentClass();
1092 for (; pit < runparams.par_end; ++pit) {
1094 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
1096 // FIXME This check should not be needed. We should
1097 // perhaps issue an error if it is.
1098 Layout const & layout = text.inset().forcePlainLayout() ?
1099 tclass.plainLayout() : par->layout();
1101 if (layout.intitle) {
1102 if (already_title) {
1103 LYXERR0("Error in latexParagraphs: You"
1104 " should not mix title layouts"
1105 " with normal ones.");
1106 } else if (!was_title) {
1108 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1110 << from_ascii(tclass.titlename())
1114 } else if (was_title && !already_title) {
1115 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1116 os << "\\end{" << from_ascii(tclass.titlename())
1120 os << "\\" << from_ascii(tclass.titlename())
1123 already_title = true;
1128 if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
1129 // This is a standard top level paragraph, TeX it and continue.
1130 TeXOnePar(buf, text, pit, os, runparams, everypar);
1134 TeXEnvironmentData const data =
1135 prepareEnvironment(buf, text, par, os, runparams);
1136 // pit can be changed in TeXEnvironment.
1137 TeXEnvironment(buf, text, runparams, pit, os);
1138 finishEnvironment(os, runparams, data);
1141 if (pit == runparams.par_end) {
1142 // Make sure that the last paragraph is
1143 // correctly terminated (because TeXOnePar does
1144 // not add a \n in this case)
1148 // It might be that we only have a title in this document
1149 if (was_title && !already_title) {
1150 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1151 os << "\\end{" << from_ascii(tclass.titlename())
1154 os << "\\" << from_ascii(tclass.titlename())
1159 // if "auto end" is switched off, explicitly close the language at the end
1160 // but only if the last par is in a babel language
1161 string const lang_end_command = runparams.use_polyglossia ?
1162 "\\end{$$lang}" : lyxrc.language_command_end;
1163 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1164 paragraphs.at(lastpit).getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1165 os << from_utf8(subst(lang_end_command,
1171 // If the last paragraph is an environment, we'll have to close
1172 // CJK at the very end to do proper nesting.
1173 if (maintext && !is_child && open_encoding_ == CJK) {
1174 os << "\\end{CJK}\n";
1175 open_encoding_ = none;
1178 // reset inherited encoding
1179 if (cjk_inherited_ > 0) {
1180 cjk_inherited_ -= 1;
1181 if (cjk_inherited_ == 0)
1182 open_encoding_ = CJK;
1187 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1188 OutputParams const & runparams, Encoding const & newEnc,
1191 Encoding const & oldEnc = *runparams.encoding;
1192 bool moving_arg = runparams.moving_arg;
1193 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
1195 return make_pair(false, 0);
1197 // Do nothing if the encoding is unchanged.
1198 if (oldEnc.name() == newEnc.name())
1199 return make_pair(false, 0);
1201 // FIXME We ignore encoding switches from/to encodings that do
1202 // neither support the inputenc package nor the CJK package here.
1203 // This does of course only work in special cases (e.g. switch from
1204 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1205 // but it is the best we can do
1206 if (oldEnc.package() == Encoding::none
1207 || newEnc.package() == Encoding::none)
1208 return make_pair(false, 0);
1210 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1211 << oldEnc.name() << " to " << newEnc.name());
1212 os << setEncoding(newEnc.iconvName());
1213 if (bparams.inputenc == "default")
1214 return make_pair(true, 0);
1216 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1217 switch (newEnc.package()) {
1218 case Encoding::none:
1219 case Encoding::japanese:
1220 // shouldn't ever reach here, see above
1221 return make_pair(true, 0);
1222 case Encoding::inputenc: {
1223 int count = inputenc_arg.length();
1224 if (oldEnc.package() == Encoding::CJK &&
1225 open_encoding_ == CJK) {
1227 open_encoding_ = none;
1230 else if (oldEnc.package() == Encoding::inputenc &&
1231 open_encoding_ == inputenc) {
1233 open_encoding_ = none;
1236 if (runparams.local_font != 0
1237 && oldEnc.package() == Encoding::CJK) {
1238 // within insets, \inputenc switches need
1239 // to be embraced within \bgroup...\egroup;
1243 open_encoding_ = inputenc;
1245 // with the japanese option, inputenc is omitted.
1246 if (runparams.use_japanese)
1247 return make_pair(true, count);
1248 os << "\\inputencoding{" << inputenc_arg << '}';
1249 return make_pair(true, count + 16);
1251 case Encoding::CJK: {
1252 int count = inputenc_arg.length();
1253 if (oldEnc.package() == Encoding::CJK &&
1254 open_encoding_ == CJK) {
1258 if (oldEnc.package() == Encoding::inputenc &&
1259 open_encoding_ == inputenc) {
1263 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1264 << from_ascii(bparams.fonts_cjk) << "}";
1265 open_encoding_ = CJK;
1266 return make_pair(true, count + 15);
1269 // Dead code to avoid a warning:
1270 return make_pair(true, 0);