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::iterator
46 TeXEnvironment(Buffer const & buf,
47 ParagraphList const & paragraphs,
48 ParagraphList::iterator pit,
49 ostream & os, TexRow & texrow,
50 OutputParams const & runparams);
52 ParagraphList::iterator
53 TeXOnePar(Buffer const & buf,
54 ParagraphList const & paragraphs,
55 ParagraphList::iterator pit,
56 ostream & os, TexRow & texrow,
57 OutputParams const & runparams,
58 string const & everypar = string());
61 ParagraphList::iterator
62 TeXDeeper(Buffer const & buf,
63 ParagraphList const & paragraphs,
64 ParagraphList::iterator pit,
65 ostream & os, TexRow & texrow,
66 OutputParams const & runparams)
68 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
69 ParagraphList::iterator par = pit;
71 while (par != const_cast<ParagraphList&>(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::iterator
88 TeXEnvironment(Buffer const & buf,
89 ParagraphList const & paragraphs,
90 ParagraphList::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 != const_cast<ParagraphList&>(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::iterator par = pit;
146 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
148 if (par == const_cast<ParagraphList&>(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 != const_cast<ParagraphList&>(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::iterator
213 TeXOnePar(Buffer const & buf,
214 ParagraphList const & paragraphs,
215 ParagraphList::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 // well we have to check if we are in an inset with unlimited
227 // length (all in one row) if that is true then we don't allow
228 // any special options in the paragraph and also we don't allow
229 // any environment other then "Standard" to be valid!
230 if (!pit->forceDefaultParagraphs()) {
231 style = pit->layout();
233 if (pit->params().startOfAppendix()) {
234 os << "\\appendix\n";
238 if (!pit->params().spacing().isDefault()
239 && (pit == const_cast<ParagraphList&>(paragraphs).begin()
240 || !boost::prior(pit)->hasSameLayout(*pit)))
242 os << pit->params().spacing().writeEnvirBegin() << '\n';
246 if (style->isCommand()) {
251 if (further_blank_line) {
256 style = bparams.getLyXTextClass().defaultLayout();
259 Language const * language = pit->getParLanguage(bparams);
260 Language const * doc_language = bparams.language;
261 Language const * previous_language =
262 (pit != const_cast<ParagraphList&>(paragraphs).begin())
263 ? boost::prior(pit)->getParLanguage(bparams)
266 if (language->babel() != previous_language->babel()
267 // check if we already put language command in TeXEnvironment()
268 && !(style->isEnvironment()
269 && (pit == const_cast<ParagraphList&>(paragraphs).begin() ||
270 (boost::prior(pit)->layout() != pit->layout() &&
271 boost::prior(pit)->getDepth() <= pit->getDepth())
272 || boost::prior(pit)->getDepth() < pit->getDepth())))
274 if (!lyxrc.language_command_end.empty() &&
275 previous_language->babel() != doc_language->babel())
277 os << subst(lyxrc.language_command_end, "$$lang",
278 previous_language->babel())
283 if (lyxrc.language_command_end.empty() ||
284 language->babel() != doc_language->babel())
286 os << subst(lyxrc.language_command_begin, "$$lang",
293 if (bparams.inputenc == "auto" &&
294 language->encoding() != previous_language->encoding()) {
295 os << "\\inputencoding{"
296 << language->encoding()->LatexName()
301 switch (style->latextype) {
303 os << '\\' << style->latexname();
305 // Separate handling of optional argument inset.
306 if (style->optionalargs == 1) {
307 InsetOptArg * it = optArgInset(*pit);
309 it->latexOptional(buf, os, runparams);
312 os << style->latexparam();
314 case LATEX_ITEM_ENVIRONMENT:
315 case LATEX_LIST_ENVIRONMENT:
318 case LATEX_BIB_ENVIRONMENT:
319 // ignore this, the inset will write itself
326 bool need_par = pit->simpleTeXOnePar(buf, bparams,
327 outerFont(pit - const_cast<ParagraphList&>(paragraphs).begin(), paragraphs),
328 os, texrow, runparams);
330 // Make sure that \\par is done with the font of the last
331 // character if this has another size as the default.
332 // This is necessary because LaTeX (and LyX on the screen)
333 // calculates the space between the baselines according
334 // to this font. (Matthias)
336 // Is this really needed ? (Dekel)
337 // We do not need to use to change the font for the last paragraph
339 LyXFont const outerfont =
340 outerFont(pit - const_cast<ParagraphList&>(paragraphs).begin(),
345 ? pit->getLayoutFont(bparams, outerfont)
346 : pit->getFont(bparams, pit->size() - 1, outerfont));
348 bool is_command = style->isCommand();
350 if (style->resfont.size() != font.size()
351 && boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
355 os << "\\" << font.latexSize() << " \\par}";
356 } else if (need_par) {
358 } else if (is_command)
361 switch (style->latextype) {
362 case LATEX_ITEM_ENVIRONMENT:
363 case LATEX_LIST_ENVIRONMENT:
364 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
365 && (pit->params().depth() < boost::next(pit)->params().depth())) {
370 case LATEX_ENVIRONMENT: {
371 // if its the last paragraph of the current environment
372 // skip it otherwise fall through
373 ParagraphList::iterator next = boost::next(pit);
375 if (next != const_cast<ParagraphList&>(paragraphs).end()
376 && (next->layout() != pit->layout()
377 || next->params().depth() != pit->params().depth()))
381 // fall through possible
383 // we don't need it for the last paragraph!!!
384 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
390 if (boost::next(pit) == const_cast<ParagraphList&>(paragraphs).end()
391 && language->babel() != doc_language->babel()) {
392 // Since \selectlanguage write the language to the aux file,
393 // we need to reset the language at the end of footnote or
396 if (lyxrc.language_command_end.empty())
397 os << subst(lyxrc.language_command_begin,
399 doc_language->babel())
402 os << subst(lyxrc.language_command_end,
409 if (!pit->forceDefaultParagraphs()) {
410 further_blank_line = false;
412 if (further_blank_line) {
417 if (!pit->params().spacing().isDefault()
418 && (boost::next(pit) == const_cast<ParagraphList&>(paragraphs).end()
419 || !boost::next(pit)->hasSameLayout(*pit)))
421 os << pit->params().spacing().writeEnvirEnd() << '\n';
426 // we don't need it for the last paragraph!!!
427 // Note from JMarc: we will re-add a \n explicitely in
428 // TeXEnvironment, because it is needed in this case
429 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
434 lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;
441 // LaTeX all paragraphs
442 void latexParagraphs(Buffer const & buf,
443 ParagraphList const & paragraphs,
446 OutputParams const & runparams,
447 string const & everypar)
449 bool was_title = false;
450 bool already_title = false;
451 LyXTextClass const & tclass = buf.params().getLyXTextClass();
452 ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
453 ParagraphList::iterator endpar = const_cast<ParagraphList&>(paragraphs).end();
456 while (par != endpar) {
457 // well we have to check if we are in an inset with unlimited
458 // length (all in one row) if that is true then we don't allow
459 // any special options in the paragraph and also we don't allow
460 // any environment other then "Standard" to be valid!
461 if (!par->forceDefaultParagraphs()) {
462 LyXLayout_ptr const & layout = par->layout();
464 if (layout->intitle) {
466 lyxerr << "Error in latexParagraphs: You"
467 " should not mix title layouts"
468 " with normal ones." << endl;
469 } else if (!was_title) {
471 if (tclass.titletype() == TITLE_ENVIRONMENT) {
473 << tclass.titlename()
478 } else if (was_title && !already_title) {
479 if (tclass.titletype() == TITLE_ENVIRONMENT) {
480 os << "\\end{" << tclass.titlename()
484 os << "\\" << tclass.titlename()
488 already_title = true;
492 if (layout->is_environment) {
493 par = TeXOnePar(buf, paragraphs, par, os, texrow,
494 runparams, everypar);
495 } else if (layout->isEnvironment() ||
496 !par->params().leftIndent().zero())
498 par = TeXEnvironment(buf, paragraphs, par, os,
501 par = TeXOnePar(buf, paragraphs, par, os, texrow,
502 runparams, everypar);
505 par = TeXOnePar(buf, paragraphs, par, os, texrow,
506 runparams, everypar);
509 // It might be that we only have a title in this document
510 if (was_title && !already_title) {
511 if (tclass.titletype() == TITLE_ENVIRONMENT) {
512 os << "\\end{" << tclass.titlename()
516 os << "\\" << tclass.titlename()