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 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
85 ostream & 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 ostream & 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 << subst(lyxrc.language_command_end, "$$lang",
112 previous_language->babel())
117 if (lyxrc.language_command_end.empty() ||
118 language->babel() != doc_language->babel()) {
119 os << subst(lyxrc.language_command_begin, "$$lang",
126 bool leftindent_open = false;
127 if (!pit->params().leftIndent().zero()) {
128 os << "\\begin{LyXParagraphLeftIndent}{" <<
129 pit->params().leftIndent().asLatexString() << "}\n";
131 leftindent_open = true;
134 if (style->isEnvironment()) {
135 os << "\\begin{" << style->latexname() << '}';
136 if (style->optionalargs > 0) {
137 int ret = latexOptArgInsets(buf, *pit, os, runparams,
138 style->optionalargs);
144 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
145 os << "{" << pit->params().labelWidthString() << "}\n";
146 } else if (style->labeltype == LABEL_BIBLIO) {
149 os << '{' << lyx::to_utf8(bibitemWidest(buf)) << "}\n";
151 os << style->latexparam() << '\n';
154 ParagraphList::const_iterator par = pit;
156 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
158 if (par == paragraphs.end()) {
159 // Make sure that the last paragraph is
160 // correctly terminated (because TeXOnePar does
161 // not add a \n in this case)
164 } else if (par->params().depth() > pit->params().depth()) {
165 if (par->layout()->isParagraph()) {
168 // How to handle this? (Lgb)
169 //&& !suffixIs(os, "\n\n")
171 // There should be at least one '\n' already
172 // but we need there to be two for Standard
173 // paragraphs that are depth-increment'ed to be
174 // output correctly. However, tables can
175 // also be paragraphs so don't adjust them.
178 // Will it ever harm to have one '\n' too
179 // many? i.e. that we sometimes will have
180 // three in a row. (Lgb)
184 par = TeXDeeper(buf, paragraphs, par, os, texrow,
187 } while (par != paragraphs.end()
188 && par->layout() == pit->layout()
189 && par->params().depth() == pit->params().depth()
190 && par->params().leftIndent() == pit->params().leftIndent());
192 if (style->isEnvironment()) {
193 os << "\\end{" << style->latexname() << "}\n";
197 if (leftindent_open) {
198 os << "\\end{LyXParagraphLeftIndent}\n";
202 if (par != paragraphs.end() && lyxerr.debugging(Debug::LATEX))
203 lyxerr << "TeXEnvironment...done " << &*par << endl;
208 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
209 ostream & os, OutputParams const & runparams, int number)
213 InsetList::const_iterator it = par.insetlist.begin();
214 InsetList::const_iterator end = par.insetlist.end();
215 for (; it != end && number > 0 ; ++it) {
216 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) {
218 static_cast<InsetOptArg *>(it->inset);
219 lines += ins->latexOptional(buf, os, runparams);
227 ParagraphList::const_iterator
228 TeXOnePar(Buffer const & buf,
229 ParagraphList const & paragraphs,
230 ParagraphList::const_iterator pit,
231 ostream & os, TexRow & texrow,
232 OutputParams const & runparams_in,
233 string const & everypar)
235 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
236 << everypar << "'" << endl;
237 BufferParams const & bparams = buf.params();
238 bool further_blank_line = false;
241 // In an an inset with unlimited length (all in one row),
242 // force layout to default
243 if (!pit->forceDefaultParagraphs())
244 style = pit->layout();
246 style = bparams.getLyXTextClass().defaultLayout();
248 OutputParams runparams = runparams_in;
249 runparams.moving_arg |= style->needprotect;
251 Language const * language = pit->getParLanguage(bparams);
252 Language const * doc_language = bparams.language;
253 Language const * previous_language =
254 (pit != paragraphs.begin())
255 ? boost::prior(pit)->getParLanguage(bparams)
258 if (language->babel() != previous_language->babel()
259 // check if we already put language command in TeXEnvironment()
260 && !(style->isEnvironment()
261 && (pit == paragraphs.begin() ||
262 (boost::prior(pit)->layout() != pit->layout() &&
263 boost::prior(pit)->getDepth() <= pit->getDepth())
264 || boost::prior(pit)->getDepth() < pit->getDepth())))
266 if (!lyxrc.language_command_end.empty() &&
267 previous_language->babel() != doc_language->babel())
269 os << subst(lyxrc.language_command_end, "$$lang",
270 previous_language->babel())
275 if (lyxrc.language_command_end.empty() ||
276 language->babel() != doc_language->babel())
278 os << subst(lyxrc.language_command_begin, "$$lang",
285 if (bparams.inputenc == "auto" &&
286 language->encoding() != previous_language->encoding()) {
287 os << "\\inputencoding{"
288 << language->encoding()->latexName()
293 // In an an inset with unlimited length (all in one row),
294 // don't allow any special options in the paragraph
295 if (!pit->forceDefaultParagraphs()) {
296 if (pit->params().startOfAppendix()) {
297 os << "\\appendix\n";
301 if (!pit->params().spacing().isDefault()
302 && (pit == paragraphs.begin()
303 || !boost::prior(pit)->hasSameLayout(*pit)))
305 os << pit->params().spacing().writeEnvirBegin() << '\n';
309 if (style->isCommand()) {
314 if (further_blank_line) {
320 switch (style->latextype) {
322 os << '\\' << style->latexname();
324 // Separate handling of optional argument inset.
325 if (style->optionalargs > 0) {
326 int ret = latexOptArgInsets(buf, *pit, os, runparams,
327 style->optionalargs);
334 os << style->latexparam();
336 case LATEX_ITEM_ENVIRONMENT:
337 case LATEX_LIST_ENVIRONMENT:
340 case LATEX_BIB_ENVIRONMENT:
341 // ignore this, the inset will write itself
348 bool need_par = pit->simpleTeXOnePar(buf, bparams,
349 outerFont(std::distance(paragraphs.begin(), pit), paragraphs),
350 os, texrow, runparams);
352 // Make sure that \\par is done with the font of the last
353 // character if this has another size as the default.
354 // This is necessary because LaTeX (and LyX on the screen)
355 // calculates the space between the baselines according
356 // to this font. (Matthias)
358 // Is this really needed ? (Dekel)
359 // We do not need to use to change the font for the last paragraph
361 LyXFont const outerfont =
362 outerFont(std::distance(paragraphs.begin(), pit),
367 ? pit->getLayoutFont(bparams, outerfont)
368 : pit->getFont(bparams, pit->size() - 1, outerfont));
370 bool is_command = style->isCommand();
372 if (style->resfont.size() != font.size()
373 && boost::next(pit) != paragraphs.end()
377 os << "\\" << font.latexSize() << " \\par}";
378 } else if (need_par) {
380 } else if (is_command)
383 switch (style->latextype) {
384 case LATEX_ITEM_ENVIRONMENT:
385 case LATEX_LIST_ENVIRONMENT:
386 if (boost::next(pit) != paragraphs.end()
387 && (pit->params().depth() < boost::next(pit)->params().depth())) {
392 case LATEX_ENVIRONMENT: {
393 // if its the last paragraph of the current environment
394 // skip it otherwise fall through
395 ParagraphList::const_iterator next = boost::next(pit);
397 if (next != paragraphs.end()
398 && (next->layout() != pit->layout()
399 || next->params().depth() != pit->params().depth()))
403 // fall through possible
405 // we don't need it for the last paragraph!!!
406 if (boost::next(pit) != paragraphs.end()) {
412 if (!pit->forceDefaultParagraphs()) {
413 further_blank_line = false;
415 if (further_blank_line) {
420 if (!pit->params().spacing().isDefault()
421 && (boost::next(pit) == paragraphs.end()
422 || !boost::next(pit)->hasSameLayout(*pit)))
424 os << pit->params().spacing().writeEnvirEnd() << '\n';
429 if (boost::next(pit) == paragraphs.end()
430 && language->babel() != doc_language->babel()) {
431 // Since \selectlanguage write the language to the aux file,
432 // we need to reset the language at the end of footnote or
435 if (lyxrc.language_command_end.empty())
436 os << subst(lyxrc.language_command_begin,
438 doc_language->babel())
441 os << subst(lyxrc.language_command_end,
448 // we don't need it for the last paragraph!!!
449 // Note from JMarc: we will re-add a \n explicitely in
450 // TeXEnvironment, because it is needed in this case
451 if (boost::next(pit) != paragraphs.end()) {
456 if (boost::next(pit) != paragraphs.end() &&
457 lyxerr.debugging(Debug::LATEX))
458 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
465 // LaTeX all paragraphs
466 void latexParagraphs(Buffer const & buf,
467 ParagraphList const & paragraphs,
470 OutputParams const & runparams,
471 string const & everypar)
473 bool was_title = false;
474 bool already_title = false;
475 LyXTextClass const & tclass = buf.params().getLyXTextClass();
476 ParagraphList::const_iterator par = paragraphs.begin();
477 ParagraphList::const_iterator endpar = paragraphs.end();
479 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
480 // if only part of the paragraphs will be outputed
481 if (runparams.par_begin != runparams.par_end) {
482 par = boost::next(paragraphs.begin(), runparams.par_begin);
483 endpar = boost::next(paragraphs.begin(), runparams.par_end);
484 // runparams will be passed to nested paragraphs, so
485 // we have to reset the range parameters.
486 const_cast<OutputParams&>(runparams).par_begin = 0;
487 const_cast<OutputParams&>(runparams).par_end = 0;
491 while (par != endpar) {
492 ParagraphList::const_iterator lastpar = par;
493 // well we have to check if we are in an inset with unlimited
494 // length (all in one row) if that is true then we don't allow
495 // any special options in the paragraph and also we don't allow
496 // any environment other then "Standard" to be valid!
497 if (!par->forceDefaultParagraphs()) {
498 LyXLayout_ptr const & layout = par->layout();
500 if (layout->intitle) {
502 lyxerr << "Error in latexParagraphs: You"
503 " should not mix title layouts"
504 " with normal ones." << endl;
505 } else if (!was_title) {
507 if (tclass.titletype() == TITLE_ENVIRONMENT) {
509 << tclass.titlename()
514 } else if (was_title && !already_title) {
515 if (tclass.titletype() == TITLE_ENVIRONMENT) {
516 os << "\\end{" << tclass.titlename()
520 os << "\\" << tclass.titlename()
524 already_title = true;
528 if (layout->is_environment) {
529 par = TeXOnePar(buf, paragraphs, par, os, texrow,
530 runparams, everypar);
531 } else if (layout->isEnvironment() ||
532 !par->params().leftIndent().zero())
534 par = TeXEnvironment(buf, paragraphs, par, os,
537 par = TeXOnePar(buf, paragraphs, par, os, texrow,
538 runparams, everypar);
541 par = TeXOnePar(buf, paragraphs, par, os, texrow,
542 runparams, everypar);
544 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
547 // It might be that we only have a title in this document
548 if (was_title && !already_title) {
549 if (tclass.titletype() == TITLE_ENVIRONMENT) {
550 os << "\\end{" << tclass.titlename()
554 os << "\\" << tclass.titlename()