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"
46 ParagraphList::const_iterator
47 TeXEnvironment(Buffer const & buf,
48 ParagraphList const & paragraphs,
49 ParagraphList::const_iterator pit,
50 odocstream & os, TexRow & texrow,
51 OutputParams const & runparams);
53 ParagraphList::const_iterator
54 TeXOnePar(Buffer const & buf,
55 ParagraphList const & paragraphs,
56 ParagraphList::const_iterator pit,
57 odocstream & os, TexRow & texrow,
58 OutputParams const & runparams,
59 string const & everypar = string());
62 ParagraphList::const_iterator
63 TeXDeeper(Buffer const & buf,
64 ParagraphList const & paragraphs,
65 ParagraphList::const_iterator pit,
66 odocstream & os, TexRow & texrow,
67 OutputParams const & runparams)
69 LYXERR(Debug::LATEX) << "TeXDeeper... " << &*pit << endl;
70 ParagraphList::const_iterator par = pit;
72 while (par != paragraphs.end() &&
73 par->params().depth() == pit->params().depth()) {
74 if (par->layout()->isEnvironment()) {
75 par = TeXEnvironment(buf, paragraphs, par,
76 os, texrow, runparams);
78 par = TeXOnePar(buf, paragraphs, par,
79 os, texrow, runparams);
82 LYXERR(Debug::LATEX) << "TeXDeeper...done " << endl;
88 ParagraphList::const_iterator
89 TeXEnvironment(Buffer const & buf,
90 ParagraphList const & paragraphs,
91 ParagraphList::const_iterator pit,
92 odocstream & os, TexRow & texrow,
93 OutputParams const & runparams)
95 LYXERR(Debug::LATEX) << "TeXEnvironment... " << &*pit << endl;
97 BufferParams const & bparams = buf.params();
99 Layout_ptr const & style = pit->layout();
101 Language const * const par_language = pit->getParLanguage(bparams);
102 Language const * const doc_language = bparams.language;
103 Language const * const prev_par_language =
104 (pit != paragraphs.begin())
105 ? boost::prior(pit)->getParLanguage(bparams)
107 if (par_language->babel() != prev_par_language->babel()) {
109 if (!lyxrc.language_command_end.empty() &&
110 prev_par_language->babel() != doc_language->babel() &&
111 !prev_par_language->babel().empty()) {
112 os << from_ascii(subst(
113 lyxrc.language_command_end,
115 prev_par_language->babel()))
120 if ((lyxrc.language_command_end.empty() ||
121 par_language->babel() != doc_language->babel()) &&
122 !par_language->babel().empty()) {
123 os << from_ascii(subst(
124 lyxrc.language_command_begin,
126 par_language->babel()))
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())
211 LYXERR(Debug::LATEX) << "TeXEnvironment...done " << &*par << endl;
218 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
219 odocstream & os, OutputParams const & runparams, int number)
223 InsetList::const_iterator it = par.insetlist.begin();
224 InsetList::const_iterator end = par.insetlist.end();
225 for (; it != end && number > 0 ; ++it) {
226 if (it->inset->lyxCode() == Inset::OPTARG_CODE) {
228 static_cast<InsetOptArg *>(it->inset);
229 lines += ins->latexOptional(buf, os, runparams);
239 ParagraphList::const_iterator
240 TeXOnePar(Buffer const & buf,
241 ParagraphList const & paragraphs,
242 ParagraphList::const_iterator pit,
243 odocstream & os, TexRow & texrow,
244 OutputParams const & runparams_in,
245 string const & everypar)
247 LYXERR(Debug::LATEX) << "TeXOnePar... " << &*pit << " '"
248 << everypar << "'" << endl;
249 BufferParams const & bparams = buf.params();
252 // In an inset with unlimited length (all in one row),
253 // force layout to default
254 if (!pit->forceDefaultParagraphs())
255 style = pit->layout();
257 style = bparams.getTextClass().defaultLayout();
259 OutputParams runparams = runparams_in;
260 runparams.moving_arg |= style->needprotect;
262 Language const * const par_language = pit->getParLanguage(bparams);
263 Language const * const doc_language = bparams.language;
264 Language const * const prev_par_language =
265 (pit != paragraphs.begin())
266 ? boost::prior(pit)->getParLanguage(bparams)
269 if (par_language->babel() != prev_par_language->babel()
270 // check if we already put language command in TeXEnvironment()
271 && !(style->isEnvironment()
272 && (pit == paragraphs.begin() ||
273 (boost::prior(pit)->layout() != pit->layout() &&
274 boost::prior(pit)->getDepth() <= pit->getDepth())
275 || boost::prior(pit)->getDepth() < pit->getDepth())))
277 if (!lyxrc.language_command_end.empty() &&
278 prev_par_language->babel() != doc_language->babel() &&
279 !prev_par_language->babel().empty())
281 os << from_ascii(subst(lyxrc.language_command_end,
283 prev_par_language->babel()))
288 if ((lyxrc.language_command_end.empty() ||
289 par_language->babel() != doc_language->babel()) &&
290 !par_language->babel().empty()) {
291 os << from_ascii(subst(
292 lyxrc.language_command_begin,
294 par_language->babel()))
300 // Switch file encoding if necessary; no need to do this for "default"
301 // encoding, since this only affects the position of the outputted
302 // \inputencoding command; the encoding switch will occur when necessary
303 if (bparams.inputenc == "auto" &&
304 runparams.encoding->package() == Encoding::inputenc) {
305 // Look ahead for future encoding changes.
306 // We try to output them at the beginning of the paragraph,
307 // since the \inputencoding command is not allowed e.g. in
309 for (pos_type i = 0; i < pit->size(); ++i) {
310 char_type const c = pit->getChar(i);
315 // All characters before c are in the ASCII range, and
316 // c is non-ASCII (but no inset), so change the
317 // encoding to that required by the language of c.
318 Encoding const * const encoding =
319 pit->getFontSettings(bparams, i).language()->encoding();
320 pair<bool, int> enc_switch = switchEncoding(os, bparams, false,
321 *(runparams.encoding), *encoding);
322 if (encoding->package() == Encoding::inputenc && enc_switch.first) {
323 runparams.encoding = encoding;
324 if (enc_switch.second > 0) {
333 // In an inset with unlimited length (all in one row),
334 // don't allow any special options in the paragraph
335 if (!pit->forceDefaultParagraphs()) {
336 if (pit->params().startOfAppendix()) {
337 os << "\\appendix\n";
341 if (!pit->params().spacing().isDefault()
342 && (pit == paragraphs.begin()
343 || !boost::prior(pit)->hasSameLayout(*pit)))
345 os << from_ascii(pit->params().spacing().writeEnvirBegin())
350 if (style->isCommand()) {
356 switch (style->latextype) {
358 os << '\\' << from_ascii(style->latexname());
360 // Separate handling of optional argument inset.
361 if (style->optionalargs > 0) {
362 int ret = latexOptArgInsets(buf, *pit, os, runparams,
363 style->optionalargs);
370 os << from_ascii(style->latexparam());
372 case LATEX_ITEM_ENVIRONMENT:
373 case LATEX_LIST_ENVIRONMENT:
376 case LATEX_BIB_ENVIRONMENT:
377 // ignore this, the inset will write itself
383 Font const outerfont =
384 outerFont(std::distance(paragraphs.begin(), pit),
388 os << from_utf8(everypar);
389 bool need_par = pit->simpleTeXOnePar(buf, bparams, outerfont,
390 os, texrow, runparams);
392 // Make sure that \\par is done with the font of the last
393 // character if this has another size as the default.
394 // This is necessary because LaTeX (and LyX on the screen)
395 // calculates the space between the baselines according
396 // to this font. (Matthias)
398 // Is this really needed ? (Dekel)
399 // We do not need to use to change the font for the last paragraph
404 ? pit->getLayoutFont(bparams, outerfont)
405 : pit->getFont(bparams, pit->size() - 1, outerfont));
407 bool is_command = style->isCommand();
409 if (style->resfont.size() != font.size()
410 && boost::next(pit) != paragraphs.end()
414 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
415 } else if (need_par) {
417 } else if (is_command)
420 bool pending_newline = false;
421 switch (style->latextype) {
422 case LATEX_ITEM_ENVIRONMENT:
423 case LATEX_LIST_ENVIRONMENT:
424 if (boost::next(pit) != paragraphs.end()
425 && (pit->params().depth() < boost::next(pit)->params().depth()))
426 pending_newline = true;
428 case LATEX_ENVIRONMENT: {
429 // if its the last paragraph of the current environment
430 // skip it otherwise fall through
431 ParagraphList::const_iterator next = boost::next(pit);
433 if (next != paragraphs.end()
434 && (next->layout() != pit->layout()
435 || next->params().depth() != pit->params().depth()))
439 // fall through possible
441 // we don't need it for the last paragraph!!!
442 if (boost::next(pit) != paragraphs.end())
443 pending_newline = true;
446 if (!pit->forceDefaultParagraphs()) {
447 if (!pit->params().spacing().isDefault()
448 && (boost::next(pit) == paragraphs.end()
449 || !boost::next(pit)->hasSameLayout(*pit)))
451 if (pending_newline) {
455 os << from_ascii(pit->params().spacing().writeEnvirEnd());
456 pending_newline = true;
460 if (boost::next(pit) == paragraphs.end()
461 && par_language->babel() != doc_language->babel()) {
462 // Since \selectlanguage write the language to the aux file,
463 // we need to reset the language at the end of footnote or
466 if (pending_newline) {
470 if (lyxrc.language_command_end.empty()) {
471 if (!doc_language->babel().empty()) {
472 os << from_ascii(subst(
473 lyxrc.language_command_begin,
475 doc_language->babel()));
476 pending_newline = true;
478 } else if (!par_language->babel().empty()) {
479 os << from_ascii(subst(
480 lyxrc.language_command_end,
482 par_language->babel()));
483 pending_newline = true;
487 if (pending_newline) {
491 runparams_in.encoding = runparams.encoding;
493 // we don't need it for the last paragraph!!!
494 // Note from JMarc: we will re-add a \n explicitely in
495 // TeXEnvironment, because it is needed in this case
496 if (boost::next(pit) != paragraphs.end()) {
501 if (boost::next(pit) != paragraphs.end())
502 LYXERR(Debug::LATEX) << "TeXOnePar...done " << &*boost::next(pit) << endl;
510 // LaTeX all paragraphs
511 void latexParagraphs(Buffer const & buf,
512 ParagraphList const & paragraphs,
515 OutputParams const & runparams,
516 string const & everypar)
518 bool was_title = false;
519 bool already_title = false;
520 TextClass const & tclass = buf.params().getTextClass();
521 ParagraphList::const_iterator par = paragraphs.begin();
522 ParagraphList::const_iterator endpar = paragraphs.end();
524 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
525 // if only part of the paragraphs will be outputed
526 if (runparams.par_begin != runparams.par_end) {
527 par = boost::next(paragraphs.begin(), runparams.par_begin);
528 endpar = boost::next(paragraphs.begin(), runparams.par_end);
529 // runparams will be passed to nested paragraphs, so
530 // we have to reset the range parameters.
531 const_cast<OutputParams&>(runparams).par_begin = 0;
532 const_cast<OutputParams&>(runparams).par_end = 0;
536 while (par != endpar) {
537 ParagraphList::const_iterator lastpar = par;
538 // well we have to check if we are in an inset with unlimited
539 // length (all in one row) if that is true then we don't allow
540 // any special options in the paragraph and also we don't allow
541 // any environment other than the default layout of the
542 // text class to be valid!
543 if (!par->forceDefaultParagraphs()) {
544 Layout_ptr const & layout = par->layout();
546 if (layout->intitle) {
548 lyxerr << "Error in latexParagraphs: You"
549 " should not mix title layouts"
550 " with normal ones." << endl;
551 } else if (!was_title) {
553 if (tclass.titletype() == TITLE_ENVIRONMENT) {
555 << from_ascii(tclass.titlename())
560 } else if (was_title && !already_title) {
561 if (tclass.titletype() == TITLE_ENVIRONMENT) {
562 os << "\\end{" << from_ascii(tclass.titlename())
566 os << "\\" << from_ascii(tclass.titlename())
570 already_title = true;
574 if (layout->is_environment) {
575 par = TeXOnePar(buf, paragraphs, par, os, texrow,
576 runparams, everypar);
577 } else if (layout->isEnvironment() ||
578 !par->params().leftIndent().zero()) {
579 par = TeXEnvironment(buf, paragraphs, par, os,
582 par = TeXOnePar(buf, paragraphs, par, os, texrow,
583 runparams, everypar);
586 par = TeXOnePar(buf, paragraphs, par, os, texrow,
587 runparams, everypar);
589 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
592 // It might be that we only have a title in this document
593 if (was_title && !already_title) {
594 if (tclass.titletype() == TITLE_ENVIRONMENT) {
595 os << "\\end{" << from_ascii(tclass.titlename())
599 os << "\\" << from_ascii(tclass.titlename())
607 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
608 bool moving_arg, Encoding const & oldEnc,
609 Encoding const & newEnc)
611 if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
613 return make_pair(false, 0);
615 // Do nothing if the encoding is unchanged.
616 if (oldEnc.name() == newEnc.name())
617 return make_pair(false, 0);
619 // FIXME We ignore encoding switches from/to encodings that do
620 // neither support the inputenc package nor the CJK package here.
621 // This does of course only work in special cases (e.g. switch from
622 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
623 // but it is the best we can do
624 if (oldEnc.package() == Encoding::none
625 || newEnc.package() == Encoding::none)
626 return make_pair(false, 0);
628 LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
629 << oldEnc.name() << " to "
630 << newEnc.name() << endl;
631 os << setEncoding(newEnc.iconvName());
632 if (bparams.inputenc == "default")
633 return make_pair(true, 0);
635 docstring const inputenc(from_ascii(newEnc.latexName()));
636 switch (newEnc.package()) {
638 // shouldn't ever reach here, see above
639 return make_pair(true, 0);
640 case Encoding::inputenc: {
641 int count = inputenc.length();
642 if (oldEnc.package() == Encoding::CJK) {
646 os << "\\inputencoding{" << inputenc << '}';
647 return make_pair(true, count + 16);
649 case Encoding::CJK: {
650 int count = inputenc.length();
651 if (oldEnc.package() == Encoding::CJK) {
655 os << "\\begin{CJK}{" << inputenc << "}{}";
656 return make_pair(true, count + 15);
659 // Dead code to avoid a warning:
660 return make_pair(true, 0);