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/insetoptarg.h"
30 #include "support/lstrings.h"
35 using lyx::support::subst;
41 extern string bibitemWidest(Buffer const &);
46 ParagraphList::const_iterator
47 TeXEnvironment(Buffer const & buf,
48 ParagraphList const & paragraphs,
49 ParagraphList::const_iterator pit,
50 ostream & os, TexRow & texrow,
51 OutputParams const & runparams);
53 ParagraphList::const_iterator
54 TeXOnePar(Buffer const & buf,
55 ParagraphList const & paragraphs,
56 ParagraphList::const_iterator pit,
57 ostream & os, TexRow & texrow,
58 OutputParams const & runparams,
59 string const & everypar = string());
62 ParagraphList::const_iterator
63 TeXDeeper(Buffer const & buf,
64 ParagraphList const & paragraphs,
65 ParagraphList::const_iterator pit,
66 ostream & os, TexRow & texrow,
67 OutputParams const & runparams)
69 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
70 ParagraphList::const_iterator par = pit;
72 while (par != paragraphs.end() &&
73 par->params().depth() == pit->params().depth()) {
74 if (par->layout()->isEnvironment()) {
75 par = TeXEnvironment(buf, paragraphs, par,
76 os, texrow, runparams);
78 par = TeXOnePar(buf, paragraphs, par,
79 os, texrow, runparams);
82 lyxerr[Debug::LATEX] << "TeXDeeper...done " << &*par << endl;
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->latextype == LATEX_LIST_ENVIRONMENT) {
137 os << "{" << pit->params().labelWidthString() << "}\n";
138 } else if (style->labeltype == LABEL_BIBLIO) {
140 os << "{" << bibitemWidest(buf) << "}\n";
142 os << style->latexparam() << '\n';
145 ParagraphList::const_iterator par = pit;
147 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
149 if (par == paragraphs.end()) {
150 // Make sure that the last paragraph is
151 // correctly terminated (because TeXOnePar does
152 // not add a \n in this case)
155 } else if (par->params().depth() > pit->params().depth()) {
156 if (par->layout()->isParagraph()) {
159 // How to handle this? (Lgb)
160 //&& !suffixIs(os, "\n\n")
162 // There should be at least one '\n' already
163 // but we need there to be two for Standard
164 // paragraphs that are depth-increment'ed to be
165 // output correctly. However, tables can
166 // also be paragraphs so don't adjust them.
169 // Will it ever harm to have one '\n' too
170 // many? i.e. that we sometimes will have
171 // three in a row. (Lgb)
175 par = TeXDeeper(buf, paragraphs, par, os, texrow,
178 } while (par != paragraphs.end()
179 && par->layout() == pit->layout()
180 && par->params().depth() == pit->params().depth()
181 && par->params().leftIndent() == pit->params().leftIndent());
183 if (style->isEnvironment()) {
184 os << "\\end{" << style->latexname() << "}\n";
188 if (leftindent_open) {
189 os << "\\end{LyXParagraphLeftIndent}\n";
193 if (par != paragraphs.end() && lyxerr.debugging(Debug::LATEX))
194 lyxerr << "TeXEnvironment...done " << &*par << endl;
199 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
200 ostream & os, OutputParams const & runparams, int number)
204 InsetList::const_iterator it = par.insetlist.begin();
205 InsetList::const_iterator end = par.insetlist.end();
206 for (; it != end && number > 0 ; ++it) {
207 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) {
209 static_cast<InsetOptArg *>(it->inset);
210 lines += ins->latexOptional(buf, os, runparams);
218 ParagraphList::const_iterator
219 TeXOnePar(Buffer const & buf,
220 ParagraphList const & paragraphs,
221 ParagraphList::const_iterator pit,
222 ostream & os, TexRow & texrow,
223 OutputParams const & runparams_in,
224 string const & everypar)
226 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
227 << everypar << "'" << endl;
228 BufferParams const & bparams = buf.params();
229 bool further_blank_line = false;
232 // In an an inset with unlimited length (all in one row),
233 // force layout to default
234 if (!pit->forceDefaultParagraphs())
235 style = pit->layout();
237 style = bparams.getLyXTextClass().defaultLayout();
239 OutputParams runparams = runparams_in;
240 runparams.moving_arg |= style->needprotect;
242 Language const * language = pit->getParLanguage(bparams);
243 Language const * doc_language = bparams.language;
244 Language const * previous_language =
245 (pit != paragraphs.begin())
246 ? boost::prior(pit)->getParLanguage(bparams)
249 if (language->babel() != previous_language->babel()
250 // check if we already put language command in TeXEnvironment()
251 && !(style->isEnvironment()
252 && (pit == paragraphs.begin() ||
253 (boost::prior(pit)->layout() != pit->layout() &&
254 boost::prior(pit)->getDepth() <= pit->getDepth())
255 || boost::prior(pit)->getDepth() < pit->getDepth())))
257 if (!lyxrc.language_command_end.empty() &&
258 previous_language->babel() != doc_language->babel())
260 os << subst(lyxrc.language_command_end, "$$lang",
261 previous_language->babel())
266 if (lyxrc.language_command_end.empty() ||
267 language->babel() != doc_language->babel())
269 os << subst(lyxrc.language_command_begin, "$$lang",
276 if (bparams.inputenc == "auto" &&
277 language->encoding() != previous_language->encoding()) {
278 os << "\\inputencoding{"
279 << language->encoding()->LatexName()
284 // In an an inset with unlimited length (all in one row),
285 // don't allow any special options in the paragraph
286 if (!pit->forceDefaultParagraphs()) {
287 if (pit->params().startOfAppendix()) {
288 os << "\\appendix\n";
292 if (!pit->params().spacing().isDefault()
293 && (pit == paragraphs.begin()
294 || !boost::prior(pit)->hasSameLayout(*pit)))
296 os << pit->params().spacing().writeEnvirBegin() << '\n';
300 if (style->isCommand()) {
305 if (further_blank_line) {
311 switch (style->latextype) {
313 os << '\\' << style->latexname();
315 // Separate handling of optional argument inset.
316 if (style->optionalargs > 0) {
317 int ret = latexOptArgInsets(buf, *pit, os, runparams,
318 style->optionalargs);
325 os << style->latexparam();
327 case LATEX_ITEM_ENVIRONMENT:
328 case LATEX_LIST_ENVIRONMENT:
331 case LATEX_BIB_ENVIRONMENT:
332 // ignore this, the inset will write itself
339 bool need_par = pit->simpleTeXOnePar(buf, bparams,
340 outerFont(pit - paragraphs.begin(), paragraphs),
341 os, texrow, runparams);
343 // Make sure that \\par is done with the font of the last
344 // character if this has another size as the default.
345 // This is necessary because LaTeX (and LyX on the screen)
346 // calculates the space between the baselines according
347 // to this font. (Matthias)
349 // Is this really needed ? (Dekel)
350 // We do not need to use to change the font for the last paragraph
352 LyXFont const outerfont =
353 outerFont(pit - paragraphs.begin(),
358 ? pit->getLayoutFont(bparams, outerfont)
359 : pit->getFont(bparams, pit->size() - 1, outerfont));
361 bool is_command = style->isCommand();
363 if (style->resfont.size() != font.size()
364 && boost::next(pit) != paragraphs.end()
368 os << "\\" << font.latexSize() << " \\par}";
369 } else if (need_par) {
371 } else if (is_command)
374 switch (style->latextype) {
375 case LATEX_ITEM_ENVIRONMENT:
376 case LATEX_LIST_ENVIRONMENT:
377 if (boost::next(pit) != paragraphs.end()
378 && (pit->params().depth() < boost::next(pit)->params().depth())) {
383 case LATEX_ENVIRONMENT: {
384 // if its the last paragraph of the current environment
385 // skip it otherwise fall through
386 ParagraphList::const_iterator next = boost::next(pit);
388 if (next != paragraphs.end()
389 && (next->layout() != pit->layout()
390 || next->params().depth() != pit->params().depth()))
394 // fall through possible
396 // we don't need it for the last paragraph!!!
397 if (boost::next(pit) != paragraphs.end()) {
403 if (!pit->forceDefaultParagraphs()) {
404 further_blank_line = false;
406 if (further_blank_line) {
411 if (!pit->params().spacing().isDefault()
412 && (boost::next(pit) == paragraphs.end()
413 || !boost::next(pit)->hasSameLayout(*pit)))
415 os << pit->params().spacing().writeEnvirEnd() << '\n';
420 if (boost::next(pit) == paragraphs.end()
421 && language->babel() != doc_language->babel()) {
422 // Since \selectlanguage write the language to the aux file,
423 // we need to reset the language at the end of footnote or
426 if (lyxrc.language_command_end.empty())
427 os << subst(lyxrc.language_command_begin,
429 doc_language->babel())
432 os << subst(lyxrc.language_command_end,
439 // we don't need it for the last paragraph!!!
440 // Note from JMarc: we will re-add a \n explicitely in
441 // TeXEnvironment, because it is needed in this case
442 if (boost::next(pit) != paragraphs.end()) {
447 if (boost::next(pit) != paragraphs.end() &&
448 lyxerr.debugging(Debug::LATEX))
449 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
456 // LaTeX all paragraphs
457 void latexParagraphs(Buffer const & buf,
458 ParagraphList const & paragraphs,
461 OutputParams const & runparams,
462 string const & everypar)
464 bool was_title = false;
465 bool already_title = false;
466 LyXTextClass const & tclass = buf.params().getLyXTextClass();
467 ParagraphList::const_iterator par = paragraphs.begin();
468 ParagraphList::const_iterator endpar = paragraphs.end();
471 while (par != endpar) {
472 // well we have to check if we are in an inset with unlimited
473 // length (all in one row) if that is true then we don't allow
474 // any special options in the paragraph and also we don't allow
475 // any environment other then "Standard" to be valid!
476 if (!par->forceDefaultParagraphs()) {
477 LyXLayout_ptr const & layout = par->layout();
479 if (layout->intitle) {
481 lyxerr << "Error in latexParagraphs: You"
482 " should not mix title layouts"
483 " with normal ones." << endl;
484 } else if (!was_title) {
486 if (tclass.titletype() == TITLE_ENVIRONMENT) {
488 << tclass.titlename()
493 } else if (was_title && !already_title) {
494 if (tclass.titletype() == TITLE_ENVIRONMENT) {
495 os << "\\end{" << tclass.titlename()
499 os << "\\" << tclass.titlename()
503 already_title = true;
507 if (layout->is_environment) {
508 par = TeXOnePar(buf, paragraphs, par, os, texrow,
509 runparams, everypar);
510 } else if (layout->isEnvironment() ||
511 !par->params().leftIndent().zero())
513 par = TeXEnvironment(buf, paragraphs, par, os,
516 par = TeXOnePar(buf, paragraphs, par, os, texrow,
517 runparams, everypar);
520 par = TeXOnePar(buf, paragraphs, par, os, texrow,
521 runparams, everypar);
524 // It might be that we only have a title in this document
525 if (was_title && !already_title) {
526 if (tclass.titletype() == TITLE_ENVIRONMENT) {
527 os << "\\end{" << tclass.titlename()
531 os << "\\" << tclass.titlename()