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