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"
33 using lyx::support::subst;
36 using lyx::odocstream;
42 ParagraphList::const_iterator
43 TeXEnvironment(Buffer const & buf,
44 ParagraphList const & paragraphs,
45 ParagraphList::const_iterator pit,
46 odocstream & os, TexRow & texrow,
47 OutputParams const & runparams);
49 ParagraphList::const_iterator
50 TeXOnePar(Buffer const & buf,
51 ParagraphList const & paragraphs,
52 ParagraphList::const_iterator pit,
53 odocstream & os, TexRow & texrow,
54 OutputParams const & runparams,
55 string const & everypar = string());
58 ParagraphList::const_iterator
59 TeXDeeper(Buffer const & buf,
60 ParagraphList const & paragraphs,
61 ParagraphList::const_iterator pit,
62 odocstream & os, TexRow & texrow,
63 OutputParams const & runparams)
65 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
66 ParagraphList::const_iterator par = pit;
68 while (par != paragraphs.end() &&
69 par->params().depth() == pit->params().depth()) {
70 if (par->layout()->isEnvironment()) {
71 par = TeXEnvironment(buf, paragraphs, par,
72 os, texrow, runparams);
74 par = TeXOnePar(buf, paragraphs, par,
75 os, texrow, runparams);
78 lyxerr[Debug::LATEX] << "TeXDeeper...done " << endl;
84 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
85 odocstream & os, OutputParams const & runparams, int number);
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 LyXLayout_ptr const & style = pit->layout();
101 Language const * language = pit->getParLanguage(bparams);
102 Language const * doc_language = bparams.language;
103 Language const * previous_language =
104 (pit != paragraphs.begin())
105 ? boost::prior(pit)->getParLanguage(bparams)
107 if (language->babel() != previous_language->babel()) {
109 if (!lyxrc.language_command_end.empty() &&
110 previous_language->babel() != doc_language->babel()) {
111 os << lyx::from_ascii(subst(
112 lyxrc.language_command_end,
114 previous_language->babel()))
119 if (lyxrc.language_command_end.empty() ||
120 language->babel() != doc_language->babel()) {
121 os << lyx::from_ascii(subst(
122 lyxrc.language_command_begin,
130 bool leftindent_open = false;
131 if (!pit->params().leftIndent().zero()) {
132 os << "\\begin{LyXParagraphLeftIndent}{"
133 << lyx::from_ascii(pit->params().leftIndent().asLatexString())
136 leftindent_open = true;
139 if (style->isEnvironment()) {
140 os << "\\begin{" << lyx::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 << lyx::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{" << lyx::from_ascii(style->latexname()) << "}\n";
203 if (leftindent_open) {
204 os << "\\end{LyXParagraphLeftIndent}\n";
208 if (par != paragraphs.end() && lyxerr.debugging(Debug::LATEX))
209 lyxerr << "TeXEnvironment...done " << &*par << endl;
214 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
215 odocstream & os, OutputParams const & runparams, int number)
219 InsetList::const_iterator it = par.insetlist.begin();
220 InsetList::const_iterator end = par.insetlist.end();
221 for (; it != end && number > 0 ; ++it) {
222 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) {
224 static_cast<InsetOptArg *>(it->inset);
225 lines += ins->latexOptional(buf, os, runparams);
233 ParagraphList::const_iterator
234 TeXOnePar(Buffer const & buf,
235 ParagraphList const & paragraphs,
236 ParagraphList::const_iterator pit,
237 odocstream & os, TexRow & texrow,
238 OutputParams const & runparams_in,
239 string const & everypar)
241 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
242 << everypar << "'" << endl;
243 BufferParams const & bparams = buf.params();
244 bool further_blank_line = false;
247 // In an an inset with unlimited length (all in one row),
248 // force layout to default
249 if (!pit->forceDefaultParagraphs())
250 style = pit->layout();
252 style = bparams.getLyXTextClass().defaultLayout();
254 OutputParams runparams = runparams_in;
255 runparams.moving_arg |= style->needprotect;
257 Language const * language = pit->getParLanguage(bparams);
258 Language const * doc_language = bparams.language;
259 Language const * previous_language =
260 (pit != paragraphs.begin())
261 ? boost::prior(pit)->getParLanguage(bparams)
264 if (language->babel() != previous_language->babel()
265 // check if we already put language command in TeXEnvironment()
266 && !(style->isEnvironment()
267 && (pit == paragraphs.begin() ||
268 (boost::prior(pit)->layout() != pit->layout() &&
269 boost::prior(pit)->getDepth() <= pit->getDepth())
270 || boost::prior(pit)->getDepth() < pit->getDepth())))
272 if (!lyxrc.language_command_end.empty() &&
273 previous_language->babel() != doc_language->babel())
275 os << lyx::from_ascii(subst(lyxrc.language_command_end,
277 previous_language->babel()))
282 if (lyxrc.language_command_end.empty() ||
283 language->babel() != doc_language->babel())
285 os << lyx::from_ascii(subst(
286 lyxrc.language_command_begin,
295 if (bparams.inputenc == "auto" &&
296 language->encoding() != previous_language->encoding()) {
297 os << "\\inputencoding{"
298 << lyx::from_ascii(language->encoding()->latexName())
304 // In an an inset with unlimited length (all in one row),
305 // don't allow any special options in the paragraph
306 if (!pit->forceDefaultParagraphs()) {
307 if (pit->params().startOfAppendix()) {
308 os << "\\appendix\n";
312 if (!pit->params().spacing().isDefault()
313 && (pit == paragraphs.begin()
314 || !boost::prior(pit)->hasSameLayout(*pit)))
316 os << lyx::from_ascii(pit->params().spacing().writeEnvirBegin())
321 if (style->isCommand()) {
326 if (further_blank_line) {
332 switch (style->latextype) {
334 os << '\\' << lyx::from_ascii(style->latexname());
336 // Separate handling of optional argument inset.
337 if (style->optionalargs > 0) {
338 int ret = latexOptArgInsets(buf, *pit, os, runparams,
339 style->optionalargs);
346 os << lyx::from_ascii(style->latexparam());
348 case LATEX_ITEM_ENVIRONMENT:
349 case LATEX_LIST_ENVIRONMENT:
352 case LATEX_BIB_ENVIRONMENT:
353 // ignore this, the inset will write itself
360 os << lyx::from_utf8(everypar);
361 bool need_par = pit->simpleTeXOnePar(buf, bparams,
362 outerFont(std::distance(paragraphs.begin(), pit), paragraphs),
363 os, texrow, runparams);
365 // Make sure that \\par is done with the font of the last
366 // character if this has another size as the default.
367 // This is necessary because LaTeX (and LyX on the screen)
368 // calculates the space between the baselines according
369 // to this font. (Matthias)
371 // Is this really needed ? (Dekel)
372 // We do not need to use to change the font for the last paragraph
374 LyXFont const outerfont =
375 outerFont(std::distance(paragraphs.begin(), pit),
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 << "\\" << lyx::from_ascii(font.latexSize()) << " \\par}";
391 } else if (need_par) {
393 } else if (is_command)
396 switch (style->latextype) {
397 case LATEX_ITEM_ENVIRONMENT:
398 case LATEX_LIST_ENVIRONMENT:
399 if (boost::next(pit) != paragraphs.end()
400 && (pit->params().depth() < boost::next(pit)->params().depth())) {
405 case LATEX_ENVIRONMENT: {
406 // if its the last paragraph of the current environment
407 // skip it otherwise fall through
408 ParagraphList::const_iterator next = boost::next(pit);
410 if (next != paragraphs.end()
411 && (next->layout() != pit->layout()
412 || next->params().depth() != pit->params().depth()))
416 // fall through possible
418 // we don't need it for the last paragraph!!!
419 if (boost::next(pit) != paragraphs.end()) {
425 if (!pit->forceDefaultParagraphs()) {
426 further_blank_line = false;
428 if (further_blank_line) {
433 if (!pit->params().spacing().isDefault()
434 && (boost::next(pit) == paragraphs.end()
435 || !boost::next(pit)->hasSameLayout(*pit)))
437 os << lyx::from_ascii(pit->params().spacing().writeEnvirEnd())
443 if (boost::next(pit) == paragraphs.end()
444 && language->babel() != doc_language->babel()) {
445 // Since \selectlanguage write the language to the aux file,
446 // we need to reset the language at the end of footnote or
449 if (lyxrc.language_command_end.empty())
450 os << lyx::from_ascii(subst(
451 lyxrc.language_command_begin,
453 doc_language->babel()))
456 os << lyx::from_ascii(subst(
457 lyxrc.language_command_end,
464 // we don't need it for the last paragraph!!!
465 // Note from JMarc: we will re-add a \n explicitely in
466 // TeXEnvironment, because it is needed in this case
467 if (boost::next(pit) != paragraphs.end()) {
472 if (boost::next(pit) != paragraphs.end() &&
473 lyxerr.debugging(Debug::LATEX))
474 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
481 // LaTeX all paragraphs
482 void latexParagraphs(Buffer const & buf,
483 ParagraphList const & paragraphs,
486 OutputParams const & runparams,
487 string const & everypar)
489 bool was_title = false;
490 bool already_title = false;
491 LyXTextClass const & tclass = buf.params().getLyXTextClass();
492 ParagraphList::const_iterator par = paragraphs.begin();
493 ParagraphList::const_iterator endpar = paragraphs.end();
495 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
496 // if only part of the paragraphs will be outputed
497 if (runparams.par_begin != runparams.par_end) {
498 par = boost::next(paragraphs.begin(), runparams.par_begin);
499 endpar = boost::next(paragraphs.begin(), runparams.par_end);
500 // runparams will be passed to nested paragraphs, so
501 // we have to reset the range parameters.
502 const_cast<OutputParams&>(runparams).par_begin = 0;
503 const_cast<OutputParams&>(runparams).par_end = 0;
507 while (par != endpar) {
508 ParagraphList::const_iterator lastpar = par;
509 // well we have to check if we are in an inset with unlimited
510 // length (all in one row) if that is true then we don't allow
511 // any special options in the paragraph and also we don't allow
512 // any environment other then "Standard" to be valid!
513 if (!par->forceDefaultParagraphs()) {
514 LyXLayout_ptr const & layout = par->layout();
516 if (layout->intitle) {
518 lyxerr << "Error in latexParagraphs: You"
519 " should not mix title layouts"
520 " with normal ones." << endl;
521 } else if (!was_title) {
523 if (tclass.titletype() == TITLE_ENVIRONMENT) {
525 << lyx::from_ascii(tclass.titlename())
530 } else if (was_title && !already_title) {
531 if (tclass.titletype() == TITLE_ENVIRONMENT) {
532 os << "\\end{" << lyx::from_ascii(tclass.titlename())
536 os << "\\" << lyx::from_ascii(tclass.titlename())
540 already_title = true;
544 if (layout->is_environment) {
545 par = TeXOnePar(buf, paragraphs, par, os, texrow,
546 runparams, everypar);
547 } else if (layout->isEnvironment() ||
548 !par->params().leftIndent().zero())
550 par = TeXEnvironment(buf, paragraphs, par, os,
553 par = TeXOnePar(buf, paragraphs, par, os, texrow,
554 runparams, everypar);
557 par = TeXOnePar(buf, paragraphs, par, os, texrow,
558 runparams, everypar);
560 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
563 // It might be that we only have a title in this document
564 if (was_title && !already_title) {
565 if (tclass.titletype() == TITLE_ENVIRONMENT) {
566 os << "\\end{" << lyx::from_ascii(tclass.titlename())
570 os << "\\" << lyx::from_ascii(tclass.titlename())