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"
32 #include "support/unicode.h"
45 ParagraphList::const_iterator
46 TeXEnvironment(Buffer const & buf,
47 ParagraphList const & paragraphs,
48 ParagraphList::const_iterator pit,
49 odocstream & os, TexRow & texrow,
50 OutputParams const & runparams);
52 ParagraphList::const_iterator
53 TeXOnePar(Buffer const & buf,
54 ParagraphList const & paragraphs,
55 ParagraphList::const_iterator pit,
56 odocstream & os, TexRow & texrow,
57 OutputParams const & runparams,
58 string const & everypar = string());
61 ParagraphList::const_iterator
62 TeXDeeper(Buffer const & buf,
63 ParagraphList const & paragraphs,
64 ParagraphList::const_iterator pit,
65 odocstream & os, TexRow & texrow,
66 OutputParams const & runparams)
68 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
69 ParagraphList::const_iterator par = pit;
71 while (par != paragraphs.end() &&
72 par->params().depth() == pit->params().depth()) {
73 if (par->layout()->isEnvironment()) {
74 par = TeXEnvironment(buf, paragraphs, par,
75 os, texrow, runparams);
77 par = TeXOnePar(buf, paragraphs, par,
78 os, texrow, runparams);
81 lyxerr[Debug::LATEX] << "TeXDeeper...done " << endl;
87 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
88 odocstream & os, OutputParams const & runparams, int number);
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 LyXLayout_ptr const & style = pit->layout();
104 Language const * language = pit->getParLanguage(bparams);
105 Language const * doc_language = bparams.language;
106 Language const * previous_language =
107 (pit != paragraphs.begin())
108 ? boost::prior(pit)->getParLanguage(bparams)
110 if (language->babel() != previous_language->babel()) {
112 if (!lyxrc.language_command_end.empty() &&
113 previous_language->babel() != doc_language->babel()) {
114 os << from_ascii(subst(
115 lyxrc.language_command_end,
117 previous_language->babel()))
122 if (lyxrc.language_command_end.empty() ||
123 language->babel() != doc_language->babel()) {
124 os << from_ascii(subst(
125 lyxrc.language_command_begin,
133 bool leftindent_open = false;
134 if (!pit->params().leftIndent().zero()) {
135 os << "\\begin{LyXParagraphLeftIndent}{"
136 << from_ascii(pit->params().leftIndent().asLatexString())
139 leftindent_open = true;
142 if (style->isEnvironment()) {
143 os << "\\begin{" << from_ascii(style->latexname()) << '}';
144 if (style->optionalargs > 0) {
145 int ret = latexOptArgInsets(buf, *pit, os, runparams,
146 style->optionalargs);
152 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
154 << pit->params().labelWidthString()
156 } else if (style->labeltype == LABEL_BIBLIO) {
158 os << '{' << bibitemWidest(buf) << "}\n";
160 os << from_ascii(style->latexparam()) << '\n';
163 ParagraphList::const_iterator par = pit;
165 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
167 if (par == paragraphs.end()) {
168 // Make sure that the last paragraph is
169 // correctly terminated (because TeXOnePar does
170 // not add a \n in this case)
173 } else if (par->params().depth() > pit->params().depth()) {
174 if (par->layout()->isParagraph()) {
177 // How to handle this? (Lgb)
178 //&& !suffixIs(os, "\n\n")
180 // There should be at least one '\n' already
181 // but we need there to be two for Standard
182 // paragraphs that are depth-increment'ed to be
183 // output correctly. However, tables can
184 // also be paragraphs so don't adjust them.
187 // Will it ever harm to have one '\n' too
188 // many? i.e. that we sometimes will have
189 // three in a row. (Lgb)
193 par = TeXDeeper(buf, paragraphs, par, os, texrow,
196 } while (par != paragraphs.end()
197 && par->layout() == pit->layout()
198 && par->params().depth() == pit->params().depth()
199 && par->params().leftIndent() == pit->params().leftIndent());
201 if (style->isEnvironment()) {
202 os << "\\end{" << from_ascii(style->latexname()) << "}\n";
206 if (leftindent_open) {
207 os << "\\end{LyXParagraphLeftIndent}\n";
211 if (par != paragraphs.end() && lyxerr.debugging(Debug::LATEX))
212 lyxerr << "TeXEnvironment...done " << &*par << endl;
217 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
218 odocstream & os, OutputParams const & runparams, int number)
222 InsetList::const_iterator it = par.insetlist.begin();
223 InsetList::const_iterator end = par.insetlist.end();
224 for (; it != end && number > 0 ; ++it) {
225 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) {
227 static_cast<InsetOptArg *>(it->inset);
228 lines += ins->latexOptional(buf, os, runparams);
236 ParagraphList::const_iterator
237 TeXOnePar(Buffer const & buf,
238 ParagraphList const & paragraphs,
239 ParagraphList::const_iterator pit,
240 odocstream & ucs4, TexRow & texrow,
241 OutputParams const & runparams_in,
242 string const & everypar)
244 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
245 << everypar << "'" << endl;
246 BufferParams const & bparams = buf.params();
247 bool further_blank_line = false;
250 // In an 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.getLyXTextClass().defaultLayout();
257 OutputParams runparams = runparams_in;
258 runparams.moving_arg |= style->needprotect;
260 Language const * language = pit->getParLanguage(bparams);
261 Language const * doc_language = bparams.language;
262 Language const * previous_language =
263 (pit != paragraphs.begin())
264 ? boost::prior(pit)->getParLanguage(bparams)
267 if (language->babel() != previous_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 previous_language->babel() != doc_language->babel())
278 ucs4 << from_ascii(subst(lyxrc.language_command_end,
280 previous_language->babel()))
285 if (lyxrc.language_command_end.empty() ||
286 language->babel() != doc_language->babel())
288 ucs4 << from_ascii(subst(
289 lyxrc.language_command_begin,
297 if (bparams.inputenc == "auto" &&
298 language->encoding() != previous_language->encoding()) {
299 ucs4 << "\\inputencoding{"
300 << from_ascii(language->encoding()->latexName())
304 // We need to output the paragraph to a temporary stream if we
305 // need to change the encoding. Don't do this if the result does
306 // not go to a file but to the builtin source viewer.
307 odocstringstream par_stream;
308 bool const change_encoding = !runparams_in.dryrun &&
309 bparams.inputenc == "auto" &&
310 language->encoding() != doc_language->encoding();
311 // don't trigger the copy ctor because it's private on msvc
312 odocstream & os = *(change_encoding ? &par_stream : &ucs4);
314 // In an an inset with unlimited length (all in one row),
315 // don't allow any special options in the paragraph
316 if (!pit->forceDefaultParagraphs()) {
317 if (pit->params().startOfAppendix()) {
318 os << "\\appendix\n";
322 if (!pit->params().spacing().isDefault()
323 && (pit == paragraphs.begin()
324 || !boost::prior(pit)->hasSameLayout(*pit)))
326 os << from_ascii(pit->params().spacing().writeEnvirBegin())
331 if (style->isCommand()) {
336 if (further_blank_line) {
342 switch (style->latextype) {
344 os << '\\' << from_ascii(style->latexname());
346 // Separate handling of optional argument inset.
347 if (style->optionalargs > 0) {
348 int ret = latexOptArgInsets(buf, *pit, os, runparams,
349 style->optionalargs);
356 os << from_ascii(style->latexparam());
358 case LATEX_ITEM_ENVIRONMENT:
359 case LATEX_LIST_ENVIRONMENT:
362 case LATEX_BIB_ENVIRONMENT:
363 // ignore this, the inset will write itself
370 os << from_utf8(everypar);
371 bool need_par = pit->simpleTeXOnePar(buf, bparams,
372 outerFont(std::distance(paragraphs.begin(), pit), paragraphs),
373 os, texrow, runparams);
375 // Make sure that \\par is done with the font of the last
376 // character if this has another size as the default.
377 // This is necessary because LaTeX (and LyX on the screen)
378 // calculates the space between the baselines according
379 // to this font. (Matthias)
381 // Is this really needed ? (Dekel)
382 // We do not need to use to change the font for the last paragraph
384 LyXFont const outerfont =
385 outerFont(std::distance(paragraphs.begin(), pit),
390 ? pit->getLayoutFont(bparams, outerfont)
391 : pit->getFont(bparams, pit->size() - 1, outerfont));
393 bool is_command = style->isCommand();
395 if (style->resfont.size() != font.size()
396 && boost::next(pit) != paragraphs.end()
400 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
401 } else if (need_par) {
403 } else if (is_command)
406 switch (style->latextype) {
407 case LATEX_ITEM_ENVIRONMENT:
408 case LATEX_LIST_ENVIRONMENT:
409 if (boost::next(pit) != paragraphs.end()
410 && (pit->params().depth() < boost::next(pit)->params().depth())) {
415 case LATEX_ENVIRONMENT: {
416 // if its the last paragraph of the current environment
417 // skip it otherwise fall through
418 ParagraphList::const_iterator next = boost::next(pit);
420 if (next != paragraphs.end()
421 && (next->layout() != pit->layout()
422 || next->params().depth() != pit->params().depth()))
426 // fall through possible
428 // we don't need it for the last paragraph!!!
429 if (boost::next(pit) != paragraphs.end()) {
435 if (!pit->forceDefaultParagraphs()) {
436 further_blank_line = false;
438 if (further_blank_line) {
443 if (!pit->params().spacing().isDefault()
444 && (boost::next(pit) == paragraphs.end()
445 || !boost::next(pit)->hasSameLayout(*pit)))
447 os << from_ascii(pit->params().spacing().writeEnvirEnd())
453 if (boost::next(pit) == paragraphs.end()
454 && 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 (lyxrc.language_command_end.empty())
460 os << from_ascii(subst(
461 lyxrc.language_command_begin,
463 doc_language->babel()))
466 os << from_ascii(subst(
467 lyxrc.language_command_end,
474 // we don't need it for the last paragraph!!!
475 // Note from JMarc: we will re-add a \n explicitely in
476 // TeXEnvironment, because it is needed in this case
477 if (boost::next(pit) != paragraphs.end()) {
482 if (boost::next(pit) != paragraphs.end() &&
483 lyxerr.debugging(Debug::LATEX))
484 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
486 if (change_encoding) {
487 lyxerr[Debug::LATEX] << "Converting paragraph to encoding "
488 << language->encoding()->iconvName() << endl;
489 docstring const par = par_stream.str();
490 // Convert the paragraph to the 8bit encoding that we need to
492 std::vector<char> const encoded = lyx::ucs4_to_eightbit(par.c_str(),
493 par.size(), language->encoding()->iconvName());
494 // Interpret this as if it was in the 8 bit encoding of the
495 // document language and convert it back to UCS4. That means
496 // that faked does not contain pure UCS4 anymore, but what
497 // will be written to the output file will be correct, because
498 // the real output stream will do a UCS4 -> document language
499 // encoding conversion.
500 // This is of course a hack, but not a bigger one than mixing
501 // two encodings in one file.
502 // FIXME: Catch iconv conversion errors and display an error
505 // Here follows an explanation how I (gb) came to the current
508 // codecvt facets are only used by file streams -> OK, maybe
509 // we could use file streams and not generic streams in the
510 // latex() methods? No, that does not work, we use them at
511 // several places to write to string streams.
512 // Next try: Maybe we could do something else than codecvt
513 // in our streams, and add a setEncoding() method? That
514 // does not work unless we rebuild the functionality of file
515 // and string streams, since both odocfstream and
516 // odocstringstream inherit from std::basic_ostream<docstring>
517 // and we can neither add a method to that class nor change
518 // the inheritance of the file and string streams.
520 // What might be possible is to encapsulate the real file and
521 // string streams in our own version, and use a homemade
522 // streambuf that would do the encoding conversion and then
523 // forward to the real stream. That would probably work, but
524 // would require far more code and a good understanding of
525 // stream buffers to get it right.
527 // Another idea by JMarc is to use a modifier like
528 // os << setencoding("iso-8859-1");
529 // That currently looks like the best idea.
531 std::vector<char_type> const faked = lyx::eightbit_to_ucs4(&(encoded[0]),
532 encoded.size(), doc_language->encoding()->iconvName());
533 std::vector<char_type>::const_iterator const end = faked.end();
534 std::vector<char_type>::const_iterator it = faked.begin();
535 for (; it != end; ++it)
545 // LaTeX all paragraphs
546 void latexParagraphs(Buffer const & buf,
547 ParagraphList const & paragraphs,
550 OutputParams const & runparams,
551 string const & everypar)
553 bool was_title = false;
554 bool already_title = false;
555 LyXTextClass const & tclass = buf.params().getLyXTextClass();
556 ParagraphList::const_iterator par = paragraphs.begin();
557 ParagraphList::const_iterator endpar = paragraphs.end();
559 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
560 // if only part of the paragraphs will be outputed
561 if (runparams.par_begin != runparams.par_end) {
562 par = boost::next(paragraphs.begin(), runparams.par_begin);
563 endpar = boost::next(paragraphs.begin(), runparams.par_end);
564 // runparams will be passed to nested paragraphs, so
565 // we have to reset the range parameters.
566 const_cast<OutputParams&>(runparams).par_begin = 0;
567 const_cast<OutputParams&>(runparams).par_end = 0;
571 while (par != endpar) {
572 ParagraphList::const_iterator lastpar = par;
573 // well we have to check if we are in an inset with unlimited
574 // length (all in one row) if that is true then we don't allow
575 // any special options in the paragraph and also we don't allow
576 // any environment other then "Standard" to be valid!
577 if (!par->forceDefaultParagraphs()) {
578 LyXLayout_ptr const & layout = par->layout();
580 if (layout->intitle) {
582 lyxerr << "Error in latexParagraphs: You"
583 " should not mix title layouts"
584 " with normal ones." << endl;
585 } else if (!was_title) {
587 if (tclass.titletype() == TITLE_ENVIRONMENT) {
589 << from_ascii(tclass.titlename())
594 } else if (was_title && !already_title) {
595 if (tclass.titletype() == TITLE_ENVIRONMENT) {
596 os << "\\end{" << from_ascii(tclass.titlename())
600 os << "\\" << from_ascii(tclass.titlename())
604 already_title = true;
608 if (layout->is_environment) {
609 par = TeXOnePar(buf, paragraphs, par, os, texrow,
610 runparams, everypar);
611 } else if (layout->isEnvironment() ||
612 !par->params().leftIndent().zero())
614 par = TeXEnvironment(buf, paragraphs, par, os,
617 par = TeXOnePar(buf, paragraphs, par, os, texrow,
618 runparams, everypar);
621 par = TeXOnePar(buf, paragraphs, par, os, texrow,
622 runparams, everypar);
624 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
627 // It might be that we only have a title in this document
628 if (was_title && !already_title) {
629 if (tclass.titletype() == TITLE_ENVIRONMENT) {
630 os << "\\end{" << from_ascii(tclass.titlename())
634 os << "\\" << from_ascii(tclass.titlename())