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