1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
18 #include "texparser.h"
29 using std::istringstream;
31 using std::ostringstream;
39 char const OPEN = '<';
40 char const CLOSE = '>';
41 char const TAB = '\001';
42 char const LINE = '\002';
44 const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
45 "breton", "british", "bulgarian", "catalan", "croatian", "czech",
46 "danish", "dutch", "english", "esperanto", "estonian", "finnish",
47 "francais", "frenchb", "galician", "germanb", "greek", "hebcal", "hebfont",
48 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
49 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
50 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
51 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
52 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
53 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
54 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
55 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
57 const char * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
60 const char * known_math_envs[] = {"equation", "eqnarray", "eqnarray*",
61 "align", "align*", 0};
65 ostringstream h_preamble;
66 string h_textclass = "article";
67 string h_options = "";
68 string h_language = "english";
69 string h_inputencoding = "latin1";
70 string h_fontscheme = "default";
71 string h_graphics = "default";
72 string h_paperfontsize = "default";
73 string h_spacing = "single";
74 string h_papersize = "default";
75 string h_paperpackage = "default";
76 string h_use_geometry = "0";
77 string h_use_amsmath = "0";
78 string h_use_natbib = "0";
79 string h_use_numerical_citations = "0";
80 string h_paperorientation = "portrait";
81 string h_secnumdepth = "3";
82 string h_tocdepth = "3";
83 string h_paragraph_separation = "indent";
84 string h_defskip = "medskip";
85 string h_quotes_language = "english";
86 string h_quotes_times = "1";
87 string h_papercolumns = "1";
88 string h_papersides = "1";
89 string h_paperpagestyle = "default";
90 string h_tracking_changes = "0";
92 // indicates whether we are in the preamble
93 bool in_preamble = true;
95 // current stack of nested environments
96 stack<string> active_environments;
102 s[0] = toupper(s[0]);
107 string const trim(string const & a, char const * p = " ")
111 if (a.empty() || !*p)
114 string::size_type r = a.find_last_not_of(p);
115 string::size_type l = a.find_first_not_of(p);
117 // Is this the minimal test? (lgb)
118 if (r == string::npos && l == string::npos)
121 return a.substr(l, r - l + 1);
125 void split(string const & s, vector<string> & result, char delim)
129 while (getline(is, t, delim))
134 string join(vector<string> const & input, char delim)
137 for (size_t i = 0; i != input.size(); ++i) {
146 void handle_opt(vector<string> & opts, char const ** what, string & target)
151 for ( ; *what; ++what) {
152 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
153 if (it != opts.end()) {
154 //cerr << "### found option '" << *what << "'\n";
163 bool is_math_env(string const & name)
165 for (char const ** what = known_math_envs; *what; ++what)
172 bool is_heading(string const & name)
178 name == "paragraph" ||
181 name == "subsection" ||
182 name == "subsubsection";
186 bool is_latex_command(string const & name)
193 name == "printindex";
197 void begin_inset(ostream & os, string const & name)
199 os << "\n\\begin_inset " << name;
203 void end_inset(ostream & os)
205 os << "\n\\end_inset\n";
211 return active_environments.empty() ? string() : active_environments.top();
215 void handle_ert(ostream & os, string const & s)
217 begin_inset(os, "ERT");
218 os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
219 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
221 os << "\n\\backslash\n";
229 void handle_par(ostream & os)
231 if (active_environments.empty())
234 string s = curr_env();
235 if (s == "document" || s == "table") {
236 os << "Standard\n\n";
239 if (s == "lyxcode") {
240 os << "LyX-Code\n\n";
243 os << cap(s) << "\n\n";
247 void handle_package(string const & name, string const & options)
249 if (name == "a4wide") {
250 h_papersize = "a4paper";
251 h_paperpackage = "widemarginsa4";
252 } else if (name == "ae")
254 else if (name == "aecompl")
256 else if (name == "amsmath")
258 else if (name == "amssymb")
260 else if (name == "babel")
262 else if (name == "fontenc")
264 else if (name == "inputenc")
265 h_inputencoding = options;
266 else if (name == "makeidx")
268 else if (name == "verbatim")
272 h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
274 h_preamble << "\\usepackage{" << name << "}\n";
279 void handle_table(Parser &, ostream &)
281 // \begin{table} has been read
286 string wrap(string const & cmd, string const & str)
288 return OPEN + cmd + ' ' + str + CLOSE;
292 vector<string> extract_col_align(string const & s)
295 for (size_t i = 0; i < s.size(); ++i) {
298 res.push_back("center");
301 res.push_back("left");
304 res.push_back("right");
307 res.push_back("right");
315 void end_preamble(ostream & os)
318 os << "# tex2lyx 0.0.2 created this file\n"
319 << "\\lyxformat 222\n"
320 << "\\textclass " << h_textclass << "\n"
321 << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n"
322 << "\\options " << h_options << "\n"
323 << "\\language " << h_language << "\n"
324 << "\\inputencoding " << h_inputencoding << "\n"
325 << "\\fontscheme " << h_fontscheme << "\n"
326 << "\\graphics " << h_graphics << "\n"
327 << "\\paperfontsize " << h_paperfontsize << "\n"
328 << "\\spacing " << h_spacing << "\n"
329 << "\\papersize " << h_papersize << "\n"
330 << "\\paperpackage " << h_paperpackage << "\n"
331 << "\\use_geometry " << h_use_geometry << "\n"
332 << "\\use_amsmath " << h_use_amsmath << "\n"
333 << "\\use_natbib " << h_use_natbib << "\n"
334 << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
335 << "\\paperorientation " << h_paperorientation << "\n"
336 << "\\secnumdepth " << h_secnumdepth << "\n"
337 << "\\tocdepth " << h_tocdepth << "\n"
338 << "\\paragraph_separation " << h_paragraph_separation << "\n"
339 << "\\defskip " << h_defskip << "\n"
340 << "\\quotes_language " << h_quotes_language << "\n"
341 << "\\quotes_times " << h_quotes_times << "\n"
342 << "\\papercolumns " << h_papercolumns << "\n"
343 << "\\papersides " << h_papersides << "\n"
344 << "\\paperpagestyle " << h_paperpagestyle << "\n"
345 << "\\tracking_changes " << h_tracking_changes << "\n";
349 void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
352 Token const & t = p.getToken();
355 cerr << "t: " << t << " flags: " << flags << "\n";
359 if (flags & FLAG_ITEM) {
360 if (t.cat() == catSpace)
364 if (t.cat() == catBegin) {
365 // skip the brace and collect everything to the next matching
367 flags |= FLAG_BRACE_LAST;
371 // handle only this single token, leave the loop if done
376 if (flags & FLAG_BRACED) {
377 if (t.cat() == catSpace)
380 if (t.cat() != catBegin) {
381 p.error("opening brace expected");
385 // skip the brace and collect everything to the next matching
387 flags = FLAG_BRACE_LAST;
391 if (flags & FLAG_OPTION) {
392 if (t.cat() == catOther && t.character() == '[') {
393 parse(p, os, FLAG_BRACK_LAST, mode);
395 // no option found, put back token and we are done
404 if (t.cat() == catMath) {
405 if (mode == TEXT_MODE || mode == MATHTEXT_MODE) {
406 // we are inside some text mode thingy, so opening new math is allowed
407 if (mode == TEXT_MODE)
408 begin_inset(os, "Formula ");
409 Token const & n = p.getToken();
410 if (n.cat() == catMath) {
411 // TeX's $$...$$ syntax for displayed math
413 parse(p, os, FLAG_SIMPLE, MATH_MODE);
415 p.getToken(); // skip the second '$' token
417 // simple $...$ stuff
420 parse(p, os, FLAG_SIMPLE, MATH_MODE);
423 if (mode == TEXT_MODE)
427 else if (flags & FLAG_SIMPLE) {
428 // this is the end of the formula
433 cerr << "mode: " << mode << endl;
434 p.error("something strange in the parser\n");
439 else if (t.cat() == catLetter)
442 else if (t.cat() == catSpace)
445 else if (t.cat() == catNewline)
448 else if (t.cat() == catSuper)
451 else if (t.cat() == catSub)
454 else if (t.cat() == catParameter) {
455 Token const & n = p.getToken();
456 os << wrap("macroarg", string(1, n.character()));
459 else if (t.cat() == catActive) {
460 if (t.character() == '~')
461 os << (curr_env() == "lyxcode" ? ' ' : '~');
466 else if (t.cat() == catBegin) {
467 if (mode == MATH_MODE)
473 else if (t.cat() == catEnd) {
474 if (flags & FLAG_BRACE_LAST)
476 if (mode == MATH_MODE)
482 else if (t.cat() == catAlign) {
483 if (mode == MATH_MODE)
489 else if (t.cs() == "tabularnewline") {
490 if (mode == MATH_MODE)
496 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
497 //cerr << "finished reading option\n";
501 else if (t.cat() == catOther)
502 os << string(1, t.character());
504 else if (t.cat() == catComment) {
507 Token const & t = p.getToken();
508 if (t.cat() == catNewline)
512 //os << wrap("comment", s);
520 else if (t.cs() == "lyxlock")
523 else if (t.cs() == "newcommand" || t.cs() == "providecommand") {
524 string const name = p.verbatimItem();
525 string const opts = p.getArg('[', ']');
526 string const body = p.verbatimItem();
527 // only non-lyxspecific stuff
528 if (name != "\\noun " && name != "\\tabularnewline ") {
529 ostream & out = in_preamble ? h_preamble : os;
531 begin_inset(os, "FormulaMacro\n");
532 out << "\\" << t.cs() << "{" << name << "}";
534 out << "[" << opts << "]";
535 out << "{" << body << "}";
541 else if (t.cs() == "(") {
542 begin_inset(os, "Formula");
544 parse(p, os, FLAG_SIMPLE2, MATH_MODE);
549 else if (t.cs() == "[") {
550 begin_inset(os, "Formula");
552 parse(p, os, FLAG_EQUATION, MATH_MODE);
557 else if (t.cs() == "protect")
558 // ignore \\protect, will hopefully be re-added during output
561 else if (t.cs() == "begin") {
562 string const name = p.getArg('{', '}');
563 active_environments.push(name);
564 if (name == "document") {
566 parse(p, os, FLAG_END, mode);
567 } else if (name == "abstract") {
569 parse(p, os, FLAG_END, mode);
570 } else if (is_math_env(name)) {
571 begin_inset(os, "Formula ");
572 os << "\\begin{" << name << "}";
573 parse(p, os, FLAG_END, MATH_MODE);
574 os << "\\end{" << name << "}";
576 } else if (name == "tabular") {
577 begin_inset(os, "Tabular \n");
578 string colopts = p.verbatimItem();
579 vector<string> colalign = extract_col_align(colopts);
581 parse(p, ss, FLAG_END, mode);
582 vector<string> lines;
583 split(ss.str(), lines, LINE);
584 const size_t cols = colalign.size();
585 const size_t rows = lines.size();
586 os << "<lyxtabular version=\"3\" rows=\"" << rows
587 << "\" columns=\"" << cols << "\">\n"
589 for (size_t c = 0; c < cols; ++c)
590 os << "<column alignment=\"" << colalign[c] << "\""
591 << " valignment=\"top\""
594 for (size_t r = 0; r < rows; ++r) {
595 vector<string> cells;
596 split(lines[r], cells, TAB);
597 while (cells.size() < cols)
598 cells.push_back(string());
599 //os << "<row bottomline=\"true\">\n";
601 for (size_t c = 0; c < cols; ++c) {
602 os << "<cell alignment=\"center\""
603 << " valignment=\"top\""
604 << " topline=\"true\""
605 << " leftline=\"true\""
606 << " usebox=\"none\""
608 begin_inset(os, "Text");
609 os << "\n\n\\layout Standard\n\n";
616 os << "</lyxtabular>\n";
618 } else if (name == "table") {
619 begin_inset(os, "Float table\n");
621 << "collapsed false\n"
623 << "\\layout Standard\n";
624 parse(p, os, FLAG_END, mode);
626 } else if (name == "thebibliography") {
627 p.verbatimItem(); // swallow next arg
628 parse(p, os, FLAG_END, mode);
629 os << "\n\\layout Standard\n\n";
630 } else if (mode == MATH_MODE) {
631 os << "\\begin{" << name << "}";
632 parse(p, os, FLAG_END, mode);
633 os << "\\end{" << name << "}";
635 parse(p, os, FLAG_END, mode);
639 else if (t.cs() == "end") {
640 if (flags & FLAG_END) {
641 // eat environment name
642 string const name = p.getArg('{', '}');
643 if (name != curr_env())
644 p.error("\\end{" + name + "} does not match \\begin{"
646 active_environments.pop();
649 p.error("found 'end' unexpectedly");
652 else if (t.cs() == "item")
655 else if (t.cs() == ")") {
656 if (flags & FLAG_SIMPLE2)
658 p.error("found '\\)' unexpectedly");
661 else if (t.cs() == "]") {
662 if (flags & FLAG_EQUATION)
664 p.error("found '\\]' unexpectedly");
667 else if (t.cs() == "documentclass") {
669 split(p.getArg('[', ']'), opts, ',');
670 handle_opt(opts, known_languages, h_language);
671 handle_opt(opts, known_fontsizes, h_paperfontsize);
672 h_quotes_language = h_language;
673 h_options = join(opts, ',');
674 h_textclass = p.getArg('{', '}');
677 else if (t.cs() == "usepackage") {
678 string const options = p.getArg('[', ']');
679 string const name = p.getArg('{', '}');
680 if (options.empty() && name.find(',')) {
681 vector<string> vecnames;
682 split(name, vecnames, ',');
683 vector<string>::const_iterator it = vecnames.begin();
684 vector<string>::const_iterator end = vecnames.end();
685 for (; it != end; ++it)
686 handle_package(trim(*it), string());
688 handle_package(name, options);
692 else if (t.cs() == "newenvironment") {
693 string const name = p.getArg('{', '}');
695 string const begin = p.verbatimItem();
697 string const end = p.verbatimItem();
699 if (name != "lyxcode")
700 os << wrap("newenvironment", begin + end);
703 else if (t.cs() == "def") {
704 string name = p.getToken().cs();
705 while (p.nextToken().cat() != catBegin)
706 name += p.getToken().asString();
707 handle_ert(os, "\\def\\" + name + '{' + p.verbatimItem() + '}');
710 else if (t.cs() == "setcounter") {
711 string const name = p.getArg('{', '}');
712 string const content = p.getArg('{', '}');
713 if (name == "secnumdepth")
714 h_secnumdepth = content;
715 else if (name == "tocdepth")
716 h_tocdepth = content;
718 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
721 else if (t.cs() == "setlength") {
722 string const name = p.getToken().cs();
723 string const content = p.getArg('{', '}');
724 if (name == "parskip")
725 h_paragraph_separation = "skip";
726 else if (name == "parindent")
727 h_paragraph_separation = "skip";
729 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
732 else if (t.cs() == "par")
735 else if (is_heading(t.cs())) {
736 string name = t.cs();
737 if (p.nextToken().asInput() == "*") {
741 os << "\\layout " << cap(name) << "\n\n";
742 parse(p, os, FLAG_ITEM, mode);
745 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
748 else if (t.cs() == "tableofcontents")
749 p.verbatimItem(); // swallow this
751 else if (t.cs() == "textrm") {
752 os << '\\' << t.cs() << '{';
753 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
757 else if ((t.cs() == "emph" || t.cs() == "noun") && mode == TEXT_MODE) {
758 os << "\n\\" << t.cs() << " on\n";
759 parse(p, os, FLAG_ITEM, mode);
760 os << "\n\\" << t.cs() << " default\n";
763 else if (is_latex_command(t.cs()) && mode == TEXT_MODE) {
764 begin_inset(os, "LatexCommand ");
765 os << '\\' << t.cs() << '{';
766 parse(p, os, FLAG_ITEM, TEXT_MODE);
771 else if (t.cs() == "bibitem") {
772 os << "\n\\layout Bibliography\n\\bibitem ";
773 string opt = p.getArg('[',']');
775 os << '[' << opt << ']';
776 os << '{' << p.getArg('{','}') << '}' << "\n\n";
779 else if (t.cs() == "textasciitilde")
782 else if (t.cs() == "_" && mode == TEXT_MODE)
785 else if (t.cs() == "&" && mode == TEXT_MODE)
788 else if (t.cs() == "pagestyle" && in_preamble)
789 h_paperpagestyle == p.getArg('{','}');
792 if (mode == MATH_MODE)
794 else if (in_preamble)
795 h_preamble << t.asInput();
797 // heuristic: read up to next non-nested space
799 string s = t.asInput();
800 string z = p.verbatimItem();
801 while (p.good() && z != " " && z.size()) {
802 //cerr << "read: " << z << endl;
804 z = p.verbatimItem();
806 cerr << "found ERT: " << s << endl;
807 handle_ert(os, s + ' ');
809 handle_ert(os, t.asInput() + ' ');
813 if (flags & FLAG_LEAVE) {
814 flags &= ~FLAG_LEAVE;
822 } // anonymous namespace
825 int main(int argc, char * argv[])
828 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
832 ifstream is(argv[1]);
834 parse(p, cout, 0, TEXT_MODE);
835 cout << "\n\\the_end";