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/insetbibitem.h"
29 #include "insets/insetoptarg.h"
31 #include "support/lstrings.h"
44 ParagraphList::const_iterator
45 TeXEnvironment(Buffer const & buf,
46 ParagraphList const & paragraphs,
47 ParagraphList::const_iterator pit,
48 odocstream & os, TexRow & texrow,
49 OutputParams const & runparams);
51 ParagraphList::const_iterator
52 TeXOnePar(Buffer const & buf,
53 ParagraphList const & paragraphs,
54 ParagraphList::const_iterator pit,
55 odocstream & os, TexRow & texrow,
56 OutputParams const & runparams,
57 string const & everypar = string());
60 ParagraphList::const_iterator
61 TeXDeeper(Buffer const & buf,
62 ParagraphList const & paragraphs,
63 ParagraphList::const_iterator pit,
64 odocstream & os, TexRow & texrow,
65 OutputParams const & runparams)
67 lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl;
68 ParagraphList::const_iterator par = pit;
70 while (par != paragraphs.end() &&
71 par->params().depth() == pit->params().depth()) {
72 if (par->layout()->isEnvironment()) {
73 par = TeXEnvironment(buf, paragraphs, par,
74 os, texrow, runparams);
76 par = TeXOnePar(buf, paragraphs, par,
77 os, texrow, runparams);
80 lyxerr[Debug::LATEX] << "TeXDeeper...done " << endl;
86 ParagraphList::const_iterator
87 TeXEnvironment(Buffer const & buf,
88 ParagraphList const & paragraphs,
89 ParagraphList::const_iterator pit,
90 odocstream & os, TexRow & texrow,
91 OutputParams const & runparams)
93 lyxerr[Debug::LATEX] << "TeXEnvironment... " << &*pit << endl;
95 BufferParams const & bparams = buf.params();
97 LyXLayout_ptr const & style = pit->layout();
99 Language const * const par_language = pit->getParLanguage(bparams);
100 Language const * const doc_language = bparams.language;
101 Language const * const prev_par_language =
102 (pit != paragraphs.begin())
103 ? boost::prior(pit)->getParLanguage(bparams)
105 if (par_language->babel() != prev_par_language->babel()) {
107 if (!lyxrc.language_command_end.empty() &&
108 prev_par_language->babel() != doc_language->babel()) {
109 os << from_ascii(subst(
110 lyxrc.language_command_end,
112 prev_par_language->babel()))
117 if (lyxrc.language_command_end.empty() ||
118 par_language->babel() != doc_language->babel()) {
119 os << from_ascii(subst(
120 lyxrc.language_command_begin,
122 par_language->babel()))
128 bool leftindent_open = false;
129 if (!pit->params().leftIndent().zero()) {
130 os << "\\begin{LyXParagraphLeftIndent}{"
131 << from_ascii(pit->params().leftIndent().asLatexString())
134 leftindent_open = true;
137 if (style->isEnvironment()) {
138 os << "\\begin{" << from_ascii(style->latexname()) << '}';
139 if (style->optionalargs > 0) {
140 int ret = latexOptArgInsets(buf, *pit, os, runparams,
141 style->optionalargs);
147 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
149 << pit->params().labelWidthString()
151 } else if (style->labeltype == LABEL_BIBLIO) {
153 os << '{' << bibitemWidest(buf) << "}\n";
155 os << from_ascii(style->latexparam()) << '\n';
158 ParagraphList::const_iterator par = pit;
160 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
162 if (par == paragraphs.end()) {
163 // Make sure that the last paragraph is
164 // correctly terminated (because TeXOnePar does
165 // not add a \n in this case)
168 } else if (par->params().depth() > pit->params().depth()) {
169 if (par->layout()->isParagraph()) {
172 // How to handle this? (Lgb)
173 //&& !suffixIs(os, "\n\n")
175 // There should be at least one '\n' already
176 // but we need there to be two for Standard
177 // paragraphs that are depth-increment'ed to be
178 // output correctly. However, tables can
179 // also be paragraphs so don't adjust them.
182 // Will it ever harm to have one '\n' too
183 // many? i.e. that we sometimes will have
184 // three in a row. (Lgb)
188 par = TeXDeeper(buf, paragraphs, par, os, texrow,
191 } while (par != paragraphs.end()
192 && par->layout() == pit->layout()
193 && par->params().depth() == pit->params().depth()
194 && par->params().leftIndent() == pit->params().leftIndent());
196 if (style->isEnvironment()) {
197 os << "\\end{" << from_ascii(style->latexname()) << "}\n";
201 if (leftindent_open) {
202 os << "\\end{LyXParagraphLeftIndent}\n";
206 if (par != paragraphs.end() && lyxerr.debugging(Debug::LATEX))
207 lyxerr << "TeXEnvironment...done " << &*par << endl;
214 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
215 odocstream & os, OutputParams const & runparams, int number)
219 InsetList::const_iterator it = par.insetlist.begin();
220 InsetList::const_iterator end = par.insetlist.end();
221 for (; it != end && number > 0 ; ++it) {
222 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) {
224 static_cast<InsetOptArg *>(it->inset);
225 lines += ins->latexOptional(buf, os, runparams);
235 ParagraphList::const_iterator
236 TeXOnePar(Buffer const & buf,
237 ParagraphList const & paragraphs,
238 ParagraphList::const_iterator pit,
239 odocstream & os, TexRow & texrow,
240 OutputParams const & runparams_in,
241 string const & everypar)
243 lyxerr[Debug::LATEX] << "TeXOnePar... " << &*pit << " '"
244 << everypar << "'" << endl;
245 BufferParams const & bparams = buf.params();
248 // In an inset with unlimited length (all in one row),
249 // force layout to default
250 if (!pit->forceDefaultParagraphs())
251 style = pit->layout();
253 style = bparams.getLyXTextClass().defaultLayout();
255 OutputParams runparams = runparams_in;
256 runparams.moving_arg |= style->needprotect;
258 Language const * const par_language = pit->getParLanguage(bparams);
259 Language const * const doc_language = bparams.language;
260 Language const * const prev_par_language =
261 (pit != paragraphs.begin())
262 ? boost::prior(pit)->getParLanguage(bparams)
265 if (par_language->babel() != prev_par_language->babel()
266 // check if we already put language command in TeXEnvironment()
267 && !(style->isEnvironment()
268 && (pit == paragraphs.begin() ||
269 (boost::prior(pit)->layout() != pit->layout() &&
270 boost::prior(pit)->getDepth() <= pit->getDepth())
271 || boost::prior(pit)->getDepth() < pit->getDepth())))
273 if (!lyxrc.language_command_end.empty() &&
274 prev_par_language->babel() != doc_language->babel())
276 os << from_ascii(subst(lyxrc.language_command_end,
278 prev_par_language->babel()))
283 if (lyxrc.language_command_end.empty() ||
284 par_language->babel() != doc_language->babel())
286 os << from_ascii(subst(
287 lyxrc.language_command_begin,
289 par_language->babel()))
295 // In an inset with unlimited length (all in one row),
296 // don't allow any special options in the paragraph
297 if (!pit->forceDefaultParagraphs()) {
298 if (pit->params().startOfAppendix()) {
299 os << "\\appendix\n";
303 if (!pit->params().spacing().isDefault()
304 && (pit == paragraphs.begin()
305 || !boost::prior(pit)->hasSameLayout(*pit)))
307 os << from_ascii(pit->params().spacing().writeEnvirBegin())
312 if (style->isCommand()) {
318 switch (style->latextype) {
320 os << '\\' << from_ascii(style->latexname());
322 // Separate handling of optional argument inset.
323 if (style->optionalargs > 0) {
324 int ret = latexOptArgInsets(buf, *pit, os, runparams,
325 style->optionalargs);
332 os << from_ascii(style->latexparam());
334 case LATEX_ITEM_ENVIRONMENT:
335 case LATEX_LIST_ENVIRONMENT:
338 case LATEX_BIB_ENVIRONMENT:
339 // ignore this, the inset will write itself
345 LyXFont const outerfont =
346 outerFont(std::distance(paragraphs.begin(), pit),
350 os << from_utf8(everypar);
351 bool need_par = pit->simpleTeXOnePar(buf, bparams, outerfont,
353 os, texrow, runparams);
355 // Make sure that \\par is done with the font of the last
356 // character if this has another size as the default.
357 // This is necessary because LaTeX (and LyX on the screen)
358 // calculates the space between the baselines according
359 // to this font. (Matthias)
361 // Is this really needed ? (Dekel)
362 // We do not need to use to change the font for the last paragraph
367 ? pit->getLayoutFont(bparams, outerfont)
368 : pit->getFont(bparams, pit->size() - 1, outerfont));
370 bool is_command = style->isCommand();
372 if (style->resfont.size() != font.size()
373 && boost::next(pit) != paragraphs.end()
377 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
378 } else if (need_par) {
380 } else if (is_command)
383 bool pending_newline = false;
384 switch (style->latextype) {
385 case LATEX_ITEM_ENVIRONMENT:
386 case LATEX_LIST_ENVIRONMENT:
387 if (boost::next(pit) != paragraphs.end()
388 && (pit->params().depth() < boost::next(pit)->params().depth()))
389 pending_newline = true;
391 case LATEX_ENVIRONMENT: {
392 // if its the last paragraph of the current environment
393 // skip it otherwise fall through
394 ParagraphList::const_iterator next = boost::next(pit);
396 if (next != paragraphs.end()
397 && (next->layout() != pit->layout()
398 || next->params().depth() != pit->params().depth()))
402 // fall through possible
404 // we don't need it for the last paragraph!!!
405 if (boost::next(pit) != paragraphs.end())
406 pending_newline = true;
409 if (!pit->forceDefaultParagraphs()) {
410 if (!pit->params().spacing().isDefault()
411 && (boost::next(pit) == paragraphs.end()
412 || !boost::next(pit)->hasSameLayout(*pit)))
414 if (pending_newline) {
418 os << from_ascii(pit->params().spacing().writeEnvirEnd());
419 pending_newline = true;
423 if (boost::next(pit) == paragraphs.end()
424 && par_language->babel() != doc_language->babel()) {
425 // Since \selectlanguage write the language to the aux file,
426 // we need to reset the language at the end of footnote or
429 if (pending_newline) {
433 if (lyxrc.language_command_end.empty())
434 os << from_ascii(subst(
435 lyxrc.language_command_begin,
437 doc_language->babel()));
439 os << from_ascii(subst(
440 lyxrc.language_command_end,
442 par_language->babel()));
443 pending_newline = true;
446 if (pending_newline) {
450 runparams_in.encoding = runparams.encoding;
452 // we don't need it for the last paragraph!!!
453 // Note from JMarc: we will re-add a \n explicitely in
454 // TeXEnvironment, because it is needed in this case
455 if (boost::next(pit) != paragraphs.end()) {
460 if (boost::next(pit) != paragraphs.end() &&
461 lyxerr.debugging(Debug::LATEX))
462 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
470 // LaTeX all paragraphs
471 void latexParagraphs(Buffer const & buf,
472 ParagraphList const & paragraphs,
475 OutputParams const & runparams,
476 string const & everypar)
478 bool was_title = false;
479 bool already_title = false;
480 LyXTextClass const & tclass = buf.params().getLyXTextClass();
481 ParagraphList::const_iterator par = paragraphs.begin();
482 ParagraphList::const_iterator endpar = paragraphs.end();
484 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
485 // if only part of the paragraphs will be outputed
486 if (runparams.par_begin != runparams.par_end) {
487 par = boost::next(paragraphs.begin(), runparams.par_begin);
488 endpar = boost::next(paragraphs.begin(), runparams.par_end);
489 // runparams will be passed to nested paragraphs, so
490 // we have to reset the range parameters.
491 const_cast<OutputParams&>(runparams).par_begin = 0;
492 const_cast<OutputParams&>(runparams).par_end = 0;
496 while (par != endpar) {
497 ParagraphList::const_iterator lastpar = par;
498 // well we have to check if we are in an inset with unlimited
499 // length (all in one row) if that is true then we don't allow
500 // any special options in the paragraph and also we don't allow
501 // any environment other than the default layout of the
502 // text class to be valid!
503 if (!par->forceDefaultParagraphs()) {
504 LyXLayout_ptr const & layout = par->layout();
506 if (layout->intitle) {
508 lyxerr << "Error in latexParagraphs: You"
509 " should not mix title layouts"
510 " with normal ones." << endl;
511 } else if (!was_title) {
513 if (tclass.titletype() == TITLE_ENVIRONMENT) {
515 << from_ascii(tclass.titlename())
520 } else if (was_title && !already_title) {
521 if (tclass.titletype() == TITLE_ENVIRONMENT) {
522 os << "\\end{" << from_ascii(tclass.titlename())
526 os << "\\" << from_ascii(tclass.titlename())
530 already_title = true;
534 if (layout->is_environment) {
535 par = TeXOnePar(buf, paragraphs, par, os, texrow,
536 runparams, everypar);
537 } else if (layout->isEnvironment() ||
538 !par->params().leftIndent().zero()) {
539 par = TeXEnvironment(buf, paragraphs, par, os,
542 par = TeXOnePar(buf, paragraphs, par, os, texrow,
543 runparams, everypar);
546 par = TeXOnePar(buf, paragraphs, par, os, texrow,
547 runparams, everypar);
549 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
552 // It might be that we only have a title in this document
553 if (was_title && !already_title) {
554 if (tclass.titletype() == TITLE_ENVIRONMENT) {
555 os << "\\end{" << from_ascii(tclass.titlename())
559 os << "\\" << from_ascii(tclass.titlename())
567 int switchEncoding(odocstream & os, BufferParams const & bparams,
568 Encoding const & oldEnc, Encoding const & newEnc)
570 // FIXME thailatex does not support the inputenc package, so we
571 // ignore switches from/to tis620-0 encoding here. This does of
572 // course only work as long as the non-thai text contains ASCII
573 // only, but it is the best we can do.
574 if ((bparams.inputenc == "auto" || bparams.inputenc == "default") &&
575 oldEnc.name() != newEnc.name() &&
576 oldEnc.name() != "ascii" && newEnc.name() != "ascii" &&
577 oldEnc.name() != "tis620-0" && newEnc.name() != "tis620-0") {
578 lyxerr[Debug::LATEX] << "Changing LaTeX encoding from "
579 << oldEnc.name() << " to "
580 << newEnc.name() << endl;
581 os << setEncoding(newEnc.iconvName());
582 if (bparams.inputenc != "default") {
583 docstring const inputenc(from_ascii(newEnc.latexName()));
584 os << "\\inputencoding{" << inputenc << '}';
585 return 16 + inputenc.length();