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) {
137 os << '{' << lyx::to_utf8(bibitemWidest(buf)) << "}\n";
139 os << style->latexparam() << '\n';
142 ParagraphList::const_iterator par = pit;
144 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
146 if (par == paragraphs.end()) {
147 // Make sure that the last paragraph is
148 // correctly terminated (because TeXOnePar does
149 // not add a \n in this case)
152 } else if (par->params().depth() > pit->params().depth()) {
153 if (par->layout()->isParagraph()) {
156 // How to handle this? (Lgb)
157 //&& !suffixIs(os, "\n\n")
159 // There should be at least one '\n' already
160 // but we need there to be two for Standard
161 // paragraphs that are depth-increment'ed to be
162 // output correctly. However, tables can
163 // also be paragraphs so don't adjust them.
166 // Will it ever harm to have one '\n' too
167 // many? i.e. that we sometimes will have
168 // three in a row. (Lgb)
172 par = TeXDeeper(buf, paragraphs, par, os, texrow,
175 } while (par != paragraphs.end()
176 && par->layout() == pit->layout()
177 && par->params().depth() == pit->params().depth()
178 && par->params().leftIndent() == pit->params().leftIndent());
180 if (style->isEnvironment()) {
181 os << "\\end{" << style->latexname() << "}\n";
185 if (leftindent_open) {
186 os << "\\end{LyXParagraphLeftIndent}\n";
190 if (par != paragraphs.end() && lyxerr.debugging(Debug::LATEX))
191 lyxerr << "TeXEnvironment...done " << &*par << endl;
196 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
197 ostream & os, OutputParams const & runparams, int number)
201 InsetList::const_iterator it = par.insetlist.begin();
202 InsetList::const_iterator end = par.insetlist.end();
203 for (; it != end && number > 0 ; ++it) {
204 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) {
206 static_cast<InsetOptArg *>(it->inset);
207 lines += ins->latexOptional(buf, os, runparams);
215 ParagraphList::const_iterator
216 TeXOnePar(Buffer const & buf,
217 ParagraphList const & paragraphs,
218 ParagraphList::const_iterator pit,
219 ostream & os, TexRow & texrow,
220 OutputParams const & runparams_in,
221 string const & everypar)
223 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
224 << everypar << "'" << endl;
225 BufferParams const & bparams = buf.params();
226 bool further_blank_line = false;
229 // In an an inset with unlimited length (all in one row),
230 // force layout to default
231 if (!pit->forceDefaultParagraphs())
232 style = pit->layout();
234 style = bparams.getLyXTextClass().defaultLayout();
236 OutputParams runparams = runparams_in;
237 runparams.moving_arg |= style->needprotect;
239 Language const * language = pit->getParLanguage(bparams);
240 Language const * doc_language = bparams.language;
241 Language const * previous_language =
242 (pit != paragraphs.begin())
243 ? boost::prior(pit)->getParLanguage(bparams)
246 if (language->babel() != previous_language->babel()
247 // check if we already put language command in TeXEnvironment()
248 && !(style->isEnvironment()
249 && (pit == paragraphs.begin() ||
250 (boost::prior(pit)->layout() != pit->layout() &&
251 boost::prior(pit)->getDepth() <= pit->getDepth())
252 || boost::prior(pit)->getDepth() < pit->getDepth())))
254 if (!lyxrc.language_command_end.empty() &&
255 previous_language->babel() != doc_language->babel())
257 os << subst(lyxrc.language_command_end, "$$lang",
258 previous_language->babel())
263 if (lyxrc.language_command_end.empty() ||
264 language->babel() != doc_language->babel())
266 os << subst(lyxrc.language_command_begin, "$$lang",
273 if (bparams.inputenc == "auto" &&
274 language->encoding() != previous_language->encoding()) {
275 os << "\\inputencoding{"
276 << language->encoding()->latexName()
281 // In an an inset with unlimited length (all in one row),
282 // don't allow any special options in the paragraph
283 if (!pit->forceDefaultParagraphs()) {
284 if (pit->params().startOfAppendix()) {
285 os << "\\appendix\n";
289 if (!pit->params().spacing().isDefault()
290 && (pit == paragraphs.begin()
291 || !boost::prior(pit)->hasSameLayout(*pit)))
293 os << pit->params().spacing().writeEnvirBegin() << '\n';
297 if (style->isCommand()) {
302 if (further_blank_line) {
308 switch (style->latextype) {
310 os << '\\' << style->latexname();
312 // Separate handling of optional argument inset.
313 if (style->optionalargs > 0) {
314 int ret = latexOptArgInsets(buf, *pit, os, runparams,
315 style->optionalargs);
322 os << style->latexparam();
324 case LATEX_ITEM_ENVIRONMENT:
325 case LATEX_LIST_ENVIRONMENT:
328 case LATEX_BIB_ENVIRONMENT:
329 // ignore this, the inset will write itself
336 bool need_par = pit->simpleTeXOnePar(buf, bparams,
337 outerFont(std::distance(paragraphs.begin(), pit), paragraphs),
338 os, texrow, runparams);
340 // Make sure that \\par is done with the font of the last
341 // character if this has another size as the default.
342 // This is necessary because LaTeX (and LyX on the screen)
343 // calculates the space between the baselines according
344 // to this font. (Matthias)
346 // Is this really needed ? (Dekel)
347 // We do not need to use to change the font for the last paragraph
349 LyXFont const outerfont =
350 outerFont(std::distance(paragraphs.begin(), pit),
355 ? pit->getLayoutFont(bparams, outerfont)
356 : pit->getFont(bparams, pit->size() - 1, outerfont));
358 bool is_command = style->isCommand();
360 if (style->resfont.size() != font.size()
361 && boost::next(pit) != paragraphs.end()
365 os << "\\" << font.latexSize() << " \\par}";
366 } else if (need_par) {
368 } else if (is_command)
371 switch (style->latextype) {
372 case LATEX_ITEM_ENVIRONMENT:
373 case LATEX_LIST_ENVIRONMENT:
374 if (boost::next(pit) != paragraphs.end()
375 && (pit->params().depth() < boost::next(pit)->params().depth())) {
380 case LATEX_ENVIRONMENT: {
381 // if its the last paragraph of the current environment
382 // skip it otherwise fall through
383 ParagraphList::const_iterator next = boost::next(pit);
385 if (next != paragraphs.end()
386 && (next->layout() != pit->layout()
387 || next->params().depth() != pit->params().depth()))
391 // fall through possible
393 // we don't need it for the last paragraph!!!
394 if (boost::next(pit) != paragraphs.end()) {
400 if (!pit->forceDefaultParagraphs()) {
401 further_blank_line = false;
403 if (further_blank_line) {
408 if (!pit->params().spacing().isDefault()
409 && (boost::next(pit) == paragraphs.end()
410 || !boost::next(pit)->hasSameLayout(*pit)))
412 os << pit->params().spacing().writeEnvirEnd() << '\n';
417 if (boost::next(pit) == paragraphs.end()
418 && language->babel() != doc_language->babel()) {
419 // Since \selectlanguage write the language to the aux file,
420 // we need to reset the language at the end of footnote or
423 if (lyxrc.language_command_end.empty())
424 os << subst(lyxrc.language_command_begin,
426 doc_language->babel())
429 os << subst(lyxrc.language_command_end,
436 // we don't need it for the last paragraph!!!
437 // Note from JMarc: we will re-add a \n explicitely in
438 // TeXEnvironment, because it is needed in this case
439 if (boost::next(pit) != paragraphs.end()) {
444 if (boost::next(pit) != paragraphs.end() &&
445 lyxerr.debugging(Debug::LATEX))
446 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
453 // LaTeX all paragraphs
454 void latexParagraphs(Buffer const & buf,
455 ParagraphList const & paragraphs,
458 OutputParams const & runparams,
459 string const & everypar)
461 bool was_title = false;
462 bool already_title = false;
463 LyXTextClass const & tclass = buf.params().getLyXTextClass();
464 ParagraphList::const_iterator par = paragraphs.begin();
465 ParagraphList::const_iterator endpar = paragraphs.end();
467 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
468 // if only part of the paragraphs will be outputed
469 if (runparams.par_begin != runparams.par_end) {
470 par = boost::next(paragraphs.begin(), runparams.par_begin);
471 endpar = boost::next(paragraphs.begin(), runparams.par_end);
472 // runparams will be passed to nested paragraphs, so
473 // we have to reset the range parameters.
474 const_cast<OutputParams&>(runparams).par_begin = 0;
475 const_cast<OutputParams&>(runparams).par_end = 0;
479 while (par != endpar) {
480 ParagraphList::const_iterator lastpar = par;
481 // well we have to check if we are in an inset with unlimited
482 // length (all in one row) if that is true then we don't allow
483 // any special options in the paragraph and also we don't allow
484 // any environment other then "Standard" to be valid!
485 if (!par->forceDefaultParagraphs()) {
486 LyXLayout_ptr const & layout = par->layout();
488 if (layout->intitle) {
490 lyxerr << "Error in latexParagraphs: You"
491 " should not mix title layouts"
492 " with normal ones." << endl;
493 } else if (!was_title) {
495 if (tclass.titletype() == TITLE_ENVIRONMENT) {
497 << tclass.titlename()
502 } else if (was_title && !already_title) {
503 if (tclass.titletype() == TITLE_ENVIRONMENT) {
504 os << "\\end{" << tclass.titlename()
508 os << "\\" << tclass.titlename()
512 already_title = true;
516 if (layout->is_environment) {
517 par = TeXOnePar(buf, paragraphs, par, os, texrow,
518 runparams, everypar);
519 } else if (layout->isEnvironment() ||
520 !par->params().leftIndent().zero())
522 par = TeXEnvironment(buf, paragraphs, par, os,
525 par = TeXOnePar(buf, paragraphs, par, os, texrow,
526 runparams, everypar);
529 par = TeXOnePar(buf, paragraphs, par, os, texrow,
530 runparams, everypar);
532 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
535 // It might be that we only have a title in this document
536 if (was_title && !already_title) {
537 if (tclass.titletype() == TITLE_ENVIRONMENT) {
538 os << "\\end{" << tclass.titlename()
542 os << "\\" << tclass.titlename()