]> git.lyx.org Git - lyx.git/blob - src/output_latex.C
Add margin to paragraph dialog.
[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
33
34 namespace lyx {
35
36 using support::subst;
37
38 using std::endl;
39 using std::string;
40
41
42 namespace {
43
44 ParagraphList::const_iterator
45 TeXEnvironment(Buffer const & buf,
46                ParagraphList const & paragraphs,
47                ParagraphList::const_iterator pit,
48                odocstream & os, TexRow & texrow,
49                OutputParams const & runparams);
50
51 ParagraphList::const_iterator
52 TeXOnePar(Buffer const & buf,
53           ParagraphList const & paragraphs,
54           ParagraphList::const_iterator pit,
55           odocstream & os, TexRow & texrow,
56           OutputParams const & runparams,
57           string const & everypar = string());
58
59
60 ParagraphList::const_iterator
61 TeXDeeper(Buffer const & buf,
62           ParagraphList const & paragraphs,
63           ParagraphList::const_iterator pit,
64           odocstream & os, TexRow & texrow,
65           OutputParams const & runparams)
66 {
67         LYXERR(Debug::LATEX) << "TeXDeeper...     " << &*pit << endl;
68         ParagraphList::const_iterator par = pit;
69
70         while (par != paragraphs.end() &&
71                      par->params().depth() == pit->params().depth()) {
72                 if (par->layout()->isEnvironment()) {
73                         par = TeXEnvironment(buf, paragraphs, par,
74                                              os, texrow, runparams);
75                 } else {
76                         par = TeXOnePar(buf, paragraphs, par,
77                                              os, texrow, runparams);
78                 }
79         }
80         LYXERR(Debug::LATEX) << "TeXDeeper...done " << endl;
81
82         return par;
83 }
84
85
86 ParagraphList::const_iterator
87 TeXEnvironment(Buffer const & buf,
88                ParagraphList const & paragraphs,
89                ParagraphList::const_iterator pit,
90                odocstream & os, TexRow & texrow,
91                OutputParams const & runparams)
92 {
93         LYXERR(Debug::LATEX) << "TeXEnvironment...     " << &*pit << endl;
94
95         BufferParams const & bparams = buf.params();
96
97         LyXLayout_ptr const & style = pit->layout();
98
99         Language const * const par_language = pit->getParLanguage(bparams);
100         Language const * const doc_language = bparams.language;
101         Language const * const prev_par_language =
102                 (pit != paragraphs.begin())
103                 ? boost::prior(pit)->getParLanguage(bparams)
104                 : doc_language;
105         if (par_language->babel() != prev_par_language->babel()) {
106
107                 if (!lyxrc.language_command_end.empty() &&
108                     prev_par_language->babel() != doc_language->babel()) {
109                         os << from_ascii(subst(
110                                 lyxrc.language_command_end,
111                                 "$$lang",
112                                 prev_par_language->babel()))
113                            << '\n';
114                         texrow.newline();
115                 }
116
117                 if (lyxrc.language_command_end.empty() ||
118                     par_language->babel() != doc_language->babel()) {
119                         os << from_ascii(subst(
120                                 lyxrc.language_command_begin,
121                                 "$$lang",
122                                 par_language->babel()))
123                            << '\n';
124                         texrow.newline();
125                 }
126         }
127
128         bool leftindent_open = false;
129         if (!pit->params().leftIndent().zero()) {
130                 os << "\\begin{LyXParagraphLeftIndent}{"
131                    << from_ascii(pit->params().leftIndent().asLatexString())
132                    << "}\n";
133                 texrow.newline();
134                 leftindent_open = true;
135         }
136
137         if (style->isEnvironment()) {
138                 os << "\\begin{" << from_ascii(style->latexname()) << '}';
139                 if (style->optionalargs > 0) {
140                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
141                                                     style->optionalargs);
142                         while (ret > 0) {
143                                 texrow.newline();
144                                 --ret;
145                         }
146                 }
147                 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
148                         os << '{'
149                            << pit->params().labelWidthString()
150                            << "}\n";
151                 } else if (style->labeltype == LABEL_BIBLIO) {
152                         // ale970405
153                         os << '{' << bibitemWidest(buf) << "}\n";
154                 } else
155                         os << from_ascii(style->latexparam()) << '\n';
156                 texrow.newline();
157         }
158         ParagraphList::const_iterator par = pit;
159         do {
160                 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
161
162                 if (par == paragraphs.end()) {
163                         // Make sure that the last paragraph is
164                         // correctly terminated (because TeXOnePar does
165                         // not add a \n in this case)
166                         os << '\n';
167                         texrow.newline();
168                 } else if (par->params().depth() > pit->params().depth()) {
169                             if (par->layout()->isParagraph()) {
170
171                             // Thinko!
172                             // How to handle this? (Lgb)
173                             //&& !suffixIs(os, "\n\n")
174                                     //) {
175                                 // There should be at least one '\n' already
176                                 // but we need there to be two for Standard
177                                 // paragraphs that are depth-increment'ed to be
178                                 // output correctly.  However, tables can
179                                 // also be paragraphs so don't adjust them.
180                                 // ARRae
181                                 // Thinkee:
182                                 // Will it ever harm to have one '\n' too
183                                 // many? i.e. that we sometimes will have
184                                 // three in a row. (Lgb)
185                                 os << '\n';
186                                 texrow.newline();
187                         }
188                         par = TeXDeeper(buf, paragraphs, par, os, texrow,
189                                         runparams);
190                 }
191         } while (par != paragraphs.end()
192                  && par->layout() == pit->layout()
193                  && par->params().depth() == pit->params().depth()
194                  && par->params().leftIndent() == pit->params().leftIndent());
195
196         if (style->isEnvironment()) {
197                 os << "\\end{" << from_ascii(style->latexname()) << "}\n";
198                 texrow.newline();
199         }
200
201         if (leftindent_open) {
202                 os << "\\end{LyXParagraphLeftIndent}\n";
203                 texrow.newline();
204         }
205
206         if (par != paragraphs.end())
207                 LYXERR(Debug::LATEX) << "TeXEnvironment...done " << &*par << endl;
208         return par;
209 }
210
211 }
212
213
214 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
215                       odocstream & os, OutputParams const & runparams, int number)
216 {
217         int lines = 0;
218
219         InsetList::const_iterator it = par.insetlist.begin();
220         InsetList::const_iterator end = par.insetlist.end();
221         for (; it != end && number > 0 ; ++it) {
222                 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) {
223                         InsetOptArg * ins =
224                                 static_cast<InsetOptArg *>(it->inset);
225                         lines += ins->latexOptional(buf, os, runparams);
226                         --number;
227                 }
228         }
229         return lines;
230 }
231
232
233 namespace {
234
235 ParagraphList::const_iterator
236 TeXOnePar(Buffer const & buf,
237           ParagraphList const & paragraphs,
238           ParagraphList::const_iterator pit,
239           odocstream & os, TexRow & texrow,
240           OutputParams const & runparams_in,
241           string const & everypar)
242 {
243         LYXERR(Debug::LATEX) << "TeXOnePar...     " << &*pit << " '"
244                 << everypar << "'" << endl;
245         BufferParams const & bparams = buf.params();
246         LyXLayout_ptr style;
247
248         // In an inset with unlimited length (all in one row),
249         // force layout to default
250         if (!pit->forceDefaultParagraphs())
251                 style = pit->layout();
252         else
253                 style = bparams.getLyXTextClass().defaultLayout();
254
255         OutputParams runparams = runparams_in;
256         runparams.moving_arg |= style->needprotect;
257
258         Language const * const par_language = pit->getParLanguage(bparams);
259         Language const * const doc_language = bparams.language;
260         Language const * const prev_par_language =
261                 (pit != paragraphs.begin())
262                 ? boost::prior(pit)->getParLanguage(bparams)
263                 : doc_language;
264
265         if (par_language->babel() != prev_par_language->babel()
266             // check if we already put language command in TeXEnvironment()
267             && !(style->isEnvironment()
268                  && (pit == paragraphs.begin() ||
269                      (boost::prior(pit)->layout() != pit->layout() &&
270                       boost::prior(pit)->getDepth() <= pit->getDepth())
271                      || boost::prior(pit)->getDepth() < pit->getDepth())))
272         {
273                 if (!lyxrc.language_command_end.empty() &&
274                     prev_par_language->babel() != doc_language->babel())
275                 {
276                         os << from_ascii(subst(lyxrc.language_command_end,
277                                 "$$lang",
278                                 prev_par_language->babel()))
279                            << '\n';
280                         texrow.newline();
281                 }
282
283                 if (lyxrc.language_command_end.empty() ||
284                     par_language->babel() != doc_language->babel())
285                 {
286                         os << from_ascii(subst(
287                                 lyxrc.language_command_begin,
288                                 "$$lang",
289                                 par_language->babel()))
290                            << '\n';
291                         texrow.newline();
292                 }
293         }
294
295         // In an inset with unlimited length (all in one row),
296         // don't allow any special options in the paragraph
297         if (!pit->forceDefaultParagraphs()) {
298                 if (pit->params().startOfAppendix()) {
299                         os << "\\appendix\n";
300                         texrow.newline();
301                 }
302
303                 if (!pit->params().spacing().isDefault()
304                         && (pit == paragraphs.begin()
305                             || !boost::prior(pit)->hasSameLayout(*pit)))
306                 {
307                         os << from_ascii(pit->params().spacing().writeEnvirBegin())
308                             << '\n';
309                         texrow.newline();
310                 }
311
312                 if (style->isCommand()) {
313                         os << '\n';
314                         texrow.newline();
315                 }
316         }
317
318         switch (style->latextype) {
319         case LATEX_COMMAND:
320                 os << '\\' << from_ascii(style->latexname());
321
322                 // Separate handling of optional argument inset.
323                 if (style->optionalargs > 0) {
324                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
325                                                     style->optionalargs);
326                         while (ret > 0) {
327                                 texrow.newline();
328                                 --ret;
329                         }
330                 }
331                 else
332                         os << from_ascii(style->latexparam());
333                 break;
334         case LATEX_ITEM_ENVIRONMENT:
335         case LATEX_LIST_ENVIRONMENT:
336                 os << "\\item ";
337                 break;
338         case LATEX_BIB_ENVIRONMENT:
339                 // ignore this, the inset will write itself
340                 break;
341         default:
342                 break;
343         }
344
345         LyXFont const outerfont =
346                 outerFont(std::distance(paragraphs.begin(), pit),
347                           paragraphs);
348
349         // FIXME UNICODE
350         os << from_utf8(everypar);
351         bool need_par = pit->simpleTeXOnePar(buf, bparams, outerfont,
352                                              os, texrow, runparams);
353
354         // Make sure that \\par is done with the font of the last
355         // character if this has another size as the default.
356         // This is necessary because LaTeX (and LyX on the screen)
357         // calculates the space between the baselines according
358         // to this font. (Matthias)
359         //
360         // Is this really needed ? (Dekel)
361         // We do not need to use to change the font for the last paragraph
362         // or for a command.
363
364         LyXFont const font =
365                 (pit->empty()
366                  ? pit->getLayoutFont(bparams, outerfont)
367                  : pit->getFont(bparams, pit->size() - 1, outerfont));
368
369         bool is_command = style->isCommand();
370
371         if (style->resfont.size() != font.size()
372             && boost::next(pit) != paragraphs.end()
373             && !is_command) {
374                 if (!need_par)
375                         os << '{';
376                 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
377         } else if (need_par) {
378                 os << "\\par}";
379         } else if (is_command)
380                 os << '}';
381
382         bool pending_newline = false;
383         switch (style->latextype) {
384         case LATEX_ITEM_ENVIRONMENT:
385         case LATEX_LIST_ENVIRONMENT:
386                 if (boost::next(pit) != paragraphs.end()
387                     && (pit->params().depth() < boost::next(pit)->params().depth()))
388                         pending_newline = true;
389                 break;
390         case LATEX_ENVIRONMENT: {
391                 // if its the last paragraph of the current environment
392                 // skip it otherwise fall through
393                 ParagraphList::const_iterator next = boost::next(pit);
394
395                 if (next != paragraphs.end()
396                     && (next->layout() != pit->layout()
397                         || next->params().depth() != pit->params().depth()))
398                         break;
399         }
400
401                 // fall through possible
402         default:
403                 // we don't need it for the last paragraph!!!
404                 if (boost::next(pit) != paragraphs.end())
405                         pending_newline = true;
406         }
407
408         if (!pit->forceDefaultParagraphs()) {
409                 if (!pit->params().spacing().isDefault()
410                         && (boost::next(pit) == paragraphs.end()
411                             || !boost::next(pit)->hasSameLayout(*pit)))
412                 {
413                         if (pending_newline) {
414                                 os << '\n';
415                                 texrow.newline();
416                         }
417                         os << from_ascii(pit->params().spacing().writeEnvirEnd());
418                         pending_newline = true;
419                 }
420         }
421
422         if (boost::next(pit) == paragraphs.end()
423             && par_language->babel() != doc_language->babel()) {
424                 // Since \selectlanguage write the language to the aux file,
425                 // we need to reset the language at the end of footnote or
426                 // float.
427
428                 if (pending_newline) {
429                         os << '\n';
430                         texrow.newline();
431                 }
432                 if (lyxrc.language_command_end.empty())
433                         os << from_ascii(subst(
434                                 lyxrc.language_command_begin,
435                                 "$$lang",
436                                 doc_language->babel()));
437                 else
438                         os << from_ascii(subst(
439                                 lyxrc.language_command_end,
440                                 "$$lang",
441                                 par_language->babel()));
442                 pending_newline = true;
443         }
444
445         if (pending_newline) {
446                 os << '\n';
447                 texrow.newline();
448         }
449         runparams_in.encoding = runparams.encoding;
450
451         // we don't need it for the last paragraph!!!
452         // Note from JMarc: we will re-add a \n explicitely in
453         // TeXEnvironment, because it is needed in this case
454         if (boost::next(pit) != paragraphs.end()) {
455                 os << '\n';
456                 texrow.newline();
457         }
458
459         if (boost::next(pit) != paragraphs.end())
460                 LYXERR(Debug::LATEX) << "TeXOnePar...done " << &*boost::next(pit) << endl;
461
462         return ++pit;
463 }
464
465 } // anon namespace
466
467
468 // LaTeX all paragraphs
469 void latexParagraphs(Buffer const & buf,
470                      ParagraphList const & paragraphs,
471                      odocstream & os,
472                      TexRow & texrow,
473                      OutputParams const & runparams,
474                      string const & everypar)
475 {
476         bool was_title = false;
477         bool already_title = false;
478         LyXTextClass const & tclass = buf.params().getLyXTextClass();
479         ParagraphList::const_iterator par = paragraphs.begin();
480         ParagraphList::const_iterator endpar = paragraphs.end();
481
482         BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
483         // if only part of the paragraphs will be outputed
484         if (runparams.par_begin !=  runparams.par_end) {
485                 par = boost::next(paragraphs.begin(), runparams.par_begin);
486                 endpar = boost::next(paragraphs.begin(), runparams.par_end);
487                 // runparams will be passed to nested paragraphs, so
488                 // we have to reset the range parameters.
489                 const_cast<OutputParams&>(runparams).par_begin = 0;
490                 const_cast<OutputParams&>(runparams).par_end = 0;
491         }
492
493         // if only_body
494         while (par != endpar) {
495                 ParagraphList::const_iterator lastpar = par;
496                 // well we have to check if we are in an inset with unlimited
497                 // length (all in one row) if that is true then we don't allow
498                 // any special options in the paragraph and also we don't allow
499                 // any environment other than the default layout of the
500                 // text class to be valid!
501                 if (!par->forceDefaultParagraphs()) {
502                         LyXLayout_ptr const & layout = par->layout();
503
504                         if (layout->intitle) {
505                                 if (already_title) {
506                                         lyxerr << "Error in latexParagraphs: You"
507                                                 " should not mix title layouts"
508                                                 " with normal ones." << endl;
509                                 } else if (!was_title) {
510                                         was_title = true;
511                                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
512                                                 os << "\\begin{"
513                                                     << from_ascii(tclass.titlename())
514                                                     << "}\n";
515                                                 texrow.newline();
516                                         }
517                                 }
518                         } else if (was_title && !already_title) {
519                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
520                                         os << "\\end{" << from_ascii(tclass.titlename())
521                                             << "}\n";
522                                 }
523                                 else {
524                                         os << "\\" << from_ascii(tclass.titlename())
525                                             << "\n";
526                                 }
527                                 texrow.newline();
528                                 already_title = true;
529                                 was_title = false;
530                         }
531
532                         if (layout->is_environment) {
533                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
534                                                 runparams, everypar);
535                         } else if (layout->isEnvironment() ||
536                                    !par->params().leftIndent().zero()) {
537                                 par = TeXEnvironment(buf, paragraphs, par, os,
538                                                      texrow, runparams);
539                         } else {
540                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
541                                                 runparams, everypar);
542                         }
543                 } else {
544                         par = TeXOnePar(buf, paragraphs, par, os, texrow,
545                                         runparams, everypar);
546                 }
547                 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
548                         break;
549         }
550         // It might be that we only have a title in this document
551         if (was_title && !already_title) {
552                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
553                         os << "\\end{" << from_ascii(tclass.titlename())
554                             << "}\n";
555                 }
556                 else {
557                         os << "\\" << from_ascii(tclass.titlename())
558                             << "\n";
559                                 }
560                 texrow.newline();
561         }
562 }
563
564
565 int switchEncoding(odocstream & os, BufferParams const & bparams,
566                    Encoding const & oldEnc, Encoding const & newEnc)
567 {
568         // FIXME thailatex does not support the inputenc package, so we
569         // ignore switches from/to tis620-0 encoding here. This does of
570         // course only work as long as the non-thai text contains ASCII
571         // only, but it is the best we can do.
572         if ((bparams.inputenc == "auto" || bparams.inputenc == "default") &&
573             oldEnc.name() != newEnc.name() &&
574             oldEnc.name() != "ascii" && newEnc.name() != "ascii" &&
575             oldEnc.name() != "tis620-0" && newEnc.name() != "tis620-0") {
576                 LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
577                                      << oldEnc.name() << " to "
578                                      << newEnc.name() << endl;
579                 os << setEncoding(newEnc.iconvName());
580                 if (bparams.inputenc != "default") {
581                         docstring const inputenc(from_ascii(newEnc.latexName()));
582                         os << "\\inputencoding{" << inputenc << '}';
583                         return 16 + inputenc.length();
584                 }
585         }
586         return 0;
587 }
588
589 } // namespace lyx