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