2 * \file output_latex.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "output_latex.h"
16 #include "BufferParams.h"
19 #include "InsetList.h"
23 #include "OutputParams.h"
24 #include "Paragraph.h"
25 #include "ParagraphParameters.h"
26 #include "TextClass.h"
29 #include "insets/InsetBibitem.h"
30 #include "insets/InsetArgument.h"
32 #include "support/lassert.h"
33 #include "support/debug.h"
34 #include "support/lstrings.h"
36 #include <boost/next_prior.hpp>
40 using namespace lyx::support;
53 static int open_encoding_ = none;
54 static int cjk_inherited_ = 0;
55 Language const * prev_env_language_ = 0;
58 string const getPolyglossiaEnvName(Language const * lang)
60 string result = lang->polyglossia();
61 if (result == "arabic")
62 // exceptional spelling; see polyglossia docs.
68 struct TeXEnvironmentData
72 Language const * par_language;
73 Encoding const * prev_encoding;
78 static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
80 ParagraphList::const_iterator pit,
82 OutputParams const & runparams)
84 TeXEnvironmentData data;
86 BufferParams const & bparams = buf.params();
88 // FIXME This test should not be necessary.
89 // We should perhaps issue an error if it is.
90 Layout const & style = text.inset().forcePlainLayout() ?
91 bparams.documentClass().plainLayout() : pit->layout();
93 ParagraphList const & paragraphs = text.paragraphs();
94 ParagraphList::const_iterator const priorpit =
95 pit == paragraphs.begin() ? pit : boost::prior(pit);
97 bool const use_prev_env_language = prev_env_language_ != 0
98 && priorpit->layout().isEnvironment()
99 && (priorpit->getDepth() > pit->getDepth()
100 || (priorpit->getDepth() == pit->getDepth()
101 && priorpit->layout() != pit->layout()));
103 data.prev_encoding = runparams.encoding;
104 data.par_language = pit->getParLanguage(bparams);
105 Language const * const doc_language = bparams.language;
106 Language const * const prev_par_language =
107 (pit != paragraphs.begin())
108 ? (use_prev_env_language ? prev_env_language_
109 : priorpit->getParLanguage(bparams))
112 bool const use_pg = runparams.use_polyglossia;
113 string const par_lang = use_pg ?
114 getPolyglossiaEnvName(data.par_language) : data.par_language->babel();
115 string const prev_par_lang = use_pg ?
116 getPolyglossiaEnvName(prev_par_language) : prev_par_language->babel();
117 string const doc_lang = use_pg ?
118 getPolyglossiaEnvName(doc_language) : doc_language->babel();
119 string const lang_begin_command = use_pg ?
120 "\\begin{$$lang}" : lyxrc.language_command_begin;
121 string const lang_end_command = use_pg ?
122 "\\end{$$lang}" : lyxrc.language_command_end;
124 if (par_lang != prev_par_lang) {
125 if (!lang_end_command.empty() &&
126 prev_par_lang != doc_lang &&
127 !prev_par_lang.empty()) {
128 os << from_ascii(subst(
132 // the '%' is necessary to prevent unwanted whitespace
136 if ((lang_end_command.empty() ||
137 par_lang != doc_lang) &&
139 os << from_ascii(subst(
143 if (runparams.use_polyglossia
144 && !data.par_language->polyglossiaOpts().empty())
146 << from_ascii(data.par_language->polyglossiaOpts())
148 // the '%' is necessary to prevent unwanted whitespace
153 data.leftindent_open = false;
154 if (!pit->params().leftIndent().zero()) {
155 os << "\\begin{LyXParagraphLeftIndent}{"
156 << from_ascii(pit->params().leftIndent().asLatexString())
158 data.leftindent_open = true;
161 if (style.isEnvironment()) {
162 os << "\\begin{" << from_ascii(style.latexname()) << '}';
163 if (style.optargs != 0 || style.reqargs != 0)
164 latexArgInsets(*pit, os, runparams, style.reqargs, style.optargs);
165 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
167 << pit->params().labelWidthString()
169 } else if (style.labeltype == LABEL_BIBLIO) {
170 if (pit->params().labelWidthString().empty())
171 os << '{' << bibitemWidest(buf, runparams) << "}\n";
174 << pit->params().labelWidthString()
177 os << from_ascii(style.latexparam()) << '\n';
181 // in multilingual environments, the CJK tags have to be nested properly
182 data.cjk_nested = false;
183 if (data.par_language->encoding()->package() == Encoding::CJK &&
184 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
185 if (prev_par_language->encoding()->package() == Encoding::CJK)
186 os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
187 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
188 open_encoding_ = CJK;
189 data.cjk_nested = true;
195 static void finishEnvironment(otexstream & os, OutputParams const & runparams,
196 TeXEnvironmentData const & data)
198 if (open_encoding_ == CJK && data.cjk_nested) {
199 // We need to close the encoding even if it does not change
200 // to do correct environment nesting
201 os << "\\end{CJK}\n";
202 open_encoding_ = none;
205 if (data.style->isEnvironment()) {
206 os << "\\end{" << from_ascii(data.style->latexname()) << "}\n";
207 prev_env_language_ = data.par_language;
208 if (runparams.encoding != data.prev_encoding) {
209 runparams.encoding = data.prev_encoding;
210 if (!runparams.isFullUnicode())
211 os << setEncoding(data.prev_encoding->iconvName());
215 if (data.leftindent_open) {
216 os << "\\end{LyXParagraphLeftIndent}\n";
217 prev_env_language_ = data.par_language;
218 if (runparams.encoding != data.prev_encoding) {
219 runparams.encoding = data.prev_encoding;
220 if (!runparams.isFullUnicode())
221 os << setEncoding(data.prev_encoding->iconvName());
227 void TeXEnvironment(Buffer const & buf, Text const & text,
228 OutputParams const & runparams,
229 pit_type & pit, otexstream & os)
231 ParagraphList const & paragraphs = text.paragraphs();
232 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
233 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
235 Layout const & current_layout = par->layout();
236 depth_type const current_depth = par->params().depth();
237 Length const & current_left_indent = par->params().leftIndent();
239 // This is for debugging purpose at the end.
240 pit_type const par_begin = pit;
241 for (; pit < runparams.par_end; ++pit) {
243 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
245 // check first if this is an higher depth paragraph.
246 bool go_out = (par->params().depth() < current_depth);
247 if (par->params().depth() == current_depth) {
248 // This environment is finished.
249 go_out |= (par->layout() != current_layout);
250 go_out |= (par->params().leftIndent() != current_left_indent);
253 // nothing to do here, restore pit and go out.
258 if (par->layout() == current_layout
259 && par->params().depth() == current_depth
260 && par->params().leftIndent() == current_left_indent) {
261 // We are still in the same environment so TeXOnePar and continue;
262 TeXOnePar(buf, text, pit, os, runparams);
266 // We are now in a deeper environment.
267 // Either par->layout() != current_layout
268 // Or par->params().depth() > current_depth
269 // Or par->params().leftIndent() != current_left_indent)
271 if (par->layout().isParagraph()) {
272 // FIXME (Lgb): How to handle this?
273 //&& !suffixIs(os, "\n\n")
275 // (ARRae) There should be at least one '\n' already but we need there to
276 // be two for Standard paragraphs that are depth-increment'ed to be
277 // output correctly. However, tables can also be paragraphs so
278 // don't adjust them.
281 // FIXME (Lgb): Will it ever harm to have one '\n' too
282 // many? i.e. that we sometimes will have
287 // FIXME This test should not be necessary.
288 // We should perhaps issue an error if it is.
289 bool const force_plain_layout = text.inset().forcePlainLayout();
290 Layout const & style = force_plain_layout
291 ? buf.params().documentClass().plainLayout()
294 if (!style.isEnvironment()) {
295 // This is a standard paragraph, no need to call TeXEnvironment.
296 TeXOnePar(buf, text, pit, os, runparams);
300 // This is a new environment.
301 TeXEnvironmentData const data =
302 prepareEnvironment(buf, text, par, os, runparams);
303 // Recursive call to TeXEnvironment!
304 TeXEnvironment(buf, text, runparams, pit, os);
305 finishEnvironment(os, runparams, data);
308 if (pit != runparams.par_end)
309 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << par_begin << " done.");
315 void latexArgInsets(Paragraph const & par, otexstream & os,
316 OutputParams const & runparams, unsigned int reqargs,
317 unsigned int optargs)
319 unsigned int totalargs = reqargs + optargs;
320 list<InsetArgument const *> ilist;
322 InsetList::const_iterator it = par.insetList().begin();
323 InsetList::const_iterator end = par.insetList().end();
324 for (; it != end; ++it) {
325 if (it->inset->lyxCode() == ARG_CODE) {
326 if (ilist.size() >= totalargs) {
327 LYXERR0("WARNING: Found extra argument inset.");
330 InsetArgument const * ins =
331 static_cast<InsetArgument const *>(it->inset);
332 ilist.push_back(ins);
336 if (!reqargs && ilist.size() == 0)
339 bool const have_optional_args = ilist.size() > reqargs;
340 if (have_optional_args) {
341 unsigned int todo = ilist.size() - reqargs;
342 for (unsigned int i = 0; i < todo; ++i) {
343 InsetArgument const * ins = ilist.front();
345 ins->latexArgument(os, runparams, true);
349 // we should now have no more insets than there are required
351 LASSERT(ilist.size() <= reqargs, /* */);
355 for (unsigned int i = 0; i < reqargs; ++i) {
357 // a required argument wasn't given, so we output {}
360 InsetArgument const * ins = ilist.front();
362 ins->latexArgument(os, runparams, false);
369 // output the proper paragraph start according to latextype.
370 void parStartCommand(Paragraph const & par, otexstream & os,
371 OutputParams const & runparams, Layout const & style)
373 switch (style.latextype) {
375 os << '\\' << from_ascii(style.latexname());
377 // Separate handling of optional argument inset.
378 if (style.optargs != 0 || style.reqargs != 0)
379 latexArgInsets(par, os, runparams, style.reqargs, style.optargs);
381 os << from_ascii(style.latexparam());
383 case LATEX_ITEM_ENVIRONMENT:
384 case LATEX_LIST_ENVIRONMENT:
387 case LATEX_BIB_ENVIRONMENT:
388 // ignore this, the inset will write itself
397 // FIXME: this should be anonymous
398 void TeXOnePar(Buffer const & buf,
402 OutputParams const & runparams_in,
403 string const & everypar,
404 int start_pos, int end_pos)
406 BufferParams const & bparams = buf.params();
407 ParagraphList const & paragraphs = text.paragraphs();
408 Paragraph const & par = paragraphs.at(pit);
409 // FIXME This check should not really be needed.
410 // Perhaps we should issue an error if it is.
411 Layout const style = text.inset().forcePlainLayout() ?
412 bparams.documentClass().plainLayout() : par.layout();
414 if (style.inpreamble)
417 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
420 OutputParams runparams = runparams_in;
421 runparams.isLastPar = (pit == pit_type(paragraphs.size() - 1));
422 // We reinitialze par begin and end to be on the safe side
423 // with embedded inset as we don't know if they set those
425 runparams.par_begin = 0;
426 runparams.par_end = 0;
428 bool const maintext = text.isMainText();
429 // we are at the beginning of an inset and CJK is already open;
430 // we count inheritation levels to get the inset nesting right.
431 if (pit == 0 && !maintext
432 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
434 open_encoding_ = none;
437 if (text.inset().getLayout().isPassThru()) {
438 Font const outerfont = text.outerFont(pit);
440 // No newline before first paragraph in this lyxtext
443 if (!text.inset().getLayout().parbreakIsNewline())
447 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
451 Paragraph const * nextpar = runparams.isLastPar
452 ? 0 : ¶graphs.at(pit + 1);
454 if (style.pass_thru) {
455 Font const outerfont = text.outerFont(pit);
456 parStartCommand(par, os, runparams, style);
458 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
460 // I did not create a parEndCommand for this minuscule
461 // task because in the other user of parStartCommand
462 // the code is different (JMarc)
463 if (style.isCommand())
467 if (!style.parbreak_is_newline) {
469 } else if (nextpar) {
470 Layout const nextstyle = text.inset().forcePlainLayout()
471 ? bparams.documentClass().plainLayout()
473 if (nextstyle.name() != style.name())
480 // This paragraph's language
481 Language const * const par_language = par.getParLanguage(bparams);
482 // The document's language
483 Language const * const doc_language = bparams.language;
484 // The language that was in effect when the environment this paragraph is
485 // inside of was opened
486 Language const * const outer_language =
487 (runparams.local_font != 0) ?
488 runparams.local_font->language() : doc_language;
490 Paragraph const * priorpar = (pit == 0) ? 0 : ¶graphs.at(pit - 1);
492 // The previous language that was in effect is the language of the
493 // previous paragraph, unless the previous paragraph is inside an
494 // environment with nesting depth greater than (or equal to, but with
495 // a different layout) the current one. If there is no previous
496 // paragraph, the previous language is the outer language.
497 bool const use_prev_env_language = prev_env_language_ != 0
499 && priorpar->layout().isEnvironment()
500 && (priorpar->getDepth() > par.getDepth()
501 || (priorpar->getDepth() == par.getDepth()
502 && priorpar->layout() != par.layout()));
503 Language const * const prev_language =
505 ? (use_prev_env_language ? prev_env_language_
506 : priorpar->getParLanguage(bparams))
510 bool const use_pg = runparams.use_polyglossia;
511 string const par_lang = use_pg ?
512 getPolyglossiaEnvName(par_language): par_language->babel();
513 string const prev_lang = use_pg ?
514 getPolyglossiaEnvName(prev_language) : prev_language->babel();
515 string const doc_lang = use_pg ?
516 getPolyglossiaEnvName(doc_language) : doc_language->babel();
517 string const outer_lang = use_pg ?
518 getPolyglossiaEnvName(outer_language) : outer_language->babel();
519 string const lang_begin_command = use_pg ?
520 "\\begin{$$lang}" : lyxrc.language_command_begin;
521 string const lang_end_command = use_pg ?
522 "\\end{$$lang}" : lyxrc.language_command_end;
524 if (par_lang != prev_lang
525 // check if we already put language command in TeXEnvironment()
526 && !(style.isEnvironment()
527 && (pit == 0 || (priorpar->layout() != par.layout()
528 && priorpar->getDepth() <= par.getDepth())
529 || priorpar->getDepth() < par.getDepth())))
531 if (!lang_end_command.empty() &&
532 prev_lang != outer_lang &&
535 os << from_ascii(subst(lang_end_command,
538 // the '%' is necessary to prevent unwanted whitespace
542 // We need to open a new language if we couldn't close the previous
543 // one (because there's no language_command_end); and even if we closed
544 // the previous one, if the current language is different than the
545 // outer_language (which is currently in effect once the previous one
547 if ((lang_end_command.empty() || par_lang != outer_lang)
548 && !par_lang.empty()) {
549 // If we're inside an inset, and that inset is within an \L or \R
550 // (or equivalents), then within the inset, too, any opposite
551 // language paragraph should appear within an \L or \R (in addition
552 // to, outside of, the normal language switch commands).
553 // This behavior is not correct for ArabTeX, though.
554 if (!runparams.use_polyglossia
556 && par_language->lang() != "arabic_arabtex"
557 && outer_language->lang() != "arabic_arabtex"
558 // are we in an inset?
559 && runparams.local_font != 0
560 // is the inset within an \L or \R?
562 // FIXME: currently, we don't check this; this means that
563 // we'll have unnnecessary \L and \R commands, but that
564 // doesn't seem to hurt (though latex will complain)
566 // is this paragraph in the opposite direction?
567 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()) {
568 // FIXME: I don't have a working copy of the Arabi package, so
569 // I'm not sure if the farsi and arabic_arabi stuff is correct
571 if (par_language->lang() == "farsi")
573 else if (outer_language->lang() == "farsi")
575 else if (par_language->lang() == "arabic_arabi")
577 else if (outer_language->lang() == "arabic_arabi")
579 // remaining RTL languages currently is hebrew
580 else if (par_language->rightToLeft())
585 // With CJK, the CJK tag has to be closed first (see below)
586 if (runparams.encoding->package() != Encoding::CJK) {
587 os << from_ascii(subst(
591 if (runparams.use_polyglossia
592 && !par_language->polyglossiaOpts().empty())
594 << from_ascii(par_language->polyglossiaOpts())
596 // the '%' is necessary to prevent unwanted whitespace
602 // Switch file encoding if necessary; no need to do this for "default"
603 // encoding, since this only affects the position of the outputted
604 // \inputencoding command; the encoding switch will occur when necessary
605 if (bparams.inputenc == "auto"
606 && runparams.encoding->package() != Encoding::none) {
607 // Look ahead for future encoding changes.
608 // We try to output them at the beginning of the paragraph,
609 // since the \inputencoding command is not allowed e.g. in
610 // sections. For this reason we only set runparams.moving_arg
611 // after checking for the encoding change, otherwise the
612 // change would be always avoided by switchEncoding().
613 for (pos_type i = 0; i < par.size(); ++i) {
614 char_type const c = par.getChar(i);
615 Encoding const * const encoding =
616 par.getFontSettings(bparams, i).language()->encoding();
617 if (encoding->package() != Encoding::CJK
618 && runparams.encoding->package() == Encoding::inputenc
623 // All characters before c are in the ASCII range, and
624 // c is non-ASCII (but no inset), so change the
625 // encoding to that required by the language of c.
626 // With CJK, only add switch if we have CJK content at the beginning
628 if (i != 0 && encoding->package() == Encoding::CJK)
631 pair<bool, int> enc_switch = switchEncoding(os.os(),
632 bparams, runparams, *encoding);
633 // the following is necessary after a CJK environment in a multilingual
634 // context (nesting issue).
635 if (par_language->encoding()->package() == Encoding::CJK
636 && open_encoding_ != CJK && cjk_inherited_ == 0) {
637 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
638 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
639 open_encoding_ = CJK;
641 if (encoding->package() != Encoding::none && enc_switch.first) {
642 if (enc_switch.second > 0) {
643 // the '%' is necessary to prevent unwanted whitespace
646 // With CJK, the CJK tag had to be closed first (see above)
647 if (runparams.encoding->package() == Encoding::CJK) {
648 os << from_ascii(subst(
652 // the '%' is necessary to prevent unwanted whitespace
655 runparams.encoding = encoding;
661 runparams.moving_arg |= style.needprotect;
662 Encoding const * const prev_encoding = runparams.encoding;
664 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
665 if (par.allowParagraphCustomization()) {
666 if (par.params().startOfAppendix()) {
667 os << "\\appendix\n";
670 if (!par.params().spacing().isDefault()
671 && (pit == 0 || !priorpar->hasSameLayout(par)))
673 os << from_ascii(par.params().spacing().writeEnvirBegin(useSetSpace))
677 if (style.isCommand()) {
682 parStartCommand(par, os, runparams, style);
684 Font const outerfont = text.outerFont(pit);
687 os << from_utf8(everypar);
688 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
690 // Make sure that \\par is done with the font of the last
691 // character if this has another size as the default.
692 // This is necessary because LaTeX (and LyX on the screen)
693 // calculates the space between the baselines according
694 // to this font. (Matthias)
696 // Is this really needed ? (Dekel)
697 // We do not need to use to change the font for the last paragraph
700 Font const font = par.empty()
701 ? par.getLayoutFont(bparams, outerfont)
702 : par.getFont(bparams, par.size() - 1, outerfont);
704 bool const is_command = style.isCommand();
706 if (style.resfont.size() != font.fontInfo().size()
710 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
711 } else if (is_command) {
713 if (runparams.encoding != prev_encoding) {
714 runparams.encoding = prev_encoding;
715 if (!runparams.isFullUnicode())
716 os << setEncoding(prev_encoding->iconvName());
720 bool pending_newline = false;
721 bool unskip_newline = false;
722 switch (style.latextype) {
723 case LATEX_ITEM_ENVIRONMENT:
724 case LATEX_LIST_ENVIRONMENT:
725 if (nextpar && (par.params().depth() < nextpar->params().depth()))
726 pending_newline = true;
728 case LATEX_ENVIRONMENT: {
729 // if its the last paragraph of the current environment
730 // skip it otherwise fall through
732 && (nextpar->layout() != par.layout()
733 || nextpar->params().depth() != par.params().depth()))
737 // fall through possible
739 // we don't need it for the last paragraph!!!
741 pending_newline = true;
744 if (par.allowParagraphCustomization()) {
745 if (!par.params().spacing().isDefault()
746 && (runparams.isLastPar || !nextpar->hasSameLayout(par))) {
749 os << from_ascii(par.params().spacing().writeEnvirEnd(useSetSpace));
750 pending_newline = true;
754 // Closing the language is needed for the last paragraph; it is also
755 // needed if we're within an \L or \R that we may have opened above (not
756 // necessarily in this paragraph) and are about to close.
757 bool closing_rtl_ltr_environment = !runparams.use_polyglossia
759 && (par_language->lang() != "arabic_arabtex"
760 && outer_language->lang() != "arabic_arabtex")
761 // have we opened and \L or \R environment?
762 && runparams.local_font != 0
763 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()
764 // are we about to close the language?
765 &&((nextpar && par_language->babel() != (nextpar->getParLanguage(bparams))->babel())
766 || (runparams.isLastPar && par_language->babel() != outer_language->babel()));
768 if (closing_rtl_ltr_environment
769 || (runparams.isLastPar && par_language->babel() != outer_language->babel())) {
770 // Since \selectlanguage write the language to the aux file,
771 // we need to reset the language at the end of footnote or
777 // when the paragraph uses CJK, the language has to be closed earlier
778 if (font.language()->encoding()->package() != Encoding::CJK) {
779 if (lang_end_command.empty()) {
780 // If this is a child, we should restore the
781 // master language after the last paragraph.
782 Language const * const current_language =
783 (runparams.isLastPar && runparams.master_language)
784 ? runparams.master_language
786 string const current_lang = runparams.use_polyglossia
787 ? getPolyglossiaEnvName(current_language)
788 : current_language->babel();
789 if (!current_lang.empty()) {
790 os << from_ascii(subst(
794 pending_newline = true;
795 unskip_newline = true;
797 } else if (!par_lang.empty()) {
798 os << from_ascii(subst(
802 pending_newline = true;
803 unskip_newline = true;
807 if (closing_rtl_ltr_environment)
810 if (pending_newline) {
812 // prevent unwanted whitespace
817 // if this is a CJK-paragraph and the next isn't, close CJK
818 // also if the next paragraph is a multilingual environment (because of nesting)
820 && open_encoding_ == CJK
821 && (nextpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK
822 || (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
823 // inbetween environments, CJK has to be closed later (nesting!)
824 && (!style.isEnvironment() || !nextpar->layout().isEnvironment())) {
825 os << "\\end{CJK}\n";
826 open_encoding_ = none;
829 // If this is the last paragraph, close the CJK environment
830 // if necessary. If it's an environment, we'll have to \end that first.
831 if (runparams.isLastPar && !style.isEnvironment()) {
832 switch (open_encoding_) {
834 // do nothing at the end of child documents
835 if (maintext && buf.masterBuffer() != &buf)
839 os << "\n\\end{CJK}\n";
843 open_encoding_ = none;
848 open_encoding_ = none;
858 // If this is the last paragraph, and a local_font was set upon entering
859 // the inset, and we're using "auto" or "default" encoding, the encoding
860 // should be set back to that local_font's encoding.
861 // However, do not change the encoding when a fully unicode aware backend
862 // such as XeTeX is used.
863 if (runparams.isLastPar && runparams_in.local_font != 0
864 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
865 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
866 && (!runparams.isFullUnicode())) {
867 runparams_in.encoding = runparams_in.local_font->language()->encoding();
868 os << setEncoding(runparams_in.encoding->iconvName());
870 // Otherwise, the current encoding should be set for the next paragraph.
872 runparams_in.encoding = runparams.encoding;
875 // we don't need a newline for the last paragraph!!!
876 // Note from JMarc: we will re-add a \n explicitly in
877 // TeXEnvironment, because it is needed in this case
879 Layout const & next_layout = nextpar->layout();
880 if (style == next_layout
881 // no blank lines before environments!
882 || !next_layout.isEnvironment()
883 // unless there's a depth change
884 // FIXME What we really want to do here is put every \begin and \end
885 // tag on a new line (which was not the case with nested environments).
886 // But in the present state of play, we don't have access to the
887 // information whether the current TeX row is empty or not.
888 // For some ideas about how to fix this, see this thread:
889 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
890 || nextpar->params().depth() != par.params().depth()) {
895 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " done; ptr "
896 << &par << " next " << nextpar);
902 // LaTeX all paragraphs
903 void latexParagraphs(Buffer const & buf,
906 OutputParams const & runparams,
907 string const & everypar)
909 BufferParams const & bparams = buf.params();
911 bool const maintext = text.isMainText();
912 bool const is_child = buf.masterBuffer() != &buf;
914 // Open a CJK environment at the beginning of the main buffer
915 // if the document's language is a CJK language
916 // (but not in child documents)
917 if (maintext && !is_child
918 && bparams.encoding().package() == Encoding::CJK) {
919 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
920 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
921 open_encoding_ = CJK;
923 // if "auto begin" is switched off, explicitly switch the
924 // language on at start
925 string const mainlang = runparams.use_polyglossia
926 ? getPolyglossiaEnvName(bparams.language)
927 : bparams.language->babel();
928 string const lang_begin_command = runparams.use_polyglossia ?
929 "\\begin{$$lang}" : lyxrc.language_command_begin;
931 if (maintext && !lyxrc.language_auto_begin &&
934 os << from_utf8(subst(lang_begin_command,
937 if (runparams.use_polyglossia
938 && !bparams.language->polyglossiaOpts().empty())
940 << from_ascii(bparams.language->polyglossiaOpts())
945 ParagraphList const & paragraphs = text.paragraphs();
946 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
948 if (runparams.par_begin == runparams.par_end) {
949 // The full doc will be exported but it is easier to just rely on
950 // runparams range parameters that will be passed TeXEnvironment.
951 runparams.par_begin = 0;
952 runparams.par_end = paragraphs.size();
955 pit_type pit = runparams.par_begin;
956 // lastpit is for the language check after the loop.
957 pit_type lastpit = pit;
958 // variables used in the loop:
959 bool was_title = false;
960 bool already_title = false;
961 DocumentClass const & tclass = bparams.documentClass();
963 for (; pit < runparams.par_end; ++pit) {
965 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
967 // FIXME This check should not be needed. We should
968 // perhaps issue an error if it is.
969 Layout const & layout = text.inset().forcePlainLayout() ?
970 tclass.plainLayout() : par->layout();
972 if (layout.intitle) {
974 LYXERR0("Error in latexParagraphs: You"
975 " should not mix title layouts"
976 " with normal ones.");
977 } else if (!was_title) {
979 if (tclass.titletype() == TITLE_ENVIRONMENT) {
981 << from_ascii(tclass.titlename())
985 } else if (was_title && !already_title) {
986 if (tclass.titletype() == TITLE_ENVIRONMENT) {
987 os << "\\end{" << from_ascii(tclass.titlename())
991 os << "\\" << from_ascii(tclass.titlename())
994 already_title = true;
999 if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
1000 // This is a standard top level paragraph, TeX it and continue.
1001 TeXOnePar(buf, text, pit, os, runparams, everypar);
1005 TeXEnvironmentData const data =
1006 prepareEnvironment(buf, text, par, os, runparams);
1007 // pit can be changed in TeXEnvironment.
1008 TeXEnvironment(buf, text, runparams, pit, os);
1009 finishEnvironment(os, runparams, data);
1012 if (pit == runparams.par_end) {
1013 // Make sure that the last paragraph is
1014 // correctly terminated (because TeXOnePar does
1015 // not add a \n in this case)
1019 // It might be that we only have a title in this document
1020 if (was_title && !already_title) {
1021 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1022 os << "\\end{" << from_ascii(tclass.titlename())
1025 os << "\\" << from_ascii(tclass.titlename())
1030 // if "auto end" is switched off, explicitly close the language at the end
1031 // but only if the last par is in a babel language
1032 string const lang_end_command = runparams.use_polyglossia ?
1033 "\\end{$$lang}" : lyxrc.language_command_end;
1034 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1035 paragraphs.at(lastpit).getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1036 os << from_utf8(subst(lang_end_command,
1042 // If the last paragraph is an environment, we'll have to close
1043 // CJK at the very end to do proper nesting.
1044 if (maintext && !is_child && open_encoding_ == CJK) {
1045 os << "\\end{CJK}\n";
1046 open_encoding_ = none;
1049 // reset inherited encoding
1050 if (cjk_inherited_ > 0) {
1051 cjk_inherited_ -= 1;
1052 if (cjk_inherited_ == 0)
1053 open_encoding_ = CJK;
1058 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1059 OutputParams const & runparams, Encoding const & newEnc,
1062 Encoding const & oldEnc = *runparams.encoding;
1063 bool moving_arg = runparams.moving_arg;
1064 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
1066 return make_pair(false, 0);
1068 // Do nothing if the encoding is unchanged.
1069 if (oldEnc.name() == newEnc.name())
1070 return make_pair(false, 0);
1072 // FIXME We ignore encoding switches from/to encodings that do
1073 // neither support the inputenc package nor the CJK package here.
1074 // This does of course only work in special cases (e.g. switch from
1075 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1076 // but it is the best we can do
1077 if (oldEnc.package() == Encoding::none
1078 || newEnc.package() == Encoding::none)
1079 return make_pair(false, 0);
1081 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1082 << oldEnc.name() << " to " << newEnc.name());
1083 os << setEncoding(newEnc.iconvName());
1084 if (bparams.inputenc == "default")
1085 return make_pair(true, 0);
1087 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1088 switch (newEnc.package()) {
1089 case Encoding::none:
1090 case Encoding::japanese:
1091 // shouldn't ever reach here, see above
1092 return make_pair(true, 0);
1093 case Encoding::inputenc: {
1094 int count = inputenc_arg.length();
1095 if (oldEnc.package() == Encoding::CJK &&
1096 open_encoding_ == CJK) {
1098 open_encoding_ = none;
1101 else if (oldEnc.package() == Encoding::inputenc &&
1102 open_encoding_ == inputenc) {
1104 open_encoding_ = none;
1107 if (runparams.local_font != 0
1108 && oldEnc.package() == Encoding::CJK) {
1109 // within insets, \inputenc switches need
1110 // to be embraced within \bgroup...\egroup;
1114 open_encoding_ = inputenc;
1116 // with the japanese option, inputenc is omitted.
1117 if (runparams.use_japanese)
1118 return make_pair(true, count);
1119 os << "\\inputencoding{" << inputenc_arg << '}';
1120 return make_pair(true, count + 16);
1122 case Encoding::CJK: {
1123 int count = inputenc_arg.length();
1124 if (oldEnc.package() == Encoding::CJK &&
1125 open_encoding_ == CJK) {
1129 if (oldEnc.package() == Encoding::inputenc &&
1130 open_encoding_ == inputenc) {
1134 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1135 << from_ascii(bparams.fonts_cjk) << "}";
1136 open_encoding_ = CJK;
1137 return make_pair(true, count + 15);
1140 // Dead code to avoid a warning:
1141 return make_pair(true, 0);