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 "outputparams.h"
22 #include "paragraph.h"
23 #include "paragraph_funcs.h"
24 #include "ParagraphParameters.h"
28 #include "insets/insetoptarg.h"
30 #include "support/lstrings.h"
35 using lyx::support::subst;
41 extern string bibitemWidest(Buffer const &);
46 ParagraphList::const_iterator
47 TeXEnvironment(Buffer const & buf,
48 ParagraphList const & paragraphs,
49 ParagraphList::const_iterator pit,
50 ostream & os, TexRow & texrow,
51 OutputParams const & runparams);
53 ParagraphList::const_iterator
54 TeXOnePar(Buffer const & buf,
55 ParagraphList const & paragraphs,
56 ParagraphList::const_iterator pit,
57 ostream & os, TexRow & texrow,
58 OutputParams const & runparams,
59 string const & everypar = string());
62 ParagraphList::const_iterator
63 TeXDeeper(Buffer const & buf,
64 ParagraphList const & paragraphs,
65 ParagraphList::const_iterator pit,
66 ostream & os, TexRow & texrow,
67 OutputParams const & runparams)
69 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
70 ParagraphList::const_iterator par = pit;
72 while (par != paragraphs.end() &&
73 par->params().depth() == pit->params().depth()) {
74 if (par->layout()->isEnvironment()) {
75 par = TeXEnvironment(buf, paragraphs, par,
76 os, texrow, runparams);
78 par = TeXOnePar(buf, paragraphs, par,
79 os, texrow, runparams);
82 lyxerr[Debug::LATEX] << "TeXDeeper...done " << &*par << endl;
88 ParagraphList::const_iterator
89 TeXEnvironment(Buffer const & buf,
90 ParagraphList const & paragraphs,
91 ParagraphList::const_iterator pit,
92 ostream & os, TexRow & texrow,
93 OutputParams const & runparams)
95 lyxerr[Debug::LATEX] << "TeXEnvironment... " << &*pit << endl;
97 BufferParams const & bparams = buf.params();
99 LyXLayout_ptr const & style = pit->layout();
101 Language const * language = pit->getParLanguage(bparams);
102 Language const * doc_language = bparams.language;
103 Language const * previous_language =
104 (pit != paragraphs.begin())
105 ? boost::prior(pit)->getParLanguage(bparams)
107 if (language->babel() != previous_language->babel()) {
109 if (!lyxrc.language_command_end.empty() &&
110 previous_language->babel() != doc_language->babel()) {
111 os << subst(lyxrc.language_command_end, "$$lang",
112 previous_language->babel())
117 if (lyxrc.language_command_end.empty() ||
118 language->babel() != doc_language->babel()) {
119 os << subst(lyxrc.language_command_begin, "$$lang",
126 bool leftindent_open = false;
127 if (!pit->params().leftIndent().zero()) {
128 os << "\\begin{LyXParagraphLeftIndent}{" <<
129 pit->params().leftIndent().asLatexString() << "}\n";
131 leftindent_open = true;
134 if (style->isEnvironment()) {
135 os << "\\begin{" << style->latexname() << '}';
136 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
137 os << "{" << pit->params().labelWidthString() << "}\n";
138 } else if (style->labeltype == LABEL_BIBLIO) {
140 os << "{" << bibitemWidest(buf) << "}\n";
142 os << style->latexparam() << '\n';
145 ParagraphList::const_iterator par = pit;
147 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
149 if (par == paragraphs.end()) {
150 // Make sure that the last paragraph is
151 // correctly terminated (because TeXOnePar does
152 // not add a \n in this case)
155 } else if (par->params().depth() > pit->params().depth()) {
156 if (par->layout()->isParagraph()) {
159 // How to handle this? (Lgb)
160 //&& !suffixIs(os, "\n\n")
162 // There should be at least one '\n' already
163 // but we need there to be two for Standard
164 // paragraphs that are depth-increment'ed to be
165 // output correctly. However, tables can
166 // also be paragraphs so don't adjust them.
169 // Will it ever harm to have one '\n' too
170 // many? i.e. that we sometimes will have
171 // three in a row. (Lgb)
175 par = TeXDeeper(buf, paragraphs, par, os, texrow,
178 } while (par != paragraphs.end()
179 && par->layout() == pit->layout()
180 && par->params().depth() == pit->params().depth()
181 && par->params().leftIndent() == pit->params().leftIndent());
183 if (style->isEnvironment()) {
184 os << "\\end{" << style->latexname() << "}\n";
188 if (leftindent_open) {
189 os << "\\end{LyXParagraphLeftIndent}\n";
193 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << &*par << endl;
194 return par; // ale970302
198 InsetOptArg * optArgInset(Paragraph const & par)
201 InsetList::const_iterator it = par.insetlist.begin();
202 InsetList::const_iterator end = par.insetlist.end();
203 for (; it != end; ++it) {
204 InsetBase * ins = it->inset;
205 if (ins->lyxCode() == InsetBase::OPTARG_CODE) {
206 return static_cast<InsetOptArg *>(ins);
213 ParagraphList::const_iterator
214 TeXOnePar(Buffer const & buf,
215 ParagraphList const & paragraphs,
216 ParagraphList::const_iterator pit,
217 ostream & os, TexRow & texrow,
218 OutputParams const & runparams_in,
219 string const & everypar)
221 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
222 << everypar << "'" << endl;
223 BufferParams const & bparams = buf.params();
224 bool further_blank_line = false;
227 // In an an inset with unlimited length (all in one row),
228 // force layout to default
229 if (!pit->forceDefaultParagraphs())
230 style = pit->layout();
232 style = bparams.getLyXTextClass().defaultLayout();
234 OutputParams runparams = runparams_in;
235 runparams.moving_arg |= style->needprotect;
237 Language const * language = pit->getParLanguage(bparams);
238 Language const * doc_language = bparams.language;
239 Language const * previous_language =
240 (pit != paragraphs.begin())
241 ? boost::prior(pit)->getParLanguage(bparams)
244 if (language->babel() != previous_language->babel()
245 // check if we already put language command in TeXEnvironment()
246 && !(style->isEnvironment()
247 && (pit == paragraphs.begin() ||
248 (boost::prior(pit)->layout() != pit->layout() &&
249 boost::prior(pit)->getDepth() <= pit->getDepth())
250 || boost::prior(pit)->getDepth() < pit->getDepth())))
252 if (!lyxrc.language_command_end.empty() &&
253 previous_language->babel() != doc_language->babel())
255 os << subst(lyxrc.language_command_end, "$$lang",
256 previous_language->babel())
261 if (lyxrc.language_command_end.empty() ||
262 language->babel() != doc_language->babel())
264 os << subst(lyxrc.language_command_begin, "$$lang",
271 if (bparams.inputenc == "auto" &&
272 language->encoding() != previous_language->encoding()) {
273 os << "\\inputencoding{"
274 << language->encoding()->LatexName()
279 // In an an inset with unlimited length (all in one row),
280 // don't allow any special options in the paragraph
281 if (!pit->forceDefaultParagraphs()) {
282 if (pit->params().startOfAppendix()) {
283 os << "\\appendix\n";
287 if (!pit->params().spacing().isDefault()
288 && (pit == paragraphs.begin()
289 || !boost::prior(pit)->hasSameLayout(*pit)))
291 os << pit->params().spacing().writeEnvirBegin() << '\n';
295 if (style->isCommand()) {
300 if (further_blank_line) {
306 switch (style->latextype) {
308 os << '\\' << style->latexname();
310 // Separate handling of optional argument inset.
311 if (style->optionalargs == 1) {
312 InsetOptArg * it = optArgInset(*pit);
314 it->latexOptional(buf, os, runparams);
317 os << style->latexparam();
319 case LATEX_ITEM_ENVIRONMENT:
320 case LATEX_LIST_ENVIRONMENT:
323 case LATEX_BIB_ENVIRONMENT:
324 // ignore this, the inset will write itself
331 bool need_par = pit->simpleTeXOnePar(buf, bparams,
332 outerFont(pit - paragraphs.begin(), paragraphs),
333 os, texrow, runparams);
335 // Make sure that \\par is done with the font of the last
336 // character if this has another size as the default.
337 // This is necessary because LaTeX (and LyX on the screen)
338 // calculates the space between the baselines according
339 // to this font. (Matthias)
341 // Is this really needed ? (Dekel)
342 // We do not need to use to change the font for the last paragraph
344 LyXFont const outerfont =
345 outerFont(pit - paragraphs.begin(),
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) != 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) != 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::const_iterator next = boost::next(pit);
380 if (next != 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) != paragraphs.end()) {
395 if (!pit->forceDefaultParagraphs()) {
396 further_blank_line = false;
398 if (further_blank_line) {
403 if (!pit->params().spacing().isDefault()
404 && (boost::next(pit) == paragraphs.end()
405 || !boost::next(pit)->hasSameLayout(*pit)))
407 os << pit->params().spacing().writeEnvirEnd() << '\n';
412 if (boost::next(pit) == const_cast<ParagraphList&>(paragraphs).end()
413 && language->babel() != doc_language->babel()) {
414 // Since \selectlanguage write the language to the aux file,
415 // we need to reset the language at the end of footnote or
418 if (lyxrc.language_command_end.empty())
419 os << subst(lyxrc.language_command_begin,
421 doc_language->babel())
424 os << subst(lyxrc.language_command_end,
431 // we don't need it for the last paragraph!!!
432 // Note from JMarc: we will re-add a \n explicitely in
433 // TeXEnvironment, because it is needed in this case
434 if (boost::next(pit) != paragraphs.end()) {
439 lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;
446 // LaTeX all paragraphs
447 void latexParagraphs(Buffer const & buf,
448 ParagraphList const & paragraphs,
451 OutputParams const & runparams,
452 string const & everypar)
454 bool was_title = false;
455 bool already_title = false;
456 LyXTextClass const & tclass = buf.params().getLyXTextClass();
457 ParagraphList::const_iterator par = paragraphs.begin();
458 ParagraphList::const_iterator endpar = paragraphs.end();
461 while (par != endpar) {
462 // well we have to check if we are in an inset with unlimited
463 // length (all in one row) if that is true then we don't allow
464 // any special options in the paragraph and also we don't allow
465 // any environment other then "Standard" to be valid!
466 if (!par->forceDefaultParagraphs()) {
467 LyXLayout_ptr const & layout = par->layout();
469 if (layout->intitle) {
471 lyxerr << "Error in latexParagraphs: You"
472 " should not mix title layouts"
473 " with normal ones." << endl;
474 } else if (!was_title) {
476 if (tclass.titletype() == TITLE_ENVIRONMENT) {
478 << tclass.titlename()
483 } else if (was_title && !already_title) {
484 if (tclass.titletype() == TITLE_ENVIRONMENT) {
485 os << "\\end{" << tclass.titlename()
489 os << "\\" << tclass.titlename()
493 already_title = true;
497 if (layout->is_environment) {
498 par = TeXOnePar(buf, paragraphs, par, os, texrow,
499 runparams, everypar);
500 } else if (layout->isEnvironment() ||
501 !par->params().leftIndent().zero())
503 par = TeXEnvironment(buf, paragraphs, par, os,
506 par = TeXOnePar(buf, paragraphs, par, os, texrow,
507 runparams, everypar);
510 par = TeXOnePar(buf, paragraphs, par, os, texrow,
511 runparams, everypar);
514 // It might be that we only have a title in this document
515 if (was_title && !already_title) {
516 if (tclass.titletype() == TITLE_ENVIRONMENT) {
517 os << "\\end{" << tclass.titlename()
521 os << "\\" << tclass.titlename()