]> git.lyx.org Git - features.git/blob - src/output_latex.C
Introducing a number of tags parametrizing various
[features.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                 InsetOld * ins = it->inset;
198                 if (ins->lyxCode() == InsetOld::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
218         InsetOld const * in = pit->inInset();
219         bool further_blank_line = false;
220         LyXLayout_ptr style;
221
222         // well we have to check if we are in an inset with unlimited
223         // length (all in one row) if that is true then we don't allow
224         // any special options in the paragraph and also we don't allow
225         // any environment other then "Standard" to be valid!
226         if (in == 0 || !in->forceDefaultParagraphs(in)) {
227                 style = pit->layout();
228
229                 if (pit->params().startOfAppendix()) {
230                         os << "\\appendix\n";
231                         texrow.newline();
232                 }
233
234                 if (!pit->params().spacing().isDefault()
235                         && (pit == const_cast<ParagraphList&>(paragraphs).begin()
236                             || !boost::prior(pit)->hasSameLayout(*pit)))
237                 {
238                         os << pit->params().spacing().writeEnvirBegin() << '\n';
239                         texrow.newline();
240                 }
241
242                 if (style->isCommand()) {
243                         os << '\n';
244                         texrow.newline();
245                 }
246
247                 if (pit->params().spaceTop().kind() != VSpace::NONE) {
248                         os << pit->params().spaceTop().asLatexCommand(bparams);
249                         further_blank_line = true;
250                 }
251
252                 if (further_blank_line) {
253                         os << '\n';
254                         texrow.newline();
255                 }
256         } else {
257                 style = bparams.getLyXTextClass().defaultLayout();
258         }
259
260         Language const * language = pit->getParLanguage(bparams);
261         Language const * doc_language = bparams.language;
262         Language const * previous_language =
263                 (pit != const_cast<ParagraphList&>(paragraphs).begin())
264                 ? boost::prior(pit)->getParLanguage(bparams)
265                 : doc_language;
266
267         if (language->babel() != previous_language->babel()
268             // check if we already put language command in TeXEnvironment()
269             && !(style->isEnvironment()
270                  && (pit == const_cast<ParagraphList&>(paragraphs).begin() ||
271                      (boost::prior(pit)->layout() != pit->layout() &&
272                       boost::prior(pit)->getDepth() <= pit->getDepth())
273                      || boost::prior(pit)->getDepth() < pit->getDepth())))
274         {
275                 if (!lyxrc.language_command_end.empty() &&
276                     previous_language->babel() != doc_language->babel())
277                 {
278                         os << subst(lyxrc.language_command_end, "$$lang",
279                                     previous_language->babel())
280                            << endl;
281                         texrow.newline();
282                 }
283
284                 if (lyxrc.language_command_end.empty() ||
285                     language->babel() != doc_language->babel())
286                 {
287                         os << subst(lyxrc.language_command_begin, "$$lang",
288                                     language->babel())
289                            << endl;
290                         texrow.newline();
291                 }
292         }
293
294         if (bparams.inputenc == "auto" &&
295             language->encoding() != previous_language->encoding()) {
296                 os << "\\inputencoding{"
297                    << language->encoding()->LatexName()
298                    << "}\n";
299                 texrow.newline();
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),
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(outerFont(pit, paragraphs));
341
342         LyXFont const font =
343                 (pit->empty()
344                  ? pit->getLayoutFont(bparams, outerfont)
345                  : pit->getFont(bparams, pit->size() - 1, outerfont));
346
347         bool is_command = style->isCommand();
348
349         if (style->resfont.size() != font.size()
350             && boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
351             && !is_command) {
352                 if (!need_par)
353                         os << '{';
354                 os << "\\" << font.latexSize() << " \\par}";
355         } else if (need_par) {
356                 os << "\\par}";
357         } else if (is_command)
358                 os << '}';
359
360         switch (style->latextype) {
361         case LATEX_ITEM_ENVIRONMENT:
362         case LATEX_LIST_ENVIRONMENT:
363                 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
364                     && (pit->params().depth() < boost::next(pit)->params().depth())) {
365                         os << '\n';
366                         texrow.newline();
367                 }
368                 break;
369         case LATEX_ENVIRONMENT: {
370                 // if its the last paragraph of the current environment
371                 // skip it otherwise fall through
372                 ParagraphList::iterator next = boost::next(pit);
373
374                 if (next != const_cast<ParagraphList&>(paragraphs).end()
375                     && (next->layout() != pit->layout()
376                         || next->params().depth() != pit->params().depth()))
377                         break;
378         }
379
380                 // fall through possible
381         default:
382                 // we don't need it for the last paragraph!!!
383                 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
384                         os << '\n';
385                         texrow.newline();
386                 }
387         }
388
389         if (in == 0 || !in->forceDefaultParagraphs(in)) {
390                 further_blank_line = false;
391
392                 if (pit->params().spaceBottom().kind() != VSpace::NONE) {
393                         os << pit->params().spaceBottom().asLatexCommand(bparams);
394                         further_blank_line = true;
395                 }
396
397                 if (further_blank_line) {
398                         os << '\n';
399                         texrow.newline();
400                 }
401
402                 if (!pit->params().spacing().isDefault()
403                         && (boost::next(pit) == const_cast<ParagraphList&>(paragraphs).end()
404                             || !boost::next(pit)->hasSameLayout(*pit)))
405                 {
406                         os << pit->params().spacing().writeEnvirEnd() << '\n';
407                         texrow.newline();
408                 }
409         }
410
411         // we don't need it for the last paragraph!!!
412         if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
413                 os << '\n';
414                 texrow.newline();
415         } else {
416                 // Since \selectlanguage write the language to the aux file,
417                 // we need to reset the language at the end of footnote or
418                 // float.
419
420                 if (language->babel() != doc_language->babel()) {
421                         if (lyxrc.language_command_end.empty())
422                                 os << subst(lyxrc.language_command_begin,
423                                             "$$lang",
424                                             doc_language->babel())
425                                    << endl;
426                         else
427                                 os << subst(lyxrc.language_command_end,
428                                             "$$lang",
429                                             language->babel())
430                                    << endl;
431                         texrow.newline();
432                 }
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 from par to endpar, if endpar == 0 then to the end
443 //
444 void latexParagraphs(Buffer const & buf,
445                      ParagraphList const & paragraphs,
446                      ostream & os,
447                      TexRow & texrow,
448                      OutputParams const & runparams,
449                      string const & everypar)
450 {
451         bool was_title = false;
452         bool already_title = false;
453         LyXTextClass const & tclass = buf.params().getLyXTextClass();
454         ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
455         ParagraphList::iterator endpar = const_cast<ParagraphList&>(paragraphs).end();
456
457         // if only_body
458         while (par != endpar) {
459                 InsetOld * in = par->inInset();
460                 // well we have to check if we are in an inset with unlimited
461                 // length (all in one row) if that is true then we don't allow
462                 // any special options in the paragraph and also we don't allow
463                 // any environment other then "Standard" to be valid!
464                 if (in == 0 || !in->forceDefaultParagraphs(in)) {
465                         LyXLayout_ptr const & layout = par->layout();
466
467                         if (layout->intitle) {
468                                 if (already_title) {
469                                         lyxerr <<"Error in latexParagraphs: You"
470                                                 " should not mix title layouts"
471                                                 " with normal ones." << endl;
472                                 } else if (!was_title) {
473                                         was_title = true;
474                                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
475                                                 os << "\\begin{"
476                                                     << tclass.titlename()
477                                                     << "}\n";
478                                                 texrow.newline();
479                                         }
480                                 }
481                         } else if (was_title && !already_title) {
482                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
483                                         os << "\\end{" << tclass.titlename()
484                                             << "}\n";
485                                 }
486                                 else {
487                                         os << "\\" << tclass.titlename()
488                                             << "\n";
489                                 }
490                                 texrow.newline();
491                                 already_title = true;
492                                 was_title = false;
493                         }
494
495                         if (layout->is_environment) {
496                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
497                                                 runparams, everypar);
498                         } else if (layout->isEnvironment() ||
499                                 !par->params().leftIndent().zero())
500                         {
501                                 par = TeXEnvironment(buf, paragraphs, par, os,
502                                                      texrow, runparams);
503                         } else {
504                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
505                                                 runparams, everypar);
506                         }
507                 } else {
508                         par = TeXOnePar(buf, paragraphs, par, os, texrow,
509                                         runparams, everypar);
510                 }
511         }
512         // It might be that we only have a title in this document
513         if (was_title && !already_title) {
514                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
515                         os << "\\end{" << tclass.titlename()
516                             << "}\n";
517                 }
518                 else {
519                         os << "\\" << tclass.titlename()
520                             << "\n";
521                                 }
522                 texrow.newline();
523         }
524 }