]> git.lyx.org Git - lyx.git/blob - src/output_latex.C
a852b3080594e2ee3a26f1f6fb0eb028eaf09404
[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                            << endl;
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                            << endl;
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 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                              << endl;
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                              << endl;
293                         texrow.newline();
294                 }
295         }
296
297         if (bparams.inputenc == "auto" &&
298             language->encoding() != previous_language->encoding()) {
299                 ucs4 << "\\inputencoding{"
300                      << from_ascii(language->encoding()->latexName())
301                      << "}\n";
302                 texrow.newline();
303         }
304         // We need to output the paragraph to a temporary stream if we
305         // need to change the encoding. Don't do this if the result does
306         // not go to a file but to the builtin source viewer.
307         odocstringstream par_stream;
308         bool const change_encoding = !runparams_in.dryrun &&
309                         bparams.inputenc == "auto" &&
310                         language->encoding() != doc_language->encoding();
311         odocstream & os = *(change_encoding ? &par_stream : &ucs4);
312
313         // In an an inset with unlimited length (all in one row),
314         // don't allow any special options in the paragraph
315         if (!pit->forceDefaultParagraphs()) {
316                 if (pit->params().startOfAppendix()) {
317                         os << "\\appendix\n";
318                         texrow.newline();
319                 }
320
321                 if (!pit->params().spacing().isDefault()
322                         && (pit == paragraphs.begin()
323                             || !boost::prior(pit)->hasSameLayout(*pit)))
324                 {
325                         os << from_ascii(pit->params().spacing().writeEnvirBegin())
326                             << '\n';
327                         texrow.newline();
328                 }
329
330                 if (style->isCommand()) {
331                         os << '\n';
332                         texrow.newline();
333                 }
334
335                 if (further_blank_line) {
336                         os << '\n';
337                         texrow.newline();
338                 }
339         }
340
341         switch (style->latextype) {
342         case LATEX_COMMAND:
343                 os << '\\' << from_ascii(style->latexname());
344
345                 // Separate handling of optional argument inset.
346                 if (style->optionalargs > 0) {
347                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
348                                                     style->optionalargs);
349                         while (ret > 0) {
350                                 texrow.newline();
351                                 --ret;
352                         }
353                 }
354                 else
355                         os << from_ascii(style->latexparam());
356                 break;
357         case LATEX_ITEM_ENVIRONMENT:
358         case LATEX_LIST_ENVIRONMENT:
359                 os << "\\item ";
360                 break;
361         case LATEX_BIB_ENVIRONMENT:
362                 // ignore this, the inset will write itself
363                 break;
364         default:
365                 break;
366         }
367
368         // FIXME UNICODE
369         os << from_utf8(everypar);
370         bool need_par = pit->simpleTeXOnePar(buf, bparams,
371                                              outerFont(std::distance(paragraphs.begin(), pit), paragraphs),
372                                              os, texrow, runparams);
373
374         // Make sure that \\par is done with the font of the last
375         // character if this has another size as the default.
376         // This is necessary because LaTeX (and LyX on the screen)
377         // calculates the space between the baselines according
378         // to this font. (Matthias)
379         //
380         // Is this really needed ? (Dekel)
381         // We do not need to use to change the font for the last paragraph
382         // or for a command.
383         LyXFont const outerfont =
384                 outerFont(std::distance(paragraphs.begin(), pit),
385                           paragraphs);
386
387         LyXFont const font =
388                 (pit->empty()
389                  ? pit->getLayoutFont(bparams, outerfont)
390                  : pit->getFont(bparams, pit->size() - 1, outerfont));
391
392         bool is_command = style->isCommand();
393
394         if (style->resfont.size() != font.size()
395             && boost::next(pit) != paragraphs.end()
396             && !is_command) {
397                 if (!need_par)
398                         os << '{';
399                 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
400         } else if (need_par) {
401                 os << "\\par}";
402         } else if (is_command)
403                 os << '}';
404
405         switch (style->latextype) {
406         case LATEX_ITEM_ENVIRONMENT:
407         case LATEX_LIST_ENVIRONMENT:
408                 if (boost::next(pit) != paragraphs.end()
409                     && (pit->params().depth() < boost::next(pit)->params().depth())) {
410                         os << '\n';
411                         texrow.newline();
412                 }
413                 break;
414         case LATEX_ENVIRONMENT: {
415                 // if its the last paragraph of the current environment
416                 // skip it otherwise fall through
417                 ParagraphList::const_iterator next = boost::next(pit);
418
419                 if (next != paragraphs.end()
420                     && (next->layout() != pit->layout()
421                         || next->params().depth() != pit->params().depth()))
422                         break;
423         }
424
425                 // fall through possible
426         default:
427                 // we don't need it for the last paragraph!!!
428                 if (boost::next(pit) != paragraphs.end()) {
429                         os << '\n';
430                         texrow.newline();
431                 }
432         }
433
434         if (!pit->forceDefaultParagraphs()) {
435                 further_blank_line = false;
436
437                 if (further_blank_line) {
438                         os << '\n';
439                         texrow.newline();
440                 }
441
442                 if (!pit->params().spacing().isDefault()
443                         && (boost::next(pit) == paragraphs.end()
444                             || !boost::next(pit)->hasSameLayout(*pit)))
445                 {
446                         os << from_ascii(pit->params().spacing().writeEnvirEnd())
447                            << '\n';
448                         texrow.newline();
449                 }
450         }
451
452         if (boost::next(pit) == paragraphs.end()
453             && language->babel() != doc_language->babel()) {
454                 // Since \selectlanguage write the language to the aux file,
455                 // we need to reset the language at the end of footnote or
456                 // float.
457
458                 if (lyxrc.language_command_end.empty())
459                         os << from_ascii(subst(
460                                 lyxrc.language_command_begin,
461                                 "$$lang",
462                                 doc_language->babel()))
463                            << endl;
464                 else
465                         os << from_ascii(subst(
466                                 lyxrc.language_command_end,
467                                 "$$lang",
468                                 language->babel()))
469                            << endl;
470                 texrow.newline();
471         }
472
473         // we don't need it for the last paragraph!!!
474         // Note from JMarc: we will re-add a \n explicitely in
475         // TeXEnvironment, because it is needed in this case
476         if (boost::next(pit) != paragraphs.end()) {
477                 os << '\n';
478                 texrow.newline();
479         }
480
481         if (boost::next(pit) != paragraphs.end() &&
482             lyxerr.debugging(Debug::LATEX))
483                 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
484
485         if (change_encoding) {
486                 lyxerr[Debug::LATEX] << "Converting paragraph to encoding "
487                         << language->encoding()->iconvName() << endl;
488                 docstring const par = par_stream.str();
489                 // Convert the paragraph to the 8bit encoding that we need to
490                 // output.
491                 std::vector<char> const encoded = lyx::ucs4_to_eightbit(par.c_str(),
492                         par.size(), language->encoding()->iconvName());
493                 // Interpret this as if it was in the 8 bit encoding of the
494                 // document language and convert it back to UCS4. That means
495                 // that faked does not contain pure UCS4 anymore, but what
496                 // will be written to the output file will be correct, because
497                 // the real output stream will do a UCS4 -> document language
498                 // encoding conversion.
499                 // This is of course a hack, but not a bigger one than mixing
500                 // two encodings in one file.
501                 // FIXME: Catch iconv conversion errors and display an error
502                 // dialog.
503
504                 // Here follows an explanation how I (gb) came to the current
505                 // solution:
506
507                 // codecvt facets are only used by file streams -> OK, maybe
508                 // we could use file streams and not generic streams in the
509                 // latex() methods? No, that does not  work, we use them at
510                 // several places to write to string streams.
511                 // Next try: Maybe we could do something else than codecvt
512                 // in our streams, and  add a setEncoding() method? That
513                 // does not work unless we rebuild the functionality of file
514                 // and string streams, since both odocfstream and
515                 // odocstringstream inherit from std::basic_ostream<docstring>
516                 // and we can  neither add a method to that class nor change
517                 // the inheritance of the file and string streams.
518
519                 // What might be possible is to encapsulate the real file and
520                 // string streams in our own version, and use a homemade
521                 // streambuf that would do the encoding conversion and then
522                 // forward to the real stream. That would probably work, but
523                 // would require far more code and a good understanding of
524                 // stream buffers to get it right.
525
526                 // Another idea by JMarc is to use a modifier like
527                 // os << setencoding("iso-8859-1");
528                 // That currently looks like the best idea.
529
530                 std::vector<char_type> const faked = lyx::eightbit_to_ucs4(&(encoded[0]),
531                         encoded.size(), doc_language->encoding()->iconvName());
532                 std::vector<char_type>::const_iterator const end = faked.end();
533                 std::vector<char_type>::const_iterator it = faked.begin();
534                 for (; it != end; ++it)
535                         ucs4.put(*it);
536         }
537
538         return ++pit;
539 }
540
541 } // anon namespace
542
543
544 // LaTeX all paragraphs
545 void latexParagraphs(Buffer const & buf,
546                      ParagraphList const & paragraphs,
547                      odocstream & os,
548                      TexRow & texrow,
549                      OutputParams const & runparams,
550                      string const & everypar)
551 {
552         bool was_title = false;
553         bool already_title = false;
554         LyXTextClass const & tclass = buf.params().getLyXTextClass();
555         ParagraphList::const_iterator par = paragraphs.begin();
556         ParagraphList::const_iterator endpar = paragraphs.end();
557
558         BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
559         // if only part of the paragraphs will be outputed
560         if (runparams.par_begin !=  runparams.par_end) {
561                 par = boost::next(paragraphs.begin(), runparams.par_begin);
562                 endpar = boost::next(paragraphs.begin(), runparams.par_end);
563                 // runparams will be passed to nested paragraphs, so
564                 // we have to reset the range parameters.
565                 const_cast<OutputParams&>(runparams).par_begin = 0;
566                 const_cast<OutputParams&>(runparams).par_end = 0;
567         }
568
569         // if only_body
570         while (par != endpar) {
571                 ParagraphList::const_iterator lastpar = par;
572                 // well we have to check if we are in an inset with unlimited
573                 // length (all in one row) if that is true then we don't allow
574                 // any special options in the paragraph and also we don't allow
575                 // any environment other then "Standard" to be valid!
576                 if (!par->forceDefaultParagraphs()) {
577                         LyXLayout_ptr const & layout = par->layout();
578
579                         if (layout->intitle) {
580                                 if (already_title) {
581                                         lyxerr << "Error in latexParagraphs: You"
582                                                 " should not mix title layouts"
583                                                 " with normal ones." << endl;
584                                 } else if (!was_title) {
585                                         was_title = true;
586                                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
587                                                 os << "\\begin{"
588                                                     << from_ascii(tclass.titlename())
589                                                     << "}\n";
590                                                 texrow.newline();
591                                         }
592                                 }
593                         } else if (was_title && !already_title) {
594                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
595                                         os << "\\end{" << from_ascii(tclass.titlename())
596                                             << "}\n";
597                                 }
598                                 else {
599                                         os << "\\" << from_ascii(tclass.titlename())
600                                             << "\n";
601                                 }
602                                 texrow.newline();
603                                 already_title = true;
604                                 was_title = false;
605                         }
606
607                         if (layout->is_environment) {
608                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
609                                                 runparams, everypar);
610                         } else if (layout->isEnvironment() ||
611                                 !par->params().leftIndent().zero())
612                         {
613                                 par = TeXEnvironment(buf, paragraphs, par, os,
614                                                      texrow, runparams);
615                         } else {
616                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
617                                                 runparams, everypar);
618                         }
619                 } else {
620                         par = TeXOnePar(buf, paragraphs, par, os, texrow,
621                                         runparams, everypar);
622                 }
623                 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
624                         break;
625         }
626         // It might be that we only have a title in this document
627         if (was_title && !already_title) {
628                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
629                         os << "\\end{" << from_ascii(tclass.titlename())
630                             << "}\n";
631                 }
632                 else {
633                         os << "\\" << from_ascii(tclass.titlename())
634                             << "\n";
635                                 }
636                 texrow.newline();
637         }
638 }
639
640
641 } // namespace lyx