]> git.lyx.org Git - lyx.git/blob - src/output_latex.C
f7acbf5dedf96e55c0af5ad8c729d95b3a12d893
[lyx.git] / src / output_latex.C
1 /**
2  * \file output_latex.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "output_latex.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "debug.h"
18 #include "encoding.h"
19 #include "language.h"
20 #include "lyxrc.h"
21 #include "outputparams.h"
22 #include "paragraph.h"
23 #include "paragraph_funcs.h"
24 #include "ParagraphParameters.h"
25 #include "texrow.h"
26 #include "vspace.h"
27
28 #include "insets/insetbibitem.h"
29 #include "insets/insetoptarg.h"
30
31 #include "support/lstrings.h"
32 #include "support/unicode.h"
33
34
35 namespace lyx {
36
37 using support::subst;
38
39 using std::endl;
40 using std::string;
41
42
43 namespace {
44
45 ParagraphList::const_iterator
46 TeXEnvironment(Buffer const & buf,
47                ParagraphList const & paragraphs,
48                ParagraphList::const_iterator pit,
49                odocstream & os, TexRow & texrow,
50                OutputParams const & runparams);
51
52 ParagraphList::const_iterator
53 TeXOnePar(Buffer const & buf,
54           ParagraphList const & paragraphs,
55           ParagraphList::const_iterator pit,
56           odocstream & os, TexRow & texrow,
57           OutputParams const & runparams,
58           string const & everypar = string());
59
60
61 ParagraphList::const_iterator
62 TeXDeeper(Buffer const & buf,
63           ParagraphList const & paragraphs,
64           ParagraphList::const_iterator pit,
65           odocstream & os, TexRow & texrow,
66           OutputParams const & runparams)
67 {
68         lyxerr[Debug::LATEX] << "TeXDeeper...     " << &*pit << endl;
69         ParagraphList::const_iterator par = pit;
70
71         while (par != paragraphs.end() &&
72                      par->params().depth() == pit->params().depth()) {
73                 if (par->layout()->isEnvironment()) {
74                         par = TeXEnvironment(buf, paragraphs, par,
75                                              os, texrow, runparams);
76                 } else {
77                         par = TeXOnePar(buf, paragraphs, par,
78                                              os, texrow, runparams);
79                 }
80         }
81         lyxerr[Debug::LATEX] << "TeXDeeper...done " << endl;
82
83         return par;
84 }
85
86
87 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
88                       odocstream & os, OutputParams const & runparams, int number);
89
90
91 ParagraphList::const_iterator
92 TeXEnvironment(Buffer const & buf,
93                ParagraphList const & paragraphs,
94                ParagraphList::const_iterator pit,
95                odocstream & os, TexRow & texrow,
96                OutputParams const & runparams)
97 {
98         lyxerr[Debug::LATEX] << "TeXEnvironment...     " << &*pit << endl;
99
100         BufferParams const & bparams = buf.params();
101
102         LyXLayout_ptr const & style = pit->layout();
103
104         Language const * language = pit->getParLanguage(bparams);
105         Language const * doc_language = bparams.language;
106         Language const * previous_language =
107                 (pit != paragraphs.begin())
108                 ? boost::prior(pit)->getParLanguage(bparams)
109                 : doc_language;
110         if (language->babel() != previous_language->babel()) {
111
112                 if (!lyxrc.language_command_end.empty() &&
113                     previous_language->babel() != doc_language->babel()) {
114                         os << from_ascii(subst(
115                                 lyxrc.language_command_end,
116                                 "$$lang",
117                                 previous_language->babel()))
118                            << '\n';
119                         texrow.newline();
120                 }
121
122                 if (lyxrc.language_command_end.empty() ||
123                     language->babel() != doc_language->babel()) {
124                         os << from_ascii(subst(
125                                 lyxrc.language_command_begin,
126                                 "$$lang",
127                                 language->babel()))
128                            << '\n';
129                         texrow.newline();
130                 }
131         }
132
133         bool leftindent_open = false;
134         if (!pit->params().leftIndent().zero()) {
135                 os << "\\begin{LyXParagraphLeftIndent}{"
136                    << from_ascii(pit->params().leftIndent().asLatexString())
137                    << "}\n";
138                 texrow.newline();
139                 leftindent_open = true;
140         }
141
142         if (style->isEnvironment()) {
143                 os << "\\begin{" << from_ascii(style->latexname()) << '}';
144                 if (style->optionalargs > 0) {
145                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
146                                                     style->optionalargs);
147                         while (ret > 0) {
148                                 texrow.newline();
149                                 --ret;
150                         }
151                 }
152                 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
153                         os << '{'
154                            << pit->params().labelWidthString()
155                            << "}\n";
156                 } else if (style->labeltype == LABEL_BIBLIO) {
157                         // ale970405
158                         os << '{' << bibitemWidest(buf) << "}\n";
159                 } else
160                         os << from_ascii(style->latexparam()) << '\n';
161                 texrow.newline();
162         }
163         ParagraphList::const_iterator par = pit;
164         do {
165                 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
166
167                 if (par == paragraphs.end()) {
168                         // Make sure that the last paragraph is
169                         // correctly terminated (because TeXOnePar does
170                         // not add a \n in this case)
171                         os << '\n';
172                         texrow.newline();
173                 } else if (par->params().depth() > pit->params().depth()) {
174                             if (par->layout()->isParagraph()) {
175
176                             // Thinko!
177                             // How to handle this? (Lgb)
178                             //&& !suffixIs(os, "\n\n")
179                                     //) {
180                                 // There should be at least one '\n' already
181                                 // but we need there to be two for Standard
182                                 // paragraphs that are depth-increment'ed to be
183                                 // output correctly.  However, tables can
184                                 // also be paragraphs so don't adjust them.
185                                 // ARRae
186                                 // Thinkee:
187                                 // Will it ever harm to have one '\n' too
188                                 // many? i.e. that we sometimes will have
189                                 // three in a row. (Lgb)
190                                 os << '\n';
191                                 texrow.newline();
192                         }
193                         par = TeXDeeper(buf, paragraphs, par, os, texrow,
194                                         runparams);
195                 }
196         } while (par != paragraphs.end()
197                  && par->layout() == pit->layout()
198                  && par->params().depth() == pit->params().depth()
199                  && par->params().leftIndent() == pit->params().leftIndent());
200
201         if (style->isEnvironment()) {
202                 os << "\\end{" << from_ascii(style->latexname()) << "}\n";
203                 texrow.newline();
204         }
205
206         if (leftindent_open) {
207                 os << "\\end{LyXParagraphLeftIndent}\n";
208                 texrow.newline();
209         }
210
211         if (par != paragraphs.end() && lyxerr.debugging(Debug::LATEX))
212                 lyxerr << "TeXEnvironment...done " << &*par << endl;
213         return par;
214 }
215
216
217 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
218                       odocstream & os, OutputParams const & runparams, int number)
219 {
220         int lines = 0;
221
222         InsetList::const_iterator it = par.insetlist.begin();
223         InsetList::const_iterator end = par.insetlist.end();
224         for (; it != end && number > 0 ; ++it) {
225                 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) {
226                         InsetOptArg * ins =
227                                 static_cast<InsetOptArg *>(it->inset);
228                         lines += ins->latexOptional(buf, os, runparams);
229                         --number;
230                 }
231         }
232         return lines;
233 }
234
235
236 ParagraphList::const_iterator
237 TeXOnePar(Buffer const & buf,
238           ParagraphList const & paragraphs,
239           ParagraphList::const_iterator pit,
240           odocstream & ucs4, TexRow & texrow,
241           OutputParams const & runparams_in,
242           string const & everypar)
243 {
244         lyxerr[Debug::LATEX] << "TeXOnePar...     " << &*pit << " '"
245                 << everypar << "'" << endl;
246         BufferParams const & bparams = buf.params();
247         bool further_blank_line = false;
248         LyXLayout_ptr style;
249
250         // In an inset with unlimited length (all in one row),
251         // force layout to default
252         if (!pit->forceDefaultParagraphs())
253                 style = pit->layout();
254         else
255                 style = bparams.getLyXTextClass().defaultLayout();
256
257         OutputParams runparams = runparams_in;
258         runparams.moving_arg |= style->needprotect;
259
260         Language const * language = pit->getParLanguage(bparams);
261         Language const * doc_language = bparams.language;
262         Language const * previous_language =
263                 (pit != paragraphs.begin())
264                 ? boost::prior(pit)->getParLanguage(bparams)
265                 : doc_language;
266
267         if (language->babel() != previous_language->babel()
268             // check if we already put language command in TeXEnvironment()
269             && !(style->isEnvironment()
270                  && (pit == paragraphs.begin() ||
271                      (boost::prior(pit)->layout() != pit->layout() &&
272                       boost::prior(pit)->getDepth() <= pit->getDepth())
273                      || boost::prior(pit)->getDepth() < pit->getDepth())))
274         {
275                 if (!lyxrc.language_command_end.empty() &&
276                     previous_language->babel() != doc_language->babel())
277                 {
278                         ucs4 << from_ascii(subst(lyxrc.language_command_end,
279                                 "$$lang",
280                                 previous_language->babel()))
281                              << '\n';
282                         texrow.newline();
283                 }
284
285                 if (lyxrc.language_command_end.empty() ||
286                     language->babel() != doc_language->babel())
287                 {
288                         ucs4 << from_ascii(subst(
289                                 lyxrc.language_command_begin,
290                                 "$$lang",
291                                 language->babel()))
292                              << '\n';
293                         texrow.newline();
294                 }
295         }
296
297         // FIXME thailatex does not support the inputenc package, so we
298         // ignore switches from/to tis620-0 encoding here. This does of
299         // course only work as long as the non-thai text contains ASCII
300         // only, but it is the best we can do.
301         bool const use_thailatex = (language->encoding()->name() == "tis620-0" ||
302                                     previous_language->encoding()->name() == "tis620-0");
303         if (bparams.inputenc == "auto" &&
304             language->encoding() != previous_language->encoding() &&
305             !use_thailatex) {
306                 ucs4 << "\\inputencoding{"
307                      << from_ascii(language->encoding()->latexName())
308                      << "}\n";
309                 texrow.newline();
310         }
311         // We need to output the paragraph to a temporary stream if we
312         // need to change the encoding. Don't do this if the result does
313         // not go to a file but to the builtin source viewer.
314         odocstringstream par_stream;
315         bool const change_encoding = !runparams_in.dryrun &&
316                         bparams.inputenc == "auto" &&
317                         language->encoding() != doc_language->encoding() &&
318                         !use_thailatex;
319         // don't trigger the copy ctor because it's private on msvc 
320         odocstream & os = *(change_encoding ? &par_stream : &ucs4);
321
322         // In an inset with unlimited length (all in one row),
323         // don't allow any special options in the paragraph
324         if (!pit->forceDefaultParagraphs()) {
325                 if (pit->params().startOfAppendix()) {
326                         os << "\\appendix\n";
327                         texrow.newline();
328                 }
329
330                 if (!pit->params().spacing().isDefault()
331                         && (pit == paragraphs.begin()
332                             || !boost::prior(pit)->hasSameLayout(*pit)))
333                 {
334                         os << from_ascii(pit->params().spacing().writeEnvirBegin())
335                             << '\n';
336                         texrow.newline();
337                 }
338
339                 if (style->isCommand()) {
340                         os << '\n';
341                         texrow.newline();
342                 }
343
344                 if (further_blank_line) {
345                         os << '\n';
346                         texrow.newline();
347                 }
348         }
349
350         switch (style->latextype) {
351         case LATEX_COMMAND:
352                 os << '\\' << from_ascii(style->latexname());
353
354                 // Separate handling of optional argument inset.
355                 if (style->optionalargs > 0) {
356                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
357                                                     style->optionalargs);
358                         while (ret > 0) {
359                                 texrow.newline();
360                                 --ret;
361                         }
362                 }
363                 else
364                         os << from_ascii(style->latexparam());
365                 break;
366         case LATEX_ITEM_ENVIRONMENT:
367         case LATEX_LIST_ENVIRONMENT:
368                 os << "\\item ";
369                 break;
370         case LATEX_BIB_ENVIRONMENT:
371                 // ignore this, the inset will write itself
372                 break;
373         default:
374                 break;
375         }
376
377         // FIXME UNICODE
378         os << from_utf8(everypar);
379         LyXFont const outerfont =
380                 outerFont(std::distance(paragraphs.begin(), pit),
381                           paragraphs);
382         bool need_par = pit->simpleTeXOnePar(buf, bparams, outerfont,
383                                              os, texrow, runparams);
384
385         // Make sure that \\par is done with the font of the last
386         // character if this has another size as the default.
387         // This is necessary because LaTeX (and LyX on the screen)
388         // calculates the space between the baselines according
389         // to this font. (Matthias)
390         //
391         // Is this really needed ? (Dekel)
392         // We do not need to use to change the font for the last paragraph
393         // or for a command.
394
395         LyXFont const font =
396                 (pit->empty()
397                  ? pit->getLayoutFont(bparams, outerfont)
398                  : pit->getFont(bparams, pit->size() - 1, outerfont));
399
400         bool is_command = style->isCommand();
401
402         if (style->resfont.size() != font.size()
403             && boost::next(pit) != paragraphs.end()
404             && !is_command) {
405                 if (!need_par)
406                         os << '{';
407                 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
408         } else if (need_par) {
409                 os << "\\par}";
410         } else if (is_command)
411                 os << '}';
412
413         switch (style->latextype) {
414         case LATEX_ITEM_ENVIRONMENT:
415         case LATEX_LIST_ENVIRONMENT:
416                 if (boost::next(pit) != paragraphs.end()
417                     && (pit->params().depth() < boost::next(pit)->params().depth())) {
418                         os << '\n';
419                         texrow.newline();
420                 }
421                 break;
422         case LATEX_ENVIRONMENT: {
423                 // if its the last paragraph of the current environment
424                 // skip it otherwise fall through
425                 ParagraphList::const_iterator next = boost::next(pit);
426
427                 if (next != paragraphs.end()
428                     && (next->layout() != pit->layout()
429                         || next->params().depth() != pit->params().depth()))
430                         break;
431         }
432
433                 // fall through possible
434         default:
435                 // we don't need it for the last paragraph!!!
436                 if (boost::next(pit) != paragraphs.end()) {
437                         os << '\n';
438                         texrow.newline();
439                 }
440         }
441
442         if (!pit->forceDefaultParagraphs()) {
443                 further_blank_line = false;
444
445                 if (further_blank_line) {
446                         os << '\n';
447                         texrow.newline();
448                 }
449
450                 if (!pit->params().spacing().isDefault()
451                         && (boost::next(pit) == paragraphs.end()
452                             || !boost::next(pit)->hasSameLayout(*pit)))
453                 {
454                         os << from_ascii(pit->params().spacing().writeEnvirEnd())
455                            << '\n';
456                         texrow.newline();
457                 }
458         }
459
460         if (boost::next(pit) == paragraphs.end()
461             && language->babel() != doc_language->babel()) {
462                 // Since \selectlanguage write the language to the aux file,
463                 // we need to reset the language at the end of footnote or
464                 // float.
465
466                 if (lyxrc.language_command_end.empty())
467                         os << from_ascii(subst(
468                                 lyxrc.language_command_begin,
469                                 "$$lang",
470                                 doc_language->babel()))
471                            << '\n';
472                 else
473                         os << from_ascii(subst(
474                                 lyxrc.language_command_end,
475                                 "$$lang",
476                                 language->babel()))
477                            << '\n';
478                 texrow.newline();
479         }
480
481         // we don't need it for the last paragraph!!!
482         // Note from JMarc: we will re-add a \n explicitely in
483         // TeXEnvironment, because it is needed in this case
484         if (boost::next(pit) != paragraphs.end()) {
485                 os << '\n';
486                 texrow.newline();
487         }
488
489         if (boost::next(pit) != paragraphs.end() &&
490             lyxerr.debugging(Debug::LATEX))
491                 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
492
493         if (change_encoding) {
494                 lyxerr[Debug::LATEX] << "Converting paragraph to encoding "
495                         << language->encoding()->iconvName() << endl;
496                 docstring const par = par_stream.str();
497                 // Convert the paragraph to the 8bit encoding that we need to
498                 // output.
499                 std::vector<char> const encoded = lyx::ucs4_to_eightbit(par.c_str(),
500                         par.size(), language->encoding()->iconvName());
501                 // Interpret this as if it was in the 8 bit encoding of the
502                 // document language and convert it back to UCS4. That means
503                 // that faked does not contain pure UCS4 anymore, but what
504                 // will be written to the output file will be correct, because
505                 // the real output stream will do a UCS4 -> document language
506                 // encoding conversion.
507                 // This is of course a hack, but not a bigger one than mixing
508                 // two encodings in one file.
509                 // FIXME: Catch iconv conversion errors and display an error
510                 // dialog.
511
512                 // Here follows an explanation how I (gb) came to the current
513                 // solution:
514
515                 // codecvt facets are only used by file streams -> OK, maybe
516                 // we could use file streams and not generic streams in the
517                 // latex() methods? No, that does not  work, we use them at
518                 // several places to write to string streams.
519                 // Next try: Maybe we could do something else than codecvt
520                 // in our streams, and  add a setEncoding() method? That
521                 // does not work unless we rebuild the functionality of file
522                 // and string streams, since both odocfstream and
523                 // odocstringstream inherit from std::basic_ostream<docstring>
524                 // and we can  neither add a method to that class nor change
525                 // the inheritance of the file and string streams.
526
527                 // What might be possible is to encapsulate the real file and
528                 // string streams in our own version, and use a homemade
529                 // streambuf that would do the encoding conversion and then
530                 // forward to the real stream. That would probably work, but
531                 // would require far more code and a good understanding of
532                 // stream buffers to get it right.
533
534                 // Another idea by JMarc is to use a modifier like
535                 // os << setencoding("iso-8859-1");
536                 // That currently looks like the best idea.
537
538                 std::vector<char_type> const faked = lyx::eightbit_to_ucs4(&(encoded[0]),
539                         encoded.size(), doc_language->encoding()->iconvName());
540                 std::vector<char_type>::const_iterator const end = faked.end();
541                 std::vector<char_type>::const_iterator it = faked.begin();
542                 for (; it != end; ++it)
543                         ucs4.put(*it);
544         }
545
546         return ++pit;
547 }
548
549 } // anon namespace
550
551
552 // LaTeX all paragraphs
553 void latexParagraphs(Buffer const & buf,
554                      ParagraphList const & paragraphs,
555                      odocstream & os,
556                      TexRow & texrow,
557                      OutputParams const & runparams,
558                      string const & everypar)
559 {
560         bool was_title = false;
561         bool already_title = false;
562         LyXTextClass const & tclass = buf.params().getLyXTextClass();
563         ParagraphList::const_iterator par = paragraphs.begin();
564         ParagraphList::const_iterator endpar = paragraphs.end();
565
566         BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
567         // if only part of the paragraphs will be outputed
568         if (runparams.par_begin !=  runparams.par_end) {
569                 par = boost::next(paragraphs.begin(), runparams.par_begin);
570                 endpar = boost::next(paragraphs.begin(), runparams.par_end);
571                 // runparams will be passed to nested paragraphs, so
572                 // we have to reset the range parameters.
573                 const_cast<OutputParams&>(runparams).par_begin = 0;
574                 const_cast<OutputParams&>(runparams).par_end = 0;
575         }
576
577         // if only_body
578         while (par != endpar) {
579                 ParagraphList::const_iterator lastpar = par;
580                 // well we have to check if we are in an inset with unlimited
581                 // length (all in one row) if that is true then we don't allow
582                 // any special options in the paragraph and also we don't allow
583                 // any environment other then "Standard" to be valid!
584                 if (!par->forceDefaultParagraphs()) {
585                         LyXLayout_ptr const & layout = par->layout();
586
587                         if (layout->intitle) {
588                                 if (already_title) {
589                                         lyxerr << "Error in latexParagraphs: You"
590                                                 " should not mix title layouts"
591                                                 " with normal ones." << endl;
592                                 } else if (!was_title) {
593                                         was_title = true;
594                                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
595                                                 os << "\\begin{"
596                                                     << from_ascii(tclass.titlename())
597                                                     << "}\n";
598                                                 texrow.newline();
599                                         }
600                                 }
601                         } else if (was_title && !already_title) {
602                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
603                                         os << "\\end{" << from_ascii(tclass.titlename())
604                                             << "}\n";
605                                 }
606                                 else {
607                                         os << "\\" << from_ascii(tclass.titlename())
608                                             << "\n";
609                                 }
610                                 texrow.newline();
611                                 already_title = true;
612                                 was_title = false;
613                         }
614
615                         if (layout->is_environment) {
616                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
617                                                 runparams, everypar);
618                         } else if (layout->isEnvironment() ||
619                                 !par->params().leftIndent().zero())
620                         {
621                                 par = TeXEnvironment(buf, paragraphs, par, os,
622                                                      texrow, runparams);
623                         } else {
624                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
625                                                 runparams, everypar);
626                         }
627                 } else {
628                         par = TeXOnePar(buf, paragraphs, par, os, texrow,
629                                         runparams, everypar);
630                 }
631                 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
632                         break;
633         }
634         // It might be that we only have a title in this document
635         if (was_title && !already_title) {
636                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
637                         os << "\\end{" << from_ascii(tclass.titlename())
638                             << "}\n";
639                 }
640                 else {
641                         os << "\\" << from_ascii(tclass.titlename())
642                             << "\n";
643                                 }
644                 texrow.newline();
645         }
646 }
647
648
649 } // namespace lyx