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"
21 #include "OutputParams.h"
22 #include "Paragraph.h"
23 #include "paragraph_funcs.h"
24 #include "ParagraphParameters.h"
28 #include "insets/InsetBibitem.h"
29 #include "insets/InsetOptArg.h"
31 #include "support/lstrings.h"
44 ParagraphList::const_iterator
45 TeXEnvironment(Buffer const & buf,
46 ParagraphList const & paragraphs,
47 ParagraphList::const_iterator pit,
48 odocstream & os, TexRow & texrow,
49 OutputParams const & runparams);
51 ParagraphList::const_iterator
52 TeXOnePar(Buffer const & buf,
53 ParagraphList const & paragraphs,
54 ParagraphList::const_iterator pit,
55 odocstream & os, TexRow & texrow,
56 OutputParams const & runparams,
57 string const & everypar = string());
60 ParagraphList::const_iterator
61 TeXDeeper(Buffer const & buf,
62 ParagraphList const & paragraphs,
63 ParagraphList::const_iterator pit,
64 odocstream & os, TexRow & texrow,
65 OutputParams const & runparams)
67 LYXERR(Debug::LATEX) << "TeXDeeper... " << &*pit << endl;
68 ParagraphList::const_iterator par = pit;
70 while (par != paragraphs.end() &&
71 par->params().depth() == pit->params().depth()) {
72 if (par->layout()->isEnvironment()) {
73 par = TeXEnvironment(buf, paragraphs, par,
74 os, texrow, runparams);
76 par = TeXOnePar(buf, paragraphs, par,
77 os, texrow, runparams);
80 LYXERR(Debug::LATEX) << "TeXDeeper...done " << endl;
86 ParagraphList::const_iterator
87 TeXEnvironment(Buffer const & buf,
88 ParagraphList const & paragraphs,
89 ParagraphList::const_iterator pit,
90 odocstream & os, TexRow & texrow,
91 OutputParams const & runparams)
93 LYXERR(Debug::LATEX) << "TeXEnvironment... " << &*pit << endl;
95 BufferParams const & bparams = buf.params();
97 Layout_ptr const & style = pit->layout();
99 Language const * const par_language = pit->getParLanguage(bparams);
100 Language const * const doc_language = bparams.language;
101 Language const * const prev_par_language =
102 (pit != paragraphs.begin())
103 ? boost::prior(pit)->getParLanguage(bparams)
105 if (par_language->babel() != prev_par_language->babel()) {
107 if (!lyxrc.language_command_end.empty() &&
108 prev_par_language->babel() != doc_language->babel() &&
109 !prev_par_language->babel().empty()) {
110 os << from_ascii(subst(
111 lyxrc.language_command_end,
113 prev_par_language->babel()))
118 if (lyxrc.language_command_end.empty() ||
119 par_language->babel() != doc_language->babel() &&
120 !par_language->babel().empty()) {
121 os << from_ascii(subst(
122 lyxrc.language_command_begin,
124 par_language->babel()))
130 bool leftindent_open = false;
131 if (!pit->params().leftIndent().zero()) {
132 os << "\\begin{LyXParagraphLeftIndent}{"
133 << from_ascii(pit->params().leftIndent().asLatexString())
136 leftindent_open = true;
139 if (style->isEnvironment()) {
140 os << "\\begin{" << from_ascii(style->latexname()) << '}';
141 if (style->optionalargs > 0) {
142 int ret = latexOptArgInsets(buf, *pit, os, runparams,
143 style->optionalargs);
149 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
151 << pit->params().labelWidthString()
153 } else if (style->labeltype == LABEL_BIBLIO) {
155 os << '{' << bibitemWidest(buf) << "}\n";
157 os << from_ascii(style->latexparam()) << '\n';
160 ParagraphList::const_iterator par = pit;
162 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
164 if (par == paragraphs.end()) {
165 // Make sure that the last paragraph is
166 // correctly terminated (because TeXOnePar does
167 // not add a \n in this case)
170 } else if (par->params().depth() > pit->params().depth()) {
171 if (par->layout()->isParagraph()) {
174 // How to handle this? (Lgb)
175 //&& !suffixIs(os, "\n\n")
177 // There should be at least one '\n' already
178 // but we need there to be two for Standard
179 // paragraphs that are depth-increment'ed to be
180 // output correctly. However, tables can
181 // also be paragraphs so don't adjust them.
184 // Will it ever harm to have one '\n' too
185 // many? i.e. that we sometimes will have
186 // three in a row. (Lgb)
190 par = TeXDeeper(buf, paragraphs, par, os, texrow,
193 } while (par != paragraphs.end()
194 && par->layout() == pit->layout()
195 && par->params().depth() == pit->params().depth()
196 && par->params().leftIndent() == pit->params().leftIndent());
198 if (style->isEnvironment()) {
199 os << "\\end{" << from_ascii(style->latexname()) << "}\n";
203 if (leftindent_open) {
204 os << "\\end{LyXParagraphLeftIndent}\n";
208 if (par != paragraphs.end())
209 LYXERR(Debug::LATEX) << "TeXEnvironment...done " << &*par << endl;
216 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
217 odocstream & os, OutputParams const & runparams, int number)
221 InsetList::const_iterator it = par.insetlist.begin();
222 InsetList::const_iterator end = par.insetlist.end();
223 for (; it != end && number > 0 ; ++it) {
224 if (it->inset->lyxCode() == Inset::OPTARG_CODE) {
226 static_cast<InsetOptArg *>(it->inset);
227 lines += ins->latexOptional(buf, os, runparams);
237 ParagraphList::const_iterator
238 TeXOnePar(Buffer const & buf,
239 ParagraphList const & paragraphs,
240 ParagraphList::const_iterator pit,
241 odocstream & os, TexRow & texrow,
242 OutputParams const & runparams_in,
243 string const & everypar)
245 LYXERR(Debug::LATEX) << "TeXOnePar... " << &*pit << " '"
246 << everypar << "'" << endl;
247 BufferParams const & bparams = buf.params();
250 // In an inset with unlimited length (all in one row),
251 // force layout to default
252 if (!pit->forceDefaultParagraphs())
253 style = pit->layout();
255 style = bparams.getTextClass().defaultLayout();
257 OutputParams runparams = runparams_in;
258 runparams.moving_arg |= style->needprotect;
260 Language const * const par_language = pit->getParLanguage(bparams);
261 Language const * const doc_language = bparams.language;
262 Language const * const prev_par_language =
263 (pit != paragraphs.begin())
264 ? boost::prior(pit)->getParLanguage(bparams)
267 if (par_language->babel() != prev_par_language->babel()
268 // check if we already put language command in TeXEnvironment()
269 && !(style->isEnvironment()
270 && (pit == paragraphs.begin() ||
271 (boost::prior(pit)->layout() != pit->layout() &&
272 boost::prior(pit)->getDepth() <= pit->getDepth())
273 || boost::prior(pit)->getDepth() < pit->getDepth())))
275 if (!lyxrc.language_command_end.empty() &&
276 prev_par_language->babel() != doc_language->babel() &&
277 !prev_par_language->babel().empty())
279 os << from_ascii(subst(lyxrc.language_command_end,
281 prev_par_language->babel()))
286 if (lyxrc.language_command_end.empty() ||
287 par_language->babel() != doc_language->babel() &&
288 !par_language->babel().empty())
290 os << from_ascii(subst(
291 lyxrc.language_command_begin,
293 par_language->babel()))
299 // Switch file encoding if necessary
300 if (bparams.inputenc == "auto") {
301 // Look ahead for future encoding changes.
302 // We try to output them at the beginning of the paragraph,
303 // since the \inputencoding command is not allowed e.g. in
305 for (pos_type i = 0; i < pit->size(); ++i) {
306 char_type const c = pit->getChar(i);
311 // All characters before c are in the ASCII range, and
312 // c is non-ASCII (but no inset), so change the
313 // encoding to that required by the language of c.
314 Encoding const * const encoding =
315 pit->getFontSettings(bparams, i).language()->encoding();
316 if (switchEncoding(os, bparams, false,
317 *(runparams.encoding), *encoding) > 0) {
318 runparams.encoding = encoding;
326 // In an inset with unlimited length (all in one row),
327 // don't allow any special options in the paragraph
328 if (!pit->forceDefaultParagraphs()) {
329 if (pit->params().startOfAppendix()) {
330 os << "\\appendix\n";
334 if (!pit->params().spacing().isDefault()
335 && (pit == paragraphs.begin()
336 || !boost::prior(pit)->hasSameLayout(*pit)))
338 os << from_ascii(pit->params().spacing().writeEnvirBegin())
343 if (style->isCommand()) {
349 switch (style->latextype) {
351 os << '\\' << from_ascii(style->latexname());
353 // Separate handling of optional argument inset.
354 if (style->optionalargs > 0) {
355 int ret = latexOptArgInsets(buf, *pit, os, runparams,
356 style->optionalargs);
363 os << from_ascii(style->latexparam());
365 case LATEX_ITEM_ENVIRONMENT:
366 case LATEX_LIST_ENVIRONMENT:
369 case LATEX_BIB_ENVIRONMENT:
370 // ignore this, the inset will write itself
376 Font const outerfont =
377 outerFont(std::distance(paragraphs.begin(), pit),
381 os << from_utf8(everypar);
382 bool need_par = pit->simpleTeXOnePar(buf, bparams, outerfont,
383 os, texrow, runparams);
385 // Make sure that \\par is done with the font of the last
386 // character if this has another size as the default.
387 // This is necessary because LaTeX (and LyX on the screen)
388 // calculates the space between the baselines according
389 // to this font. (Matthias)
391 // Is this really needed ? (Dekel)
392 // We do not need to use to change the font for the last paragraph
397 ? pit->getLayoutFont(bparams, outerfont)
398 : pit->getFont(bparams, pit->size() - 1, outerfont));
400 bool is_command = style->isCommand();
402 if (style->resfont.size() != font.size()
403 && boost::next(pit) != paragraphs.end()
407 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
408 } else if (need_par) {
410 } else if (is_command)
413 bool pending_newline = false;
414 switch (style->latextype) {
415 case LATEX_ITEM_ENVIRONMENT:
416 case LATEX_LIST_ENVIRONMENT:
417 if (boost::next(pit) != paragraphs.end()
418 && (pit->params().depth() < boost::next(pit)->params().depth()))
419 pending_newline = true;
421 case LATEX_ENVIRONMENT: {
422 // if its the last paragraph of the current environment
423 // skip it otherwise fall through
424 ParagraphList::const_iterator next = boost::next(pit);
426 if (next != paragraphs.end()
427 && (next->layout() != pit->layout()
428 || next->params().depth() != pit->params().depth()))
432 // fall through possible
434 // we don't need it for the last paragraph!!!
435 if (boost::next(pit) != paragraphs.end())
436 pending_newline = true;
439 if (!pit->forceDefaultParagraphs()) {
440 if (!pit->params().spacing().isDefault()
441 && (boost::next(pit) == paragraphs.end()
442 || !boost::next(pit)->hasSameLayout(*pit)))
444 if (pending_newline) {
448 os << from_ascii(pit->params().spacing().writeEnvirEnd());
449 pending_newline = true;
453 if (boost::next(pit) == paragraphs.end()
454 && par_language->babel() != doc_language->babel()) {
455 // Since \selectlanguage write the language to the aux file,
456 // we need to reset the language at the end of footnote or
459 if (pending_newline) {
463 if (lyxrc.language_command_end.empty()) {
464 if (!doc_language->babel().empty()) {
465 os << from_ascii(subst(
466 lyxrc.language_command_begin,
468 doc_language->babel()));
469 pending_newline = true;
471 } else if (!par_language->babel().empty())
472 os << from_ascii(subst(
473 lyxrc.language_command_end,
475 par_language->babel()));
476 pending_newline = true;
479 if (pending_newline) {
483 runparams_in.encoding = runparams.encoding;
485 // we don't need it for the last paragraph!!!
486 // Note from JMarc: we will re-add a \n explicitely in
487 // TeXEnvironment, because it is needed in this case
488 if (boost::next(pit) != paragraphs.end()) {
493 if (boost::next(pit) != paragraphs.end())
494 LYXERR(Debug::LATEX) << "TeXOnePar...done " << &*boost::next(pit) << endl;
502 // LaTeX all paragraphs
503 void latexParagraphs(Buffer const & buf,
504 ParagraphList const & paragraphs,
507 OutputParams const & runparams,
508 string const & everypar)
510 bool was_title = false;
511 bool already_title = false;
512 TextClass const & tclass = buf.params().getTextClass();
513 ParagraphList::const_iterator par = paragraphs.begin();
514 ParagraphList::const_iterator endpar = paragraphs.end();
516 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
517 // if only part of the paragraphs will be outputed
518 if (runparams.par_begin != runparams.par_end) {
519 par = boost::next(paragraphs.begin(), runparams.par_begin);
520 endpar = boost::next(paragraphs.begin(), runparams.par_end);
521 // runparams will be passed to nested paragraphs, so
522 // we have to reset the range parameters.
523 const_cast<OutputParams&>(runparams).par_begin = 0;
524 const_cast<OutputParams&>(runparams).par_end = 0;
528 while (par != endpar) {
529 ParagraphList::const_iterator lastpar = par;
530 // well we have to check if we are in an inset with unlimited
531 // length (all in one row) if that is true then we don't allow
532 // any special options in the paragraph and also we don't allow
533 // any environment other than the default layout of the
534 // text class to be valid!
535 if (!par->forceDefaultParagraphs()) {
536 Layout_ptr const & layout = par->layout();
538 if (layout->intitle) {
540 lyxerr << "Error in latexParagraphs: You"
541 " should not mix title layouts"
542 " with normal ones." << endl;
543 } else if (!was_title) {
545 if (tclass.titletype() == TITLE_ENVIRONMENT) {
547 << from_ascii(tclass.titlename())
552 } else if (was_title && !already_title) {
553 if (tclass.titletype() == TITLE_ENVIRONMENT) {
554 os << "\\end{" << from_ascii(tclass.titlename())
558 os << "\\" << from_ascii(tclass.titlename())
562 already_title = true;
566 if (layout->is_environment) {
567 par = TeXOnePar(buf, paragraphs, par, os, texrow,
568 runparams, everypar);
569 } else if (layout->isEnvironment() ||
570 !par->params().leftIndent().zero()) {
571 par = TeXEnvironment(buf, paragraphs, par, os,
574 par = TeXOnePar(buf, paragraphs, par, os, texrow,
575 runparams, everypar);
578 par = TeXOnePar(buf, paragraphs, par, os, texrow,
579 runparams, everypar);
581 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
584 // It might be that we only have a title in this document
585 if (was_title && !already_title) {
586 if (tclass.titletype() == TITLE_ENVIRONMENT) {
587 os << "\\end{" << from_ascii(tclass.titlename())
591 os << "\\" << from_ascii(tclass.titlename())
599 int switchEncoding(odocstream & os, BufferParams const & bparams,
600 bool moving_arg, Encoding const & oldEnc,
601 Encoding const & newEnc)
603 // FIXME thailatex does not support the inputenc package, so we
604 // ignore switches from/to tis620-0 encoding here. This does of
605 // course only work as long as the non-thai text contains ASCII
606 // only, but it is the best we can do.
607 // Since the \inputencoding command does not work inside sections
608 // we ignore the encoding switch also in moving arguments.
609 if (((bparams.inputenc == "auto" && !moving_arg) ||
610 bparams.inputenc == "default") &&
611 oldEnc.name() != newEnc.name() &&
612 oldEnc.name() != "ascii" && newEnc.name() != "ascii" &&
613 oldEnc.name() != "tis620-0" && newEnc.name() != "tis620-0") {
614 LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
615 << oldEnc.name() << " to "
616 << newEnc.name() << endl;
617 os << setEncoding(newEnc.iconvName());
618 if (bparams.inputenc != "default") {
619 docstring const inputenc(from_ascii(newEnc.latexName()));
620 os << "\\inputencoding{" << inputenc << '}';
621 return 16 + inputenc.length();