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 InsetOld * ins = it->inset;
198 if (ins->lyxCode() == InsetOld::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 (pit->params().spaceTop().kind() != VSpace::NONE) {
248 os << pit->params().spaceTop().asLatexCommand(bparams);
249 further_blank_line = true;
252 if (further_blank_line) {
257 style = bparams.getLyXTextClass().defaultLayout();
260 Language const * language = pit->getParLanguage(bparams);
261 Language const * doc_language = bparams.language;
262 Language const * previous_language =
263 (pit != const_cast<ParagraphList&>(paragraphs).begin())
264 ? boost::prior(pit)->getParLanguage(bparams)
267 if (language->babel() != previous_language->babel()
268 // check if we already put language command in TeXEnvironment()
269 && !(style->isEnvironment()
270 && (pit == const_cast<ParagraphList&>(paragraphs).begin() ||
271 (boost::prior(pit)->layout() != pit->layout() &&
272 boost::prior(pit)->getDepth() <= pit->getDepth())
273 || boost::prior(pit)->getDepth() < pit->getDepth())))
275 if (!lyxrc.language_command_end.empty() &&
276 previous_language->babel() != doc_language->babel())
278 os << subst(lyxrc.language_command_end, "$$lang",
279 previous_language->babel())
284 if (lyxrc.language_command_end.empty() ||
285 language->babel() != doc_language->babel())
287 os << subst(lyxrc.language_command_begin, "$$lang",
294 if (bparams.inputenc == "auto" &&
295 language->encoding() != previous_language->encoding()) {
296 os << "\\inputencoding{"
297 << language->encoding()->LatexName()
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),
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(outerFont(pit, paragraphs));
344 ? pit->getLayoutFont(bparams, outerfont)
345 : pit->getFont(bparams, pit->size() - 1, outerfont));
347 bool is_command = style->isCommand();
349 if (style->resfont.size() != font.size()
350 && boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
354 os << "\\" << font.latexSize() << " \\par}";
355 } else if (need_par) {
357 } else if (is_command)
360 switch (style->latextype) {
361 case LATEX_ITEM_ENVIRONMENT:
362 case LATEX_LIST_ENVIRONMENT:
363 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
364 && (pit->params().depth() < boost::next(pit)->params().depth())) {
369 case LATEX_ENVIRONMENT: {
370 // if its the last paragraph of the current environment
371 // skip it otherwise fall through
372 ParagraphList::iterator next = boost::next(pit);
374 if (next != const_cast<ParagraphList&>(paragraphs).end()
375 && (next->layout() != pit->layout()
376 || next->params().depth() != pit->params().depth()))
380 // fall through possible
382 // we don't need it for the last paragraph!!!
383 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
389 if (in == 0 || !in->forceDefaultParagraphs(in)) {
390 further_blank_line = false;
392 if (pit->params().spaceBottom().kind() != VSpace::NONE) {
393 os << pit->params().spaceBottom().asLatexCommand(bparams);
394 further_blank_line = true;
397 if (further_blank_line) {
402 if (!pit->params().spacing().isDefault()
403 && (boost::next(pit) == const_cast<ParagraphList&>(paragraphs).end()
404 || !boost::next(pit)->hasSameLayout(*pit)))
406 os << pit->params().spacing().writeEnvirEnd() << '\n';
411 // we don't need it for the last paragraph!!!
412 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
416 // Since \selectlanguage write the language to the aux file,
417 // we need to reset the language at the end of footnote or
420 if (language->babel() != doc_language->babel()) {
421 if (lyxrc.language_command_end.empty())
422 os << subst(lyxrc.language_command_begin,
424 doc_language->babel())
427 os << subst(lyxrc.language_command_end,
435 lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;
442 // LaTeX all paragraphs from par to endpar, if endpar == 0 then to the end
444 void latexParagraphs(Buffer const & buf,
445 ParagraphList const & paragraphs,
448 OutputParams const & runparams,
449 string const & everypar)
451 bool was_title = false;
452 bool already_title = false;
453 LyXTextClass const & tclass = buf.params().getLyXTextClass();
454 ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
455 ParagraphList::iterator endpar = const_cast<ParagraphList&>(paragraphs).end();
458 while (par != endpar) {
459 InsetOld * in = par->inInset();
460 // well we have to check if we are in an inset with unlimited
461 // length (all in one row) if that is true then we don't allow
462 // any special options in the paragraph and also we don't allow
463 // any environment other then "Standard" to be valid!
464 if (in == 0 || !in->forceDefaultParagraphs(in)) {
465 LyXLayout_ptr const & layout = par->layout();
467 if (layout->intitle) {
469 lyxerr <<"Error in latexParagraphs: You"
470 " should not mix title layouts"
471 " with normal ones." << endl;
472 } else if (!was_title) {
474 if (tclass.titletype() == TITLE_ENVIRONMENT) {
476 << tclass.titlename()
481 } else if (was_title && !already_title) {
482 if (tclass.titletype() == TITLE_ENVIRONMENT) {
483 os << "\\end{" << tclass.titlename()
487 os << "\\" << tclass.titlename()
491 already_title = true;
495 if (layout->is_environment) {
496 par = TeXOnePar(buf, paragraphs, par, os, texrow,
497 runparams, everypar);
498 } else if (layout->isEnvironment() ||
499 !par->params().leftIndent().zero())
501 par = TeXEnvironment(buf, paragraphs, par, os,
504 par = TeXOnePar(buf, paragraphs, par, os, texrow,
505 runparams, everypar);
508 par = TeXOnePar(buf, paragraphs, par, os, texrow,
509 runparams, everypar);
512 // It might be that we only have a title in this document
513 if (was_title && !already_title) {
514 if (tclass.titletype() == TITLE_ENVIRONMENT) {
515 os << "\\end{" << tclass.titlename()
519 os << "\\" << tclass.titlename()