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 "paragraph_funcs.h"
26 #include "ParagraphParameters.h"
30 #include "insets/InsetBibitem.h"
31 #include "insets/InsetOptArg.h"
33 #include "support/lstrings.h"
35 #include <boost/next_prior.hpp>
49 ParagraphList::const_iterator
50 TeXEnvironment(Buffer const & buf,
51 ParagraphList const & paragraphs,
52 ParagraphList::const_iterator pit,
53 odocstream & os, TexRow & texrow,
54 OutputParams const & runparams);
56 ParagraphList::const_iterator
57 TeXOnePar(Buffer const & buf,
58 ParagraphList const & paragraphs,
59 ParagraphList::const_iterator pit,
60 odocstream & os, TexRow & texrow,
61 OutputParams const & runparams,
62 string const & everypar = string());
65 ParagraphList::const_iterator
66 TeXDeeper(Buffer const & buf,
67 ParagraphList const & paragraphs,
68 ParagraphList::const_iterator pit,
69 odocstream & os, TexRow & texrow,
70 OutputParams const & runparams)
72 LYXERR(Debug::LATEX) << "TeXDeeper... " << &*pit << endl;
73 ParagraphList::const_iterator par = pit;
75 while (par != paragraphs.end() &&
76 par->params().depth() == pit->params().depth()) {
77 if (par->layout()->isEnvironment()) {
78 par = TeXEnvironment(buf, paragraphs, par,
79 os, texrow, runparams);
81 par = TeXOnePar(buf, paragraphs, par,
82 os, texrow, runparams);
85 LYXERR(Debug::LATEX) << "TeXDeeper...done " << endl;
91 ParagraphList::const_iterator
92 TeXEnvironment(Buffer const & buf,
93 ParagraphList const & paragraphs,
94 ParagraphList::const_iterator pit,
95 odocstream & os, TexRow & texrow,
96 OutputParams const & runparams)
98 LYXERR(Debug::LATEX) << "TeXEnvironment... " << &*pit << endl;
100 BufferParams const & bparams = buf.params();
102 LayoutPtr const & style = pit->layout();
104 Language const * const 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 ? boost::prior(pit)->getParLanguage(bparams)
110 if (par_language->babel() != prev_par_language->babel()) {
112 if (!lyxrc.language_command_end.empty() &&
113 prev_par_language->babel() != doc_language->babel() &&
114 !prev_par_language->babel().empty()) {
115 os << from_ascii(subst(
116 lyxrc.language_command_end,
118 prev_par_language->babel()))
123 if ((lyxrc.language_command_end.empty() ||
124 par_language->babel() != doc_language->babel()) &&
125 !par_language->babel().empty()) {
126 os << from_ascii(subst(
127 lyxrc.language_command_begin,
129 par_language->babel()))
135 bool leftindent_open = false;
136 if (!pit->params().leftIndent().zero()) {
137 os << "\\begin{LyXParagraphLeftIndent}{"
138 << from_ascii(pit->params().leftIndent().asLatexString())
141 leftindent_open = true;
144 if (style->isEnvironment()) {
145 os << "\\begin{" << from_ascii(style->latexname()) << '}';
146 if (style->optionalargs > 0) {
147 int ret = latexOptArgInsets(buf, *pit, os, runparams,
148 style->optionalargs);
154 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
156 << pit->params().labelWidthString()
158 } else if (style->labeltype == LABEL_BIBLIO) {
160 os << '{' << bibitemWidest(buf) << "}\n";
162 os << from_ascii(style->latexparam()) << '\n';
165 ParagraphList::const_iterator par = pit;
167 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
169 if (par == paragraphs.end()) {
170 // Make sure that the last paragraph is
171 // correctly terminated (because TeXOnePar does
172 // not add a \n in this case)
175 } else if (par->params().depth() > pit->params().depth()) {
176 if (par->layout()->isParagraph()) {
178 // How to handle this? (Lgb)
179 //&& !suffixIs(os, "\n\n")
182 // There should be at least one '\n' already
183 // but we need there to be two for Standard
184 // paragraphs that are depth-increment'ed to be
185 // output correctly. However, tables can
186 // also be paragraphs so don't adjust them.
189 // Will it ever harm to have one '\n' too
190 // many? i.e. that we sometimes will have
191 // three in a row. (Lgb)
195 par = TeXDeeper(buf, paragraphs, par, os, texrow,
198 } while (par != paragraphs.end()
199 && par->layout() == pit->layout()
200 && par->params().depth() == pit->params().depth()
201 && par->params().leftIndent() == pit->params().leftIndent());
203 if (style->isEnvironment()) {
204 os << "\\end{" << from_ascii(style->latexname()) << "}\n";
208 if (leftindent_open) {
209 os << "\\end{LyXParagraphLeftIndent}\n";
213 if (par != paragraphs.end()) {
214 LYXERR(Debug::LATEX) << "TeXEnvironment...done " << &*par << endl;
222 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
223 odocstream & os, OutputParams const & runparams, int number)
227 InsetList::const_iterator it = par.insetList().begin();
228 InsetList::const_iterator end = par.insetList().end();
229 for (; it != end && number > 0 ; ++it) {
230 if (it->inset->lyxCode() == OPTARG_CODE) {
232 static_cast<InsetOptArg *>(it->inset);
233 lines += ins->latexOptional(buf, os, runparams);
243 ParagraphList::const_iterator
244 TeXOnePar(Buffer const & buf,
245 ParagraphList const & paragraphs,
246 ParagraphList::const_iterator pit,
247 odocstream & os, TexRow & texrow,
248 OutputParams const & runparams_in,
249 string const & everypar)
251 LYXERR(Debug::LATEX) << "TeXOnePar... " << &*pit << " '"
252 << everypar << "'" << endl;
253 BufferParams const & bparams = buf.params();
256 if (runparams_in.verbatim) {
257 Font const outerfont =
258 outerFont(std::distance(paragraphs.begin(), pit),
261 bool need_par = pit->latex(buf, bparams, outerfont,
262 os, texrow, runparams_in);
268 // In an inset with unlimited length (all in one row),
269 // force layout to default
270 if (!pit->forceDefaultParagraphs())
271 style = pit->layout();
273 style = bparams.getTextClass().defaultLayout();
275 OutputParams runparams = runparams_in;
276 runparams.moving_arg |= style->needprotect;
278 // This paragraph's language
279 Language const * const par_language = pit->getParLanguage(bparams);
280 // The document's language
281 Language const * const doc_language = bparams.language;
282 // The language that was in effect when the environemnt this paragraph is
283 // inside of was opened
284 Language const * const outer_language =
285 (runparams.local_font != 0) ?
286 runparams.local_font->language() : doc_language;
287 // The previous language that was in effect is either the language of
288 // the previous paragraph, if there is one, or else the outer language
289 // if there is no previous paragraph
290 Language const * const prev_language =
291 (pit != paragraphs.begin()) ?
292 boost::prior(pit)->getParLanguage(bparams) : outer_language;
294 if (par_language->babel() != prev_language->babel()
295 // check if we already put language command in TeXEnvironment()
296 && !(style->isEnvironment()
297 && (pit == paragraphs.begin() ||
298 (boost::prior(pit)->layout() != pit->layout() &&
299 boost::prior(pit)->getDepth() <= pit->getDepth())
300 || boost::prior(pit)->getDepth() < pit->getDepth())))
302 if (!lyxrc.language_command_end.empty() &&
303 prev_language->babel() != outer_language->babel() &&
304 !prev_language->babel().empty())
306 os << from_ascii(subst(lyxrc.language_command_end,
308 prev_language->babel()))
313 // We need to open a new language if we couldn't close the previous
314 // one (because there's no language_command_end); and even if we closed
315 // the previous one, if the current language is different than the
316 // outer_language (which is currently in effect once the previous one
318 if ((lyxrc.language_command_end.empty() ||
319 par_language->babel() != outer_language->babel()) &&
320 !par_language->babel().empty()) {
321 // If we're inside an inset, and that inset is within an \L or \R
322 // (or equivalents), then within the inset, too, any opposite
323 // language paragraph should appear within an \L or \R (in addition
324 // to, outside of, the normal language switch commands).
325 // This behavior is not correct for ArabTeX, though.
326 if ( // not for ArabTeX
327 (par_language->lang() != "arabic_arabtex" &&
328 outer_language->lang() != "arabic_arabtex") &&
329 // are we in an inset?
330 runparams.local_font != 0 &&
331 // is the inset within an \L or \R?
333 // FIXME: currently, we don't check this; this means that
334 // we'll have unnnecessary \L and \R commands, but that
335 // doesn't seem to hurt (though latex will complain)
337 // is this paragraph in the opposite direction?
338 runparams.local_font->isRightToLeft() !=
339 par_language->rightToLeft()
341 // FIXME: I don't have a working copy of the Arabi package, so
342 // I'm not sure if the farsi and arabic_arabi stuff is correct
344 if (par_language->lang() == "farsi")
346 else if (outer_language->lang() == "farsi")
348 else if (par_language->lang() == "arabic_arabi")
350 else if (outer_language->lang() == "arabic_arabi")
352 // remaining RTL languages currently is hebrew
353 else if (par_language->rightToLeft())
358 os << from_ascii(subst(
359 lyxrc.language_command_begin,
361 par_language->babel()))
367 // Switch file encoding if necessary; no need to do this for "default"
368 // encoding, since this only affects the position of the outputted
369 // \inputencoding command; the encoding switch will occur when necessary
370 if (bparams.inputenc == "auto" &&
371 runparams.encoding->package() == Encoding::inputenc) {
372 // Look ahead for future encoding changes.
373 // We try to output them at the beginning of the paragraph,
374 // since the \inputencoding command is not allowed e.g. in
376 for (pos_type i = 0; i < pit->size(); ++i) {
377 char_type const c = pit->getChar(i);
382 // All characters before c are in the ASCII range, and
383 // c is non-ASCII (but no inset), so change the
384 // encoding to that required by the language of c.
385 Encoding const * const encoding =
386 pit->getFontSettings(bparams, i).language()->encoding();
387 pair<bool, int> enc_switch = switchEncoding(os, bparams, false,
388 *(runparams.encoding), *encoding);
389 if (encoding->package() == Encoding::inputenc && enc_switch.first) {
390 runparams.encoding = encoding;
391 if (enc_switch.second > 0) {
400 // In an inset with unlimited length (all in one row),
401 // don't allow any special options in the paragraph
402 if (!pit->forceDefaultParagraphs()) {
403 if (pit->params().startOfAppendix()) {
404 os << "\\appendix\n";
408 if (!pit->params().spacing().isDefault()
409 && (pit == paragraphs.begin()
410 || !boost::prior(pit)->hasSameLayout(*pit)))
412 os << from_ascii(pit->params().spacing().writeEnvirBegin())
417 if (style->isCommand()) {
423 switch (style->latextype) {
425 os << '\\' << from_ascii(style->latexname());
427 // Separate handling of optional argument inset.
428 if (style->optionalargs > 0) {
429 int ret = latexOptArgInsets(buf, *pit, os, runparams,
430 style->optionalargs);
437 os << from_ascii(style->latexparam());
439 case LATEX_ITEM_ENVIRONMENT:
440 case LATEX_LIST_ENVIRONMENT:
443 case LATEX_BIB_ENVIRONMENT:
444 // ignore this, the inset will write itself
450 Font const outerfont =
451 outerFont(std::distance(paragraphs.begin(), pit),
455 os << from_utf8(everypar);
456 bool need_par = pit->latex(buf, bparams, outerfont,
457 os, texrow, runparams);
459 // Make sure that \\par is done with the font of the last
460 // character if this has another size as the default.
461 // This is necessary because LaTeX (and LyX on the screen)
462 // calculates the space between the baselines according
463 // to this font. (Matthias)
465 // Is this really needed ? (Dekel)
466 // We do not need to use to change the font for the last paragraph
471 ? pit->getLayoutFont(bparams, outerfont)
472 : pit->getFont(bparams, pit->size() - 1, outerfont));
474 bool is_command = style->isCommand();
476 if (style->resfont.size() != font.size()
477 && boost::next(pit) != paragraphs.end()
481 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
482 } else if (need_par) {
484 } else if (is_command)
487 bool pending_newline = false;
488 switch (style->latextype) {
489 case LATEX_ITEM_ENVIRONMENT:
490 case LATEX_LIST_ENVIRONMENT:
491 if (boost::next(pit) != paragraphs.end()
492 && (pit->params().depth() < boost::next(pit)->params().depth()))
493 pending_newline = true;
495 case LATEX_ENVIRONMENT: {
496 // if its the last paragraph of the current environment
497 // skip it otherwise fall through
498 ParagraphList::const_iterator next = boost::next(pit);
500 if (next != paragraphs.end()
501 && (next->layout() != pit->layout()
502 || next->params().depth() != pit->params().depth()))
506 // fall through possible
508 // we don't need it for the last paragraph!!!
509 if (boost::next(pit) != paragraphs.end())
510 pending_newline = true;
513 if (!pit->forceDefaultParagraphs()) {
514 if (!pit->params().spacing().isDefault()
515 && (boost::next(pit) == paragraphs.end()
516 || !boost::next(pit)->hasSameLayout(*pit)))
518 if (pending_newline) {
522 os << from_ascii(pit->params().spacing().writeEnvirEnd());
523 pending_newline = true;
527 // Closing the language is needed for the last paragraph; it is also
528 // needed if we're within an \L or \R that we may have opened above (not
529 // necessarily in this paragraph) and are about to close.
530 bool closing_rtl_ltr_environment =
532 (par_language->lang() != "arabic_arabtex" &&
533 outer_language->lang() != "arabic_arabtex") &&
534 // have we opened and \L or \R environment?
535 runparams.local_font != 0 &&
536 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
537 // are we about to close the language?
538 ((boost::next(pit) != paragraphs.end() &&
539 par_language->babel() !=
540 (boost::next(pit)->getParLanguage(bparams))->babel()) ||
541 (boost::next(pit) == paragraphs.end() &&
542 par_language->babel() != outer_language->babel()));
544 if (closing_rtl_ltr_environment || (boost::next(pit) == paragraphs.end()
545 && par_language->babel() != outer_language->babel())) {
546 // Since \selectlanguage write the language to the aux file,
547 // we need to reset the language at the end of footnote or
550 if (pending_newline) {
554 if (lyxrc.language_command_end.empty()) {
555 if (!prev_language->babel().empty()) {
556 os << from_ascii(subst(
557 lyxrc.language_command_begin,
559 prev_language->babel()));
560 pending_newline = true;
562 } else if (!par_language->babel().empty()) {
563 os << from_ascii(subst(
564 lyxrc.language_command_end,
566 par_language->babel()));
567 pending_newline = true;
570 if (closing_rtl_ltr_environment)
573 if (pending_newline) {
578 // If this is the last paragraph, and a local_font was set upon entering
579 // the inset, the encoding should be set back to that local_font's
580 // encoding. We don't use switchEncoding(), because no explicit encoding
581 // switch command is needed, since latex will automatically revert to it
582 // when this inset closes.
583 // This switch is only necessary if we're using "auto" or "default"
585 if (boost::next(pit) == paragraphs.end() && runparams_in.local_font != 0) {
586 runparams_in.encoding = runparams_in.local_font->language()->encoding();
587 if (bparams.inputenc == "auto" || bparams.inputenc == "default")
588 os << setEncoding(runparams_in.encoding->iconvName());
591 // Otherwise, the current encoding should be set for the next paragraph.
593 runparams_in.encoding = runparams.encoding;
596 // we don't need it for the last paragraph!!!
597 // Note from JMarc: we will re-add a \n explicitely in
598 // TeXEnvironment, because it is needed in this case
599 if (boost::next(pit) != paragraphs.end()) {
604 if (boost::next(pit) != paragraphs.end()) {
605 LYXERR(Debug::LATEX) << "TeXOnePar...done " << &*boost::next(pit) << endl;
614 // LaTeX all paragraphs
615 void latexParagraphs(Buffer const & buf,
616 ParagraphList const & paragraphs,
619 OutputParams const & runparams,
620 string const & everypar)
622 bool was_title = false;
623 bool already_title = false;
624 TextClass const & tclass = buf.params().getTextClass();
625 ParagraphList::const_iterator par = paragraphs.begin();
626 ParagraphList::const_iterator endpar = paragraphs.end();
628 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
629 // if only part of the paragraphs will be outputed
630 if (runparams.par_begin != runparams.par_end) {
631 par = boost::next(paragraphs.begin(), runparams.par_begin);
632 endpar = boost::next(paragraphs.begin(), runparams.par_end);
633 // runparams will be passed to nested paragraphs, so
634 // we have to reset the range parameters.
635 const_cast<OutputParams&>(runparams).par_begin = 0;
636 const_cast<OutputParams&>(runparams).par_end = 0;
640 while (par != endpar) {
641 ParagraphList::const_iterator lastpar = par;
642 // well we have to check if we are in an inset with unlimited
643 // length (all in one row) if that is true then we don't allow
644 // any special options in the paragraph and also we don't allow
645 // any environment other than the default layout of the
646 // text class to be valid!
647 if (!par->forceDefaultParagraphs()) {
648 LayoutPtr const & layout = par->layout();
650 if (layout->intitle) {
652 lyxerr << "Error in latexParagraphs: You"
653 " should not mix title layouts"
654 " with normal ones." << endl;
655 } else if (!was_title) {
657 if (tclass.titletype() == TITLE_ENVIRONMENT) {
659 << from_ascii(tclass.titlename())
664 } else if (was_title && !already_title) {
665 if (tclass.titletype() == TITLE_ENVIRONMENT) {
666 os << "\\end{" << from_ascii(tclass.titlename())
670 os << "\\" << from_ascii(tclass.titlename())
674 already_title = true;
678 if (layout->is_environment) {
679 par = TeXOnePar(buf, paragraphs, par, os, texrow,
680 runparams, everypar);
681 } else if (layout->isEnvironment() ||
682 !par->params().leftIndent().zero()) {
683 par = TeXEnvironment(buf, paragraphs, par, os,
686 par = TeXOnePar(buf, paragraphs, par, os, texrow,
687 runparams, everypar);
690 par = TeXOnePar(buf, paragraphs, par, os, texrow,
691 runparams, everypar);
693 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
696 // It might be that we only have a title in this document
697 if (was_title && !already_title) {
698 if (tclass.titletype() == TITLE_ENVIRONMENT) {
699 os << "\\end{" << from_ascii(tclass.titlename())
703 os << "\\" << from_ascii(tclass.titlename())
711 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
712 bool moving_arg, Encoding const & oldEnc,
713 Encoding const & newEnc)
715 if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
717 return make_pair(false, 0);
719 // Do nothing if the encoding is unchanged.
720 if (oldEnc.name() == newEnc.name())
721 return make_pair(false, 0);
723 // FIXME We ignore encoding switches from/to encodings that do
724 // neither support the inputenc package nor the CJK package here.
725 // This does of course only work in special cases (e.g. switch from
726 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
727 // but it is the best we can do
728 if (oldEnc.package() == Encoding::none
729 || newEnc.package() == Encoding::none)
730 return make_pair(false, 0);
732 LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
733 << oldEnc.name() << " to "
734 << newEnc.name() << endl;
735 os << setEncoding(newEnc.iconvName());
736 if (bparams.inputenc == "default")
737 return make_pair(true, 0);
739 docstring const inputenc(from_ascii(newEnc.latexName()));
740 switch (newEnc.package()) {
742 // shouldn't ever reach here, see above
743 return make_pair(true, 0);
744 case Encoding::inputenc: {
745 int count = inputenc.length();
746 if (oldEnc.package() == Encoding::CJK) {
750 os << "\\inputencoding{" << inputenc << '}';
751 return make_pair(true, count + 16);
753 case Encoding::CJK: {
754 int count = inputenc.length();
755 if (oldEnc.package() == Encoding::CJK) {
759 os << "\\begin{CJK}{" << inputenc << "}{}";
760 return make_pair(true, count + 15);
763 // Dead code to avoid a warning:
764 return make_pair(true, 0);