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