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 "paragraph_funcs.h"
25 #include "ParagraphParameters.h"
26 #include "TextClass.h"
30 #include "insets/InsetBibitem.h"
31 #include "insets/InsetOptArg.h"
33 #include "support/lassert.h"
34 #include "support/debug.h"
35 #include "support/lstrings.h"
37 #include <boost/next_prior.hpp>
40 using namespace lyx::support
;
53 static int open_encoding_
= none
;
54 static int cjk_inherited_
= 0;
57 ParagraphList::const_iterator
58 TeXEnvironment(Buffer
const & buf
,
60 ParagraphList::const_iterator pit
,
61 odocstream
& os
, TexRow
& texrow
,
62 OutputParams
const & runparams
);
64 ParagraphList::const_iterator
65 TeXOnePar(Buffer
const & buf
,
67 ParagraphList::const_iterator pit
,
68 odocstream
& os
, TexRow
& texrow
,
69 OutputParams
const & runparams
,
70 string
const & everypar
= string());
73 ParagraphList::const_iterator
74 TeXDeeper(Buffer
const & buf
,
76 ParagraphList::const_iterator pit
,
77 odocstream
& os
, TexRow
& texrow
,
78 OutputParams
const & runparams
)
80 LYXERR(Debug::LATEX
, "TeXDeeper... " << &*pit
);
81 ParagraphList::const_iterator par
= pit
;
83 ParagraphList
const & paragraphs
= text
.paragraphs();
85 while (par
!= paragraphs
.end() &&
86 par
->params().depth() == pit
->params().depth()) {
87 if (par
->layout().isEnvironment()) {
88 par
= TeXEnvironment(buf
, text
, par
,
89 os
, texrow
, runparams
);
91 par
= TeXOnePar(buf
, text
, par
,
92 os
, texrow
, runparams
);
95 LYXERR(Debug::LATEX
, "TeXDeeper...done ");
101 ParagraphList::const_iterator
102 TeXEnvironment(Buffer
const & buf
,
104 ParagraphList::const_iterator pit
,
105 odocstream
& os
, TexRow
& texrow
,
106 OutputParams
const & runparams
)
108 LYXERR(Debug::LATEX
, "TeXEnvironment... " << &*pit
);
110 BufferParams
const & bparams
= buf
.params();
112 Layout
const & style
= pit
->forcePlainLayout() ?
113 bparams
.documentClass().plainLayout() : pit
->layout();
115 ParagraphList
const & paragraphs
= text
.paragraphs();
117 Language
const * const par_language
= pit
->getParLanguage(bparams
);
118 Language
const * const doc_language
= bparams
.language
;
119 Language
const * const prev_par_language
=
120 (pit
!= paragraphs
.begin())
121 ? boost::prior(pit
)->getParLanguage(bparams
)
123 if (par_language
->babel() != prev_par_language
->babel()) {
125 if (!lyxrc
.language_command_end
.empty() &&
126 prev_par_language
->babel() != doc_language
->babel() &&
127 !prev_par_language
->babel().empty()) {
128 os
<< from_ascii(subst(
129 lyxrc
.language_command_end
,
131 prev_par_language
->babel()))
132 // the '%' is necessary to prevent unwanted whitespace
137 if ((lyxrc
.language_command_end
.empty() ||
138 par_language
->babel() != doc_language
->babel()) &&
139 !par_language
->babel().empty()) {
140 os
<< from_ascii(subst(
141 lyxrc
.language_command_begin
,
143 par_language
->babel()))
144 // the '%' is necessary to prevent unwanted whitespace
150 bool leftindent_open
= false;
151 if (!pit
->params().leftIndent().zero()) {
152 os
<< "\\begin{LyXParagraphLeftIndent}{"
153 << from_ascii(pit
->params().leftIndent().asLatexString())
156 leftindent_open
= true;
159 if (style
.isEnvironment()) {
160 os
<< "\\begin{" << from_ascii(style
.latexname()) << '}';
161 if (style
.optionalargs
> 0) {
162 int ret
= latexOptArgInsets(*pit
, os
, runparams
,
169 if (style
.latextype
== LATEX_LIST_ENVIRONMENT
) {
171 << pit
->params().labelWidthString()
173 } else if (style
.labeltype
== LABEL_BIBLIO
) {
175 os
<< '{' << bibitemWidest(buf
) << "}\n";
177 os
<< from_ascii(style
.latexparam()) << '\n';
181 // in multilingual environments, the CJK tags have to be nested properly
182 bool cjk_nested
= false;
183 if (par_language
->encoding()->package() == Encoding::CJK
&&
184 open_encoding_
!= CJK
&& pit
->isMultiLingual(bparams
)) {
185 os
<< "\\begin{CJK}{" << from_ascii(par_language
->encoding()->latexName())
186 << "}{" << from_ascii(bparams
.fontsCJK
) << "}%\n";
187 open_encoding_
= CJK
;
192 ParagraphList::const_iterator par
= pit
;
194 par
= TeXOnePar(buf
, text
, par
, os
, texrow
, runparams
);
196 if (par
== paragraphs
.end()) {
197 // Make sure that the last paragraph is
198 // correctly terminated (because TeXOnePar does
199 // not add a \n in this case)
202 } else if (par
->params().depth() > pit
->params().depth()) {
203 if (par
->layout().isParagraph()) {
205 // How to handle this? (Lgb)
206 //&& !suffixIs(os, "\n\n")
208 // There should be at least one '\n' already
209 // but we need there to be two for Standard
210 // paragraphs that are depth-increment'ed to be
211 // output correctly. However, tables can
212 // also be paragraphs so don't adjust them.
215 // Will it ever harm to have one '\n' too
216 // many? i.e. that we sometimes will have
217 // three in a row. (Lgb)
221 par
= TeXDeeper(buf
, text
, par
, os
, texrow
,
224 } while (par
!= paragraphs
.end()
225 && par
->layout() == pit
->layout()
226 && par
->params().depth() == pit
->params().depth()
227 && par
->params().leftIndent() == pit
->params().leftIndent());
229 if (open_encoding_
== CJK
&& cjk_nested
) {
230 // We need to close the encoding even if it does not change
231 // to do correct environment nesting
232 os
<< "\\end{CJK}\n";
234 open_encoding_
= none
;
237 if (style
.isEnvironment()) {
238 os
<< "\\end{" << from_ascii(style
.latexname()) << "}\n";
242 if (leftindent_open
) {
243 os
<< "\\end{LyXParagraphLeftIndent}\n";
247 if (par
!= paragraphs
.end())
248 LYXERR(Debug::LATEX
, "TeXEnvironment...done " << &*par
);
256 int latexOptArgInsets(Paragraph
const & par
, odocstream
& os
,
257 OutputParams
const & runparams
, int number
)
261 InsetList::const_iterator it
= par
.insetList().begin();
262 InsetList::const_iterator end
= par
.insetList().end();
263 for (; it
!= end
&& number
> 0 ; ++it
) {
264 if (it
->inset
->lyxCode() == OPTARG_CODE
) {
266 static_cast<InsetOptArg
*>(it
->inset
);
267 lines
+= ins
->latexOptional(os
, runparams
);
277 ParagraphList::const_iterator
278 TeXOnePar(Buffer
const & buf
,
280 ParagraphList::const_iterator
const pit
,
281 odocstream
& os
, TexRow
& texrow
,
282 OutputParams
const & runparams_in
,
283 string
const & everypar
)
285 LYXERR(Debug::LATEX
, "TeXOnePar... " << &*pit
<< " '"
287 BufferParams
const & bparams
= buf
.params();
288 ParagraphList
const & paragraphs
= text
.paragraphs();
290 ParagraphList::const_iterator priorpit
= pit
;
291 if (priorpit
!= paragraphs
.begin())
293 ParagraphList::const_iterator nextpit
= pit
;
294 if (nextpit
!= paragraphs
.end())
297 OutputParams runparams
= runparams_in
;
298 runparams
.isLastPar
= nextpit
== paragraphs
.end();
300 if (runparams
.verbatim
) {
301 int const dist
= distance(paragraphs
.begin(), pit
);
302 Font
const outerfont
= outerFont(dist
, paragraphs
);
304 // No newline if only one paragraph in this lyxtext
310 /*bool need_par = */ pit
->latex(bparams
, outerfont
,
311 os
, texrow
, runparams
);
315 Layout
const style
= pit
->forcePlainLayout() ?
316 bparams
.documentClass().plainLayout() : pit
->layout();
318 runparams
.moving_arg
|= style
.needprotect
;
320 bool const maintext
= text
.isMainText(buf
);
321 // we are at the beginning of an inset and CJK is already open;
322 // we count inheritation levels to get the inset nesting right.
323 if (pit
== paragraphs
.begin() && !maintext
324 && (cjk_inherited_
> 0 || open_encoding_
== CJK
)) {
326 open_encoding_
= none
;
329 // This paragraph's language
330 Language
const * const par_language
= pit
->getParLanguage(bparams
);
331 // The document's language
332 Language
const * const doc_language
= bparams
.language
;
333 // The language that was in effect when the environment this paragraph is
334 // inside of was opened
335 Language
const * const outer_language
=
336 (runparams
.local_font
!= 0) ?
337 runparams
.local_font
->language() : doc_language
;
338 // The previous language that was in effect is either the language of
339 // the previous paragraph, if there is one, or else the outer language
340 // if there is no previous paragraph
341 Language
const * const prev_language
=
342 (pit
!= paragraphs
.begin()) ?
343 priorpit
->getParLanguage(bparams
) : outer_language
;
345 if (par_language
->babel() != prev_language
->babel()
346 // check if we already put language command in TeXEnvironment()
347 && !(style
.isEnvironment()
348 && (pit
== paragraphs
.begin() ||
349 (priorpit
->layout() != pit
->layout() &&
350 priorpit
->getDepth() <= pit
->getDepth())
351 || priorpit
->getDepth() < pit
->getDepth())))
353 if (!lyxrc
.language_command_end
.empty() &&
354 prev_language
->babel() != outer_language
->babel() &&
355 !prev_language
->babel().empty())
357 os
<< from_ascii(subst(lyxrc
.language_command_end
,
359 prev_language
->babel()))
360 // the '%' is necessary to prevent unwanted whitespace
365 // We need to open a new language if we couldn't close the previous
366 // one (because there's no language_command_end); and even if we closed
367 // the previous one, if the current language is different than the
368 // outer_language (which is currently in effect once the previous one
370 if ((lyxrc
.language_command_end
.empty() ||
371 par_language
->babel() != outer_language
->babel()) &&
372 !par_language
->babel().empty()) {
373 // If we're inside an inset, and that inset is within an \L or \R
374 // (or equivalents), then within the inset, too, any opposite
375 // language paragraph should appear within an \L or \R (in addition
376 // to, outside of, the normal language switch commands).
377 // This behavior is not correct for ArabTeX, though.
378 if ( // not for ArabTeX
379 (par_language
->lang() != "arabic_arabtex" &&
380 outer_language
->lang() != "arabic_arabtex") &&
381 // are we in an inset?
382 runparams
.local_font
!= 0 &&
383 // is the inset within an \L or \R?
385 // FIXME: currently, we don't check this; this means that
386 // we'll have unnnecessary \L and \R commands, but that
387 // doesn't seem to hurt (though latex will complain)
389 // is this paragraph in the opposite direction?
390 runparams
.local_font
->isRightToLeft() !=
391 par_language
->rightToLeft()
393 // FIXME: I don't have a working copy of the Arabi package, so
394 // I'm not sure if the farsi and arabic_arabi stuff is correct
396 if (par_language
->lang() == "farsi")
398 else if (outer_language
->lang() == "farsi")
400 else if (par_language
->lang() == "arabic_arabi")
402 else if (outer_language
->lang() == "arabic_arabi")
404 // remaining RTL languages currently is hebrew
405 else if (par_language
->rightToLeft())
410 // With CJK, the CJK tag has to be closed first (see below)
411 if (runparams
.encoding
->package() != Encoding::CJK
) {
412 os
<< from_ascii(subst(
413 lyxrc
.language_command_begin
,
415 par_language
->babel()))
416 // the '%' is necessary to prevent unwanted whitespace
423 // Switch file encoding if necessary; no need to do this for "default"
424 // encoding, since this only affects the position of the outputted
425 // \inputencoding command; the encoding switch will occur when necessary
426 if (bparams
.inputenc
== "auto" &&
427 runparams
.encoding
->package() != Encoding::none
) {
428 // Look ahead for future encoding changes.
429 // We try to output them at the beginning of the paragraph,
430 // since the \inputencoding command is not allowed e.g. in
432 for (pos_type i
= 0; i
< pit
->size(); ++i
) {
433 char_type
const c
= pit
->getChar(i
);
434 Encoding
const * const encoding
=
435 pit
->getFontSettings(bparams
, i
).language()->encoding();
436 if (encoding
->package() != Encoding::CJK
&&
437 runparams
.encoding
->package() == Encoding::inputenc
&&
442 // All characters before c are in the ASCII range, and
443 // c is non-ASCII (but no inset), so change the
444 // encoding to that required by the language of c.
445 // With CJK, only add switch if we have CJK content at the beginning
447 if (encoding
->package() != Encoding::CJK
|| i
== 0) {
448 OutputParams tmp_rp
= runparams
;
449 runparams
.moving_arg
= false;
450 pair
<bool, int> enc_switch
= switchEncoding(os
, bparams
, runparams
,
453 // the following is necessary after a CJK environment in a multilingual
454 // context (nesting issue).
455 if (par_language
->encoding()->package() == Encoding::CJK
&&
456 open_encoding_
!= CJK
&& cjk_inherited_
== 0) {
457 os
<< "\\begin{CJK}{" << from_ascii(par_language
->encoding()->latexName())
458 << "}{" << from_ascii(bparams
.fontsCJK
) << "}%\n";
459 open_encoding_
= CJK
;
462 if (encoding
->package() != Encoding::none
&& enc_switch
.first
) {
463 if (enc_switch
.second
> 0) {
464 // the '%' is necessary to prevent unwanted whitespace
468 // With CJK, the CJK tag had to be closed first (see above)
469 if (runparams
.encoding
->package() == Encoding::CJK
) {
470 os
<< from_ascii(subst(
471 lyxrc
.language_command_begin
,
473 par_language
->babel()))
474 // the '%' is necessary to prevent unwanted whitespace
478 runparams
.encoding
= encoding
;
485 bool const useSetSpace
= bparams
.documentClass().provides("SetSpace");
486 if (pit
->allowParagraphCustomization()) {
487 if (pit
->params().startOfAppendix()) {
488 os
<< "\\appendix\n";
492 if (!pit
->params().spacing().isDefault()
493 && (pit
== paragraphs
.begin()
494 || !priorpit
->hasSameLayout(*pit
)))
496 os
<< from_ascii(pit
->params().spacing().writeEnvirBegin(useSetSpace
))
501 if (style
.isCommand()) {
507 switch (style
.latextype
) {
509 os
<< '\\' << from_ascii(style
.latexname());
511 // Separate handling of optional argument inset.
512 if (style
.optionalargs
> 0) {
513 int ret
= latexOptArgInsets(*pit
, os
, runparams
,
521 os
<< from_ascii(style
.latexparam());
523 case LATEX_ITEM_ENVIRONMENT
:
524 case LATEX_LIST_ENVIRONMENT
:
527 case LATEX_BIB_ENVIRONMENT
:
528 // ignore this, the inset will write itself
534 Font
const outerfont
= outerFont(distance(paragraphs
.begin(), pit
),
538 os
<< from_utf8(everypar
);
539 bool need_par
= pit
->latex(bparams
, outerfont
,
540 os
, texrow
, runparams
);
542 // Make sure that \\par is done with the font of the last
543 // character if this has another size as the default.
544 // This is necessary because LaTeX (and LyX on the screen)
545 // calculates the space between the baselines according
546 // to this font. (Matthias)
548 // Is this really needed ? (Dekel)
549 // We do not need to use to change the font for the last paragraph
552 Font
const font
= pit
->empty()
553 ? pit
->getLayoutFont(bparams
, outerfont
)
554 : pit
->getFont(bparams
, pit
->size() - 1, outerfont
);
556 bool is_command
= style
.isCommand();
558 if (style
.resfont
.size() != font
.fontInfo().size()
559 && nextpit
!= paragraphs
.end()
563 os
<< "\\" << from_ascii(font
.latexSize()) << " \\par}";
564 } else if (need_par
) {
566 } else if (is_command
)
569 bool pending_newline
= false;
570 switch (style
.latextype
) {
571 case LATEX_ITEM_ENVIRONMENT
:
572 case LATEX_LIST_ENVIRONMENT
:
573 if (nextpit
!= paragraphs
.end()
574 && (pit
->params().depth() < nextpit
->params().depth()))
575 pending_newline
= true;
577 case LATEX_ENVIRONMENT
: {
578 // if its the last paragraph of the current environment
579 // skip it otherwise fall through
580 ParagraphList::const_iterator next
= nextpit
;
582 if (next
!= paragraphs
.end() && (next
->layout() != pit
->layout()
583 || next
->params().depth() != pit
->params().depth()))
587 // fall through possible
589 // we don't need it for the last paragraph!!!
590 if (nextpit
!= paragraphs
.end())
591 pending_newline
= true;
594 if (pit
->allowParagraphCustomization()) {
595 if (!pit
->params().spacing().isDefault()
596 && (nextpit
== paragraphs
.end() || !nextpit
->hasSameLayout(*pit
)))
598 if (pending_newline
) {
602 os
<< from_ascii(pit
->params().spacing().writeEnvirEnd(useSetSpace
));
603 pending_newline
= true;
607 // Closing the language is needed for the last paragraph; it is also
608 // needed if we're within an \L or \R that we may have opened above (not
609 // necessarily in this paragraph) and are about to close.
610 bool closing_rtl_ltr_environment
=
612 (par_language
->lang() != "arabic_arabtex" &&
613 outer_language
->lang() != "arabic_arabtex") &&
614 // have we opened and \L or \R environment?
615 runparams
.local_font
!= 0 &&
616 runparams
.local_font
->isRightToLeft() != par_language
->rightToLeft() &&
617 // are we about to close the language?
618 ((nextpit
!= paragraphs
.end() &&
619 par_language
->babel() !=
620 (nextpit
->getParLanguage(bparams
))->babel()) ||
621 (nextpit
== paragraphs
.end() &&
622 par_language
->babel() != outer_language
->babel()));
624 if (closing_rtl_ltr_environment
|| (nextpit
== paragraphs
.end()
625 && par_language
->babel() != outer_language
->babel())) {
626 // Since \selectlanguage write the language to the aux file,
627 // we need to reset the language at the end of footnote or
630 if (pending_newline
) {
634 // when the paragraph uses CJK, the language has to be closed earlier
635 if (font
.language()->encoding()->package() != Encoding::CJK
) {
636 if (lyxrc
.language_command_end
.empty()) {
637 if (!prev_language
->babel().empty()) {
638 os
<< from_ascii(subst(
639 lyxrc
.language_command_begin
,
641 prev_language
->babel()));
642 pending_newline
= true;
644 } else if (!par_language
->babel().empty()) {
645 os
<< from_ascii(subst(
646 lyxrc
.language_command_end
,
648 par_language
->babel()));
649 pending_newline
= true;
653 if (closing_rtl_ltr_environment
)
656 if (pending_newline
) {
661 // if this is a CJK-paragraph and the next isn't, close CJK
662 // also if the next paragraph is a multilingual environment (because of nesting)
663 if (nextpit
!= paragraphs
.end() && open_encoding_
== CJK
&&
664 (nextpit
->getParLanguage(bparams
)->encoding()->package() != Encoding::CJK
||
665 nextpit
->layout().isEnvironment() && nextpit
->isMultiLingual(bparams
))
666 // in environments, CJK has to be closed later (nesting!)
667 && !style
.isEnvironment()) {
668 os
<< "\\end{CJK}\n";
669 open_encoding_
= none
;
672 // If this is the last paragraph, close the CJK environment
673 // if necessary. If it's an environment, we'll have to \end that first.
674 if (nextpit
== paragraphs
.end() && !style
.isEnvironment()) {
675 switch (open_encoding_
) {
677 // do nothing at the end of child documents
678 if (maintext
&& buf
.masterBuffer() != &buf
)
684 os
<< "\\end{CJK}\n";
689 open_encoding_
= none
;
694 open_encoding_
= none
;
704 // If this is the last paragraph, and a local_font was set upon entering
705 // the inset, and we're using "auto" or "default" encoding, the encoding
706 // should be set back to that local_font's encoding.
707 if (nextpit
== paragraphs
.end() && runparams_in
.local_font
!= 0
708 && runparams_in
.encoding
!= runparams_in
.local_font
->language()->encoding()
709 && (bparams
.inputenc
== "auto" || bparams
.inputenc
== "default")) {
710 runparams_in
.encoding
= runparams_in
.local_font
->language()->encoding();
711 os
<< setEncoding(runparams_in
.encoding
->iconvName());
713 // Otherwise, the current encoding should be set for the next paragraph.
715 runparams_in
.encoding
= runparams
.encoding
;
718 // we don't need it for the last paragraph!!!
719 // Note from JMarc: we will re-add a \n explicitely in
720 // TeXEnvironment, because it is needed in this case
721 if (nextpit
!= paragraphs
.end()) {
722 Layout
const & next_layout
= nextpit
->layout();
723 // no blank lines before environments!
724 if (!next_layout
.isEnvironment() || style
== next_layout
) {
730 if (nextpit
!= paragraphs
.end())
731 LYXERR(Debug::LATEX
, "TeXOnePar...done " << &*nextpit
);
739 // LaTeX all paragraphs
740 void latexParagraphs(Buffer
const & buf
,
744 OutputParams
const & runparams
,
745 string
const & everypar
)
747 bool was_title
= false;
748 bool already_title
= false;
749 BufferParams
const & bparams
= buf
.params();
750 DocumentClass
const & tclass
= bparams
.documentClass();
751 ParagraphList
const & paragraphs
= text
.paragraphs();
752 ParagraphList::const_iterator par
= paragraphs
.begin();
753 ParagraphList::const_iterator endpar
= paragraphs
.end();
755 LASSERT(runparams
.par_begin
<= runparams
.par_end
, /**/);
756 // if only part of the paragraphs will be outputed
757 if (runparams
.par_begin
!= runparams
.par_end
) {
758 par
= boost::next(paragraphs
.begin(), runparams
.par_begin
);
759 endpar
= boost::next(paragraphs
.begin(), runparams
.par_end
);
760 // runparams will be passed to nested paragraphs, so
761 // we have to reset the range parameters.
762 const_cast<OutputParams
&>(runparams
).par_begin
= 0;
763 const_cast<OutputParams
&>(runparams
).par_end
= 0;
766 bool const maintext
= text
.isMainText(buf
);
767 bool const is_child
= buf
.masterBuffer() != &buf
;
769 // Open a CJK environment at the beginning of the main buffer
770 // if the document's language is a CJK language
771 // (but not in child documents)
772 if (maintext
&& !is_child
773 && bparams
.encoding().package() == Encoding::CJK
) {
774 os
<< "\\begin{CJK}{" << from_ascii(bparams
.encoding().latexName())
775 << "}{" << from_ascii(bparams
.fontsCJK
) << "}%\n";
777 open_encoding_
= CJK
;
779 // if "auto begin" is switched off, explicitely switch the
780 // language on at start
781 if (maintext
&& !lyxrc
.language_auto_begin
&&
782 !bparams
.language
->babel().empty()) {
784 os
<< from_utf8(subst(lyxrc
.language_command_begin
,
786 bparams
.language
->babel()))
791 ParagraphList::const_iterator lastpar
;
793 while (par
!= endpar
) {
795 Layout
const & layout
= par
->forcePlainLayout() ?
796 tclass
.plainLayout() :
799 if (layout
.intitle
) {
801 lyxerr
<< "Error in latexParagraphs: You"
802 " should not mix title layouts"
803 " with normal ones." << endl
;
804 } else if (!was_title
) {
806 if (tclass
.titletype() == TITLE_ENVIRONMENT
) {
808 << from_ascii(tclass
.titlename())
813 } else if (was_title
&& !already_title
) {
814 if (tclass
.titletype() == TITLE_ENVIRONMENT
) {
815 os
<< "\\end{" << from_ascii(tclass
.titlename())
819 os
<< "\\" << from_ascii(tclass
.titlename())
823 already_title
= true;
827 if (layout
.isEnvironment() ||
828 !par
->params().leftIndent().zero()) {
829 par
= TeXEnvironment(buf
, text
, par
, os
,
832 par
= TeXOnePar(buf
, text
, par
, os
, texrow
,
833 runparams
, everypar
);
835 if (distance(lastpar
, par
) >= distance(lastpar
, endpar
))
839 // It might be that we only have a title in this document
840 if (was_title
&& !already_title
) {
841 if (tclass
.titletype() == TITLE_ENVIRONMENT
) {
842 os
<< "\\end{" << from_ascii(tclass
.titlename())
846 os
<< "\\" << from_ascii(tclass
.titlename())
852 // if "auto end" is switched off, explicitely close the language at the end
853 // but only if the last par is in a babel language
854 if (maintext
&& !lyxrc
.language_auto_end
&& !bparams
.language
->babel().empty() &&
855 lastpar
->getParLanguage(bparams
)->encoding()->package() != Encoding::CJK
) {
856 os
<< from_utf8(subst(lyxrc
.language_command_end
,
858 bparams
.language
->babel()))
863 // If the last paragraph is an environment, we'll have to close
864 // CJK at the very end to do proper nesting.
865 if (maintext
&& !is_child
&& open_encoding_
== CJK
) {
866 os
<< "\\end{CJK}\n";
868 open_encoding_
= none
;
871 // reset inherited encoding
872 if (cjk_inherited_
> 0) {
874 if (cjk_inherited_
== 0)
875 open_encoding_
= CJK
;
880 pair
<bool, int> switchEncoding(odocstream
& os
, BufferParams
const & bparams
,
881 OutputParams
const & runparams
, Encoding
const & newEnc
,
884 Encoding
const & oldEnc
= *runparams
.encoding
;
885 bool moving_arg
= runparams
.moving_arg
;
886 if (!force
&& ((bparams
.inputenc
!= "auto" && bparams
.inputenc
!= "default")
888 return make_pair(false, 0);
890 // Do nothing if the encoding is unchanged.
891 if (oldEnc
.name() == newEnc
.name())
892 return make_pair(false, 0);
894 // FIXME We ignore encoding switches from/to encodings that do
895 // neither support the inputenc package nor the CJK package here.
896 // This does of course only work in special cases (e.g. switch from
897 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
898 // but it is the best we can do
899 if (oldEnc
.package() == Encoding::none
900 || newEnc
.package() == Encoding::none
)
901 return make_pair(false, 0);
903 LYXERR(Debug::LATEX
, "Changing LaTeX encoding from "
904 << oldEnc
.name() << " to " << newEnc
.name());
905 os
<< setEncoding(newEnc
.iconvName());
906 if (bparams
.inputenc
== "default")
907 return make_pair(true, 0);
909 docstring
const inputenc_arg(from_ascii(newEnc
.latexName()));
910 switch (newEnc
.package()) {
912 case Encoding::japanese
:
913 // shouldn't ever reach here, see above
914 return make_pair(true, 0);
915 case Encoding::inputenc
: {
916 int count
= inputenc_arg
.length();
917 if (oldEnc
.package() == Encoding::CJK
&&
918 open_encoding_
== CJK
) {
920 open_encoding_
= none
;
923 else if (oldEnc
.package() == Encoding::inputenc
&&
924 open_encoding_
== inputenc
) {
926 open_encoding_
= none
;
929 if (runparams
.local_font
!= 0
930 && oldEnc
.package() == Encoding::CJK
) {
931 // within insets, \inputenc switches need
932 // to be embraced within \bgroup...\egroup;
936 open_encoding_
= inputenc
;
938 // with the japanese option, inputenc is omitted.
939 if (runparams
.use_japanese
)
940 return make_pair(true, count
);
941 os
<< "\\inputencoding{" << inputenc_arg
<< '}';
942 return make_pair(true, count
+ 16);
944 case Encoding::CJK
: {
945 int count
= inputenc_arg
.length();
946 if (oldEnc
.package() == Encoding::CJK
&&
947 open_encoding_
== CJK
) {
951 if (oldEnc
.package() == Encoding::inputenc
&&
952 open_encoding_
== inputenc
) {
956 os
<< "\\begin{CJK}{" << inputenc_arg
<< "}{"
957 << from_ascii(bparams
.fontsCJK
) << "}";
958 open_encoding_
= CJK
;
959 return make_pair(true, count
+ 15);
962 // Dead code to avoid a warning:
963 return make_pair(true, 0);