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() && par->params().depth() > pit->params().depth()) {
149 if (par->layout()->isParagraph()) {
152 // How to handle this? (Lgb)
153 //&& !suffixIs(os, "\n\n")
155 // There should be at least one '\n' already
156 // but we need there to be two for Standard
157 // paragraphs that are depth-increment'ed to be
158 // output correctly. However, tables can
159 // also be paragraphs so don't adjust them.
162 // Will it ever harm to have one '\n' too
163 // many? i.e. that we sometimes will have
164 // three in a row. (Lgb)
168 par = TeXDeeper(buf, paragraphs, par, os, texrow,
171 } while (par != const_cast<ParagraphList&>(paragraphs).end()
172 && par->layout() == pit->layout()
173 && par->params().depth() == pit->params().depth()
174 && par->params().leftIndent() == pit->params().leftIndent());
176 if (style->isEnvironment()) {
177 os << "\\end{" << style->latexname() << "}\n";
181 if (leftindent_open) {
182 os << "\\end{LyXParagraphLeftIndent}\n";
186 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << &*par << endl;
187 return par; // ale970302
191 InsetOptArg * optArgInset(Paragraph const & par)
194 InsetList::const_iterator it = par.insetlist.begin();
195 InsetList::const_iterator end = par.insetlist.end();
196 for (; it != end; ++it) {
197 InsetBase * ins = it->inset;
198 if (ins->lyxCode() == InsetBase::OPTARG_CODE) {
199 return static_cast<InsetOptArg *>(ins);
206 ParagraphList::iterator
207 TeXOnePar(Buffer const & buf,
208 ParagraphList const & paragraphs,
209 ParagraphList::iterator pit,
210 ostream & os, TexRow & texrow,
211 OutputParams const & runparams,
212 string const & everypar)
214 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
215 << everypar << "'" << endl;
216 BufferParams const & bparams = buf.params();
218 InsetOld const * in = pit->inInset();
219 bool further_blank_line = false;
222 // well we have to check if we are in an inset with unlimited
223 // length (all in one row) if that is true then we don't allow
224 // any special options in the paragraph and also we don't allow
225 // any environment other then "Standard" to be valid!
226 if (in == 0 || !in->forceDefaultParagraphs(in)) {
227 style = pit->layout();
229 if (pit->params().startOfAppendix()) {
230 os << "\\appendix\n";
234 if (!pit->params().spacing().isDefault()
235 && (pit == const_cast<ParagraphList&>(paragraphs).begin()
236 || !boost::prior(pit)->hasSameLayout(*pit)))
238 os << pit->params().spacing().writeEnvirBegin() << '\n';
242 if (style->isCommand()) {
247 if (further_blank_line) {
252 style = bparams.getLyXTextClass().defaultLayout();
255 Language const * language = pit->getParLanguage(bparams);
256 Language const * doc_language = bparams.language;
257 Language const * previous_language =
258 (pit != const_cast<ParagraphList&>(paragraphs).begin())
259 ? boost::prior(pit)->getParLanguage(bparams)
262 if (language->babel() != previous_language->babel()
263 // check if we already put language command in TeXEnvironment()
264 && !(style->isEnvironment()
265 && (pit == const_cast<ParagraphList&>(paragraphs).begin() ||
266 (boost::prior(pit)->layout() != pit->layout() &&
267 boost::prior(pit)->getDepth() <= pit->getDepth())
268 || boost::prior(pit)->getDepth() < pit->getDepth())))
270 if (!lyxrc.language_command_end.empty() &&
271 previous_language->babel() != doc_language->babel())
273 os << subst(lyxrc.language_command_end, "$$lang",
274 previous_language->babel())
279 if (lyxrc.language_command_end.empty() ||
280 language->babel() != doc_language->babel())
282 os << subst(lyxrc.language_command_begin, "$$lang",
289 if (bparams.inputenc == "auto" &&
290 language->encoding() != previous_language->encoding()) {
291 os << "\\inputencoding{"
292 << language->encoding()->LatexName()
297 switch (style->latextype) {
299 os << '\\' << style->latexname();
301 // Separate handling of optional argument inset.
302 if (style->optionalargs == 1) {
303 InsetOptArg * it = optArgInset(*pit);
305 it->latexOptional(buf, os, runparams);
308 os << style->latexparam();
310 case LATEX_ITEM_ENVIRONMENT:
311 case LATEX_LIST_ENVIRONMENT:
314 case LATEX_BIB_ENVIRONMENT:
315 // ignore this, the inset will write itself
322 bool need_par = pit->simpleTeXOnePar(buf, bparams,
323 outerFont(pit - const_cast<ParagraphList&>(paragraphs).begin(), paragraphs),
324 os, texrow, runparams);
326 // Make sure that \\par is done with the font of the last
327 // character if this has another size as the default.
328 // This is necessary because LaTeX (and LyX on the screen)
329 // calculates the space between the baselines according
330 // to this font. (Matthias)
332 // Is this really needed ? (Dekel)
333 // We do not need to use to change the font for the last paragraph
335 LyXFont const outerfont =
336 outerFont(pit - const_cast<ParagraphList&>(paragraphs).begin(),
341 ? pit->getLayoutFont(bparams, outerfont)
342 : pit->getFont(bparams, pit->size() - 1, outerfont));
344 bool is_command = style->isCommand();
346 if (style->resfont.size() != font.size()
347 && boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
351 os << "\\" << font.latexSize() << " \\par}";
352 } else if (need_par) {
354 } else if (is_command)
357 switch (style->latextype) {
358 case LATEX_ITEM_ENVIRONMENT:
359 case LATEX_LIST_ENVIRONMENT:
360 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
361 && (pit->params().depth() < boost::next(pit)->params().depth())) {
366 case LATEX_ENVIRONMENT: {
367 // if its the last paragraph of the current environment
368 // skip it otherwise fall through
369 ParagraphList::iterator next = boost::next(pit);
371 if (next != const_cast<ParagraphList&>(paragraphs).end()
372 && (next->layout() != pit->layout()
373 || next->params().depth() != pit->params().depth()))
377 // fall through possible
379 // we don't need it for the last paragraph!!!
380 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
386 if (in == 0 || !in->forceDefaultParagraphs(in)) {
387 further_blank_line = false;
389 if (further_blank_line) {
394 if (!pit->params().spacing().isDefault()
395 && (boost::next(pit) == const_cast<ParagraphList&>(paragraphs).end()
396 || !boost::next(pit)->hasSameLayout(*pit)))
398 os << pit->params().spacing().writeEnvirEnd() << '\n';
403 // we don't need it for the last paragraph!!!
404 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
408 // Since \selectlanguage write the language to the aux file,
409 // we need to reset the language at the end of footnote or
412 if (language->babel() != doc_language->babel()) {
413 if (lyxrc.language_command_end.empty())
414 os << subst(lyxrc.language_command_begin,
416 doc_language->babel())
419 os << subst(lyxrc.language_command_end,
427 lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;
434 // LaTeX all paragraphs
435 void latexParagraphs(Buffer const & buf,
436 ParagraphList const & paragraphs,
439 OutputParams const & runparams,
440 string const & everypar)
442 bool was_title = false;
443 bool already_title = false;
444 LyXTextClass const & tclass = buf.params().getLyXTextClass();
445 ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
446 ParagraphList::iterator endpar = const_cast<ParagraphList&>(paragraphs).end();
449 while (par != endpar) {
450 InsetOld * in = par->inInset();
451 // well we have to check if we are in an inset with unlimited
452 // length (all in one row) if that is true then we don't allow
453 // any special options in the paragraph and also we don't allow
454 // any environment other then "Standard" to be valid!
455 if (in == 0 || !in->forceDefaultParagraphs(in)) {
456 LyXLayout_ptr const & layout = par->layout();
458 if (layout->intitle) {
460 lyxerr << "Error in latexParagraphs: You"
461 " should not mix title layouts"
462 " with normal ones." << endl;
463 } else if (!was_title) {
465 if (tclass.titletype() == TITLE_ENVIRONMENT) {
467 << tclass.titlename()
472 } else if (was_title && !already_title) {
473 if (tclass.titletype() == TITLE_ENVIRONMENT) {
474 os << "\\end{" << tclass.titlename()
478 os << "\\" << tclass.titlename()
482 already_title = true;
486 if (layout->is_environment) {
487 par = TeXOnePar(buf, paragraphs, par, os, texrow,
488 runparams, everypar);
489 } else if (layout->isEnvironment() ||
490 !par->params().leftIndent().zero())
492 par = TeXEnvironment(buf, paragraphs, par, os,
495 par = TeXOnePar(buf, paragraphs, par, os, texrow,
496 runparams, everypar);
499 par = TeXOnePar(buf, paragraphs, par, os, texrow,
500 runparams, everypar);
503 // It might be that we only have a title in this document
504 if (was_title && !already_title) {
505 if (tclass.titletype() == TITLE_ENVIRONMENT) {
506 os << "\\end{" << tclass.titlename()
510 os << "\\" << tclass.titlename()