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 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
135 os << "\\begin{" << style->latexname() << "}{"
136 << pit->params().labelWidthString() << "}\n";
137 } else if (style->labeltype == LABEL_BIBLIO) {
139 os << "\\begin{" << style->latexname() << "}{"
140 << bibitemWidest(buf)
142 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
143 os << "\\begin{" << style->latexname() << '}'
144 << style->latexparam() << '\n';
146 os << "\\begin{" << style->latexname() << '}'
147 << style->latexparam() << '\n';
150 ParagraphList::iterator par = pit;
152 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
154 if (par != const_cast<ParagraphList&>(paragraphs).end() && 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 InsetOld * ins = it->inset;
204 if (ins->lyxCode() == InsetOld::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();
224 InsetOld const * in = pit->inInset();
225 bool further_blank_line = false;
228 // well we have to check if we are in an inset with unlimited
229 // length (all in one row) if that is true then we don't allow
230 // any special options in the paragraph and also we don't allow
231 // any environment other then "Standard" to be valid!
232 if (in == 0 || !in->forceDefaultParagraphs(in)) {
233 style = pit->layout();
235 if (pit->params().startOfAppendix()) {
236 os << "\\appendix\n";
240 if (!pit->params().spacing().isDefault()
241 && (pit == const_cast<ParagraphList&>(paragraphs).begin()
242 || !boost::prior(pit)->hasSameLayout(*pit)))
244 os << pit->params().spacing().writeEnvirBegin() << '\n';
248 if (style->isCommand()) {
253 if (pit->params().spaceTop().kind() != VSpace::NONE) {
254 os << pit->params().spaceTop().asLatexCommand(bparams);
255 further_blank_line = true;
258 if (further_blank_line) {
263 style = bparams.getLyXTextClass().defaultLayout();
266 Language const * language = pit->getParLanguage(bparams);
267 Language const * doc_language = bparams.language;
268 Language const * previous_language =
269 (pit != const_cast<ParagraphList&>(paragraphs).begin())
270 ? boost::prior(pit)->getParLanguage(bparams)
273 if (language->babel() != previous_language->babel()
274 // check if we already put language command in TeXEnvironment()
275 && !(style->isEnvironment()
276 && (pit == const_cast<ParagraphList&>(paragraphs).begin() ||
277 (boost::prior(pit)->layout() != pit->layout() &&
278 boost::prior(pit)->getDepth() <= pit->getDepth())
279 || boost::prior(pit)->getDepth() < pit->getDepth())))
281 if (!lyxrc.language_command_end.empty() &&
282 previous_language->babel() != doc_language->babel())
284 os << subst(lyxrc.language_command_end, "$$lang",
285 previous_language->babel())
290 if (lyxrc.language_command_end.empty() ||
291 language->babel() != doc_language->babel())
293 os << subst(lyxrc.language_command_begin, "$$lang",
300 if (bparams.inputenc == "auto" &&
301 language->encoding() != previous_language->encoding()) {
302 os << "\\inputencoding{"
303 << language->encoding()->LatexName()
308 switch (style->latextype) {
310 os << '\\' << style->latexname();
312 // Separate handling of optional argument inset.
313 if (style->optionalargs == 1) {
314 InsetOptArg * it = optArgInset(*pit);
316 it->latexOptional(buf, os, runparams);
319 os << style->latexparam();
321 case LATEX_ITEM_ENVIRONMENT:
322 case LATEX_LIST_ENVIRONMENT:
325 case LATEX_BIB_ENVIRONMENT:
326 // ignore this, the inset will write itself
333 bool need_par = pit->simpleTeXOnePar(buf, bparams,
334 outerFont(pit, paragraphs),
335 os, texrow, runparams);
337 // Make sure that \\par is done with the font of the last
338 // character if this has another size as the default.
339 // This is necessary because LaTeX (and LyX on the screen)
340 // calculates the space between the baselines according
341 // to this font. (Matthias)
343 // Is this really needed ? (Dekel)
344 // We do not need to use to change the font for the last paragraph
346 LyXFont const outerfont(outerFont(pit, paragraphs));
350 ? pit->getLayoutFont(bparams, outerfont)
351 : pit->getFont(bparams, pit->size() - 1, outerfont));
353 bool is_command = style->isCommand();
355 if (style->resfont.size() != font.size()
356 && boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
360 os << "\\" << font.latexSize() << " \\par}";
361 } else if (need_par) {
363 } else if (is_command)
366 switch (style->latextype) {
367 case LATEX_ITEM_ENVIRONMENT:
368 case LATEX_LIST_ENVIRONMENT:
369 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
370 && (pit->params().depth() < boost::next(pit)->params().depth())) {
375 case LATEX_ENVIRONMENT: {
376 // if its the last paragraph of the current environment
377 // skip it otherwise fall through
378 ParagraphList::iterator next = boost::next(pit);
380 if (next != const_cast<ParagraphList&>(paragraphs).end()
381 && (next->layout() != pit->layout()
382 || next->params().depth() != pit->params().depth()))
386 // fall through possible
388 // we don't need it for the last paragraph!!!
389 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
395 if (in == 0 || !in->forceDefaultParagraphs(in)) {
396 further_blank_line = false;
398 if (pit->params().spaceBottom().kind() != VSpace::NONE) {
399 os << pit->params().spaceBottom().asLatexCommand(bparams);
400 further_blank_line = true;
403 if (further_blank_line) {
408 if (!pit->params().spacing().isDefault()
409 && (boost::next(pit) == const_cast<ParagraphList&>(paragraphs).end()
410 || !boost::next(pit)->hasSameLayout(*pit)))
412 os << pit->params().spacing().writeEnvirEnd() << '\n';
417 // we don't need it for the last paragraph!!!
418 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
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 (language->babel() != doc_language->babel()) {
427 if (lyxrc.language_command_end.empty())
428 os << subst(lyxrc.language_command_begin,
430 doc_language->babel())
433 os << subst(lyxrc.language_command_end,
441 lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;
448 // LaTeX all paragraphs from par to endpar, if endpar == 0 then to the end
450 void latexParagraphs(Buffer const & buf,
451 ParagraphList const & paragraphs,
454 OutputParams const & runparams,
455 string const & everypar)
457 bool was_title = false;
458 bool already_title = false;
459 LyXTextClass const & tclass = buf.params().getLyXTextClass();
460 ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
461 ParagraphList::iterator endpar = const_cast<ParagraphList&>(paragraphs).end();
464 while (par != endpar) {
465 InsetOld * in = par->inInset();
466 // well we have to check if we are in an inset with unlimited
467 // length (all in one row) if that is true then we don't allow
468 // any special options in the paragraph and also we don't allow
469 // any environment other then "Standard" to be valid!
470 if (in == 0 || !in->forceDefaultParagraphs(in)) {
471 LyXLayout_ptr const & layout = par->layout();
473 if (layout->intitle) {
475 lyxerr <<"Error in latexParagraphs: You"
476 " should not mix title layouts"
477 " with normal ones." << endl;
478 } else if (!was_title) {
480 if (tclass.titletype() == TITLE_ENVIRONMENT) {
482 << tclass.titlename()
487 } else if (was_title && !already_title) {
488 if (tclass.titletype() == TITLE_ENVIRONMENT) {
489 os << "\\end{" << tclass.titlename()
493 os << "\\" << tclass.titlename()
497 already_title = true;
501 if (layout->is_environment) {
502 par = TeXOnePar(buf, paragraphs, par, os, texrow,
503 runparams, everypar);
504 } else if (layout->isEnvironment() ||
505 !par->params().leftIndent().zero())
507 par = TeXEnvironment(buf, paragraphs, par, os,
510 par = TeXOnePar(buf, paragraphs, par, os, texrow,
511 runparams, everypar);
514 par = TeXOnePar(buf, paragraphs, par, os, texrow,
515 runparams, everypar);
518 // It might be that we only have a title in this document
519 if (was_title && !already_title) {
520 if (tclass.titletype() == TITLE_ENVIRONMENT) {
521 os << "\\end{" << tclass.titlename()
525 os << "\\" << tclass.titlename()