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 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
199 ostream & os, OutputParams const & runparams, int number)
203 InsetList::const_iterator it = par.insetlist.begin();
204 InsetList::const_iterator end = par.insetlist.end();
205 for (; it != end && number > 0 ; ++it) {
206 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) {
208 static_cast<InsetOptArg *>(it->inset);
209 lines += ins->latexOptional(buf, os, runparams);
217 ParagraphList::const_iterator
218 TeXOnePar(Buffer const & buf,
219 ParagraphList const & paragraphs,
220 ParagraphList::const_iterator pit,
221 ostream & os, TexRow & texrow,
222 OutputParams const & runparams_in,
223 string const & everypar)
225 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
226 << everypar << "'" << endl;
227 BufferParams const & bparams = buf.params();
228 bool further_blank_line = false;
231 // In an an inset with unlimited length (all in one row),
232 // force layout to default
233 if (!pit->forceDefaultParagraphs())
234 style = pit->layout();
236 style = bparams.getLyXTextClass().defaultLayout();
238 OutputParams runparams = runparams_in;
239 runparams.moving_arg |= style->needprotect;
241 Language const * language = pit->getParLanguage(bparams);
242 Language const * doc_language = bparams.language;
243 Language const * previous_language =
244 (pit != paragraphs.begin())
245 ? boost::prior(pit)->getParLanguage(bparams)
248 if (language->babel() != previous_language->babel()
249 // check if we already put language command in TeXEnvironment()
250 && !(style->isEnvironment()
251 && (pit == paragraphs.begin() ||
252 (boost::prior(pit)->layout() != pit->layout() &&
253 boost::prior(pit)->getDepth() <= pit->getDepth())
254 || boost::prior(pit)->getDepth() < pit->getDepth())))
256 if (!lyxrc.language_command_end.empty() &&
257 previous_language->babel() != doc_language->babel())
259 os << subst(lyxrc.language_command_end, "$$lang",
260 previous_language->babel())
265 if (lyxrc.language_command_end.empty() ||
266 language->babel() != doc_language->babel())
268 os << subst(lyxrc.language_command_begin, "$$lang",
275 if (bparams.inputenc == "auto" &&
276 language->encoding() != previous_language->encoding()) {
277 os << "\\inputencoding{"
278 << language->encoding()->LatexName()
283 // In an an inset with unlimited length (all in one row),
284 // don't allow any special options in the paragraph
285 if (!pit->forceDefaultParagraphs()) {
286 if (pit->params().startOfAppendix()) {
287 os << "\\appendix\n";
291 if (!pit->params().spacing().isDefault()
292 && (pit == paragraphs.begin()
293 || !boost::prior(pit)->hasSameLayout(*pit)))
295 os << pit->params().spacing().writeEnvirBegin() << '\n';
299 if (style->isCommand()) {
304 if (further_blank_line) {
310 switch (style->latextype) {
312 os << '\\' << style->latexname();
314 // Separate handling of optional argument inset.
315 if (style->optionalargs > 0) {
316 int ret = latexOptArgInsets(buf, *pit, os, runparams,
317 style->optionalargs);
324 os << style->latexparam();
326 case LATEX_ITEM_ENVIRONMENT:
327 case LATEX_LIST_ENVIRONMENT:
330 case LATEX_BIB_ENVIRONMENT:
331 // ignore this, the inset will write itself
338 bool need_par = pit->simpleTeXOnePar(buf, bparams,
339 outerFont(pit - paragraphs.begin(), paragraphs),
340 os, texrow, runparams);
342 // Make sure that \\par is done with the font of the last
343 // character if this has another size as the default.
344 // This is necessary because LaTeX (and LyX on the screen)
345 // calculates the space between the baselines according
346 // to this font. (Matthias)
348 // Is this really needed ? (Dekel)
349 // We do not need to use to change the font for the last paragraph
351 LyXFont const outerfont =
352 outerFont(pit - paragraphs.begin(),
357 ? pit->getLayoutFont(bparams, outerfont)
358 : pit->getFont(bparams, pit->size() - 1, outerfont));
360 bool is_command = style->isCommand();
362 if (style->resfont.size() != font.size()
363 && boost::next(pit) != paragraphs.end()
367 os << "\\" << font.latexSize() << " \\par}";
368 } else if (need_par) {
370 } else if (is_command)
373 switch (style->latextype) {
374 case LATEX_ITEM_ENVIRONMENT:
375 case LATEX_LIST_ENVIRONMENT:
376 if (boost::next(pit) != paragraphs.end()
377 && (pit->params().depth() < boost::next(pit)->params().depth())) {
382 case LATEX_ENVIRONMENT: {
383 // if its the last paragraph of the current environment
384 // skip it otherwise fall through
385 ParagraphList::const_iterator next = boost::next(pit);
387 if (next != paragraphs.end()
388 && (next->layout() != pit->layout()
389 || next->params().depth() != pit->params().depth()))
393 // fall through possible
395 // we don't need it for the last paragraph!!!
396 if (boost::next(pit) != paragraphs.end()) {
402 if (!pit->forceDefaultParagraphs()) {
403 further_blank_line = false;
405 if (further_blank_line) {
410 if (!pit->params().spacing().isDefault()
411 && (boost::next(pit) == paragraphs.end()
412 || !boost::next(pit)->hasSameLayout(*pit)))
414 os << pit->params().spacing().writeEnvirEnd() << '\n';
419 if (boost::next(pit) == const_cast<ParagraphList&>(paragraphs).end()
420 && language->babel() != doc_language->babel()) {
421 // Since \selectlanguage write the language to the aux file,
422 // we need to reset the language at the end of footnote or
425 if (lyxrc.language_command_end.empty())
426 os << subst(lyxrc.language_command_begin,
428 doc_language->babel())
431 os << subst(lyxrc.language_command_end,
438 // we don't need it for the last paragraph!!!
439 // Note from JMarc: we will re-add a \n explicitely in
440 // TeXEnvironment, because it is needed in this case
441 if (boost::next(pit) != paragraphs.end()) {
446 lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;
453 // LaTeX all paragraphs
454 void latexParagraphs(Buffer const & buf,
455 ParagraphList const & paragraphs,
458 OutputParams const & runparams,
459 string const & everypar)
461 bool was_title = false;
462 bool already_title = false;
463 LyXTextClass const & tclass = buf.params().getLyXTextClass();
464 ParagraphList::const_iterator par = paragraphs.begin();
465 ParagraphList::const_iterator endpar = paragraphs.end();
468 while (par != endpar) {
469 // well we have to check if we are in an inset with unlimited
470 // length (all in one row) if that is true then we don't allow
471 // any special options in the paragraph and also we don't allow
472 // any environment other then "Standard" to be valid!
473 if (!par->forceDefaultParagraphs()) {
474 LyXLayout_ptr const & layout = par->layout();
476 if (layout->intitle) {
478 lyxerr << "Error in latexParagraphs: You"
479 " should not mix title layouts"
480 " with normal ones." << endl;
481 } else if (!was_title) {
483 if (tclass.titletype() == TITLE_ENVIRONMENT) {
485 << tclass.titlename()
490 } else if (was_title && !already_title) {
491 if (tclass.titletype() == TITLE_ENVIRONMENT) {
492 os << "\\end{" << tclass.titlename()
496 os << "\\" << tclass.titlename()
500 already_title = true;
504 if (layout->is_environment) {
505 par = TeXOnePar(buf, paragraphs, par, os, texrow,
506 runparams, everypar);
507 } else if (layout->isEnvironment() ||
508 !par->params().leftIndent().zero())
510 par = TeXEnvironment(buf, paragraphs, par, os,
513 par = TeXOnePar(buf, paragraphs, par, os, texrow,
514 runparams, everypar);
517 par = TeXOnePar(buf, paragraphs, par, os, texrow,
518 runparams, everypar);
521 // It might be that we only have a title in this document
522 if (was_title && !already_title) {
523 if (tclass.titletype() == TITLE_ENVIRONMENT) {
524 os << "\\end{" << tclass.titlename()
528 os << "\\" << tclass.titlename()