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())
207 LYXERR(Debug::LATEX) << "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,
352 os, texrow, runparams);
354 // Make sure that \\par is done with the font of the last
355 // character if this has another size as the default.
356 // This is necessary because LaTeX (and LyX on the screen)
357 // calculates the space between the baselines according
358 // to this font. (Matthias)
360 // Is this really needed ? (Dekel)
361 // We do not need to use to change the font for the last paragraph
366 ? pit->getLayoutFont(bparams, outerfont)
367 : pit->getFont(bparams, pit->size() - 1, outerfont));
369 bool is_command = style->isCommand();
371 if (style->resfont.size() != font.size()
372 && boost::next(pit) != paragraphs.end()
376 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
377 } else if (need_par) {
379 } else if (is_command)
382 bool pending_newline = false;
383 switch (style->latextype) {
384 case LATEX_ITEM_ENVIRONMENT:
385 case LATEX_LIST_ENVIRONMENT:
386 if (boost::next(pit) != paragraphs.end()
387 && (pit->params().depth() < boost::next(pit)->params().depth()))
388 pending_newline = true;
390 case LATEX_ENVIRONMENT: {
391 // if its the last paragraph of the current environment
392 // skip it otherwise fall through
393 ParagraphList::const_iterator next = boost::next(pit);
395 if (next != paragraphs.end()
396 && (next->layout() != pit->layout()
397 || next->params().depth() != pit->params().depth()))
401 // fall through possible
403 // we don't need it for the last paragraph!!!
404 if (boost::next(pit) != paragraphs.end())
405 pending_newline = true;
408 if (!pit->forceDefaultParagraphs()) {
409 if (!pit->params().spacing().isDefault()
410 && (boost::next(pit) == paragraphs.end()
411 || !boost::next(pit)->hasSameLayout(*pit)))
413 if (pending_newline) {
417 os << from_ascii(pit->params().spacing().writeEnvirEnd());
418 pending_newline = true;
422 if (boost::next(pit) == paragraphs.end()
423 && par_language->babel() != doc_language->babel()) {
424 // Since \selectlanguage write the language to the aux file,
425 // we need to reset the language at the end of footnote or
428 if (pending_newline) {
432 if (lyxrc.language_command_end.empty())
433 os << from_ascii(subst(
434 lyxrc.language_command_begin,
436 doc_language->babel()));
438 os << from_ascii(subst(
439 lyxrc.language_command_end,
441 par_language->babel()));
442 pending_newline = true;
445 if (pending_newline) {
449 runparams_in.encoding = runparams.encoding;
451 // we don't need it for the last paragraph!!!
452 // Note from JMarc: we will re-add a \n explicitely in
453 // TeXEnvironment, because it is needed in this case
454 if (boost::next(pit) != paragraphs.end()) {
459 if (boost::next(pit) != paragraphs.end())
460 LYXERR(Debug::LATEX) << "TeXOnePar...done " << &*boost::next(pit) << endl;
468 // LaTeX all paragraphs
469 void latexParagraphs(Buffer const & buf,
470 ParagraphList const & paragraphs,
473 OutputParams const & runparams,
474 string const & everypar)
476 bool was_title = false;
477 bool already_title = false;
478 LyXTextClass const & tclass = buf.params().getLyXTextClass();
479 ParagraphList::const_iterator par = paragraphs.begin();
480 ParagraphList::const_iterator endpar = paragraphs.end();
482 BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
483 // if only part of the paragraphs will be outputed
484 if (runparams.par_begin != runparams.par_end) {
485 par = boost::next(paragraphs.begin(), runparams.par_begin);
486 endpar = boost::next(paragraphs.begin(), runparams.par_end);
487 // runparams will be passed to nested paragraphs, so
488 // we have to reset the range parameters.
489 const_cast<OutputParams&>(runparams).par_begin = 0;
490 const_cast<OutputParams&>(runparams).par_end = 0;
494 while (par != endpar) {
495 ParagraphList::const_iterator lastpar = par;
496 // well we have to check if we are in an inset with unlimited
497 // length (all in one row) if that is true then we don't allow
498 // any special options in the paragraph and also we don't allow
499 // any environment other than the default layout of the
500 // text class to be valid!
501 if (!par->forceDefaultParagraphs()) {
502 LyXLayout_ptr const & layout = par->layout();
504 if (layout->intitle) {
506 lyxerr << "Error in latexParagraphs: You"
507 " should not mix title layouts"
508 " with normal ones." << endl;
509 } else if (!was_title) {
511 if (tclass.titletype() == TITLE_ENVIRONMENT) {
513 << from_ascii(tclass.titlename())
518 } else if (was_title && !already_title) {
519 if (tclass.titletype() == TITLE_ENVIRONMENT) {
520 os << "\\end{" << from_ascii(tclass.titlename())
524 os << "\\" << from_ascii(tclass.titlename())
528 already_title = true;
532 if (layout->is_environment) {
533 par = TeXOnePar(buf, paragraphs, par, os, texrow,
534 runparams, everypar);
535 } else if (layout->isEnvironment() ||
536 !par->params().leftIndent().zero()) {
537 par = TeXEnvironment(buf, paragraphs, par, os,
540 par = TeXOnePar(buf, paragraphs, par, os, texrow,
541 runparams, everypar);
544 par = TeXOnePar(buf, paragraphs, par, os, texrow,
545 runparams, everypar);
547 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
550 // It might be that we only have a title in this document
551 if (was_title && !already_title) {
552 if (tclass.titletype() == TITLE_ENVIRONMENT) {
553 os << "\\end{" << from_ascii(tclass.titlename())
557 os << "\\" << from_ascii(tclass.titlename())
565 int switchEncoding(odocstream & os, BufferParams const & bparams,
566 Encoding const & oldEnc, Encoding const & newEnc)
568 // FIXME thailatex does not support the inputenc package, so we
569 // ignore switches from/to tis620-0 encoding here. This does of
570 // course only work as long as the non-thai text contains ASCII
571 // only, but it is the best we can do.
572 if ((bparams.inputenc == "auto" || bparams.inputenc == "default") &&
573 oldEnc.name() != newEnc.name() &&
574 oldEnc.name() != "ascii" && newEnc.name() != "ascii" &&
575 oldEnc.name() != "tis620-0" && newEnc.name() != "tis620-0") {
576 LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
577 << oldEnc.name() << " to "
578 << newEnc.name() << endl;
579 os << setEncoding(newEnc.iconvName());
580 if (bparams.inputenc != "default") {
581 docstring const inputenc(from_ascii(newEnc.latexName()));
582 os << "\\inputencoding{" << inputenc << '}';
583 return 16 + inputenc.length();