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) {
152 << lyx::from_utf8(pit->params().labelWidthString())
154 } else if (style->labeltype == LABEL_BIBLIO) {
156 os << '{' << bibitemWidest(buf) << "}\n";
158 os << lyx::from_ascii(style->latexparam()) << '\n';
161 ParagraphList::const_iterator par = pit;
163 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
165 if (par == paragraphs.end()) {
166 // Make sure that the last paragraph is
167 // correctly terminated (because TeXOnePar does
168 // not add a \n in this case)
171 } else if (par->params().depth() > pit->params().depth()) {
172 if (par->layout()->isParagraph()) {
175 // How to handle this? (Lgb)
176 //&& !suffixIs(os, "\n\n")
178 // There should be at least one '\n' already
179 // but we need there to be two for Standard
180 // paragraphs that are depth-increment'ed to be
181 // output correctly. However, tables can
182 // also be paragraphs so don't adjust them.
185 // Will it ever harm to have one '\n' too
186 // many? i.e. that we sometimes will have
187 // three in a row. (Lgb)
191 par = TeXDeeper(buf, paragraphs, par, os, texrow,
194 } while (par != paragraphs.end()
195 && par->layout() == pit->layout()
196 && par->params().depth() == pit->params().depth()
197 && par->params().leftIndent() == pit->params().leftIndent());
199 if (style->isEnvironment()) {
200 os << "\\end{" << lyx::from_ascii(style->latexname()) << "}\n";
204 if (leftindent_open) {
205 os << "\\end{LyXParagraphLeftIndent}\n";
209 if (par != paragraphs.end() && lyxerr.debugging(Debug::LATEX))
210 lyxerr << "TeXEnvironment...done " << &*par << endl;
215 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
216 odocstream & os, OutputParams const & runparams, int number)
220 InsetList::const_iterator it = par.insetlist.begin();
221 InsetList::const_iterator end = par.insetlist.end();
222 for (; it != end && number > 0 ; ++it) {
223 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) {
225 static_cast<InsetOptArg *>(it->inset);
226 lines += ins->latexOptional(buf, os, runparams);
234 ParagraphList::const_iterator
235 TeXOnePar(Buffer const & buf,
236 ParagraphList const & paragraphs,
237 ParagraphList::const_iterator pit,
238 odocstream & os, TexRow & texrow,
239 OutputParams const & runparams_in,
240 string const & everypar)
242 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
243 << everypar << "'" << endl;
244 BufferParams const & bparams = buf.params();
245 bool further_blank_line = false;
248 // In an 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 << lyx::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 << lyx::from_ascii(subst(
287 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())
303 // In an an inset with unlimited length (all in one row),
304 // don't allow any special options in the paragraph
305 if (!pit->forceDefaultParagraphs()) {
306 if (pit->params().startOfAppendix()) {
307 os << "\\appendix\n";
311 if (!pit->params().spacing().isDefault()
312 && (pit == paragraphs.begin()
313 || !boost::prior(pit)->hasSameLayout(*pit)))
315 os << lyx::from_ascii(pit->params().spacing().writeEnvirBegin())
320 if (style->isCommand()) {
325 if (further_blank_line) {
331 switch (style->latextype) {
333 os << '\\' << lyx::from_ascii(style->latexname());
335 // Separate handling of optional argument inset.
336 if (style->optionalargs > 0) {
337 int ret = latexOptArgInsets(buf, *pit, os, runparams,
338 style->optionalargs);
345 os << lyx::from_ascii(style->latexparam());
347 case LATEX_ITEM_ENVIRONMENT:
348 case LATEX_LIST_ENVIRONMENT:
351 case LATEX_BIB_ENVIRONMENT:
352 // ignore this, the inset will write itself
359 os << lyx::from_utf8(everypar);
360 bool need_par = pit->simpleTeXOnePar(buf, bparams,
361 outerFont(std::distance(paragraphs.begin(), pit), paragraphs),
362 os, texrow, runparams);
364 // Make sure that \\par is done with the font of the last
365 // character if this has another size as the default.
366 // This is necessary because LaTeX (and LyX on the screen)
367 // calculates the space between the baselines according
368 // to this font. (Matthias)
370 // Is this really needed ? (Dekel)
371 // We do not need to use to change the font for the last paragraph
373 LyXFont const outerfont =
374 outerFont(std::distance(paragraphs.begin(), pit),
379 ? pit->getLayoutFont(bparams, outerfont)
380 : pit->getFont(bparams, pit->size() - 1, outerfont));
382 bool is_command = style->isCommand();
384 if (style->resfont.size() != font.size()
385 && boost::next(pit) != paragraphs.end()
389 os << "\\" << lyx::from_ascii(font.latexSize()) << " \\par}";
390 } else if (need_par) {
392 } else if (is_command)
395 switch (style->latextype) {
396 case LATEX_ITEM_ENVIRONMENT:
397 case LATEX_LIST_ENVIRONMENT:
398 if (boost::next(pit) != paragraphs.end()
399 && (pit->params().depth() < boost::next(pit)->params().depth())) {
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()) {
424 if (!pit->forceDefaultParagraphs()) {
425 further_blank_line = false;
427 if (further_blank_line) {
432 if (!pit->params().spacing().isDefault()
433 && (boost::next(pit) == paragraphs.end()
434 || !boost::next(pit)->hasSameLayout(*pit)))
436 os << lyx::from_ascii(pit->params().spacing().writeEnvirEnd())
442 if (boost::next(pit) == paragraphs.end()
443 && language->babel() != doc_language->babel()) {
444 // Since \selectlanguage write the language to the aux file,
445 // we need to reset the language at the end of footnote or
448 if (lyxrc.language_command_end.empty())
449 os << lyx::from_ascii(subst(
450 lyxrc.language_command_begin,
452 doc_language->babel()))
455 os << lyx::from_ascii(subst(
456 lyxrc.language_command_end,
463 // we don't need it for the last paragraph!!!
464 // Note from JMarc: we will re-add a \n explicitely in
465 // TeXEnvironment, because it is needed in this case
466 if (boost::next(pit) != paragraphs.end()) {
471 if (boost::next(pit) != paragraphs.end() &&
472 lyxerr.debugging(Debug::LATEX))
473 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
480 // LaTeX all paragraphs
481 void latexParagraphs(Buffer const & buf,
482 ParagraphList const & paragraphs,
485 OutputParams const & runparams,
486 string const & everypar)
488 bool was_title = false;
489 bool already_title = false;
490 LyXTextClass const & tclass = buf.params().getLyXTextClass();
491 ParagraphList::const_iterator par = paragraphs.begin();
492 ParagraphList::const_iterator endpar = paragraphs.end();
494 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
495 // if only part of the paragraphs will be outputed
496 if (runparams.par_begin != runparams.par_end) {
497 par = boost::next(paragraphs.begin(), runparams.par_begin);
498 endpar = boost::next(paragraphs.begin(), runparams.par_end);
499 // runparams will be passed to nested paragraphs, so
500 // we have to reset the range parameters.
501 const_cast<OutputParams&>(runparams).par_begin = 0;
502 const_cast<OutputParams&>(runparams).par_end = 0;
506 while (par != endpar) {
507 ParagraphList::const_iterator lastpar = par;
508 // well we have to check if we are in an inset with unlimited
509 // length (all in one row) if that is true then we don't allow
510 // any special options in the paragraph and also we don't allow
511 // any environment other then "Standard" to be valid!
512 if (!par->forceDefaultParagraphs()) {
513 LyXLayout_ptr const & layout = par->layout();
515 if (layout->intitle) {
517 lyxerr << "Error in latexParagraphs: You"
518 " should not mix title layouts"
519 " with normal ones." << endl;
520 } else if (!was_title) {
522 if (tclass.titletype() == TITLE_ENVIRONMENT) {
524 << lyx::from_ascii(tclass.titlename())
529 } else if (was_title && !already_title) {
530 if (tclass.titletype() == TITLE_ENVIRONMENT) {
531 os << "\\end{" << lyx::from_ascii(tclass.titlename())
535 os << "\\" << lyx::from_ascii(tclass.titlename())
539 already_title = true;
543 if (layout->is_environment) {
544 par = TeXOnePar(buf, paragraphs, par, os, texrow,
545 runparams, everypar);
546 } else if (layout->isEnvironment() ||
547 !par->params().leftIndent().zero())
549 par = TeXEnvironment(buf, paragraphs, par, os,
552 par = TeXOnePar(buf, paragraphs, par, os, texrow,
553 runparams, everypar);
556 par = TeXOnePar(buf, paragraphs, par, os, texrow,
557 runparams, everypar);
559 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
562 // It might be that we only have a title in this document
563 if (was_title && !already_title) {
564 if (tclass.titletype() == TITLE_ENVIRONMENT) {
565 os << "\\end{" << lyx::from_ascii(tclass.titlename())
569 os << "\\" << lyx::from_ascii(tclass.titlename())