2 * \file output_latex.cpp
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"
18 #include "InsetList.h"
22 #include "OutputParams.h"
23 #include "Paragraph.h"
24 #include "ParagraphParameters.h"
25 #include "TextClass.h"
29 #include "insets/InsetBibitem.h"
30 #include "insets/InsetOptArg.h"
32 #include "support/lassert.h"
33 #include "support/debug.h"
34 #include "support/lstrings.h"
36 #include <boost/next_prior.hpp>
39 using namespace lyx::support
;
52 static int open_encoding_
= none
;
53 static int cjk_inherited_
= 0;
54 Language
const * prev_env_language_
= 0;
57 ParagraphList::const_iterator
58 TeXEnvironment(Buffer
const & buf
,
60 ParagraphList::const_iterator pit
,
61 odocstream
& os
, TexRow
& texrow
,
62 OutputParams
const & runparams
);
65 ParagraphList::const_iterator
66 TeXDeeper(Buffer
const & buf
,
68 ParagraphList::const_iterator pit
,
69 odocstream
& os
, TexRow
& texrow
,
70 OutputParams
const & runparams
)
72 LYXERR(Debug::LATEX
, "TeXDeeper... " << &*pit
);
73 ParagraphList::const_iterator par
= pit
;
75 ParagraphList
const & paragraphs
= text
.paragraphs();
77 bool const force_plain_layout
= text
.inset().forcePlainLayout();
78 while (par
!= paragraphs
.end() &&
79 par
->params().depth() == pit
->params().depth()) {
80 // FIXME This test should not be necessary.
81 // We should perhaps issue an error if it is.
82 Layout
const & style
= force_plain_layout
83 ? buf
.params().documentClass().plainLayout() : par
->layout();
84 if (style
.isEnvironment()) {
85 par
= TeXEnvironment(buf
, text
, par
,
86 os
, texrow
, runparams
);
88 par
= TeXOnePar(buf
, text
, par
,
89 os
, texrow
, runparams
);
92 LYXERR(Debug::LATEX
, "TeXDeeper...done ");
98 ParagraphList::const_iterator
99 TeXEnvironment(Buffer
const & buf
,
101 ParagraphList::const_iterator pit
,
102 odocstream
& os
, TexRow
& texrow
,
103 OutputParams
const & runparams
)
105 LYXERR(Debug::LATEX
, "TeXEnvironment... " << &*pit
);
107 BufferParams
const & bparams
= buf
.params();
109 // FIXME This test should not be necessary.
110 // We should perhaps issue an error if it is.
111 Layout
const & style
= text
.inset().forcePlainLayout() ?
112 bparams
.documentClass().plainLayout() : pit
->layout();
114 ParagraphList
const & paragraphs
= text
.paragraphs();
115 ParagraphList::const_iterator
const priorpit
=
116 pit
== paragraphs
.begin() ? pit
: boost::prior(pit
);
118 bool const use_prev_env_language
= prev_env_language_
!= 0
119 && priorpit
->layout().isEnvironment()
120 && (priorpit
->getDepth() > pit
->getDepth()
121 || (priorpit
->getDepth() == pit
->getDepth()
122 && priorpit
->layout() != pit
->layout()));
124 Encoding
const * const prev_encoding
= runparams
.encoding
;
125 Language
const * const par_language
= pit
->getParLanguage(bparams
);
126 Language
const * const doc_language
= bparams
.language
;
127 Language
const * const prev_par_language
=
128 (pit
!= paragraphs
.begin())
129 ? (use_prev_env_language
? prev_env_language_
130 : priorpit
->getParLanguage(bparams
))
132 if (par_language
->babel() != prev_par_language
->babel()) {
134 if (!lyxrc
.language_command_end
.empty() &&
135 prev_par_language
->babel() != doc_language
->babel() &&
136 !prev_par_language
->babel().empty()) {
137 os
<< from_ascii(subst(
138 lyxrc
.language_command_end
,
140 prev_par_language
->babel()))
141 // the '%' is necessary to prevent unwanted whitespace
146 if ((lyxrc
.language_command_end
.empty() ||
147 par_language
->babel() != doc_language
->babel()) &&
148 !par_language
->babel().empty()) {
149 os
<< from_ascii(subst(
150 lyxrc
.language_command_begin
,
152 par_language
->babel()))
153 // the '%' is necessary to prevent unwanted whitespace
159 bool leftindent_open
= false;
160 if (!pit
->params().leftIndent().zero()) {
161 os
<< "\\begin{LyXParagraphLeftIndent}{"
162 << from_ascii(pit
->params().leftIndent().asLatexString())
165 leftindent_open
= true;
168 if (style
.isEnvironment()) {
169 os
<< "\\begin{" << from_ascii(style
.latexname()) << '}';
170 if (style
.optionalargs
> 0) {
171 int ret
= latexOptArgInsets(*pit
, os
, runparams
,
178 if (style
.latextype
== LATEX_LIST_ENVIRONMENT
) {
180 << pit
->params().labelWidthString()
182 } else if (style
.labeltype
== LABEL_BIBLIO
) {
183 if (pit
->params().labelWidthString().empty())
184 os
<< '{' << bibitemWidest(buf
) << "}\n";
187 << pit
->params().labelWidthString()
190 os
<< from_ascii(style
.latexparam()) << '\n';
194 // in multilingual environments, the CJK tags have to be nested properly
195 bool cjk_nested
= false;
196 if (par_language
->encoding()->package() == Encoding::CJK
&&
197 open_encoding_
!= CJK
&& pit
->isMultiLingual(bparams
)) {
198 if (prev_par_language
->encoding()->package() == Encoding::CJK
)
199 os
<< "\\begin{CJK}{" << from_ascii(par_language
->encoding()->latexName())
200 << "}{" << from_ascii(bparams
.fontsCJK
) << "}%\n";
201 open_encoding_
= CJK
;
206 ParagraphList::const_iterator par
= pit
;
208 par
= TeXOnePar(buf
, text
, par
, os
, texrow
, runparams
);
210 if (par
== paragraphs
.end()) {
211 // Make sure that the last paragraph is
212 // correctly terminated (because TeXOnePar does
213 // not add a \n in this case)
216 } else if (par
->params().depth() > pit
->params().depth()) {
217 if (par
->layout().isParagraph()) {
219 // How to handle this? (Lgb)
220 //&& !suffixIs(os, "\n\n")
222 // There should be at least one '\n' already
223 // but we need there to be two for Standard
224 // paragraphs that are depth-increment'ed to be
225 // output correctly. However, tables can
226 // also be paragraphs so don't adjust them.
229 // Will it ever harm to have one '\n' too
230 // many? i.e. that we sometimes will have
231 // three in a row. (Lgb)
235 par
= TeXDeeper(buf
, text
, par
, os
, texrow
,
238 } while (par
!= paragraphs
.end()
239 && par
->layout() == pit
->layout()
240 && par
->params().depth() == pit
->params().depth()
241 && par
->params().leftIndent() == pit
->params().leftIndent());
243 if (open_encoding_
== CJK
&& cjk_nested
) {
244 // We need to close the encoding even if it does not change
245 // to do correct environment nesting
246 os
<< "\\end{CJK}\n";
248 open_encoding_
= none
;
251 if (style
.isEnvironment()) {
252 os
<< "\\end{" << from_ascii(style
.latexname()) << "}\n";
254 prev_env_language_
= par_language
;
255 if (runparams
.encoding
!= prev_encoding
) {
256 runparams
.encoding
= prev_encoding
;
257 if (!bparams
.useXetex
)
258 os
<< setEncoding(prev_encoding
->iconvName());
262 if (leftindent_open
) {
263 os
<< "\\end{LyXParagraphLeftIndent}\n";
265 prev_env_language_
= par_language
;
266 if (runparams
.encoding
!= prev_encoding
) {
267 runparams
.encoding
= prev_encoding
;
268 if (!bparams
.useXetex
)
269 os
<< setEncoding(prev_encoding
->iconvName());
273 if (par
!= paragraphs
.end())
274 LYXERR(Debug::LATEX
, "TeXEnvironment...done " << &*par
);
282 int latexOptArgInsets(Paragraph
const & par
, odocstream
& os
,
283 OutputParams
const & runparams
, int number
)
287 InsetList::const_iterator it
= par
.insetList().begin();
288 InsetList::const_iterator end
= par
.insetList().end();
289 for (; it
!= end
&& number
> 0 ; ++it
) {
290 if (it
->inset
->lyxCode() == OPTARG_CODE
) {
292 static_cast<InsetOptArg
*>(it
->inset
);
293 lines
+= ins
->latexOptional(os
, runparams
);
300 // FIXME: this should be anonymous
301 ParagraphList::const_iterator
TeXOnePar(Buffer
const & buf
,
303 ParagraphList::const_iterator
const pit
,
304 odocstream
& os
, TexRow
& texrow
,
305 OutputParams
const & runparams_in
,
306 string
const & everypar
,
307 int start_pos
, int end_pos
)
309 LYXERR(Debug::LATEX
, "TeXOnePar... " << &*pit
<< " '"
311 BufferParams
const & bparams
= buf
.params();
312 ParagraphList
const & paragraphs
= text
.paragraphs();
314 ParagraphList::const_iterator
const priorpit
=
315 pit
== paragraphs
.begin() ? pit
: boost::prior(pit
);
316 ParagraphList::const_iterator
const nextpit
=
317 pit
== paragraphs
.end() ? pit
: boost::next(pit
);
319 OutputParams runparams
= runparams_in
;
320 runparams
.isLastPar
= nextpit
== paragraphs
.end();
322 if (runparams
.verbatim
) {
323 int const dist
= distance(paragraphs
.begin(), pit
);
324 Font
const outerfont
= text
.outerFont(dist
);
326 // No newline if only one paragraph in this lyxtext
332 /*bool need_par = */ pit
->latex(bparams
, outerfont
,
333 os
, texrow
, runparams
, start_pos
, end_pos
);
337 // FIXME This check should not really be needed.
338 // Perhaps we should issue an error if it is.
339 Layout
const style
= text
.inset().forcePlainLayout() ?
340 bparams
.documentClass().plainLayout() : pit
->layout();
342 runparams
.moving_arg
|= style
.needprotect
;
344 bool const maintext
= text
.isMainText();
345 // we are at the beginning of an inset and CJK is already open;
346 // we count inheritation levels to get the inset nesting right.
347 if (pit
== paragraphs
.begin() && !maintext
348 && (cjk_inherited_
> 0 || open_encoding_
== CJK
)) {
350 open_encoding_
= none
;
353 // This paragraph's language
354 Language
const * const par_language
= pit
->getParLanguage(bparams
);
355 // The document's language
356 Language
const * const doc_language
= bparams
.language
;
357 // The language that was in effect when the environment this paragraph is
358 // inside of was opened
359 Language
const * const outer_language
=
360 (runparams
.local_font
!= 0) ?
361 runparams
.local_font
->language() : doc_language
;
363 // The previous language that was in effect is the language of the
364 // previous paragraph, unless the previous paragraph is inside an
365 // environment with nesting depth greater than (or equal to, but with
366 // a different layout) the current one. If there is no previous
367 // paragraph, the previous language is the outer language.
368 bool const use_prev_env_language
= prev_env_language_
!= 0
369 && priorpit
->layout().isEnvironment()
370 && (priorpit
->getDepth() > pit
->getDepth()
371 || (priorpit
->getDepth() == pit
->getDepth()
372 && priorpit
->layout() != pit
->layout()));
373 Language
const * const prev_language
=
374 (pit
!= paragraphs
.begin())
375 ? (use_prev_env_language
? prev_env_language_
376 : priorpit
->getParLanguage(bparams
))
379 if (par_language
->babel() != prev_language
->babel()
380 // check if we already put language command in TeXEnvironment()
381 && !(style
.isEnvironment()
382 && (pit
== paragraphs
.begin() ||
383 (priorpit
->layout() != pit
->layout() &&
384 priorpit
->getDepth() <= pit
->getDepth())
385 || priorpit
->getDepth() < pit
->getDepth())))
387 if (!lyxrc
.language_command_end
.empty() &&
388 prev_language
->babel() != outer_language
->babel() &&
389 !prev_language
->babel().empty())
391 os
<< from_ascii(subst(lyxrc
.language_command_end
,
393 prev_language
->babel()))
394 // the '%' is necessary to prevent unwanted whitespace
399 // We need to open a new language if we couldn't close the previous
400 // one (because there's no language_command_end); and even if we closed
401 // the previous one, if the current language is different than the
402 // outer_language (which is currently in effect once the previous one
404 if ((lyxrc
.language_command_end
.empty() ||
405 par_language
->babel() != outer_language
->babel()) &&
406 !par_language
->babel().empty()) {
407 // If we're inside an inset, and that inset is within an \L or \R
408 // (or equivalents), then within the inset, too, any opposite
409 // language paragraph should appear within an \L or \R (in addition
410 // to, outside of, the normal language switch commands).
411 // This behavior is not correct for ArabTeX, though.
412 if ( // not for ArabTeX
413 (par_language
->lang() != "arabic_arabtex" &&
414 outer_language
->lang() != "arabic_arabtex") &&
415 // are we in an inset?
416 runparams
.local_font
!= 0 &&
417 // is the inset within an \L or \R?
419 // FIXME: currently, we don't check this; this means that
420 // we'll have unnnecessary \L and \R commands, but that
421 // doesn't seem to hurt (though latex will complain)
423 // is this paragraph in the opposite direction?
424 runparams
.local_font
->isRightToLeft() !=
425 par_language
->rightToLeft()
427 // FIXME: I don't have a working copy of the Arabi package, so
428 // I'm not sure if the farsi and arabic_arabi stuff is correct
430 if (par_language
->lang() == "farsi")
432 else if (outer_language
->lang() == "farsi")
434 else if (par_language
->lang() == "arabic_arabi")
436 else if (outer_language
->lang() == "arabic_arabi")
438 // remaining RTL languages currently is hebrew
439 else if (par_language
->rightToLeft())
444 // With CJK, the CJK tag has to be closed first (see below)
445 if (runparams
.encoding
->package() != Encoding::CJK
) {
446 os
<< from_ascii(subst(
447 lyxrc
.language_command_begin
,
449 par_language
->babel()))
450 // the '%' is necessary to prevent unwanted whitespace
457 // Switch file encoding if necessary; no need to do this for "default"
458 // encoding, since this only affects the position of the outputted
459 // \inputencoding command; the encoding switch will occur when necessary
460 if (bparams
.inputenc
== "auto" &&
461 runparams
.encoding
->package() != Encoding::none
) {
462 // Look ahead for future encoding changes.
463 // We try to output them at the beginning of the paragraph,
464 // since the \inputencoding command is not allowed e.g. in
466 for (pos_type i
= 0; i
< pit
->size(); ++i
) {
467 char_type
const c
= pit
->getChar(i
);
468 Encoding
const * const encoding
=
469 pit
->getFontSettings(bparams
, i
).language()->encoding();
470 if (encoding
->package() != Encoding::CJK
&&
471 runparams
.encoding
->package() == Encoding::inputenc
&&
476 // All characters before c are in the ASCII range, and
477 // c is non-ASCII (but no inset), so change the
478 // encoding to that required by the language of c.
479 // With CJK, only add switch if we have CJK content at the beginning
481 if (encoding
->package() != Encoding::CJK
|| i
== 0) {
482 pair
<bool, int> enc_switch
= switchEncoding(os
, bparams
, runparams
,
484 // the following is necessary after a CJK environment in a multilingual
485 // context (nesting issue).
486 if (par_language
->encoding()->package() == Encoding::CJK
&&
487 open_encoding_
!= CJK
&& cjk_inherited_
== 0) {
488 os
<< "\\begin{CJK}{" << from_ascii(par_language
->encoding()->latexName())
489 << "}{" << from_ascii(bparams
.fontsCJK
) << "}%\n";
490 open_encoding_
= CJK
;
493 if (encoding
->package() != Encoding::none
&& enc_switch
.first
) {
494 if (enc_switch
.second
> 0) {
495 // the '%' is necessary to prevent unwanted whitespace
499 // With CJK, the CJK tag had to be closed first (see above)
500 if (runparams
.encoding
->package() == Encoding::CJK
) {
501 os
<< from_ascii(subst(
502 lyxrc
.language_command_begin
,
504 par_language
->babel()))
505 // the '%' is necessary to prevent unwanted whitespace
509 runparams
.encoding
= encoding
;
516 Encoding
const * const prev_encoding
= runparams
.encoding
;
518 bool const useSetSpace
= bparams
.documentClass().provides("SetSpace");
519 if (pit
->allowParagraphCustomization()) {
520 if (pit
->params().startOfAppendix()) {
521 os
<< "\\appendix\n";
525 if (!pit
->params().spacing().isDefault()
526 && (pit
== paragraphs
.begin()
527 || !priorpit
->hasSameLayout(*pit
)))
529 os
<< from_ascii(pit
->params().spacing().writeEnvirBegin(useSetSpace
))
534 if (style
.isCommand()) {
540 switch (style
.latextype
) {
542 os
<< '\\' << from_ascii(style
.latexname());
544 // Separate handling of optional argument inset.
545 if (style
.optionalargs
> 0) {
546 int ret
= latexOptArgInsets(*pit
, os
, runparams
,
554 os
<< from_ascii(style
.latexparam());
556 case LATEX_ITEM_ENVIRONMENT
:
557 case LATEX_LIST_ENVIRONMENT
:
560 case LATEX_BIB_ENVIRONMENT
:
561 // ignore this, the inset will write itself
567 Font
const outerfont
= text
.outerFont(distance(paragraphs
.begin(), pit
));
570 os
<< from_utf8(everypar
);
571 bool need_par
= pit
->latex(bparams
, outerfont
,
572 os
, texrow
, runparams
, start_pos
, end_pos
);
574 // Make sure that \\par is done with the font of the last
575 // character if this has another size as the default.
576 // This is necessary because LaTeX (and LyX on the screen)
577 // calculates the space between the baselines according
578 // to this font. (Matthias)
580 // Is this really needed ? (Dekel)
581 // We do not need to use to change the font for the last paragraph
584 Font
const font
= pit
->empty()
585 ? pit
->getLayoutFont(bparams
, outerfont
)
586 : pit
->getFont(bparams
, pit
->size() - 1, outerfont
);
588 bool const is_command
= style
.isCommand();
590 if (style
.resfont
.size() != font
.fontInfo().size()
591 && nextpit
!= paragraphs
.end()
595 os
<< "\\" << from_ascii(font
.latexSize()) << " \\par}";
596 } else if (need_par
) {
598 } else if (is_command
) {
600 if (runparams
.encoding
!= prev_encoding
) {
601 runparams
.encoding
= prev_encoding
;
602 if (!bparams
.useXetex
)
603 os
<< setEncoding(prev_encoding
->iconvName());
607 bool pending_newline
= false;
608 switch (style
.latextype
) {
609 case LATEX_ITEM_ENVIRONMENT
:
610 case LATEX_LIST_ENVIRONMENT
:
611 if (nextpit
!= paragraphs
.end()
612 && (pit
->params().depth() < nextpit
->params().depth()))
613 pending_newline
= true;
615 case LATEX_ENVIRONMENT
: {
616 // if its the last paragraph of the current environment
617 // skip it otherwise fall through
618 if (nextpit
!= paragraphs
.end() &&
619 (nextpit
->layout() != pit
->layout()
620 || nextpit
->params().depth() != pit
->params().depth()))
624 // fall through possible
626 // we don't need it for the last paragraph!!!
627 if (nextpit
!= paragraphs
.end())
628 pending_newline
= true;
631 if (pit
->allowParagraphCustomization()) {
632 if (!pit
->params().spacing().isDefault()
633 && (nextpit
== paragraphs
.end() || !nextpit
->hasSameLayout(*pit
)))
635 if (pending_newline
) {
639 os
<< from_ascii(pit
->params().spacing().writeEnvirEnd(useSetSpace
));
640 pending_newline
= true;
644 // Closing the language is needed for the last paragraph; it is also
645 // needed if we're within an \L or \R that we may have opened above (not
646 // necessarily in this paragraph) and are about to close.
647 bool closing_rtl_ltr_environment
=
649 (par_language
->lang() != "arabic_arabtex" &&
650 outer_language
->lang() != "arabic_arabtex") &&
651 // have we opened and \L or \R environment?
652 runparams
.local_font
!= 0 &&
653 runparams
.local_font
->isRightToLeft() != par_language
->rightToLeft() &&
654 // are we about to close the language?
655 ((nextpit
!= paragraphs
.end() &&
656 par_language
->babel() !=
657 (nextpit
->getParLanguage(bparams
))->babel()) ||
658 (nextpit
== paragraphs
.end() &&
659 par_language
->babel() != outer_language
->babel()));
661 if (closing_rtl_ltr_environment
|| (nextpit
== paragraphs
.end()
662 && par_language
->babel() != outer_language
->babel())) {
663 // Since \selectlanguage write the language to the aux file,
664 // we need to reset the language at the end of footnote or
667 if (pending_newline
) {
671 // when the paragraph uses CJK, the language has to be closed earlier
672 if (font
.language()->encoding()->package() != Encoding::CJK
) {
673 if (lyxrc
.language_command_end
.empty()) {
674 // If this is a child, we should restore the
675 // master language after the last paragraph.
676 Language
const * const current_language
=
677 (nextpit
== paragraphs
.end()
678 && runparams
.master_language
)
679 ? runparams
.master_language
681 if (!current_language
->babel().empty()) {
682 os
<< from_ascii(subst(
683 lyxrc
.language_command_begin
,
685 current_language
->babel()));
686 pending_newline
= true;
688 } else if (!par_language
->babel().empty()) {
689 os
<< from_ascii(subst(
690 lyxrc
.language_command_end
,
692 par_language
->babel()));
693 pending_newline
= true;
697 if (closing_rtl_ltr_environment
)
700 if (pending_newline
) {
705 // if this is a CJK-paragraph and the next isn't, close CJK
706 // also if the next paragraph is a multilingual environment (because of nesting)
707 if (nextpit
!= paragraphs
.end() && open_encoding_
== CJK
&&
708 (nextpit
->getParLanguage(bparams
)->encoding()->package() != Encoding::CJK
||
709 (nextpit
->layout().isEnvironment() && nextpit
->isMultiLingual(bparams
)))
710 // inbetween environments, CJK has to be closed later (nesting!)
711 && (!style
.isEnvironment() || !nextpit
->layout().isEnvironment())) {
712 os
<< "\\end{CJK}\n";
713 open_encoding_
= none
;
716 // If this is the last paragraph, close the CJK environment
717 // if necessary. If it's an environment, we'll have to \end that first.
718 if (nextpit
== paragraphs
.end() && !style
.isEnvironment()) {
719 switch (open_encoding_
) {
721 // do nothing at the end of child documents
722 if (maintext
&& buf
.masterBuffer() != &buf
)
728 os
<< "\\end{CJK}\n";
733 open_encoding_
= none
;
738 open_encoding_
= none
;
748 // If this is the last paragraph, and a local_font was set upon entering
749 // the inset, and we're using "auto" or "default" encoding, the encoding
750 // should be set back to that local_font's encoding.
751 // However, do not change the encoding when XeTeX is used.
752 if (nextpit
== paragraphs
.end() && runparams_in
.local_font
!= 0
753 && runparams_in
.encoding
!= runparams_in
.local_font
->language()->encoding()
754 && (bparams
.inputenc
== "auto" || bparams
.inputenc
== "default")
755 && (!bparams
.useXetex
)) {
756 runparams_in
.encoding
= runparams_in
.local_font
->language()->encoding();
757 os
<< setEncoding(runparams_in
.encoding
->iconvName());
759 // Otherwise, the current encoding should be set for the next paragraph.
761 runparams_in
.encoding
= runparams
.encoding
;
764 // we don't need a newline for the last paragraph!!!
765 // Note from JMarc: we will re-add a \n explicitly in
766 // TeXEnvironment, because it is needed in this case
767 if (nextpit
!= paragraphs
.end()) {
768 Layout
const & next_layout
= nextpit
->layout();
769 if (style
== next_layout
770 // no blank lines before environments!
771 || !next_layout
.isEnvironment()
772 // unless there's a depth change
773 // FIXME What we really want to do here is put every \begin and \end
774 // tag on a new line (which was not the case with nested environments).
775 // But in the present state of play, we don't have access to the
776 // information whether the current TeX row is empty or not.
777 // For some ideas about how to fix this, see this thread:
778 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
779 || nextpit
->params().depth() != pit
->params().depth()) {
785 if (nextpit
!= paragraphs
.end())
786 LYXERR(Debug::LATEX
, "TeXOnePar...done " << &*nextpit
);
792 // LaTeX all paragraphs
793 void latexParagraphs(Buffer
const & buf
,
797 OutputParams
const & runparams
,
798 string
const & everypar
)
800 bool was_title
= false;
801 bool already_title
= false;
802 BufferParams
const & bparams
= buf
.params();
803 DocumentClass
const & tclass
= bparams
.documentClass();
804 ParagraphList
const & paragraphs
= text
.paragraphs();
805 ParagraphList::const_iterator par
= paragraphs
.begin();
806 ParagraphList::const_iterator endpar
= paragraphs
.end();
808 LASSERT(runparams
.par_begin
<= runparams
.par_end
, /**/);
809 // if only part of the paragraphs will be outputed
810 if (runparams
.par_begin
!= runparams
.par_end
) {
811 par
= boost::next(paragraphs
.begin(), runparams
.par_begin
);
812 endpar
= boost::next(paragraphs
.begin(), runparams
.par_end
);
813 // runparams will be passed to nested paragraphs, so
814 // we have to reset the range parameters.
815 const_cast<OutputParams
&>(runparams
).par_begin
= 0;
816 const_cast<OutputParams
&>(runparams
).par_end
= 0;
819 bool const maintext
= text
.isMainText();
820 bool const is_child
= buf
.masterBuffer() != &buf
;
822 // Open a CJK environment at the beginning of the main buffer
823 // if the document's language is a CJK language
824 // (but not in child documents)
825 if (maintext
&& !is_child
826 && bparams
.encoding().package() == Encoding::CJK
) {
827 os
<< "\\begin{CJK}{" << from_ascii(bparams
.encoding().latexName())
828 << "}{" << from_ascii(bparams
.fontsCJK
) << "}%\n";
830 open_encoding_
= CJK
;
832 // if "auto begin" is switched off, explicitely switch the
833 // language on at start
834 if (maintext
&& !lyxrc
.language_auto_begin
&&
835 !bparams
.language
->babel().empty()) {
837 os
<< from_utf8(subst(lyxrc
.language_command_begin
,
839 bparams
.language
->babel()))
844 ParagraphList::const_iterator lastpar
;
846 while (par
!= endpar
) {
848 // FIXME This check should not be needed. We should
849 // perhaps issue an error if it is.
850 Layout
const & layout
= text
.inset().forcePlainLayout() ?
851 tclass
.plainLayout() : par
->layout();
853 if (layout
.intitle
) {
855 lyxerr
<< "Error in latexParagraphs: You"
856 " should not mix title layouts"
857 " with normal ones." << endl
;
858 } else if (!was_title
) {
860 if (tclass
.titletype() == TITLE_ENVIRONMENT
) {
862 << from_ascii(tclass
.titlename())
867 } else if (was_title
&& !already_title
) {
868 if (tclass
.titletype() == TITLE_ENVIRONMENT
) {
869 os
<< "\\end{" << from_ascii(tclass
.titlename())
873 os
<< "\\" << from_ascii(tclass
.titlename())
877 already_title
= true;
881 if (layout
.isEnvironment() ||
882 !par
->params().leftIndent().zero()) {
883 par
= TeXEnvironment(buf
, text
, par
, os
,
886 par
= TeXOnePar(buf
, text
, par
, os
, texrow
,
887 runparams
, everypar
);
889 if (distance(lastpar
, par
) >= distance(lastpar
, endpar
))
893 // It might be that we only have a title in this document
894 if (was_title
&& !already_title
) {
895 if (tclass
.titletype() == TITLE_ENVIRONMENT
) {
896 os
<< "\\end{" << from_ascii(tclass
.titlename())
900 os
<< "\\" << from_ascii(tclass
.titlename())
906 // if "auto end" is switched off, explicitely close the language at the end
907 // but only if the last par is in a babel language
908 if (maintext
&& !lyxrc
.language_auto_end
&& !bparams
.language
->babel().empty() &&
909 lastpar
->getParLanguage(bparams
)->encoding()->package() != Encoding::CJK
) {
910 os
<< from_utf8(subst(lyxrc
.language_command_end
,
912 bparams
.language
->babel()))
917 // If the last paragraph is an environment, we'll have to close
918 // CJK at the very end to do proper nesting.
919 if (maintext
&& !is_child
&& open_encoding_
== CJK
) {
920 os
<< "\\end{CJK}\n";
922 open_encoding_
= none
;
925 // reset inherited encoding
926 if (cjk_inherited_
> 0) {
928 if (cjk_inherited_
== 0)
929 open_encoding_
= CJK
;
934 pair
<bool, int> switchEncoding(odocstream
& os
, BufferParams
const & bparams
,
935 OutputParams
const & runparams
, Encoding
const & newEnc
,
938 Encoding
const & oldEnc
= *runparams
.encoding
;
939 bool moving_arg
= runparams
.moving_arg
;
940 if (!force
&& ((bparams
.inputenc
!= "auto" && bparams
.inputenc
!= "default")
942 return make_pair(false, 0);
944 // Do nothing if the encoding is unchanged.
945 if (oldEnc
.name() == newEnc
.name())
946 return make_pair(false, 0);
948 // FIXME We ignore encoding switches from/to encodings that do
949 // neither support the inputenc package nor the CJK package here.
950 // This does of course only work in special cases (e.g. switch from
951 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
952 // but it is the best we can do
953 if (oldEnc
.package() == Encoding::none
954 || newEnc
.package() == Encoding::none
)
955 return make_pair(false, 0);
957 LYXERR(Debug::LATEX
, "Changing LaTeX encoding from "
958 << oldEnc
.name() << " to " << newEnc
.name());
959 os
<< setEncoding(newEnc
.iconvName());
960 if (bparams
.inputenc
== "default")
961 return make_pair(true, 0);
963 docstring
const inputenc_arg(from_ascii(newEnc
.latexName()));
964 switch (newEnc
.package()) {
966 case Encoding::japanese
:
967 // shouldn't ever reach here, see above
968 return make_pair(true, 0);
969 case Encoding::inputenc
: {
970 int count
= inputenc_arg
.length();
971 if (oldEnc
.package() == Encoding::CJK
&&
972 open_encoding_
== CJK
) {
974 open_encoding_
= none
;
977 else if (oldEnc
.package() == Encoding::inputenc
&&
978 open_encoding_
== inputenc
) {
980 open_encoding_
= none
;
983 if (runparams
.local_font
!= 0
984 && oldEnc
.package() == Encoding::CJK
) {
985 // within insets, \inputenc switches need
986 // to be embraced within \bgroup...\egroup;
990 open_encoding_
= inputenc
;
992 // with the japanese option, inputenc is omitted.
993 if (runparams
.use_japanese
)
994 return make_pair(true, count
);
995 os
<< "\\inputencoding{" << inputenc_arg
<< '}';
996 return make_pair(true, count
+ 16);
998 case Encoding::CJK
: {
999 int count
= inputenc_arg
.length();
1000 if (oldEnc
.package() == Encoding::CJK
&&
1001 open_encoding_
== CJK
) {
1005 if (oldEnc
.package() == Encoding::inputenc
&&
1006 open_encoding_
== inputenc
) {
1010 os
<< "\\begin{CJK}{" << inputenc_arg
<< "}{"
1011 << from_ascii(bparams
.fontsCJK
) << "}";
1012 open_encoding_
= CJK
;
1013 return make_pair(true, count
+ 15);
1016 // Dead code to avoid a warning:
1017 return make_pair(true, 0);