]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/preamble.cpp
tex2lyx/text.cpp: \InsetSpace has no begin and end (LyX 1.5 was tolerant to the missi...
[lyx.git] / src / tex2lyx / preamble.cpp
1 /**
2  * \file preamble.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Uwe Stöhr
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 // {[(
13
14 #include <config.h>
15
16 #include "tex2lyx.h"
17
18 #include "LayoutFile.h"
19 #include "Layout.h"
20 #include "Lexer.h"
21 #include "TextClass.h"
22
23 #include "support/convert.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
27
28 #include <algorithm>
29 #include <iostream>
30 #include <sstream>
31 #include <string>
32 #include <vector>
33 #include <map>
34
35 using namespace std;
36 using namespace lyx::support;
37
38 namespace lyx {
39
40 // special columntypes
41 extern map<char, int> special_columns;
42
43 map<string, vector<string> > used_packages;
44
45 // needed to handle encodings with babel
46 bool one_language = true;
47
48 // to avoid that the babel options overwrite the documentclass options
49 bool documentclass_language;
50
51 namespace {
52
53 const char * const known_languages[] = { "afrikaans", "american", "arabic",
54 "austrian", "bahasa", "basque", "belarusian", "brazil", "breton", "british",
55 "bulgarian", "canadian", "canadien", "catalan", "croatian", "czech", "danish",
56 "dutch", "english", "esperanto", "estonian", "finnish", "francais", "french",
57 "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb", "greek",
58 "hebrew", "icelandic", "irish", "italian", "lsorbian", "magyar", "naustrian",
59 "ngerman", "ngermanb", "norsk", "nynorsk", "polish", "portuges", "romanian",
60 "russian", "russianb", "scottish", "serbian", "slovak", "slovene", "spanish",
61 "swedish", "thai", "turkish", "ukraineb", "ukrainian", "usorbian", "welsh", 0};
62
63 //note this when updating to lyxformat 305:
64 //bahasai, indonesian, and indon = equal to bahasa
65 //malay, and meyalu = equal to bahasam
66
67 const char * const known_french_languages[] = {"french", "frenchb", "francais",
68                                                 "frenchle", "frenchpro", 0};
69 const char * const known_german_languages[] = {"german", "germanb", 0};
70 const char * const known_ngerman_languages[] = {"ngerman", "ngermanb", 0};
71 const char * const known_russian_languages[] = {"russian", "russianb", 0};
72 const char * const known_ukrainian_languages[] = {"ukrainian", "ukraineb", 0};
73
74 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
75
76 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
77 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
78
79 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
80 "helvet", "lmss", 0};
81
82 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
83 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
84 "newcent", 0};
85
86 // some ugly stuff
87 ostringstream h_preamble;
88 string h_textclass               = "article";
89 string h_options                 = string();
90 string h_language                = "english";
91 string h_inputencoding           = "auto";
92 string h_font_roman              = "default";
93 string h_font_sans               = "default";
94 string h_font_typewriter         = "default";
95 string h_font_default_family     = "default";
96 string h_font_sc                 = "false";
97 string h_font_osf                = "false";
98 string h_font_sf_scale           = "100";
99 string h_font_tt_scale           = "100";
100 string h_graphics                = "default";
101 string h_paperfontsize           = "default";
102 string h_spacing                 = "single";
103 string h_papersize               = "default";
104 string h_use_geometry            = "false";
105 string h_use_amsmath             = "0";
106 string h_cite_engine             = "basic";
107 string h_use_bibtopic            = "false";
108 string h_paperorientation        = "portrait";
109 string h_secnumdepth             = "3";
110 string h_tocdepth                = "3";
111 string h_paragraph_separation    = "indent";
112 string h_defskip                 = "medskip";
113 string h_quotes_language         = "english";
114 string h_papercolumns            = "1";
115 string h_papersides              = string();
116 string h_paperpagestyle          = "default";
117 string h_tracking_changes        = "false";
118 string h_output_changes          = "false";
119
120
121 void handle_opt(vector<string> & opts, char const * const * what, string & target)
122 {
123         if (opts.empty())
124                 return;
125
126         // the last language option is the document language (for babel and LyX)
127         // the last size option is the document font size
128         vector<string>::iterator it;
129         vector<string>::iterator position = opts.begin();
130         for (; *what; ++what) {
131                 it = find(opts.begin(), opts.end(), *what);
132                 if (it != opts.end()) {
133                         documentclass_language = true;
134                         if (it >= position) {
135                                 target = *what;
136                                 position = it;
137                         }
138                 }
139         }
140 }
141
142
143 void delete_opt(vector<string> & opts, char const * const * what)
144 {
145         if (opts.empty())
146                 return;
147
148         // remove found options from the list
149         // do this after handle_opt to avoid potential memory leaks and to be able
150         // to find in every case the last language option
151         vector<string>::iterator it;
152         for (; *what; ++what) {
153                 it = find(opts.begin(), opts.end(), *what);
154                 if (it != opts.end())
155                         opts.erase(it);
156         }
157 }
158
159
160 /*!
161  * Split a package options string (keyval format) into a vector.
162  * Example input:
163  *   authorformat=smallcaps,
164  *   commabeforerest,
165  *   titleformat=colonsep,
166  *   bibformat={tabular,ibidem,numbered}
167  */
168 vector<string> split_options(string const & input)
169 {
170         vector<string> options;
171         string option;
172         Parser p(input);
173         while (p.good()) {
174                 Token const & t = p.get_token();
175                 if (t.asInput() == ",") {
176                         options.push_back(trim(option));
177                         option.erase();
178                 } else if (t.asInput() == "=") {
179                         option += '=';
180                         p.skip_spaces(true);
181                         if (p.next_token().asInput() == "{")
182                                 option += '{' + p.getArg('{', '}') + '}';
183                 } else if (t.cat() != catSpace)
184                         option += t.asInput();
185         }
186
187         if (!option.empty())
188                 options.push_back(trim(option));
189
190         return options;
191 }
192
193
194 /*!
195  * Add package \p name with options \p options to used_packages.
196  * Remove options from \p options that we don't want to output.
197  */
198 void add_package(string const & name, vector<string> & options)
199 {
200         // every package inherits the global options
201         if (used_packages.find(name) == used_packages.end())
202                 used_packages[name] = split_options(h_options);
203
204         vector<string> & v = used_packages[name];
205         v.insert(v.end(), options.begin(), options.end());
206         if (name == "jurabib") {
207                 // Don't output the order argument (see the cite command
208                 // handling code in text.cpp).
209                 vector<string>::iterator end =
210                         remove(options.begin(), options.end(), "natbiborder");
211                 end = remove(options.begin(), end, "jurabiborder");
212                 options.erase(end, options.end());
213         }
214 }
215
216
217 // Given is a string like "scaled=0.9", return 0.9 * 100
218 string const scale_as_percentage(string const & scale)
219 {
220         string::size_type pos = scale.find('=');
221         if (pos != string::npos) {
222                 string value = scale.substr(pos + 1);
223                 if (isStrDbl(value))
224                         return convert<string>(100 * convert<double>(value));
225         }
226         // If the input string didn't match our expectations.
227         // return the default value "100"
228         return "100";
229 }
230
231
232 void handle_package(string const & name, string const & opts)
233 {
234         vector<string> options = split_options(opts);
235         add_package(name, options);
236         string scale;
237
238         // roman fonts
239         if (is_known(name, known_roman_fonts))
240                 h_font_roman = name;
241
242         if (name == "fourier") {
243                 h_font_roman = "utopia";
244                 // when font uses real small capitals
245                 if (opts == "expert")
246                         h_font_sc = "true";
247         }
248         if (name == "mathpazo")
249                 h_font_roman = "palatino";
250
251         if (name == "mathptmx")
252                 h_font_roman = "times";
253
254         // sansserif fonts
255         if (is_known(name, known_sans_fonts)) {
256                 h_font_sans = name;
257                 if (!opts.empty()) {
258                         scale = opts;
259                         h_font_sf_scale = scale_as_percentage(scale);
260                 }
261         }
262         // typewriter fonts
263         if (is_known(name, known_typewriter_fonts)) {
264                 h_font_typewriter = name;
265                 if (!opts.empty()) {
266                         scale = opts;
267                         h_font_tt_scale = scale_as_percentage(scale);
268                 }
269         }
270         // font uses old-style figure
271         if (name == "eco")
272                 h_font_osf = "true";
273
274         else if (name == "amsmath" || name == "amssymb")
275                 h_use_amsmath = "1";
276
277         else if (name == "babel" && !opts.empty()) {
278                 // check if more than one option was used - used later for inputenc
279                 // in case inputenc is parsed before babel, set the encoding to auto
280                 if (options.begin() != options.end() - 1) {
281                         one_language = false;
282                         h_inputencoding = "auto";
283                 }
284                 // only set the document language when there was not already one set
285                 // via the documentclass options
286                 // babel takes the the last language given in the documentclass options
287                 // as document language. If there is no such language option, the last
288                 // option of its \usepackage call is used.
289                 if (documentclass_language == false) {
290                         handle_opt(options, known_languages, h_language);
291                         delete_opt(options, known_languages);
292                         if (is_known(h_language, known_french_languages))
293                                 h_language = "french";
294                         else if (is_known(h_language, known_german_languages))
295                                 h_language = "german";
296                         else if (is_known(h_language, known_ngerman_languages))
297                                 h_language = "ngerman";
298                         else if (is_known(h_language, known_russian_languages))
299                                 h_language = "russian";
300                         else if (is_known(h_language, known_ukrainian_languages))
301                                 h_language = "ukrainian";
302                         h_quotes_language = h_language;
303                 }
304         }
305         else if (name == "fontenc")
306                 ; // ignore this
307
308         else if (name == "inputenc") {
309                 // only set when there is not more than one inputenc option
310                 // therefore check for the "," character
311                 // also only set when there is not more then one babel language option
312                 if (opts.find(",") == string::npos && one_language == true)
313                         if (opts == "ascii")
314                                 //change ascii to auto to be in the unicode range, see
315                                 //http://bugzilla.lyx.org/show_bug.cgi?id=4719
316                                 h_inputencoding = "auto";
317                         else
318                                 h_inputencoding = opts;
319                 options.clear();
320         }
321         else if (name == "makeidx")
322                 ; // ignore this
323
324         else if (name == "verbatim")
325                 ; // ignore this
326
327         else if (name == "color")
328                 // with the following command this package is only loaded when needed for
329                 // undefined colors, since we only support the predefined colors
330                 h_preamble << "\\@ifundefined{definecolor}\n {\\usepackage{color}}{}\n";
331
332         else if (name == "graphicx")
333                 ; // ignore this
334
335         else if (name == "setspace")
336                 ; // ignore this
337
338         else if (is_known(name, known_languages)) {
339                 if (is_known(name, known_french_languages))
340                         h_language = "french";
341                 else if (is_known(name, known_german_languages))
342                         h_language = "german";
343                 else if (is_known(name, known_ngerman_languages))
344                         h_language = "ngerman";
345                 else if (is_known(name, known_russian_languages))
346                         h_language = "russian";
347                 else if (is_known(name, known_ukrainian_languages))
348                         h_language = "ukrainian";
349                 else
350                         h_language = name;
351                 h_quotes_language = h_language;
352         }
353         else if (name == "natbib") {
354                 h_cite_engine = "natbib_authoryear";
355                 vector<string>::iterator it =
356                         find(options.begin(), options.end(), "authoryear");
357                 if (it != options.end())
358                         options.erase(it);
359                 else {
360                         it = find(options.begin(), options.end(), "numbers");
361                         if (it != options.end()) {
362                                 h_cite_engine = "natbib_numerical";
363                                 options.erase(it);
364                         }
365                 }
366         }
367         else if (name == "jurabib")
368                 h_cite_engine = "jurabib";
369
370         else if (options.empty())
371                 h_preamble << "\\usepackage{" << name << "}\n";
372         else {
373                 h_preamble << "\\usepackage[" << opts << "]{" << name << "}\n";
374                 options.clear();
375         }
376         // We need to do something with the options...
377         if (!options.empty())
378                 cerr << "Ignoring options '" << join(options, ",")
379                      << "' of package " << name << '.' << endl;
380 }
381
382
383
384 void end_preamble(ostream & os, TextClass const & /*textclass*/)
385 {
386         os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
387            << "\\lyxformat 247\n"
388            << "\\begin_document\n"
389            << "\\begin_header\n"
390            << "\\textclass " << h_textclass << "\n";
391         if (!h_preamble.str().empty())
392                 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
393         if (!h_options.empty())
394                 os << "\\options " << h_options << "\n";
395         os << "\\language " << h_language << "\n"
396            << "\\inputencoding " << h_inputencoding << "\n"
397            << "\\font_roman " << h_font_roman << "\n"
398            << "\\font_sans " << h_font_sans << "\n"
399            << "\\font_typewriter " << h_font_typewriter << "\n"
400            << "\\font_default_family " << h_font_default_family << "\n"
401            << "\\font_sc " << h_font_sc << "\n"
402            << "\\font_osf " << h_font_osf << "\n"
403            << "\\font_sf_scale " << h_font_sf_scale << "\n"
404            << "\\font_tt_scale " << h_font_tt_scale << "\n"
405            << "\\graphics " << h_graphics << "\n"
406            << "\\paperfontsize " << h_paperfontsize << "\n"
407            << "\\spacing " << h_spacing << "\n"
408            << "\\papersize " << h_papersize << "\n"
409            << "\\use_geometry " << h_use_geometry << "\n"
410            << "\\use_amsmath " << h_use_amsmath << "\n"
411            << "\\cite_engine " << h_cite_engine << "\n"
412            << "\\use_bibtopic " << h_use_bibtopic << "\n"
413            << "\\paperorientation " << h_paperorientation << "\n"
414            << "\\secnumdepth " << h_secnumdepth << "\n"
415            << "\\tocdepth " << h_tocdepth << "\n"
416            << "\\paragraph_separation " << h_paragraph_separation << "\n"
417            << "\\defskip " << h_defskip << "\n"
418            << "\\quotes_language " << h_quotes_language << "\n"
419            << "\\papercolumns " << h_papercolumns << "\n"
420            << "\\papersides " << h_papersides << "\n"
421            << "\\paperpagestyle " << h_paperpagestyle << "\n"
422            << "\\tracking_changes " << h_tracking_changes << "\n"
423            << "\\output_changes " << h_output_changes << "\n"
424            << "\\end_header\n\n"
425            << "\\begin_body\n";
426         // clear preamble for subdocuments
427         h_preamble.str("");
428 }
429
430 } // anonymous namespace
431
432 void parse_preamble(Parser & p, ostream & os, 
433         string const & forceclass, TeX2LyXDocClass & tc)
434 {
435         // initialize fixed types
436         special_columns['D'] = 3;
437         bool is_full_document = false;
438
439         // determine whether this is a full document or a fragment for inclusion
440         while (p.good()) {
441                 Token const & t = p.get_token();
442
443                 if (t.cat() == catEscape && t.cs() == "documentclass") {
444                         is_full_document = true;
445                         break;
446                 }
447         }
448         p.reset();
449
450         while (is_full_document && p.good()) {
451                 Token const & t = p.get_token();
452
453 #ifdef FILEDEBUG
454                 cerr << "t: " << t << "\n";
455 #endif
456
457                 //
458                 // cat codes
459                 //
460                 if (t.cat() == catLetter ||
461                           t.cat() == catSuper ||
462                           t.cat() == catSub ||
463                           t.cat() == catOther ||
464                           t.cat() == catMath ||
465                           t.cat() == catActive ||
466                           t.cat() == catBegin ||
467                           t.cat() == catEnd ||
468                           t.cat() == catAlign ||
469                           t.cat() == catParameter)
470                 h_preamble << t.character();
471
472                 else if (t.cat() == catSpace || t.cat() == catNewline)
473                         h_preamble << t.asInput();
474
475                 else if (t.cat() == catComment)
476                         h_preamble << t.asInput();
477
478                 else if (t.cs() == "pagestyle")
479                         h_paperpagestyle = p.verbatim_item();
480
481                 else if (t.cs() == "makeatletter") {
482                         p.setCatCode('@', catLetter);
483                 }
484
485                 else if (t.cs() == "makeatother") {
486                         p.setCatCode('@', catOther);
487                 }
488
489                 else if (t.cs() == "newcommand" 
490                          || t.cs() == "renewcommand"
491                          || t.cs() == "providecommand"
492                          || t.cs() == "newlyxcommand") {
493                         bool star = false;
494                         if (p.next_token().character() == '*') {
495                                 p.get_token();
496                                 star = true;
497                         }
498                         string const name = p.verbatim_item();
499                         string const opt1 = p.getOpt();
500                         string const opt2 = p.getFullOpt();
501                         string const body = p.verbatim_item();
502                         // font settings
503                         if (name == "\\rmdefault")
504                                 if (is_known(body, known_roman_fonts))
505                                         h_font_roman = body;
506
507                         if (name == "\\sfdefault")
508                                 if (is_known(body, known_sans_fonts))
509                                         h_font_sans = body;
510
511                         if (name == "\\ttdefault")
512                                 if (is_known(body, known_typewriter_fonts))
513                                         h_font_typewriter = body;
514
515                         if (name == "\\familydefault") {
516                                 string family = body;
517                                 // remove leading "\"
518                                 h_font_default_family = family.erase(0,1);
519                         }
520                         // only non-lyxspecific stuff
521                         if (   name != "\\noun"
522                             && name != "\\tabularnewline"
523                             && name != "\\LyX"
524                             && name != "\\lyxline"
525                             && name != "\\lyxaddress"
526                             && name != "\\lyxrightaddress"
527                             && name != "\\lyxdot"
528                             && name != "\\boldsymbol"
529                             && name != "\\lyxarrow"
530                             && name != "\\rmdefault"
531                             && name != "\\sfdefault"
532                             && name != "\\ttdefault"
533                             && name != "\\familydefault") {
534                                 ostringstream ss;
535                                 ss << '\\' << t.cs();
536                                 if (star)
537                                         ss << '*';
538                                 ss << '{' << name << '}' << opt1 << opt2
539                                    << '{' << body << "}";
540                                 h_preamble << ss.str();
541
542                                 // Add the command to the known commands
543                                 add_known_command(name, opt1, !opt2.empty());
544 /*
545                                 ostream & out = in_preamble ? h_preamble : os;
546                                 out << "\\" << t.cs() << "{" << name << "}"
547                                     << opts << "{" << body << "}";
548 */
549                         }
550                 }
551
552                 else if (t.cs() == "documentclass") {
553                         vector<string> opts = split_options(p.getArg('[', ']'));
554                         handle_opt(opts, known_fontsizes, h_paperfontsize);
555                         delete_opt(opts, known_fontsizes);
556                         // delete "pt" at the end
557                         string::size_type i = h_paperfontsize.find("pt");
558                         if (i != string::npos)
559                                 h_paperfontsize.erase(i);
560                         // to avoid that the babel options overwrite the documentclass options
561                         documentclass_language = false;
562                         handle_opt(opts, known_languages, h_language);
563                         delete_opt(opts, known_languages);
564                         if (is_known(h_language, known_french_languages))
565                                 h_language = "french";
566                         else if (is_known(h_language, known_german_languages))
567                                 h_language = "german";
568                         else if (is_known(h_language, known_ngerman_languages))
569                                 h_language = "ngerman";
570                         else if (is_known(h_language, known_russian_languages))
571                                 h_language = "russian";
572                         else if (is_known(h_language, known_ukrainian_languages))
573                                 h_language = "ukrainian";
574                         h_quotes_language = h_language;
575                         h_options = join(opts, ",");
576                         h_textclass = p.getArg('{', '}');
577                 }
578
579                 else if (t.cs() == "usepackage") {
580                         string const options = p.getArg('[', ']');
581                         string const name = p.getArg('{', '}');
582                         if (options.empty() && name.find(',')) {
583                                 vector<string> vecnames;
584                                 split(name, vecnames, ',');
585                                 vector<string>::const_iterator it  = vecnames.begin();
586                                 vector<string>::const_iterator end = vecnames.end();
587                                 for (; it != end; ++it)
588                                         handle_package(trim(*it), string());
589                         } else {
590                                 handle_package(name, options);
591                         }
592                 }
593
594                 else if (t.cs() == "newenvironment") {
595                         string const name = p.getArg('{', '}');
596                         ostringstream ss;
597                         ss << "\\newenvironment{" << name << "}";
598                         ss << p.getOpt();
599                         ss << p.getOpt();
600                         ss << '{' << p.verbatim_item() << '}';
601                         ss << '{' << p.verbatim_item() << '}';
602                         if (name != "lyxcode" && name != "lyxlist" &&
603                             name != "lyxrightadress" &&
604                             name != "lyxaddress" && name != "lyxgreyedout")
605                                 h_preamble << ss.str();
606                 }
607
608                 else if (t.cs() == "def") {
609                         string name = p.get_token().cs();
610                         while (p.next_token().cat() != catBegin)
611                                 name += p.get_token().asString();
612                         h_preamble << "\\def\\" << name << '{'
613                                    << p.verbatim_item() << "}";
614                 }
615
616                 else if (t.cs() == "newcolumntype") {
617                         string const name = p.getArg('{', '}');
618                         trim(name);
619                         int nargs = 0;
620                         string opts = p.getOpt();
621                         if (!opts.empty()) {
622                                 istringstream is(string(opts, 1));
623                                 is >> nargs;
624                         }
625                         special_columns[name[0]] = nargs;
626                         h_preamble << "\\newcolumntype{" << name << "}";
627                         if (nargs)
628                                 h_preamble << "[" << nargs << "]";
629                         h_preamble << "{" << p.verbatim_item() << "}";
630                 }
631
632                 else if (t.cs() == "setcounter") {
633                         string const name = p.getArg('{', '}');
634                         string const content = p.getArg('{', '}');
635                         if (name == "secnumdepth")
636                                 h_secnumdepth = content;
637                         else if (name == "tocdepth")
638                                 h_tocdepth = content;
639                         else
640                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
641                 }
642
643                 else if (t.cs() == "setlength") {
644                         string const name = p.verbatim_item();
645                         string const content = p.verbatim_item();
646                         // the paragraphs are only not indented when \parindent is set to zero
647                         if (name == "\\parindent" && content != "") {
648                                 if (content[0] == '0')
649                                         h_paragraph_separation = "skip";
650                         } else if (name == "\\parskip") {
651                                 if (content == "\\smallskipamount")
652                                         h_defskip = "smallskip";
653                                 else if (content == "\\medskipamount")
654                                         h_defskip = "medskip";
655                                 else if (content == "\\bigskipamount")
656                                         h_defskip = "bigskip";
657                                 else
658                                         h_defskip = content;
659                         } else
660                                 h_preamble << "\\setlength{" << name << "}{" << content << "}";
661                 }
662
663                 else if (t.cs() =="onehalfspacing")
664                         h_spacing = "onehalf";
665
666                 else if (t.cs() =="doublespacing")
667                         h_spacing = "double";
668
669                 else if (t.cs() =="setstretch")
670                         h_spacing = "other " + p.verbatim_item();
671
672                 else if (t.cs() == "begin") {
673                         string const name = p.getArg('{', '}');
674                         if (name == "document")
675                                 break;
676                         h_preamble << "\\begin{" << name << "}";
677                 }
678
679                 else if (t.cs() == "jurabibsetup") {
680                         vector<string> jurabibsetup =
681                                 split_options(p.getArg('{', '}'));
682                         // add jurabibsetup to the jurabib package options
683                         add_package("jurabib", jurabibsetup);
684                         if (!jurabibsetup.empty()) {
685                                 h_preamble << "\\jurabibsetup{"
686                                            << join(jurabibsetup, ",") << '}';
687                         }
688                 }
689
690                 else if (!t.cs().empty())
691                         h_preamble << '\\' << t.cs();
692         }
693         p.skip_spaces();
694
695         // Force textclass if the user wanted it
696         if (!forceclass.empty())
697                 h_textclass = forceclass;
698         if (noweb_mode && !prefixIs(h_textclass, "literate-"))
699                 h_textclass.insert(0, "literate-");
700         FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
701         if (layoutfilename.empty()) {
702                 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
703                 exit(1);
704         }
705         tc.read(layoutfilename);
706         if (h_papersides.empty()) {
707                 ostringstream ss;
708                 ss << tc.sides();
709                 h_papersides = ss.str();
710         }
711         end_preamble(os, tc);
712 }
713
714 // }])
715
716
717 } // namespace lyx