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 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
87 odocstream & os, OutputParams const & runparams, int number);
90 ParagraphList::const_iterator
91 TeXEnvironment(Buffer const & buf,
92 ParagraphList const & paragraphs,
93 ParagraphList::const_iterator pit,
94 odocstream & os, TexRow & texrow,
95 OutputParams const & runparams)
97 lyxerr[Debug::LATEX] << "TeXEnvironment... " << &*pit << endl;
99 BufferParams const & bparams = buf.params();
101 LyXLayout_ptr const & style = pit->layout();
103 Language const * language = pit->getParLanguage(bparams);
104 Language const * doc_language = bparams.language;
105 Language const * previous_language =
106 (pit != paragraphs.begin())
107 ? boost::prior(pit)->getParLanguage(bparams)
109 if (language->babel() != previous_language->babel()) {
111 if (!lyxrc.language_command_end.empty() &&
112 previous_language->babel() != doc_language->babel()) {
113 os << from_ascii(subst(
114 lyxrc.language_command_end,
116 previous_language->babel()))
121 if (lyxrc.language_command_end.empty() ||
122 language->babel() != doc_language->babel()) {
123 os << from_ascii(subst(
124 lyxrc.language_command_begin,
132 bool leftindent_open = false;
133 if (!pit->params().leftIndent().zero()) {
134 os << "\\begin{LyXParagraphLeftIndent}{"
135 << from_ascii(pit->params().leftIndent().asLatexString())
138 leftindent_open = true;
141 if (style->isEnvironment()) {
142 os << "\\begin{" << from_ascii(style->latexname()) << '}';
143 if (style->optionalargs > 0) {
144 int ret = latexOptArgInsets(buf, *pit, os, runparams,
145 style->optionalargs);
151 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
153 << pit->params().labelWidthString()
155 } else if (style->labeltype == LABEL_BIBLIO) {
157 os << '{' << bibitemWidest(buf) << "}\n";
159 os << from_ascii(style->latexparam()) << '\n';
162 ParagraphList::const_iterator par = pit;
164 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
166 if (par == paragraphs.end()) {
167 // Make sure that the last paragraph is
168 // correctly terminated (because TeXOnePar does
169 // not add a \n in this case)
172 } else if (par->params().depth() > pit->params().depth()) {
173 if (par->layout()->isParagraph()) {
176 // How to handle this? (Lgb)
177 //&& !suffixIs(os, "\n\n")
179 // There should be at least one '\n' already
180 // but we need there to be two for Standard
181 // paragraphs that are depth-increment'ed to be
182 // output correctly. However, tables can
183 // also be paragraphs so don't adjust them.
186 // Will it ever harm to have one '\n' too
187 // many? i.e. that we sometimes will have
188 // three in a row. (Lgb)
192 par = TeXDeeper(buf, paragraphs, par, os, texrow,
195 } while (par != paragraphs.end()
196 && par->layout() == pit->layout()
197 && par->params().depth() == pit->params().depth()
198 && par->params().leftIndent() == pit->params().leftIndent());
200 if (style->isEnvironment()) {
201 os << "\\end{" << from_ascii(style->latexname()) << "}\n";
205 if (leftindent_open) {
206 os << "\\end{LyXParagraphLeftIndent}\n";
210 if (par != paragraphs.end() && lyxerr.debugging(Debug::LATEX))
211 lyxerr << "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() == InsetBase::OPTARG_CODE) {
226 static_cast<InsetOptArg *>(it->inset);
227 lines += ins->latexOptional(buf, os, runparams);
235 ParagraphList::const_iterator
236 TeXOnePar(Buffer const & buf,
237 ParagraphList const & paragraphs,
238 ParagraphList::const_iterator pit,
239 odocstream & os, TexRow & texrow,
240 OutputParams const & runparams_in,
241 string const & everypar)
243 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
244 << everypar << "'" << endl;
245 BufferParams const & bparams = buf.params();
248 // In an inset with unlimited length (all in one row),
249 // force layout to default
250 if (!pit->forceDefaultParagraphs())
251 style = pit->layout();
253 style = bparams.getLyXTextClass().defaultLayout();
255 OutputParams runparams = runparams_in;
256 runparams.moving_arg |= style->needprotect;
258 Language const * language = pit->getParLanguage(bparams);
259 Language const * doc_language = bparams.language;
260 Language const * previous_language =
261 (pit != paragraphs.begin())
262 ? boost::prior(pit)->getParLanguage(bparams)
265 if (language->babel() != previous_language->babel()
266 // check if we already put language command in TeXEnvironment()
267 && !(style->isEnvironment()
268 && (pit == paragraphs.begin() ||
269 (boost::prior(pit)->layout() != pit->layout() &&
270 boost::prior(pit)->getDepth() <= pit->getDepth())
271 || boost::prior(pit)->getDepth() < pit->getDepth())))
273 if (!lyxrc.language_command_end.empty() &&
274 previous_language->babel() != doc_language->babel())
276 os << from_ascii(subst(lyxrc.language_command_end,
278 previous_language->babel()))
283 if (lyxrc.language_command_end.empty() ||
284 language->babel() != doc_language->babel())
286 os << from_ascii(subst(
287 lyxrc.language_command_begin,
295 LyXFont const outerfont =
296 outerFont(std::distance(paragraphs.begin(), pit),
298 // This must be identical to basefont in Paragraph::simpleTeXOnePar
299 LyXFont basefont = (pit->beginOfBody() > 0) ?
300 pit->getLabelFont(bparams, outerfont) :
301 pit->getLayoutFont(bparams, outerfont);
302 Encoding const & outer_encoding(*(outerfont.language()->encoding()));
303 // FIXME we switch from the outer encoding to the encoding of
304 // this paragraph, since I could not figure out the correct
305 // logic to take the encoding of the previous paragraph into
306 // account. This may result in some unneeded encoding changes.
307 if (switchEncoding(os, bparams, outer_encoding,
308 *(basefont.language()->encoding()))) {
313 // In an inset with unlimited length (all in one row),
314 // don't allow any special options in the paragraph
315 if (!pit->forceDefaultParagraphs()) {
316 if (pit->params().startOfAppendix()) {
317 os << "\\appendix\n";
321 if (!pit->params().spacing().isDefault()
322 && (pit == paragraphs.begin()
323 || !boost::prior(pit)->hasSameLayout(*pit)))
325 os << from_ascii(pit->params().spacing().writeEnvirBegin())
330 if (style->isCommand()) {
336 switch (style->latextype) {
338 os << '\\' << from_ascii(style->latexname());
340 // Separate handling of optional argument inset.
341 if (style->optionalargs > 0) {
342 int ret = latexOptArgInsets(buf, *pit, os, runparams,
343 style->optionalargs);
350 os << from_ascii(style->latexparam());
352 case LATEX_ITEM_ENVIRONMENT:
353 case LATEX_LIST_ENVIRONMENT:
356 case LATEX_BIB_ENVIRONMENT:
357 // ignore this, the inset will write itself
364 os << from_utf8(everypar);
365 bool need_par = pit->simpleTeXOnePar(buf, bparams, outerfont,
366 os, texrow, runparams);
368 // Make sure that \\par is done with the font of the last
369 // character if this has another size as the default.
370 // This is necessary because LaTeX (and LyX on the screen)
371 // calculates the space between the baselines according
372 // to this font. (Matthias)
374 // Is this really needed ? (Dekel)
375 // We do not need to use to change the font for the last paragraph
380 ? pit->getLayoutFont(bparams, outerfont)
381 : pit->getFont(bparams, pit->size() - 1, outerfont));
383 bool is_command = style->isCommand();
385 if (style->resfont.size() != font.size()
386 && boost::next(pit) != paragraphs.end()
390 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
391 } else if (need_par) {
393 } else if (is_command)
396 bool pending_newline = false;
397 switch (style->latextype) {
398 case LATEX_ITEM_ENVIRONMENT:
399 case LATEX_LIST_ENVIRONMENT:
400 if (boost::next(pit) != paragraphs.end()
401 && (pit->params().depth() < boost::next(pit)->params().depth()))
402 pending_newline = true;
404 case LATEX_ENVIRONMENT: {
405 // if its the last paragraph of the current environment
406 // skip it otherwise fall through
407 ParagraphList::const_iterator next = boost::next(pit);
409 if (next != paragraphs.end()
410 && (next->layout() != pit->layout()
411 || next->params().depth() != pit->params().depth()))
415 // fall through possible
417 // we don't need it for the last paragraph!!!
418 if (boost::next(pit) != paragraphs.end())
419 pending_newline = true;
422 if (!pit->forceDefaultParagraphs()) {
423 if (!pit->params().spacing().isDefault()
424 && (boost::next(pit) == paragraphs.end()
425 || !boost::next(pit)->hasSameLayout(*pit)))
427 if (pending_newline) {
431 os << from_ascii(pit->params().spacing().writeEnvirEnd());
432 pending_newline = true;
436 if (boost::next(pit) == paragraphs.end()
437 && language->babel() != doc_language->babel()) {
438 // Since \selectlanguage write the language to the aux file,
439 // we need to reset the language at the end of footnote or
442 if (pending_newline) {
446 if (lyxrc.language_command_end.empty())
447 os << from_ascii(subst(
448 lyxrc.language_command_begin,
450 doc_language->babel()));
452 os << from_ascii(subst(
453 lyxrc.language_command_end,
456 pending_newline = true;
459 // FIXME we switch from the encoding of this paragraph to the
460 // outer encoding, since I could not figure out the correct logic
461 // to take the encoding of the next paragraph into account.
462 // This may result in some unneeded encoding changes.
463 basefont = pit->getLayoutFont(bparams, outerfont);
464 switchEncoding(os, bparams, *(basefont.language()->encoding()),
466 if (pending_newline) {
471 // we don't need it for the last paragraph!!!
472 // Note from JMarc: we will re-add a \n explicitely in
473 // TeXEnvironment, because it is needed in this case
474 if (boost::next(pit) != paragraphs.end()) {
479 if (boost::next(pit) != paragraphs.end() &&
480 lyxerr.debugging(Debug::LATEX))
481 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
489 // LaTeX all paragraphs
490 void latexParagraphs(Buffer const & buf,
491 ParagraphList const & paragraphs,
494 OutputParams const & runparams,
495 string const & everypar)
497 bool was_title = false;
498 bool already_title = false;
499 LyXTextClass const & tclass = buf.params().getLyXTextClass();
500 ParagraphList::const_iterator par = paragraphs.begin();
501 ParagraphList::const_iterator endpar = paragraphs.end();
503 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
504 // if only part of the paragraphs will be outputed
505 if (runparams.par_begin != runparams.par_end) {
506 par = boost::next(paragraphs.begin(), runparams.par_begin);
507 endpar = boost::next(paragraphs.begin(), runparams.par_end);
508 // runparams will be passed to nested paragraphs, so
509 // we have to reset the range parameters.
510 const_cast<OutputParams&>(runparams).par_begin = 0;
511 const_cast<OutputParams&>(runparams).par_end = 0;
515 while (par != endpar) {
516 ParagraphList::const_iterator lastpar = par;
517 // well we have to check if we are in an inset with unlimited
518 // length (all in one row) if that is true then we don't allow
519 // any special options in the paragraph and also we don't allow
520 // any environment other than the default layout of the
521 // text class to be valid!
522 if (!par->forceDefaultParagraphs()) {
523 LyXLayout_ptr const & layout = par->layout();
525 if (layout->intitle) {
527 lyxerr << "Error in latexParagraphs: You"
528 " should not mix title layouts"
529 " with normal ones." << endl;
530 } else if (!was_title) {
532 if (tclass.titletype() == TITLE_ENVIRONMENT) {
534 << from_ascii(tclass.titlename())
539 } else if (was_title && !already_title) {
540 if (tclass.titletype() == TITLE_ENVIRONMENT) {
541 os << "\\end{" << from_ascii(tclass.titlename())
545 os << "\\" << from_ascii(tclass.titlename())
549 already_title = true;
553 if (layout->is_environment) {
554 par = TeXOnePar(buf, paragraphs, par, os, texrow,
555 runparams, everypar);
556 } else if (layout->isEnvironment() ||
557 !par->params().leftIndent().zero()) {
558 par = TeXEnvironment(buf, paragraphs, par, os,
561 par = TeXOnePar(buf, paragraphs, par, os, texrow,
562 runparams, everypar);
565 par = TeXOnePar(buf, paragraphs, par, os, texrow,
566 runparams, everypar);
568 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
571 // It might be that we only have a title in this document
572 if (was_title && !already_title) {
573 if (tclass.titletype() == TITLE_ENVIRONMENT) {
574 os << "\\end{" << from_ascii(tclass.titlename())
578 os << "\\" << from_ascii(tclass.titlename())
586 int switchEncoding(odocstream & os, BufferParams const & bparams,
587 Encoding const & oldEnc, Encoding const & newEnc)
589 // FIXME thailatex does not support the inputenc package, so we
590 // ignore switches from/to tis620-0 encoding here. This does of
591 // course only work as long as the non-thai text contains ASCII
592 // only, but it is the best we can do.
593 if ((bparams.inputenc == "auto" || bparams.inputenc == "default") &&
594 oldEnc.name() != newEnc.name() &&
595 oldEnc.name() != "tis620-0" && newEnc.name() != "tis620-0") {
596 lyxerr[Debug::LATEX] << "Changing LaTeX encoding from "
597 << oldEnc.name() << " to "
598 << newEnc.name() << endl;
599 os << setEncoding(newEnc.iconvName());
600 if (bparams.inputenc != "default") {
601 docstring const inputenc(from_ascii(newEnc.latexName()));
602 os << "\\inputencoding{" << inputenc << '}';
603 return 16 + inputenc.length();