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 ParagraphList::const_iterator
241 TeXEnvironment(Buffer const & buf,
243 ParagraphList::const_iterator pit,
244 odocstream & os, TexRow & texrow,
245 OutputParams const & runparams)
247 LYXERR(Debug::LATEX, "TeXEnvironment... " << &*pit);
248 ParagraphList const & paragraphs = text.paragraphs();
250 ParagraphList::const_iterator par = pit;
252 TeXOnePar(buf, text, par, os, texrow, runparams);
253 if (par != paragraphs.end())
256 if (par == paragraphs.end()) {
257 // Make sure that the last paragraph is
258 // correctly terminated (because TeXOnePar does
259 // not add a \n in this case)
262 } else if (par->params().depth() > pit->params().depth()) {
263 if (par->layout().isParagraph()) {
265 // How to handle this? (Lgb)
266 //&& !suffixIs(os, "\n\n")
268 // There should be at least one '\n' already
269 // but we need there to be two for Standard
270 // paragraphs that are depth-increment'ed to be
271 // output correctly. However, tables can
272 // also be paragraphs so don't adjust them.
275 // Will it ever harm to have one '\n' too
276 // many? i.e. that we sometimes will have
277 // three in a row. (Lgb)
282 LYXERR(Debug::LATEX, "TeXDeeper... " << &*par);
283 bool const force_plain_layout = text.inset().forcePlainLayout();
284 depth_type const max_depth = par->params().depth();
286 // FIXME: move that to a for loop!
287 while (par != paragraphs.end()
288 && par->params().depth() == max_depth) {
289 // FIXME This test should not be necessary.
290 // We should perhaps issue an error if it is.
291 Layout const & style = force_plain_layout
292 ? buf.params().documentClass().plainLayout() : par->layout();
293 if (style.isEnvironment()) {
294 TeXEnvironementData const data = prepareEnvironement(buf,
295 text, par, os, texrow, runparams);
296 // Recursive call to TeXEnvironment!
297 par = TeXEnvironment(buf, text, par, os, texrow, runparams);
298 finishEnvironement(os, texrow, runparams, data);
300 TeXOnePar(buf, text, par, os, texrow, runparams);
301 if (par != text.paragraphs().end())
305 LYXERR(Debug::LATEX, "TeXDeeper...done ");
307 } while (par != paragraphs.end()
308 && par->layout() == pit->layout()
309 && par->params().depth() == pit->params().depth()
310 && par->params().leftIndent() == pit->params().leftIndent());
312 if (par != paragraphs.end())
313 LYXERR(Debug::LATEX, "TeXEnvironment...done " << &*par);
321 int latexArgInsets(Paragraph const & par, odocstream & os,
322 OutputParams const & runparams, unsigned int reqargs,
323 unsigned int optargs)
325 unsigned int totalargs = reqargs + optargs;
326 list<InsetArgument const *> ilist;
328 InsetList::const_iterator it = par.insetList().begin();
329 InsetList::const_iterator end = par.insetList().end();
330 for (; it != end; ++it) {
331 if (it->inset->lyxCode() == ARG_CODE) {
332 if (ilist.size() >= totalargs) {
333 LYXERR0("WARNING: Found extra argument inset.");
336 InsetArgument const * ins =
337 static_cast<InsetArgument const *>(it->inset);
338 ilist.push_back(ins);
342 if (!reqargs && ilist.size() == 0)
346 bool const have_optional_args = ilist.size() > reqargs;
347 if (have_optional_args) {
348 unsigned int todo = ilist.size() - reqargs;
349 for (unsigned int i = 0; i < todo; ++i) {
350 InsetArgument const * ins = ilist.front();
352 lines += ins->latexArgument(os, runparams, true);
356 // we should now have no more insets than there are required
358 LASSERT(ilist.size() <= reqargs, /* */);
362 for (unsigned int i = 0; i < reqargs; ++i) {
364 // a required argument wasn't given, so we output {}
367 InsetArgument const * ins = ilist.front();
369 lines += ins->latexArgument(os, runparams, false);
376 // FIXME: this should be anonymous
377 void TeXOnePar(Buffer const & buf,
379 ParagraphList::const_iterator const pit,
380 odocstream & os, TexRow & texrow,
381 OutputParams const & runparams_in,
382 string const & everypar,
383 int start_pos, int end_pos)
385 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
388 BufferParams const & bparams = buf.params();
389 // FIXME This check should not really be needed.
390 // Perhaps we should issue an error if it is.
391 Layout const style = text.inset().forcePlainLayout() ?
392 bparams.documentClass().plainLayout() : pit->layout();
394 ParagraphList const & paragraphs = text.paragraphs();
395 ParagraphList::const_iterator const priorpit =
396 pit == paragraphs.begin() ? pit : boost::prior(pit);
397 ParagraphList::const_iterator const nextpit =
398 pit == paragraphs.end() ? pit : boost::next(pit);
400 if (style.inpreamble)
403 OutputParams runparams = runparams_in;
404 runparams.isLastPar = nextpit == paragraphs.end();
406 bool const maintext = text.isMainText();
407 // we are at the beginning of an inset and CJK is already open;
408 // we count inheritation levels to get the inset nesting right.
409 if (pit == paragraphs.begin() && !maintext
410 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
412 open_encoding_ = none;
415 if (text.inset().getLayout().isPassThru()) {
416 int const dist = paragraphs.position(pit);
417 Font const outerfont = text.outerFont(dist);
419 // No newline before first paragraph in this lyxtext
423 if (!text.inset().getLayout().parbreakIsNewline()) {
429 pit->latex(bparams, outerfont, os, texrow,
430 runparams, start_pos, end_pos);
434 if (style.pass_thru) {
435 int const dist = paragraphs.position(pit);
436 Font const outerfont = text.outerFont(dist);
437 pit->latex(bparams, outerfont, os, texrow,
438 runparams, start_pos, end_pos);
441 if (!style.parbreak_is_newline) {
444 } else if (nextpit != paragraphs.end()) {
445 Layout const nextstyle = text.inset().forcePlainLayout() ?
446 bparams.documentClass().plainLayout() : nextpit->layout();
447 if (nextstyle.name() != style.name()) {
456 // This paragraph's language
457 Language const * const par_language = pit->getParLanguage(bparams);
458 // The document's language
459 Language const * const doc_language = bparams.language;
460 // The language that was in effect when the environment this paragraph is
461 // inside of was opened
462 Language const * const outer_language =
463 (runparams.local_font != 0) ?
464 runparams.local_font->language() : doc_language;
466 // The previous language that was in effect is the language of the
467 // previous paragraph, unless the previous paragraph is inside an
468 // environment with nesting depth greater than (or equal to, but with
469 // a different layout) the current one. If there is no previous
470 // paragraph, the previous language is the outer language.
471 bool const use_prev_env_language = prev_env_language_ != 0
472 && priorpit->layout().isEnvironment()
473 && (priorpit->getDepth() > pit->getDepth()
474 || (priorpit->getDepth() == pit->getDepth()
475 && priorpit->layout() != pit->layout()));
476 Language const * const prev_language =
477 (pit != paragraphs.begin())
478 ? (use_prev_env_language ? prev_env_language_
479 : priorpit->getParLanguage(bparams))
482 string par_lang = par_language->babel();
483 string prev_lang = prev_language->babel();
484 string doc_lang = doc_language->babel();
485 string outer_lang = outer_language->babel();
486 string lang_begin_command = lyxrc.language_command_begin;
487 string lang_end_command = lyxrc.language_command_end;
489 if (runparams.use_polyglossia) {
490 par_lang = getPolyglossiaEnvName(par_language);
491 prev_lang = getPolyglossiaEnvName(prev_language);
492 doc_lang = getPolyglossiaEnvName(doc_language);
493 outer_lang = getPolyglossiaEnvName(outer_language);
494 lang_begin_command = "\\begin{$$lang}";
495 lang_end_command = "\\end{$$lang}";
497 if (par_lang != prev_lang
498 // check if we already put language command in TeXEnvironment()
499 && !(style.isEnvironment()
500 && (pit == paragraphs.begin() ||
501 (priorpit->layout() != pit->layout() &&
502 priorpit->getDepth() <= pit->getDepth())
503 || priorpit->getDepth() < pit->getDepth())))
505 if (!lang_end_command.empty() &&
506 prev_lang != outer_lang &&
509 os << from_ascii(subst(lang_end_command,
512 // the '%' is necessary to prevent unwanted whitespace
517 // We need to open a new language if we couldn't close the previous
518 // one (because there's no language_command_end); and even if we closed
519 // the previous one, if the current language is different than the
520 // outer_language (which is currently in effect once the previous one
522 if ((lang_end_command.empty() ||
523 par_lang != outer_lang) &&
525 // If we're inside an inset, and that inset is within an \L or \R
526 // (or equivalents), then within the inset, too, any opposite
527 // language paragraph should appear within an \L or \R (in addition
528 // to, outside of, the normal language switch commands).
529 // This behavior is not correct for ArabTeX, though.
530 if (!runparams.use_polyglossia &&
532 (par_language->lang() != "arabic_arabtex" &&
533 outer_language->lang() != "arabic_arabtex") &&
534 // are we in an inset?
535 runparams.local_font != 0 &&
536 // is the inset within an \L or \R?
538 // FIXME: currently, we don't check this; this means that
539 // we'll have unnnecessary \L and \R commands, but that
540 // doesn't seem to hurt (though latex will complain)
542 // is this paragraph in the opposite direction?
543 runparams.local_font->isRightToLeft() !=
544 par_language->rightToLeft()
546 // FIXME: I don't have a working copy of the Arabi package, so
547 // I'm not sure if the farsi and arabic_arabi stuff is correct
549 if (par_language->lang() == "farsi")
551 else if (outer_language->lang() == "farsi")
553 else if (par_language->lang() == "arabic_arabi")
555 else if (outer_language->lang() == "arabic_arabi")
557 // remaining RTL languages currently is hebrew
558 else if (par_language->rightToLeft())
563 // With CJK, the CJK tag has to be closed first (see below)
564 if (runparams.encoding->package() != Encoding::CJK) {
565 os << from_ascii(subst(
569 if (runparams.use_polyglossia
570 && !par_language->polyglossiaOpts().empty())
572 << from_ascii(par_language->polyglossiaOpts())
574 // the '%' is necessary to prevent unwanted whitespace
581 // Switch file encoding if necessary; no need to do this for "default"
582 // encoding, since this only affects the position of the outputted
583 // \inputencoding command; the encoding switch will occur when necessary
584 if (bparams.inputenc == "auto" &&
585 runparams.encoding->package() != Encoding::none) {
586 // Look ahead for future encoding changes.
587 // We try to output them at the beginning of the paragraph,
588 // since the \inputencoding command is not allowed e.g. in
589 // sections. For this reason we only set runparams.moving_arg
590 // after checking for the encoding change, otherwise the
591 // change would be always avoided by switchEncoding().
592 for (pos_type i = 0; i < pit->size(); ++i) {
593 char_type const c = pit->getChar(i);
594 Encoding const * const encoding =
595 pit->getFontSettings(bparams, i).language()->encoding();
596 if (encoding->package() != Encoding::CJK &&
597 runparams.encoding->package() == Encoding::inputenc &&
602 // All characters before c are in the ASCII range, and
603 // c is non-ASCII (but no inset), so change the
604 // encoding to that required by the language of c.
605 // With CJK, only add switch if we have CJK content at the beginning
607 if (encoding->package() != Encoding::CJK || i == 0) {
608 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
610 // the following is necessary after a CJK environment in a multilingual
611 // context (nesting issue).
612 if (par_language->encoding()->package() == Encoding::CJK &&
613 open_encoding_ != CJK && cjk_inherited_ == 0) {
614 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
615 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
616 open_encoding_ = CJK;
619 if (encoding->package() != Encoding::none && enc_switch.first) {
620 if (enc_switch.second > 0) {
621 // the '%' is necessary to prevent unwanted whitespace
625 // With CJK, the CJK tag had to be closed first (see above)
626 if (runparams.encoding->package() == Encoding::CJK) {
627 os << from_ascii(subst(
631 // the '%' is necessary to prevent unwanted whitespace
635 runparams.encoding = encoding;
642 runparams.moving_arg |= style.needprotect;
643 Encoding const * const prev_encoding = runparams.encoding;
645 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
646 if (pit->allowParagraphCustomization()) {
647 if (pit->params().startOfAppendix()) {
648 os << "\\appendix\n";
652 if (!pit->params().spacing().isDefault()
653 && (pit == paragraphs.begin()
654 || !priorpit->hasSameLayout(*pit)))
656 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
661 if (style.isCommand()) {
667 switch (style.latextype) {
669 os << '\\' << from_ascii(style.latexname());
671 // Separate handling of optional argument inset.
672 if (style.optargs != 0 || style.reqargs != 0) {
673 int ret = latexArgInsets(*pit, os, runparams, style.reqargs, style.optargs);
680 os << from_ascii(style.latexparam());
682 case LATEX_ITEM_ENVIRONMENT:
683 case LATEX_LIST_ENVIRONMENT:
686 case LATEX_BIB_ENVIRONMENT:
687 // ignore this, the inset will write itself
693 Font const outerfont = text.outerFont(paragraphs.position(pit));
696 os << from_utf8(everypar);
697 pit->latex(bparams, outerfont, os, texrow,
698 runparams, start_pos, end_pos);
700 // Make sure that \\par is done with the font of the last
701 // character if this has another size as the default.
702 // This is necessary because LaTeX (and LyX on the screen)
703 // calculates the space between the baselines according
704 // to this font. (Matthias)
706 // Is this really needed ? (Dekel)
707 // We do not need to use to change the font for the last paragraph
710 Font const font = pit->empty()
711 ? pit->getLayoutFont(bparams, outerfont)
712 : pit->getFont(bparams, pit->size() - 1, outerfont);
714 bool const is_command = style.isCommand();
716 if (style.resfont.size() != font.fontInfo().size()
717 && nextpit != paragraphs.end()
720 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
721 } else if (is_command) {
723 if (runparams.encoding != prev_encoding) {
724 runparams.encoding = prev_encoding;
725 if (!runparams.isFullUnicode())
726 os << setEncoding(prev_encoding->iconvName());
730 bool pending_newline = false;
731 switch (style.latextype) {
732 case LATEX_ITEM_ENVIRONMENT:
733 case LATEX_LIST_ENVIRONMENT:
734 if (nextpit != paragraphs.end()
735 && (pit->params().depth() < nextpit->params().depth()))
736 pending_newline = true;
738 case LATEX_ENVIRONMENT: {
739 // if its the last paragraph of the current environment
740 // skip it otherwise fall through
741 if (nextpit != paragraphs.end() &&
742 (nextpit->layout() != pit->layout()
743 || nextpit->params().depth() != pit->params().depth()))
747 // fall through possible
749 // we don't need it for the last paragraph!!!
750 if (nextpit != paragraphs.end())
751 pending_newline = true;
754 if (pit->allowParagraphCustomization()) {
755 if (!pit->params().spacing().isDefault()
756 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
758 if (pending_newline) {
762 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
763 pending_newline = true;
767 // Closing the language is needed for the last paragraph; it is also
768 // needed if we're within an \L or \R that we may have opened above (not
769 // necessarily in this paragraph) and are about to close.
770 bool closing_rtl_ltr_environment =
771 !runparams.use_polyglossia &&
773 (par_language->lang() != "arabic_arabtex" &&
774 outer_language->lang() != "arabic_arabtex") &&
775 // have we opened and \L or \R environment?
776 runparams.local_font != 0 &&
777 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
778 // are we about to close the language?
779 ((nextpit != paragraphs.end() &&
780 par_language->babel() !=
781 (nextpit->getParLanguage(bparams))->babel()) ||
782 (nextpit == paragraphs.end() &&
783 par_language->babel() != outer_language->babel()));
785 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
786 && par_language->babel() != outer_language->babel())) {
787 // Since \selectlanguage write the language to the aux file,
788 // we need to reset the language at the end of footnote or
791 if (pending_newline) {
795 // when the paragraph uses CJK, the language has to be closed earlier
796 if (font.language()->encoding()->package() != Encoding::CJK) {
797 if (lang_end_command.empty()) {
798 // If this is a child, we should restore the
799 // master language after the last paragraph.
800 Language const * const current_language =
801 (nextpit == paragraphs.end()
802 && 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)
834 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
835 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
836 (nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams)))
837 // inbetween environments, CJK has to be closed later (nesting!)
838 && (!style.isEnvironment() || !nextpit->layout().isEnvironment())) {
839 os << "\\end{CJK}\n";
840 open_encoding_ = none;
843 // If this is the last paragraph, close the CJK environment
844 // if necessary. If it's an environment, we'll have to \end that first.
845 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
846 switch (open_encoding_) {
848 // do nothing at the end of child documents
849 if (maintext && buf.masterBuffer() != &buf)
855 os << "\\end{CJK}\n";
860 open_encoding_ = none;
865 open_encoding_ = none;
875 // If this is the last paragraph, and a local_font was set upon entering
876 // the inset, and we're using "auto" or "default" encoding, the encoding
877 // should be set back to that local_font's encoding.
878 // However, do not change the encoding when a fully unicode aware backend
879 // such as XeTeX is used.
880 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
881 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
882 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
883 && (!runparams.isFullUnicode())) {
884 runparams_in.encoding = runparams_in.local_font->language()->encoding();
885 os << setEncoding(runparams_in.encoding->iconvName());
887 // Otherwise, the current encoding should be set for the next paragraph.
889 runparams_in.encoding = runparams.encoding;
892 // we don't need a newline for the last paragraph!!!
893 // Note from JMarc: we will re-add a \n explicitly in
894 // TeXEnvironment, because it is needed in this case
895 if (nextpit != paragraphs.end()) {
896 Layout const & next_layout = nextpit->layout();
897 if (style == next_layout
898 // no blank lines before environments!
899 || !next_layout.isEnvironment()
900 // unless there's a depth change
901 // FIXME What we really want to do here is put every \begin and \end
902 // tag on a new line (which was not the case with nested environments).
903 // But in the present state of play, we don't have access to the
904 // information whether the current TeX row is empty or not.
905 // For some ideas about how to fix this, see this thread:
906 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
907 || nextpit->params().depth() != pit->params().depth()) {
913 if (nextpit != paragraphs.end())
914 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*nextpit);
920 // LaTeX all paragraphs
921 void latexParagraphs(Buffer const & buf,
925 OutputParams const & runparams,
926 string const & everypar)
928 BufferParams const & bparams = buf.params();
930 bool const maintext = text.isMainText();
931 bool const is_child = buf.masterBuffer() != &buf;
933 // Open a CJK environment at the beginning of the main buffer
934 // if the document's language is a CJK language
935 // (but not in child documents)
936 if (maintext && !is_child
937 && bparams.encoding().package() == Encoding::CJK) {
938 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
939 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
941 open_encoding_ = CJK;
943 // if "auto begin" is switched off, explicitly switch the
944 // language on at start
945 string const mainlang = runparams.use_polyglossia ?
946 getPolyglossiaEnvName(bparams.language)
947 : bparams.language->babel();
948 string const lang_begin_command = runparams.use_polyglossia ?
949 "\\begin{$$lang}" : lyxrc.language_command_begin;
951 if (maintext && !lyxrc.language_auto_begin &&
954 os << from_utf8(subst(lang_begin_command,
957 if (runparams.use_polyglossia
958 && !bparams.language->polyglossiaOpts().empty())
960 << from_ascii(bparams.language->polyglossiaOpts())
966 ParagraphList const & paragraphs = text.paragraphs();
967 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
968 // if only part of the paragraphs will be outputed
969 bool const partial_export = (runparams.par_begin != runparams.par_end);
970 pit_type const par_begin = partial_export ? runparams.par_begin : 0;
971 pit_type const par_end = partial_export ? runparams.par_end : paragraphs.size();
973 if (partial_export) {
974 // runparams will be passed to nested paragraphs, so
975 // we have to reset the range parameters.
976 const_cast<OutputParams&>(runparams).par_begin = 0;
977 const_cast<OutputParams&>(runparams).par_end = 0;
980 pit_type pit = par_begin;
981 // lastpit is for the language check after the loop.
983 // variables used in the loop:
984 bool was_title = false;
985 bool already_title = false;
986 DocumentClass const & tclass = bparams.documentClass();
988 for (; pit < par_end; ++pit) {
990 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
992 // FIXME This check should not be needed. We should
993 // perhaps issue an error if it is.
994 Layout const & layout = text.inset().forcePlainLayout() ?
995 tclass.plainLayout() : par->layout();
997 if (layout.intitle) {
999 lyxerr << "Error in latexParagraphs: You"
1000 " should not mix title layouts"
1001 " with normal ones." << endl;
1002 } else if (!was_title) {
1004 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1006 << from_ascii(tclass.titlename())
1011 } else if (was_title && !already_title) {
1012 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1013 os << "\\end{" << from_ascii(tclass.titlename())
1017 os << "\\" << from_ascii(tclass.titlename())
1021 already_title = true;
1025 if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
1026 // Standard top level paragraph.
1027 TeXOnePar(buf, text, par, os, texrow, runparams, everypar);
1031 TeXEnvironementData const data = prepareEnvironement(buf, text, par,
1032 os, texrow, runparams);
1033 par = TeXEnvironment(buf, text, par, os, texrow, runparams);
1034 finishEnvironement(os, texrow, runparams, data);
1035 pit = paragraphs.position(par);
1036 //FIXME: TeXEnvironment() advances one par too much.
1040 // It might be that we only have a title in this document
1041 if (was_title && !already_title) {
1042 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1043 os << "\\end{" << from_ascii(tclass.titlename())
1047 os << "\\" << from_ascii(tclass.titlename())
1053 // if "auto end" is switched off, explicitly close the language at the end
1054 // but only if the last par is in a babel language
1055 string const lang_end_command = runparams.use_polyglossia ?
1056 "\\end{$$lang}" : lyxrc.language_command_end;
1057 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1058 paragraphs.at(lastpit).getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1059 os << from_utf8(subst(lang_end_command,
1066 // If the last paragraph is an environment, we'll have to close
1067 // CJK at the very end to do proper nesting.
1068 if (maintext && !is_child && open_encoding_ == CJK) {
1069 os << "\\end{CJK}\n";
1071 open_encoding_ = none;
1074 // reset inherited encoding
1075 if (cjk_inherited_ > 0) {
1076 cjk_inherited_ -= 1;
1077 if (cjk_inherited_ == 0)
1078 open_encoding_ = CJK;
1083 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1084 OutputParams const & runparams, Encoding const & newEnc,
1087 Encoding const & oldEnc = *runparams.encoding;
1088 bool moving_arg = runparams.moving_arg;
1089 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
1091 return make_pair(false, 0);
1093 // Do nothing if the encoding is unchanged.
1094 if (oldEnc.name() == newEnc.name())
1095 return make_pair(false, 0);
1097 // FIXME We ignore encoding switches from/to encodings that do
1098 // neither support the inputenc package nor the CJK package here.
1099 // This does of course only work in special cases (e.g. switch from
1100 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1101 // but it is the best we can do
1102 if (oldEnc.package() == Encoding::none
1103 || newEnc.package() == Encoding::none)
1104 return make_pair(false, 0);
1106 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1107 << oldEnc.name() << " to " << newEnc.name());
1108 os << setEncoding(newEnc.iconvName());
1109 if (bparams.inputenc == "default")
1110 return make_pair(true, 0);
1112 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1113 switch (newEnc.package()) {
1114 case Encoding::none:
1115 case Encoding::japanese:
1116 // shouldn't ever reach here, see above
1117 return make_pair(true, 0);
1118 case Encoding::inputenc: {
1119 int count = inputenc_arg.length();
1120 if (oldEnc.package() == Encoding::CJK &&
1121 open_encoding_ == CJK) {
1123 open_encoding_ = none;
1126 else if (oldEnc.package() == Encoding::inputenc &&
1127 open_encoding_ == inputenc) {
1129 open_encoding_ = none;
1132 if (runparams.local_font != 0
1133 && oldEnc.package() == Encoding::CJK) {
1134 // within insets, \inputenc switches need
1135 // to be embraced within \bgroup...\egroup;
1139 open_encoding_ = inputenc;
1141 // with the japanese option, inputenc is omitted.
1142 if (runparams.use_japanese)
1143 return make_pair(true, count);
1144 os << "\\inputencoding{" << inputenc_arg << '}';
1145 return make_pair(true, count + 16);
1147 case Encoding::CJK: {
1148 int count = inputenc_arg.length();
1149 if (oldEnc.package() == Encoding::CJK &&
1150 open_encoding_ == CJK) {
1154 if (oldEnc.package() == Encoding::inputenc &&
1155 open_encoding_ == inputenc) {
1159 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1160 << from_ascii(bparams.fonts_cjk) << "}";
1161 open_encoding_ = CJK;
1162 return make_pair(true, count + 15);
1165 // Dead code to avoid a warning:
1166 return make_pair(true, 0);