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"
30 #include "insets/InsetBibitem.h"
31 #include "insets/InsetArgument.h"
33 #include "support/lassert.h"
34 #include "support/debug.h"
35 #include "support/lstrings.h"
37 #include <boost/next_prior.hpp>
41 using namespace lyx::support;
54 static int open_encoding_ = none;
55 static int cjk_inherited_ = 0;
56 Language const * prev_env_language_ = 0;
59 string const getPolyglossiaEnvName(Language const * lang)
61 string result = lang->polyglossia();
62 if (result == "arabic")
63 // exceptional spelling; see polyglossia docs.
69 struct TeXEnvironementData
73 Language const * par_language;
74 Encoding const * prev_encoding;
79 static TeXEnvironementData prepareEnvironement(Buffer const & buf, Text const & text,
80 ParagraphList::const_iterator pit, odocstream & os, TexRow & texrow,
81 OutputParams const & runparams)
83 TeXEnvironementData data;
85 BufferParams const & bparams = buf.params();
87 // FIXME This test should not be necessary.
88 // We should perhaps issue an error if it is.
89 Layout const & style = text.inset().forcePlainLayout() ?
90 bparams.documentClass().plainLayout() : pit->layout();
92 ParagraphList const & paragraphs = text.paragraphs();
93 ParagraphList::const_iterator const priorpit =
94 pit == paragraphs.begin() ? pit : boost::prior(pit);
96 bool const use_prev_env_language = prev_env_language_ != 0
97 && priorpit->layout().isEnvironment()
98 && (priorpit->getDepth() > pit->getDepth()
99 || (priorpit->getDepth() == pit->getDepth()
100 && priorpit->layout() != pit->layout()));
102 data.prev_encoding = runparams.encoding;
103 data.par_language = pit->getParLanguage(bparams);
104 Language const * const doc_language = bparams.language;
105 Language const * const prev_par_language =
106 (pit != paragraphs.begin())
107 ? (use_prev_env_language ? prev_env_language_
108 : priorpit->getParLanguage(bparams))
110 string par_lang = data.par_language->babel();
111 string prev_par_lang = prev_par_language->babel();
112 string doc_lang = doc_language->babel();
113 string lang_begin_command = lyxrc.language_command_begin;
114 string lang_end_command = lyxrc.language_command_end;
116 if (runparams.use_polyglossia) {
117 par_lang = getPolyglossiaEnvName(data.par_language);
118 prev_par_lang = getPolyglossiaEnvName(prev_par_language);
119 doc_lang = getPolyglossiaEnvName(doc_language);
120 lang_begin_command = "\\begin{$$lang}";
121 lang_end_command = "\\end{$$lang}";
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
137 if ((lang_end_command.empty() ||
138 par_lang != doc_lang) &&
140 os << from_ascii(subst(
144 if (runparams.use_polyglossia
145 && !data.par_language->polyglossiaOpts().empty())
147 << from_ascii(data.par_language->polyglossiaOpts())
149 // 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())
161 data.leftindent_open = true;
164 if (style.isEnvironment()) {
165 os << "\\begin{" << from_ascii(style.latexname()) << '}';
166 if (style.optargs != 0 || style.reqargs != 0) {
167 int ret = latexArgInsets(*pit, os, runparams, style.reqargs, style.optargs);
173 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
175 << pit->params().labelWidthString()
177 } else if (style.labeltype == LABEL_BIBLIO) {
178 if (pit->params().labelWidthString().empty())
179 os << '{' << bibitemWidest(buf, runparams) << "}\n";
182 << pit->params().labelWidthString()
185 os << from_ascii(style.latexparam()) << '\n';
190 // in multilingual environments, the CJK tags have to be nested properly
191 data.cjk_nested = false;
192 if (data.par_language->encoding()->package() == Encoding::CJK &&
193 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
194 if (prev_par_language->encoding()->package() == Encoding::CJK)
195 os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
196 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
197 open_encoding_ = CJK;
198 data.cjk_nested = true;
205 static void finishEnvironement(odocstream & os, TexRow & texrow,
206 OutputParams const & runparams, TeXEnvironementData const & data)
208 if (open_encoding_ == CJK && data.cjk_nested) {
209 // We need to close the encoding even if it does not change
210 // to do correct environment nesting
211 os << "\\end{CJK}\n";
213 open_encoding_ = none;
216 if (data.style->isEnvironment()) {
217 os << "\\end{" << from_ascii(data.style->latexname()) << "}\n";
219 prev_env_language_ = data.par_language;
220 if (runparams.encoding != data.prev_encoding) {
221 runparams.encoding = data.prev_encoding;
222 if (!runparams.isFullUnicode())
223 os << setEncoding(data.prev_encoding->iconvName());
227 if (data.leftindent_open) {
228 os << "\\end{LyXParagraphLeftIndent}\n";
230 prev_env_language_ = data.par_language;
231 if (runparams.encoding != data.prev_encoding) {
232 runparams.encoding = data.prev_encoding;
233 if (!runparams.isFullUnicode())
234 os << setEncoding(data.prev_encoding->iconvName());
240 void TeXEnvironment(Buffer const & buf,
241 Text const & text, OutputParams const & runparams,
242 pit_type & pit, odocstream & os, TexRow & texrow)
244 ParagraphList const & paragraphs = text.paragraphs();
245 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
246 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
248 Layout const & current_layout = par->layout();
249 depth_type const current_depth = par->params().depth();
250 Length const & current_left_indent = par->params().leftIndent();
252 // This is for debugging purpose at the end.
253 pit_type const par_begin = pit;
254 for (; pit < runparams.par_end; ++pit) {
256 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
258 // check first if this is an higher depth paragraph.
259 bool go_out = (par->params().depth() < current_depth);
260 if (par->params().depth() == current_depth) {
261 // This environment is finished.
262 go_out |= (par->layout() != current_layout);
263 go_out |= (par->params().leftIndent() != current_left_indent);
266 // nothing to do here, restore pit and go out.
271 if (par->layout() == current_layout
272 && par->params().depth() == current_depth
273 && par->params().leftIndent() == current_left_indent) {
274 // We are still in the same environment so TeXOnePar and continue;
275 TeXOnePar(buf, text, pit, os, texrow, runparams);
279 // We are now in a deeper environment.
280 // Either par->layout() != current_layout
281 // Or par->params().depth() > current_depth
282 // Or par->params().leftIndent() != current_left_indent)
284 if (par->layout().isParagraph()) {
285 // FIXME (Lgb): How to handle this?
286 //&& !suffixIs(os, "\n\n")
288 // (ARRae) There should be at least one '\n' already but we need there to
289 // be two for Standard paragraphs that are depth-increment'ed to be
290 // output correctly. However, tables can also be paragraphs so
291 // don't adjust them.
294 // FIXME (Lgb): Will it ever harm to have one '\n' too
295 // many? i.e. that we sometimes will have
301 // FIXME This test should not be necessary.
302 // We should perhaps issue an error if it is.
303 bool const force_plain_layout = text.inset().forcePlainLayout();
304 Layout const & style = force_plain_layout
305 ? buf.params().documentClass().plainLayout()
308 if (!style.isEnvironment()) {
309 // This is a standard paragraph, no need to call TeXEnvironment.
310 TeXOnePar(buf, text, pit, os, texrow, runparams);
314 // This is a new environment.
315 TeXEnvironementData const data = prepareEnvironement(buf, text, par,
316 os, texrow, runparams);
317 // Recursive call to TeXEnvironment!
318 TeXEnvironment(buf, text, runparams, pit, os, texrow);
319 finishEnvironement(os, texrow, runparams, data);
322 if (pit != runparams.par_end)
323 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << par_begin << " done.");
329 int latexArgInsets(Paragraph const & par, odocstream & os,
330 OutputParams const & runparams, unsigned int reqargs,
331 unsigned int optargs)
333 unsigned int totalargs = reqargs + optargs;
334 list<InsetArgument const *> ilist;
336 InsetList::const_iterator it = par.insetList().begin();
337 InsetList::const_iterator end = par.insetList().end();
338 for (; it != end; ++it) {
339 if (it->inset->lyxCode() == ARG_CODE) {
340 if (ilist.size() >= totalargs) {
341 LYXERR0("WARNING: Found extra argument inset.");
344 InsetArgument const * ins =
345 static_cast<InsetArgument const *>(it->inset);
346 ilist.push_back(ins);
350 if (!reqargs && ilist.size() == 0)
354 bool const have_optional_args = ilist.size() > reqargs;
355 if (have_optional_args) {
356 unsigned int todo = ilist.size() - reqargs;
357 for (unsigned int i = 0; i < todo; ++i) {
358 InsetArgument const * ins = ilist.front();
360 lines += ins->latexArgument(os, runparams, true);
364 // we should now have no more insets than there are required
366 LASSERT(ilist.size() <= reqargs, /* */);
370 for (unsigned int i = 0; i < reqargs; ++i) {
372 // a required argument wasn't given, so we output {}
375 InsetArgument const * ins = ilist.front();
377 lines += ins->latexArgument(os, runparams, false);
384 // FIXME: this should be anonymous
385 void TeXOnePar(Buffer const & buf,
388 odocstream & os, TexRow & texrow,
389 OutputParams const & runparams_in,
390 string const & everypar,
391 int start_pos, int end_pos)
393 BufferParams const & bparams = buf.params();
394 ParagraphList const & paragraphs = text.paragraphs();
395 Paragraph const & par = paragraphs.at(pit);
396 // FIXME This check should not really be needed.
397 // Perhaps we should issue an error if it is.
398 Layout const style = text.inset().forcePlainLayout() ?
399 bparams.documentClass().plainLayout() : par.layout();
401 if (style.inpreamble)
404 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
407 OutputParams runparams = runparams_in;
408 runparams.isLastPar = (pit == paragraphs.size() - 1);
409 // We reinitialze par begin and end to be on the safe side
410 // with embedded inset as we don't know if they set those
412 runparams.par_begin = 0;
413 runparams.par_end = 0;
415 bool const maintext = text.isMainText();
416 // we are at the beginning of an inset and CJK is already open;
417 // we count inheritation levels to get the inset nesting right.
418 if (pit == 0 && !maintext
419 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
421 open_encoding_ = none;
424 if (text.inset().getLayout().isPassThru()) {
425 Font const outerfont = text.outerFont(pit);
427 // No newline before first paragraph in this lyxtext
431 if (!text.inset().getLayout().parbreakIsNewline()) {
437 par.latex(bparams, outerfont, os, texrow, runparams, start_pos,
442 Paragraph const * nextpar = runparams.isLastPar
443 ? 0 : ¶graphs.at(pit + 1);
445 if (style.pass_thru) {
446 Font const outerfont = text.outerFont(pit);
447 par.latex(bparams, outerfont, os, texrow, runparams, start_pos,
451 if (!style.parbreak_is_newline) {
454 } else if (nextpar) {
455 Layout const nextstyle = text.inset().forcePlainLayout() ?
456 bparams.documentClass().plainLayout() : nextpar->layout();
457 if (nextstyle.name() != style.name()) {
466 // This paragraph's language
467 Language const * const par_language = par.getParLanguage(bparams);
468 // The document's language
469 Language const * const doc_language = bparams.language;
470 // The language that was in effect when the environment this paragraph is
471 // inside of was opened
472 Language const * const outer_language =
473 (runparams.local_font != 0) ?
474 runparams.local_font->language() : doc_language;
476 Paragraph const * priorpar = (pit == 0) ? 0 : ¶graphs.at(pit - 1);
478 // The previous language that was in effect is the language of the
479 // previous paragraph, unless the previous paragraph is inside an
480 // environment with nesting depth greater than (or equal to, but with
481 // a different layout) the current one. If there is no previous
482 // paragraph, the previous language is the outer language.
483 bool const use_prev_env_language = prev_env_language_ != 0
485 && priorpar->layout().isEnvironment()
486 && (priorpar->getDepth() > par.getDepth()
487 || (priorpar->getDepth() == par.getDepth()
488 && priorpar->layout() != par.layout()));
489 Language const * const prev_language =
491 ? (use_prev_env_language ? prev_env_language_
492 : priorpar->getParLanguage(bparams))
495 string par_lang = par_language->babel();
496 string prev_lang = prev_language->babel();
497 string doc_lang = doc_language->babel();
498 string outer_lang = outer_language->babel();
499 string lang_begin_command = lyxrc.language_command_begin;
500 string lang_end_command = lyxrc.language_command_end;
502 if (runparams.use_polyglossia) {
503 par_lang = getPolyglossiaEnvName(par_language);
504 prev_lang = getPolyglossiaEnvName(prev_language);
505 doc_lang = getPolyglossiaEnvName(doc_language);
506 outer_lang = getPolyglossiaEnvName(outer_language);
507 lang_begin_command = "\\begin{$$lang}";
508 lang_end_command = "\\end{$$lang}";
510 if (par_lang != prev_lang
511 // check if we already put language command in TeXEnvironment()
512 && !(style.isEnvironment()
513 && (pit == 0 || (priorpar->layout() != par.layout()
514 && priorpar->getDepth() <= par.getDepth())
515 || priorpar->getDepth() < par.getDepth())))
517 if (!lang_end_command.empty() &&
518 prev_lang != outer_lang &&
521 os << from_ascii(subst(lang_end_command,
524 // the '%' is necessary to prevent unwanted whitespace
529 // We need to open a new language if we couldn't close the previous
530 // one (because there's no language_command_end); and even if we closed
531 // the previous one, if the current language is different than the
532 // outer_language (which is currently in effect once the previous one
534 if ((lang_end_command.empty() || par_lang != outer_lang)
535 && !par_lang.empty()) {
536 // If we're inside an inset, and that inset is within an \L or \R
537 // (or equivalents), then within the inset, too, any opposite
538 // language paragraph should appear within an \L or \R (in addition
539 // to, outside of, the normal language switch commands).
540 // This behavior is not correct for ArabTeX, though.
541 if (!runparams.use_polyglossia
543 && par_language->lang() != "arabic_arabtex"
544 && outer_language->lang() != "arabic_arabtex"
545 // are we in an inset?
546 && runparams.local_font != 0
547 // is the inset within an \L or \R?
549 // FIXME: currently, we don't check this; this means that
550 // we'll have unnnecessary \L and \R commands, but that
551 // doesn't seem to hurt (though latex will complain)
553 // is this paragraph in the opposite direction?
554 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()) {
555 // FIXME: I don't have a working copy of the Arabi package, so
556 // I'm not sure if the farsi and arabic_arabi stuff is correct
558 if (par_language->lang() == "farsi")
560 else if (outer_language->lang() == "farsi")
562 else if (par_language->lang() == "arabic_arabi")
564 else if (outer_language->lang() == "arabic_arabi")
566 // remaining RTL languages currently is hebrew
567 else if (par_language->rightToLeft())
572 // With CJK, the CJK tag has to be closed first (see below)
573 if (runparams.encoding->package() != Encoding::CJK) {
574 os << from_ascii(subst(
578 if (runparams.use_polyglossia
579 && !par_language->polyglossiaOpts().empty())
581 << from_ascii(par_language->polyglossiaOpts())
583 // the '%' is necessary to prevent unwanted whitespace
590 // Switch file encoding if necessary; no need to do this for "default"
591 // encoding, since this only affects the position of the outputted
592 // \inputencoding command; the encoding switch will occur when necessary
593 if (bparams.inputenc == "auto" &&
594 runparams.encoding->package() != Encoding::none) {
595 // Look ahead for future encoding changes.
596 // We try to output them at the beginning of the paragraph,
597 // since the \inputencoding command is not allowed e.g. in
598 // sections. For this reason we only set runparams.moving_arg
599 // after checking for the encoding change, otherwise the
600 // change would be always avoided by switchEncoding().
601 for (pos_type i = 0; i < par.size(); ++i) {
602 char_type const c = par.getChar(i);
603 Encoding const * const encoding =
604 par.getFontSettings(bparams, i).language()->encoding();
605 if (encoding->package() != Encoding::CJK &&
606 runparams.encoding->package() == Encoding::inputenc &&
611 // All characters before c are in the ASCII range, and
612 // c is non-ASCII (but no inset), so change the
613 // encoding to that required by the language of c.
614 // With CJK, only add switch if we have CJK content at the beginning
616 if (encoding->package() != Encoding::CJK || i == 0) {
617 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
619 // the following is necessary after a CJK environment in a multilingual
620 // context (nesting issue).
621 if (par_language->encoding()->package() == Encoding::CJK &&
622 open_encoding_ != CJK && cjk_inherited_ == 0) {
623 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
624 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
625 open_encoding_ = CJK;
628 if (encoding->package() != Encoding::none && enc_switch.first) {
629 if (enc_switch.second > 0) {
630 // the '%' is necessary to prevent unwanted whitespace
634 // With CJK, the CJK tag had to be closed first (see above)
635 if (runparams.encoding->package() == Encoding::CJK) {
636 os << from_ascii(subst(
640 // the '%' is necessary to prevent unwanted whitespace
644 runparams.encoding = encoding;
651 runparams.moving_arg |= style.needprotect;
652 Encoding const * const prev_encoding = runparams.encoding;
654 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
655 if (par.allowParagraphCustomization()) {
656 if (par.params().startOfAppendix()) {
657 os << "\\appendix\n";
661 if (!par.params().spacing().isDefault()
662 && (pit == 0 || !priorpar->hasSameLayout(par)))
664 os << from_ascii(par.params().spacing().writeEnvirBegin(useSetSpace))
669 if (style.isCommand()) {
675 switch (style.latextype) {
677 os << '\\' << from_ascii(style.latexname());
679 // Separate handling of optional argument inset.
680 if (style.optargs != 0 || style.reqargs != 0) {
681 int ret = latexArgInsets(par, os, runparams, style.reqargs, style.optargs);
688 os << from_ascii(style.latexparam());
690 case LATEX_ITEM_ENVIRONMENT:
691 case LATEX_LIST_ENVIRONMENT:
694 case LATEX_BIB_ENVIRONMENT:
695 // ignore this, the inset will write itself
701 Font const outerfont = text.outerFont(pit);
704 os << from_utf8(everypar);
705 par.latex(bparams, outerfont, os, texrow, runparams, start_pos, end_pos);
707 // Make sure that \\par is done with the font of the last
708 // character if this has another size as the default.
709 // This is necessary because LaTeX (and LyX on the screen)
710 // calculates the space between the baselines according
711 // to this font. (Matthias)
713 // Is this really needed ? (Dekel)
714 // We do not need to use to change the font for the last paragraph
717 Font const font = par.empty()
718 ? par.getLayoutFont(bparams, outerfont)
719 : par.getFont(bparams, par.size() - 1, outerfont);
721 bool const is_command = style.isCommand();
723 if (style.resfont.size() != font.fontInfo().size()
727 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
728 } else if (is_command) {
730 if (runparams.encoding != prev_encoding) {
731 runparams.encoding = prev_encoding;
732 if (!runparams.isFullUnicode())
733 os << setEncoding(prev_encoding->iconvName());
737 bool pending_newline = false;
738 switch (style.latextype) {
739 case LATEX_ITEM_ENVIRONMENT:
740 case LATEX_LIST_ENVIRONMENT:
741 if (nextpar && (par.params().depth() < nextpar->params().depth()))
742 pending_newline = true;
744 case LATEX_ENVIRONMENT: {
745 // if its the last paragraph of the current environment
746 // skip it otherwise fall through
748 && (nextpar->layout() != par.layout()
749 || nextpar->params().depth() != par.params().depth()))
753 // fall through possible
755 // we don't need it for the last paragraph!!!
757 pending_newline = true;
760 if (par.allowParagraphCustomization()) {
761 if (!par.params().spacing().isDefault()
762 && (runparams.isLastPar || !nextpar->hasSameLayout(par))) {
763 if (pending_newline) {
767 os << from_ascii(par.params().spacing().writeEnvirEnd(useSetSpace));
768 pending_newline = true;
772 // Closing the language is needed for the last paragraph; it is also
773 // needed if we're within an \L or \R that we may have opened above (not
774 // necessarily in this paragraph) and are about to close.
775 bool closing_rtl_ltr_environment = !runparams.use_polyglossia
777 && (par_language->lang() != "arabic_arabtex"
778 && outer_language->lang() != "arabic_arabtex")
779 // have we opened and \L or \R environment?
780 && runparams.local_font != 0
781 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()
782 // are we about to close the language?
783 &&((nextpar && par_language->babel() != (nextpar->getParLanguage(bparams))->babel())
784 || (runparams.isLastPar && par_language->babel() != outer_language->babel()));
786 if (closing_rtl_ltr_environment
787 || (runparams.isLastPar && par_language->babel() != outer_language->babel())) {
788 // Since \selectlanguage write the language to the aux file,
789 // we need to reset the language at the end of footnote or
792 if (pending_newline) {
796 // when the paragraph uses CJK, the language has to be closed earlier
797 if (font.language()->encoding()->package() != Encoding::CJK) {
798 if (lang_end_command.empty()) {
799 // If this is a child, we should restore the
800 // master language after the last paragraph.
801 Language const * const current_language =
802 (runparams.isLastPar && runparams.master_language)
803 ? runparams.master_language
805 string const current_lang = runparams.use_polyglossia ?
806 getPolyglossiaEnvName(current_language)
807 : current_language->babel();
808 if (!current_lang.empty()) {
809 os << from_ascii(subst(
813 pending_newline = true;
815 } else if (!par_lang.empty()) {
816 os << from_ascii(subst(
820 pending_newline = true;
824 if (closing_rtl_ltr_environment)
827 if (pending_newline) {
832 // if this is a CJK-paragraph and the next isn't, close CJK
833 // also if the next paragraph is a multilingual environment (because of nesting)
835 && open_encoding_ == CJK
836 && (nextpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK
837 || (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
838 // inbetween environments, CJK has to be closed later (nesting!)
839 && (!style.isEnvironment() || !nextpar->layout().isEnvironment())) {
840 os << "\\end{CJK}\n";
841 open_encoding_ = none;
844 // If this is the last paragraph, close the CJK environment
845 // if necessary. If it's an environment, we'll have to \end that first.
846 if (runparams.isLastPar && !style.isEnvironment()) {
847 switch (open_encoding_) {
849 // do nothing at the end of child documents
850 if (maintext && buf.masterBuffer() != &buf)
856 os << "\\end{CJK}\n";
861 open_encoding_ = none;
866 open_encoding_ = none;
876 // If this is the last paragraph, and a local_font was set upon entering
877 // the inset, and we're using "auto" or "default" encoding, the encoding
878 // should be set back to that local_font's encoding.
879 // However, do not change the encoding when a fully unicode aware backend
880 // such as XeTeX is used.
881 if (runparams.isLastPar && runparams_in.local_font != 0
882 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
883 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
884 && (!runparams.isFullUnicode())) {
885 runparams_in.encoding = runparams_in.local_font->language()->encoding();
886 os << setEncoding(runparams_in.encoding->iconvName());
888 // Otherwise, the current encoding should be set for the next paragraph.
890 runparams_in.encoding = runparams.encoding;
893 // we don't need a newline for the last paragraph!!!
894 // Note from JMarc: we will re-add a \n explicitly in
895 // TeXEnvironment, because it is needed in this case
897 Layout const & next_layout = nextpar->layout();
898 if (style == next_layout
899 // no blank lines before environments!
900 || !next_layout.isEnvironment()
901 // unless there's a depth change
902 // FIXME What we really want to do here is put every \begin and \end
903 // tag on a new line (which was not the case with nested environments).
904 // But in the present state of play, we don't have access to the
905 // information whether the current TeX row is empty or not.
906 // For some ideas about how to fix this, see this thread:
907 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
908 || nextpar->params().depth() != par.params().depth()) {
914 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " done; ptr "
915 << &par << " next " << nextpar);
921 // LaTeX all paragraphs
922 void latexParagraphs(Buffer const & buf,
926 OutputParams const & runparams,
927 string const & everypar)
929 BufferParams const & bparams = buf.params();
931 bool const maintext = text.isMainText();
932 bool const is_child = buf.masterBuffer() != &buf;
934 // Open a CJK environment at the beginning of the main buffer
935 // if the document's language is a CJK language
936 // (but not in child documents)
937 if (maintext && !is_child
938 && bparams.encoding().package() == Encoding::CJK) {
939 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
940 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
942 open_encoding_ = CJK;
944 // if "auto begin" is switched off, explicitly switch the
945 // language on at start
946 string const mainlang = runparams.use_polyglossia ?
947 getPolyglossiaEnvName(bparams.language)
948 : bparams.language->babel();
949 string const lang_begin_command = runparams.use_polyglossia ?
950 "\\begin{$$lang}" : lyxrc.language_command_begin;
952 if (maintext && !lyxrc.language_auto_begin &&
955 os << from_utf8(subst(lang_begin_command,
958 if (runparams.use_polyglossia
959 && !bparams.language->polyglossiaOpts().empty())
961 << from_ascii(bparams.language->polyglossiaOpts())
967 ParagraphList const & paragraphs = text.paragraphs();
968 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
970 if (runparams.par_begin == runparams.par_end) {
971 // The full doc will be exported but it is easier to just rely on
972 // runparams range parameters that will be passed TeXEnvironment.
973 runparams.par_begin = 0;
974 runparams.par_end = paragraphs.size();
977 pit_type pit = runparams.par_begin;
978 // lastpit is for the language check after the loop.
980 // variables used in the loop:
981 bool was_title = false;
982 bool already_title = false;
983 DocumentClass const & tclass = bparams.documentClass();
985 for (; pit < runparams.par_end; ++pit) {
987 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
989 // FIXME This check should not be needed. We should
990 // perhaps issue an error if it is.
991 Layout const & layout = text.inset().forcePlainLayout() ?
992 tclass.plainLayout() : par->layout();
994 if (layout.intitle) {
996 LYXERR0("Error in latexParagraphs: You"
997 " should not mix title layouts"
998 " with normal ones.");
999 } else if (!was_title) {
1001 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1003 << from_ascii(tclass.titlename())
1008 } else if (was_title && !already_title) {
1009 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1010 os << "\\end{" << from_ascii(tclass.titlename())
1014 os << "\\" << from_ascii(tclass.titlename())
1018 already_title = true;
1023 if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
1024 // This is a standard top level paragraph, TeX it and continue.
1025 TeXOnePar(buf, text, pit, os, texrow, runparams, everypar);
1029 TeXEnvironementData const data = prepareEnvironement(buf, text, par,
1030 os, texrow, runparams);
1031 // pit can be changed in TeXEnvironment.
1032 TeXEnvironment(buf, text, runparams, pit, os, texrow);
1033 finishEnvironement(os, texrow, runparams, data);
1036 if (pit == runparams.par_end) {
1037 // Make sure that the last paragraph is
1038 // correctly terminated (because TeXOnePar does
1039 // not add a \n in this case)
1044 // It might be that we only have a title in this document
1045 if (was_title && !already_title) {
1046 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1047 os << "\\end{" << from_ascii(tclass.titlename())
1051 os << "\\" << from_ascii(tclass.titlename())
1057 // if "auto end" is switched off, explicitly close the language at the end
1058 // but only if the last par is in a babel language
1059 string const lang_end_command = runparams.use_polyglossia ?
1060 "\\end{$$lang}" : lyxrc.language_command_end;
1061 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1062 paragraphs.at(lastpit).getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1063 os << from_utf8(subst(lang_end_command,
1070 // If the last paragraph is an environment, we'll have to close
1071 // CJK at the very end to do proper nesting.
1072 if (maintext && !is_child && open_encoding_ == CJK) {
1073 os << "\\end{CJK}\n";
1075 open_encoding_ = none;
1078 // reset inherited encoding
1079 if (cjk_inherited_ > 0) {
1080 cjk_inherited_ -= 1;
1081 if (cjk_inherited_ == 0)
1082 open_encoding_ = CJK;
1087 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1088 OutputParams const & runparams, Encoding const & newEnc,
1091 Encoding const & oldEnc = *runparams.encoding;
1092 bool moving_arg = runparams.moving_arg;
1093 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
1095 return make_pair(false, 0);
1097 // Do nothing if the encoding is unchanged.
1098 if (oldEnc.name() == newEnc.name())
1099 return make_pair(false, 0);
1101 // FIXME We ignore encoding switches from/to encodings that do
1102 // neither support the inputenc package nor the CJK package here.
1103 // This does of course only work in special cases (e.g. switch from
1104 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1105 // but it is the best we can do
1106 if (oldEnc.package() == Encoding::none
1107 || newEnc.package() == Encoding::none)
1108 return make_pair(false, 0);
1110 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1111 << oldEnc.name() << " to " << newEnc.name());
1112 os << setEncoding(newEnc.iconvName());
1113 if (bparams.inputenc == "default")
1114 return make_pair(true, 0);
1116 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1117 switch (newEnc.package()) {
1118 case Encoding::none:
1119 case Encoding::japanese:
1120 // shouldn't ever reach here, see above
1121 return make_pair(true, 0);
1122 case Encoding::inputenc: {
1123 int count = inputenc_arg.length();
1124 if (oldEnc.package() == Encoding::CJK &&
1125 open_encoding_ == CJK) {
1127 open_encoding_ = none;
1130 else if (oldEnc.package() == Encoding::inputenc &&
1131 open_encoding_ == inputenc) {
1133 open_encoding_ = none;
1136 if (runparams.local_font != 0
1137 && oldEnc.package() == Encoding::CJK) {
1138 // within insets, \inputenc switches need
1139 // to be embraced within \bgroup...\egroup;
1143 open_encoding_ = inputenc;
1145 // with the japanese option, inputenc is omitted.
1146 if (runparams.use_japanese)
1147 return make_pair(true, count);
1148 os << "\\inputencoding{" << inputenc_arg << '}';
1149 return make_pair(true, count + 16);
1151 case Encoding::CJK: {
1152 int count = inputenc_arg.length();
1153 if (oldEnc.package() == Encoding::CJK &&
1154 open_encoding_ == CJK) {
1158 if (oldEnc.package() == Encoding::inputenc &&
1159 open_encoding_ == inputenc) {
1163 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1164 << from_ascii(bparams.fonts_cjk) << "}";
1165 open_encoding_ = CJK;
1166 return make_pair(true, count + 15);
1169 // Dead code to avoid a warning:
1170 return make_pair(true, 0);