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 "paragraph.h"
22 #include "paragraph_funcs.h"
23 #include "ParagraphParameters.h"
27 #include "insets/insetoptarg.h"
29 #include "support/lstrings.h"
34 using lyx::support::subst;
40 extern string bibitemWidest(Buffer const &);
45 ParagraphList::const_iterator
46 TeXEnvironment(Buffer const & buf,
47 ParagraphList const & paragraphs,
48 ParagraphList::const_iterator pit,
49 ostream & os, TexRow & texrow,
50 OutputParams const & runparams);
52 ParagraphList::const_iterator
53 TeXOnePar(Buffer const & buf,
54 ParagraphList const & paragraphs,
55 ParagraphList::const_iterator pit,
56 ostream & os, TexRow & texrow,
57 OutputParams const & runparams,
58 string const & everypar = string());
61 ParagraphList::const_iterator
62 TeXDeeper(Buffer const & buf,
63 ParagraphList const & paragraphs,
64 ParagraphList::const_iterator pit,
65 ostream & os, TexRow & texrow,
66 OutputParams const & runparams)
68 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
69 ParagraphList::const_iterator par = pit;
71 while (par != paragraphs.end() &&
72 par->params().depth() == pit->params().depth()) {
73 if (par->layout()->isEnvironment()) {
74 par = TeXEnvironment(buf, paragraphs, par,
75 os, texrow, runparams);
77 par = TeXOnePar(buf, paragraphs, par,
78 os, texrow, runparams);
81 lyxerr[Debug::LATEX] << "TeXDeeper...done " << &*par << endl;
87 ParagraphList::const_iterator
88 TeXEnvironment(Buffer const & buf,
89 ParagraphList const & paragraphs,
90 ParagraphList::const_iterator pit,
91 ostream & os, TexRow & texrow,
92 OutputParams const & runparams)
94 lyxerr[Debug::LATEX] << "TeXEnvironment... " << &*pit << endl;
96 BufferParams const & bparams = buf.params();
98 LyXLayout_ptr const & style = pit->layout();
100 Language const * language = pit->getParLanguage(bparams);
101 Language const * doc_language = bparams.language;
102 Language const * previous_language =
103 (pit != paragraphs.begin())
104 ? boost::prior(pit)->getParLanguage(bparams)
106 if (language->babel() != previous_language->babel()) {
108 if (!lyxrc.language_command_end.empty() &&
109 previous_language->babel() != doc_language->babel()) {
110 os << subst(lyxrc.language_command_end, "$$lang",
111 previous_language->babel())
116 if (lyxrc.language_command_end.empty() ||
117 language->babel() != doc_language->babel()) {
118 os << subst(lyxrc.language_command_begin, "$$lang",
125 bool leftindent_open = false;
126 if (!pit->params().leftIndent().zero()) {
127 os << "\\begin{LyXParagraphLeftIndent}{" <<
128 pit->params().leftIndent().asLatexString() << "}\n";
130 leftindent_open = true;
133 if (style->isEnvironment()) {
134 os << "\\begin{" << style->latexname() << '}';
135 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
136 os << "{" << pit->params().labelWidthString() << "}\n";
137 } else if (style->labeltype == LABEL_BIBLIO) {
139 os << "{" << bibitemWidest(buf) << "}\n";
141 os << style->latexparam() << '\n';
144 ParagraphList::const_iterator par = pit;
146 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
148 if (par == paragraphs.end()) {
149 // Make sure that the last paragraph is
150 // correctly terminated (because TeXOnePar does
151 // not add a \n in this case)
154 } else if (par->params().depth() > pit->params().depth()) {
155 if (par->layout()->isParagraph()) {
158 // How to handle this? (Lgb)
159 //&& !suffixIs(os, "\n\n")
161 // There should be at least one '\n' already
162 // but we need there to be two for Standard
163 // paragraphs that are depth-increment'ed to be
164 // output correctly. However, tables can
165 // also be paragraphs so don't adjust them.
168 // Will it ever harm to have one '\n' too
169 // many? i.e. that we sometimes will have
170 // three in a row. (Lgb)
174 par = TeXDeeper(buf, paragraphs, par, os, texrow,
177 } while (par != paragraphs.end()
178 && par->layout() == pit->layout()
179 && par->params().depth() == pit->params().depth()
180 && par->params().leftIndent() == pit->params().leftIndent());
182 if (style->isEnvironment()) {
183 os << "\\end{" << style->latexname() << "}\n";
187 if (leftindent_open) {
188 os << "\\end{LyXParagraphLeftIndent}\n";
192 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << &*par << endl;
193 return par; // ale970302
197 InsetOptArg * optArgInset(Paragraph const & par)
200 InsetList::const_iterator it = par.insetlist.begin();
201 InsetList::const_iterator end = par.insetlist.end();
202 for (; it != end; ++it) {
203 InsetBase * ins = it->inset;
204 if (ins->lyxCode() == InsetBase::OPTARG_CODE) {
205 return static_cast<InsetOptArg *>(ins);
212 ParagraphList::const_iterator
213 TeXOnePar(Buffer const & buf,
214 ParagraphList const & paragraphs,
215 ParagraphList::const_iterator pit,
216 ostream & os, TexRow & texrow,
217 OutputParams const & runparams,
218 string const & everypar)
220 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
221 << everypar << "'" << endl;
222 BufferParams const & bparams = buf.params();
223 bool further_blank_line = false;
226 // In an an inset with unlimited length (all in one row),
227 // force layout to default
228 if (!pit->forceDefaultParagraphs())
229 style = pit->layout();
231 style = bparams.getLyXTextClass().defaultLayout();
233 Language const * language = pit->getParLanguage(bparams);
234 Language const * doc_language = bparams.language;
235 Language const * previous_language =
236 (pit != paragraphs.begin())
237 ? boost::prior(pit)->getParLanguage(bparams)
240 if (language->babel() != previous_language->babel()
241 // check if we already put language command in TeXEnvironment()
242 && !(style->isEnvironment()
243 && (pit == paragraphs.begin() ||
244 (boost::prior(pit)->layout() != pit->layout() &&
245 boost::prior(pit)->getDepth() <= pit->getDepth())
246 || boost::prior(pit)->getDepth() < pit->getDepth())))
248 if (!lyxrc.language_command_end.empty() &&
249 previous_language->babel() != doc_language->babel())
251 os << subst(lyxrc.language_command_end, "$$lang",
252 previous_language->babel())
257 if (lyxrc.language_command_end.empty() ||
258 language->babel() != doc_language->babel())
260 os << subst(lyxrc.language_command_begin, "$$lang",
267 if (bparams.inputenc == "auto" &&
268 language->encoding() != previous_language->encoding()) {
269 os << "\\inputencoding{"
270 << language->encoding()->LatexName()
275 // In an an inset with unlimited length (all in one row),
276 // don't allow any special options in the paragraph
277 if (!pit->forceDefaultParagraphs()) {
278 if (pit->params().startOfAppendix()) {
279 os << "\\appendix\n";
283 if (!pit->params().spacing().isDefault()
284 && (pit == paragraphs.begin()
285 || !boost::prior(pit)->hasSameLayout(*pit)))
287 os << pit->params().spacing().writeEnvirBegin() << '\n';
291 if (style->isCommand()) {
296 if (further_blank_line) {
302 switch (style->latextype) {
304 os << '\\' << style->latexname();
306 // Separate handling of optional argument inset.
307 if (style->optionalargs == 1) {
308 InsetOptArg * it = optArgInset(*pit);
310 it->latexOptional(buf, os, runparams);
313 os << style->latexparam();
315 case LATEX_ITEM_ENVIRONMENT:
316 case LATEX_LIST_ENVIRONMENT:
319 case LATEX_BIB_ENVIRONMENT:
320 // ignore this, the inset will write itself
327 bool need_par = pit->simpleTeXOnePar(buf, bparams,
328 outerFont(pit - paragraphs.begin(), paragraphs),
329 os, texrow, runparams);
331 // Make sure that \\par is done with the font of the last
332 // character if this has another size as the default.
333 // This is necessary because LaTeX (and LyX on the screen)
334 // calculates the space between the baselines according
335 // to this font. (Matthias)
337 // Is this really needed ? (Dekel)
338 // We do not need to use to change the font for the last paragraph
340 LyXFont const outerfont =
341 outerFont(pit - paragraphs.begin(),
346 ? pit->getLayoutFont(bparams, outerfont)
347 : pit->getFont(bparams, pit->size() - 1, outerfont));
349 bool is_command = style->isCommand();
351 if (style->resfont.size() != font.size()
352 && boost::next(pit) != paragraphs.end()
356 os << "\\" << font.latexSize() << " \\par}";
357 } else if (need_par) {
359 } else if (is_command)
362 switch (style->latextype) {
363 case LATEX_ITEM_ENVIRONMENT:
364 case LATEX_LIST_ENVIRONMENT:
365 if (boost::next(pit) != paragraphs.end()
366 && (pit->params().depth() < boost::next(pit)->params().depth())) {
371 case LATEX_ENVIRONMENT: {
372 // if its the last paragraph of the current environment
373 // skip it otherwise fall through
374 ParagraphList::const_iterator next = boost::next(pit);
376 if (next != paragraphs.end()
377 && (next->layout() != pit->layout()
378 || next->params().depth() != pit->params().depth()))
382 // fall through possible
384 // we don't need it for the last paragraph!!!
385 if (boost::next(pit) != paragraphs.end()) {
391 if (!pit->forceDefaultParagraphs()) {
392 further_blank_line = false;
394 if (further_blank_line) {
399 if (!pit->params().spacing().isDefault()
400 && (boost::next(pit) == paragraphs.end()
401 || !boost::next(pit)->hasSameLayout(*pit)))
403 os << pit->params().spacing().writeEnvirEnd() << '\n';
408 if (boost::next(pit) == const_cast<ParagraphList&>(paragraphs).end()
409 && language->babel() != doc_language->babel()) {
410 // Since \selectlanguage write the language to the aux file,
411 // we need to reset the language at the end of footnote or
414 if (lyxrc.language_command_end.empty())
415 os << subst(lyxrc.language_command_begin,
417 doc_language->babel())
420 os << subst(lyxrc.language_command_end,
427 // we don't need it for the last paragraph!!!
428 // Note from JMarc: we will re-add a \n explicitely in
429 // TeXEnvironment, because it is needed in this case
430 if (boost::next(pit) != paragraphs.end()) {
435 lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;
442 // LaTeX all paragraphs
443 void latexParagraphs(Buffer const & buf,
444 ParagraphList const & paragraphs,
447 OutputParams const & runparams,
448 string const & everypar)
450 bool was_title = false;
451 bool already_title = false;
452 LyXTextClass const & tclass = buf.params().getLyXTextClass();
453 ParagraphList::const_iterator par = paragraphs.begin();
454 ParagraphList::const_iterator endpar = paragraphs.end();
457 while (par != endpar) {
458 // well we have to check if we are in an inset with unlimited
459 // length (all in one row) if that is true then we don't allow
460 // any special options in the paragraph and also we don't allow
461 // any environment other then "Standard" to be valid!
462 if (!par->forceDefaultParagraphs()) {
463 LyXLayout_ptr const & layout = par->layout();
465 if (layout->intitle) {
467 lyxerr << "Error in latexParagraphs: You"
468 " should not mix title layouts"
469 " with normal ones." << endl;
470 } else if (!was_title) {
472 if (tclass.titletype() == TITLE_ENVIRONMENT) {
474 << tclass.titlename()
479 } else if (was_title && !already_title) {
480 if (tclass.titletype() == TITLE_ENVIRONMENT) {
481 os << "\\end{" << tclass.titlename()
485 os << "\\" << tclass.titlename()
489 already_title = true;
493 if (layout->is_environment) {
494 par = TeXOnePar(buf, paragraphs, par, os, texrow,
495 runparams, everypar);
496 } else if (layout->isEnvironment() ||
497 !par->params().leftIndent().zero())
499 par = TeXEnvironment(buf, paragraphs, par, os,
502 par = TeXOnePar(buf, paragraphs, par, os, texrow,
503 runparams, everypar);
506 par = TeXOnePar(buf, paragraphs, par, os, texrow,
507 runparams, everypar);
510 // It might be that we only have a title in this document
511 if (was_title && !already_title) {
512 if (tclass.titletype() == TITLE_ENVIRONMENT) {
513 os << "\\end{" << tclass.titlename()
517 os << "\\" << tclass.titlename()