]> git.lyx.org Git - lyx.git/blob - src/output_latex.C
Fix my breakage. Sorry guys.
[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         if (par != paragraphs.end() && lyxerr.debugging(Debug::LATEX))
194                 lyxerr << "TeXEnvironment...done " << &*par << endl;
195         return par;
196 }
197
198
199 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
200                       ostream & os, OutputParams const & runparams, int number)
201 {
202         int lines = 0;
203
204         InsetList::const_iterator it = par.insetlist.begin();
205         InsetList::const_iterator end = par.insetlist.end();
206         for (; it != end && number > 0 ; ++it) {
207                 if (it->inset->lyxCode() == InsetBase::OPTARG_CODE) {
208                         InsetOptArg * ins =
209                                 static_cast<InsetOptArg *>(it->inset);
210                         lines += ins->latexOptional(buf, os, runparams);
211                         --number;
212                 }
213         }
214         return lines;
215 }
216
217
218 ParagraphList::const_iterator
219 TeXOnePar(Buffer const & buf,
220           ParagraphList const & paragraphs,
221           ParagraphList::const_iterator pit,
222           ostream & os, TexRow & texrow,
223           OutputParams const & runparams_in,
224           string const & everypar)
225 {
226         lyxerr[Debug::LATEX] << "TeXOnePar...     " << &*pit << " '"
227                 << everypar << "'" << endl;
228         BufferParams const & bparams = buf.params();
229         bool further_blank_line = false;
230         LyXLayout_ptr style;
231
232         // In an an inset with unlimited length (all in one row),
233         // force layout to default
234         if (!pit->forceDefaultParagraphs())
235                 style = pit->layout();
236         else
237                 style = bparams.getLyXTextClass().defaultLayout();
238
239         OutputParams runparams = runparams_in;
240         runparams.moving_arg |= style->needprotect;
241
242         Language const * language = pit->getParLanguage(bparams);
243         Language const * doc_language = bparams.language;
244         Language const * previous_language =
245                 (pit != paragraphs.begin())
246                 ? boost::prior(pit)->getParLanguage(bparams)
247                 : doc_language;
248
249         if (language->babel() != previous_language->babel()
250             // check if we already put language command in TeXEnvironment()
251             && !(style->isEnvironment()
252                  && (pit == paragraphs.begin() ||
253                      (boost::prior(pit)->layout() != pit->layout() &&
254                       boost::prior(pit)->getDepth() <= pit->getDepth())
255                      || boost::prior(pit)->getDepth() < pit->getDepth())))
256         {
257                 if (!lyxrc.language_command_end.empty() &&
258                     previous_language->babel() != doc_language->babel())
259                 {
260                         os << subst(lyxrc.language_command_end, "$$lang",
261                                     previous_language->babel())
262                            << endl;
263                         texrow.newline();
264                 }
265
266                 if (lyxrc.language_command_end.empty() ||
267                     language->babel() != doc_language->babel())
268                 {
269                         os << subst(lyxrc.language_command_begin, "$$lang",
270                                     language->babel())
271                            << endl;
272                         texrow.newline();
273                 }
274         }
275
276         if (bparams.inputenc == "auto" &&
277             language->encoding() != previous_language->encoding()) {
278                 os << "\\inputencoding{"
279                    << language->encoding()->LatexName()
280                    << "}\n";
281                 texrow.newline();
282         }
283
284         // In an an inset with unlimited length (all in one row),
285         // don't allow any special options in the paragraph
286         if (!pit->forceDefaultParagraphs()) {
287                 if (pit->params().startOfAppendix()) {
288                         os << "\\appendix\n";
289                         texrow.newline();
290                 }
291
292                 if (!pit->params().spacing().isDefault()
293                         && (pit == paragraphs.begin()
294                             || !boost::prior(pit)->hasSameLayout(*pit)))
295                 {
296                         os << pit->params().spacing().writeEnvirBegin() << '\n';
297                         texrow.newline();
298                 }
299
300                 if (style->isCommand()) {
301                         os << '\n';
302                         texrow.newline();
303                 }
304
305                 if (further_blank_line) {
306                         os << '\n';
307                         texrow.newline();
308                 }
309         }
310
311         switch (style->latextype) {
312         case LATEX_COMMAND:
313                 os << '\\' << style->latexname();
314
315                 // Separate handling of optional argument inset.
316                 if (style->optionalargs > 0) {
317                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
318                                                     style->optionalargs);
319                         while (ret > 0) {
320                                 texrow.newline();
321                                 --ret;
322                         }
323                 }
324                 else
325                         os << style->latexparam();
326                 break;
327         case LATEX_ITEM_ENVIRONMENT:
328         case LATEX_LIST_ENVIRONMENT:
329                 os << "\\item ";
330                 break;
331         case LATEX_BIB_ENVIRONMENT:
332                 // ignore this, the inset will write itself
333                 break;
334         default:
335                 break;
336         }
337
338         os << everypar;
339         bool need_par = pit->simpleTeXOnePar(buf, bparams,
340                         outerFont(pit - paragraphs.begin(), paragraphs),
341                                              os, texrow, runparams);
342
343         // Make sure that \\par is done with the font of the last
344         // character if this has another size as the default.
345         // This is necessary because LaTeX (and LyX on the screen)
346         // calculates the space between the baselines according
347         // to this font. (Matthias)
348         //
349         // Is this really needed ? (Dekel)
350         // We do not need to use to change the font for the last paragraph
351         // or for a command.
352         LyXFont const outerfont =
353                         outerFont(pit - paragraphs.begin(),
354 paragraphs);
355
356         LyXFont const font =
357                 (pit->empty()
358                  ? pit->getLayoutFont(bparams, outerfont)
359                  : pit->getFont(bparams, pit->size() - 1, outerfont));
360
361         bool is_command = style->isCommand();
362
363         if (style->resfont.size() != font.size()
364             && boost::next(pit) != paragraphs.end()
365             && !is_command) {
366                 if (!need_par)
367                         os << '{';
368                 os << "\\" << font.latexSize() << " \\par}";
369         } else if (need_par) {
370                 os << "\\par}";
371         } else if (is_command)
372                 os << '}';
373
374         switch (style->latextype) {
375         case LATEX_ITEM_ENVIRONMENT:
376         case LATEX_LIST_ENVIRONMENT:
377                 if (boost::next(pit) != paragraphs.end()
378                     && (pit->params().depth() < boost::next(pit)->params().depth())) {
379                         os << '\n';
380                         texrow.newline();
381                 }
382                 break;
383         case LATEX_ENVIRONMENT: {
384                 // if its the last paragraph of the current environment
385                 // skip it otherwise fall through
386                 ParagraphList::const_iterator next = boost::next(pit);
387
388                 if (next != paragraphs.end()
389                     && (next->layout() != pit->layout()
390                         || next->params().depth() != pit->params().depth()))
391                         break;
392         }
393
394                 // fall through possible
395         default:
396                 // we don't need it for the last paragraph!!!
397                 if (boost::next(pit) != paragraphs.end()) {
398                         os << '\n';
399                         texrow.newline();
400                 }
401         }
402
403         if (!pit->forceDefaultParagraphs()) {
404                 further_blank_line = false;
405
406                 if (further_blank_line) {
407                         os << '\n';
408                         texrow.newline();
409                 }
410
411                 if (!pit->params().spacing().isDefault()
412                         && (boost::next(pit) == paragraphs.end()
413                             || !boost::next(pit)->hasSameLayout(*pit)))
414                 {
415                         os << pit->params().spacing().writeEnvirEnd() << '\n';
416                         texrow.newline();
417                 }
418         }
419
420         if (boost::next(pit) == paragraphs.end()
421             && language->babel() != doc_language->babel()) {
422                 // Since \selectlanguage write the language to the aux file,
423                 // we need to reset the language at the end of footnote or
424                 // float.
425
426                 if (lyxrc.language_command_end.empty())
427                         os << subst(lyxrc.language_command_begin,
428                                     "$$lang",
429                                     doc_language->babel())
430                            << endl;
431                 else
432                         os << subst(lyxrc.language_command_end,
433                                     "$$lang",
434                                     language->babel())
435                            << endl;
436                 texrow.newline();
437         }
438
439         // we don't need it for the last paragraph!!!
440         // Note from JMarc: we will re-add a \n explicitely in
441         // TeXEnvironment, because it is needed in this case
442         if (boost::next(pit) != paragraphs.end()) {
443                 os << '\n';
444                 texrow.newline();
445         }
446
447         if (boost::next(pit) != paragraphs.end() &&
448             lyxerr.debugging(Debug::LATEX))
449                 lyxerr << "TeXOnePar...done " << &*boost::next(pit) << endl;
450         return ++pit;
451 }
452
453 } // anon namespace
454
455
456 // LaTeX all paragraphs
457 void latexParagraphs(Buffer const & buf,
458                      ParagraphList const & paragraphs,
459                      ostream & os,
460                      TexRow & texrow,
461                      OutputParams const & runparams,
462                      string const & everypar)
463 {
464         bool was_title = false;
465         bool already_title = false;
466         LyXTextClass const & tclass = buf.params().getLyXTextClass();
467         ParagraphList::const_iterator par = paragraphs.begin();
468         ParagraphList::const_iterator endpar = paragraphs.end();
469
470         // if only_body
471         while (par != endpar) {
472                 // well we have to check if we are in an inset with unlimited
473                 // length (all in one row) if that is true then we don't allow
474                 // any special options in the paragraph and also we don't allow
475                 // any environment other then "Standard" to be valid!
476                 if (!par->forceDefaultParagraphs()) {
477                         LyXLayout_ptr const & layout = par->layout();
478
479                         if (layout->intitle) {
480                                 if (already_title) {
481                                         lyxerr << "Error in latexParagraphs: You"
482                                                 " should not mix title layouts"
483                                                 " with normal ones." << endl;
484                                 } else if (!was_title) {
485                                         was_title = true;
486                                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
487                                                 os << "\\begin{"
488                                                     << tclass.titlename()
489                                                     << "}\n";
490                                                 texrow.newline();
491                                         }
492                                 }
493                         } else if (was_title && !already_title) {
494                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
495                                         os << "\\end{" << tclass.titlename()
496                                             << "}\n";
497                                 }
498                                 else {
499                                         os << "\\" << tclass.titlename()
500                                             << "\n";
501                                 }
502                                 texrow.newline();
503                                 already_title = true;
504                                 was_title = false;
505                         }
506
507                         if (layout->is_environment) {
508                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
509                                                 runparams, everypar);
510                         } else if (layout->isEnvironment() ||
511                                 !par->params().leftIndent().zero())
512                         {
513                                 par = TeXEnvironment(buf, paragraphs, par, os,
514                                                      texrow, runparams);
515                         } else {
516                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
517                                                 runparams, everypar);
518                         }
519                 } else {
520                         par = TeXOnePar(buf, paragraphs, par, os, texrow,
521                                         runparams, everypar);
522                 }
523         }
524         // It might be that we only have a title in this document
525         if (was_title && !already_title) {
526                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
527                         os << "\\end{" << tclass.titlename()
528                             << "}\n";
529                 }
530                 else {
531                         os << "\\" << tclass.titlename()
532                             << "\n";
533                                 }
534                 texrow.newline();
535         }
536 }