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;
42 ParagraphList::const_iterator
43 TeXEnvironment(Buffer const & buf,
44 ParagraphList const & paragraphs,
45 ParagraphList::const_iterator pit,
46 ostream & 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 ostream & 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 ostream & 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 ParagraphList::const_iterator
85 TeXEnvironment(Buffer const & buf,
86 ParagraphList const & paragraphs,
87 ParagraphList::const_iterator pit,
88 ostream & os, TexRow & texrow,
89 OutputParams const & runparams)
91 lyxerr[Debug::LATEX] << "TeXEnvironment... " << &*pit << endl;
93 BufferParams const & bparams = buf.params();
95 LyXLayout_ptr const & style = pit->layout();
97 Language const * language = pit->getParLanguage(bparams);
98 Language const * doc_language = bparams.language;
99 Language const * previous_language =
100 (pit != paragraphs.begin())
101 ? boost::prior(pit)->getParLanguage(bparams)
103 if (language->babel() != previous_language->babel()) {
105 if (!lyxrc.language_command_end.empty() &&
106 previous_language->babel() != doc_language->babel()) {
107 os << subst(lyxrc.language_command_end, "$$lang",
108 previous_language->babel())
113 if (lyxrc.language_command_end.empty() ||
114 language->babel() != doc_language->babel()) {
115 os << subst(lyxrc.language_command_begin, "$$lang",
122 bool leftindent_open = false;
123 if (!pit->params().leftIndent().zero()) {
124 os << "\\begin{LyXParagraphLeftIndent}{" <<
125 pit->params().leftIndent().asLatexString() << "}\n";
127 leftindent_open = true;
130 if (style->isEnvironment()) {
131 os << "\\begin{" << style->latexname() << '}';
132 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
133 os << "{" << pit->params().labelWidthString() << "}\n";
134 } else if (style->labeltype == LABEL_BIBLIO) {
136 os << "{" << bibitemWidest(buf) << "}\n";
138 os << style->latexparam() << '\n';
141 ParagraphList::const_iterator par = pit;
143 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
145 if (par == paragraphs.end()) {
146 // Make sure that the last paragraph is
147 // correctly terminated (because TeXOnePar does
148 // not add a \n in this case)
151 } else if (par->params().depth() > pit->params().depth()) {
152 if (par->layout()->isParagraph()) {
155 // How to handle this? (Lgb)
156 //&& !suffixIs(os, "\n\n")
158 // There should be at least one '\n' already
159 // but we need there to be two for Standard
160 // paragraphs that are depth-increment'ed to be
161 // output correctly. However, tables can
162 // also be paragraphs so don't adjust them.
165 // Will it ever harm to have one '\n' too
166 // many? i.e. that we sometimes will have
167 // three in a row. (Lgb)
171 par = TeXDeeper(buf, paragraphs, par, os, texrow,
174 } while (par != paragraphs.end()
175 && par->layout() == pit->layout()
176 && par->params().depth() == pit->params().depth()
177 && par->params().leftIndent() == pit->params().leftIndent());
179 if (style->isEnvironment()) {
180 os << "\\end{" << style->latexname() << "}\n";
184 if (leftindent_open) {
185 os << "\\end{LyXParagraphLeftIndent}\n";
189 if (par != paragraphs.end() && lyxerr.debugging(Debug::LATEX))
190 lyxerr << "TeXEnvironment...done " << &*par << endl;
195 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
196 ostream & os, OutputParams const & runparams, int number)
200 InsetList::const_iterator it = par.insetlist.begin();
201 InsetList::const_iterator end = par.insetlist.end();
202 for (; it != end && number > 0 ; ++it) {
203 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) {
205 static_cast<InsetOptArg *>(it->inset);
206 lines += ins->latexOptional(buf, os, runparams);
214 ParagraphList::const_iterator
215 TeXOnePar(Buffer const & buf,
216 ParagraphList const & paragraphs,
217 ParagraphList::const_iterator pit,
218 ostream & os, TexRow & texrow,
219 OutputParams const & runparams_in,
220 string const & everypar)
222 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
223 << everypar << "'" << endl;
224 BufferParams const & bparams = buf.params();
225 bool further_blank_line = false;
228 // In an an inset with unlimited length (all in one row),
229 // force layout to default
230 if (!pit->forceDefaultParagraphs())
231 style = pit->layout();
233 style = bparams.getLyXTextClass().defaultLayout();
235 OutputParams runparams = runparams_in;
236 runparams.moving_arg |= style->needprotect;
238 Language const * language = pit->getParLanguage(bparams);
239 Language const * doc_language = bparams.language;
240 Language const * previous_language =
241 (pit != paragraphs.begin())
242 ? boost::prior(pit)->getParLanguage(bparams)
245 if (language->babel() != previous_language->babel()
246 // check if we already put language command in TeXEnvironment()
247 && !(style->isEnvironment()
248 && (pit == paragraphs.begin() ||
249 (boost::prior(pit)->layout() != pit->layout() &&
250 boost::prior(pit)->getDepth() <= pit->getDepth())
251 || boost::prior(pit)->getDepth() < pit->getDepth())))
253 if (!lyxrc.language_command_end.empty() &&
254 previous_language->babel() != doc_language->babel())
256 os << subst(lyxrc.language_command_end, "$$lang",
257 previous_language->babel())
262 if (lyxrc.language_command_end.empty() ||
263 language->babel() != doc_language->babel())
265 os << subst(lyxrc.language_command_begin, "$$lang",
272 if (bparams.inputenc == "auto" &&
273 language->encoding() != previous_language->encoding()) {
274 os << "\\inputencoding{"
275 << language->encoding()->LatexName()
280 // In an an inset with unlimited length (all in one row),
281 // don't allow any special options in the paragraph
282 if (!pit->forceDefaultParagraphs()) {
283 if (pit->params().startOfAppendix()) {
284 os << "\\appendix\n";
288 if (!pit->params().spacing().isDefault()
289 && (pit == paragraphs.begin()
290 || !boost::prior(pit)->hasSameLayout(*pit)))
292 os << pit->params().spacing().writeEnvirBegin() << '\n';
296 if (style->isCommand()) {
301 if (further_blank_line) {
307 switch (style->latextype) {
309 os << '\\' << style->latexname();
311 // Separate handling of optional argument inset.
312 if (style->optionalargs > 0) {
313 int ret = latexOptArgInsets(buf, *pit, os, runparams,
314 style->optionalargs);
321 os << style->latexparam();
323 case LATEX_ITEM_ENVIRONMENT:
324 case LATEX_LIST_ENVIRONMENT:
327 case LATEX_BIB_ENVIRONMENT:
328 // ignore this, the inset will write itself
335 bool need_par = pit->simpleTeXOnePar(buf, bparams,
336 outerFont(std::distance(paragraphs.begin(), pit), paragraphs),
337 os, texrow, runparams);
339 // Make sure that \\par is done with the font of the last
340 // character if this has another size as the default.
341 // This is necessary because LaTeX (and LyX on the screen)
342 // calculates the space between the baselines according
343 // to this font. (Matthias)
345 // Is this really needed ? (Dekel)
346 // We do not need to use to change the font for the last paragraph
348 LyXFont const outerfont =
349 outerFont(std::distance(paragraphs.begin(), pit),
354 ? pit->getLayoutFont(bparams, outerfont)
355 : pit->getFont(bparams, pit->size() - 1, outerfont));
357 bool is_command = style->isCommand();
359 if (style->resfont.size() != font.size()
360 && boost::next(pit) != paragraphs.end()
364 os << "\\" << font.latexSize() << " \\par}";
365 } else if (need_par) {
367 } else if (is_command)
370 switch (style->latextype) {
371 case LATEX_ITEM_ENVIRONMENT:
372 case LATEX_LIST_ENVIRONMENT:
373 if (boost::next(pit) != paragraphs.end()
374 && (pit->params().depth() < boost::next(pit)->params().depth())) {
379 case LATEX_ENVIRONMENT: {
380 // if its the last paragraph of the current environment
381 // skip it otherwise fall through
382 ParagraphList::const_iterator next = boost::next(pit);
384 if (next != paragraphs.end()
385 && (next->layout() != pit->layout()
386 || next->params().depth() != pit->params().depth()))
390 // fall through possible
392 // we don't need it for the last paragraph!!!
393 if (boost::next(pit) != paragraphs.end()) {
399 if (!pit->forceDefaultParagraphs()) {
400 further_blank_line = false;
402 if (further_blank_line) {
407 if (!pit->params().spacing().isDefault()
408 && (boost::next(pit) == paragraphs.end()
409 || !boost::next(pit)->hasSameLayout(*pit)))
411 os << pit->params().spacing().writeEnvirEnd() << '\n';
416 if (boost::next(pit) == paragraphs.end()
417 && language->babel() != doc_language->babel()) {
418 // Since \selectlanguage write the language to the aux file,
419 // we need to reset the language at the end of footnote or
422 if (lyxrc.language_command_end.empty())
423 os << subst(lyxrc.language_command_begin,
425 doc_language->babel())
428 os << subst(lyxrc.language_command_end,
435 // we don't need it for the last paragraph!!!
436 // Note from JMarc: we will re-add a \n explicitely in
437 // TeXEnvironment, because it is needed in this case
438 if (boost::next(pit) != paragraphs.end()) {
443 if (boost::next(pit) != paragraphs.end() &&
444 lyxerr.debugging(Debug::LATEX))
445 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
452 // LaTeX all paragraphs
453 void latexParagraphs(Buffer const & buf,
454 ParagraphList const & paragraphs,
457 OutputParams const & runparams,
458 string const & everypar)
460 bool was_title = false;
461 bool already_title = false;
462 LyXTextClass const & tclass = buf.params().getLyXTextClass();
463 ParagraphList::const_iterator par = paragraphs.begin();
464 ParagraphList::const_iterator endpar = paragraphs.end();
467 while (par != endpar) {
468 // well we have to check if we are in an inset with unlimited
469 // length (all in one row) if that is true then we don't allow
470 // any special options in the paragraph and also we don't allow
471 // any environment other then "Standard" to be valid!
472 if (!par->forceDefaultParagraphs()) {
473 LyXLayout_ptr const & layout = par->layout();
475 if (layout->intitle) {
477 lyxerr << "Error in latexParagraphs: You"
478 " should not mix title layouts"
479 " with normal ones." << endl;
480 } else if (!was_title) {
482 if (tclass.titletype() == TITLE_ENVIRONMENT) {
484 << tclass.titlename()
489 } else if (was_title && !already_title) {
490 if (tclass.titletype() == TITLE_ENVIRONMENT) {
491 os << "\\end{" << tclass.titlename()
495 os << "\\" << tclass.titlename()
499 already_title = true;
503 if (layout->is_environment) {
504 par = TeXOnePar(buf, paragraphs, par, os, texrow,
505 runparams, everypar);
506 } else if (layout->isEnvironment() ||
507 !par->params().leftIndent().zero())
509 par = TeXEnvironment(buf, paragraphs, par, os,
512 par = TeXOnePar(buf, paragraphs, par, os, texrow,
513 runparams, everypar);
516 par = TeXOnePar(buf, paragraphs, par, os, texrow,
517 runparams, everypar);
520 // It might be that we only have a title in this document
521 if (was_title && !already_title) {
522 if (tclass.titletype() == TITLE_ENVIRONMENT) {
523 os << "\\end{" << tclass.titlename()
527 os << "\\" << tclass.titlename()