3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
21 #include "support/lassert.h"
22 #include "support/convert.h"
23 #include "support/lstrings.h"
40 ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0),
41 decimal_point('\0') {}
44 /// vertical alignment
48 /// special column alignment
50 /// number of lines on the right
52 /// number of lines on the left
59 /// row type for longtables
66 /// part of head on first page
70 /// part of foot on last page
77 RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
78 caption(false), newpage(false) {}
79 /// Does this row have any special setting?
82 return topline || bottomline || !top_space.empty() ||
83 !bottom_space.empty() || !interline_space.empty() ||
84 type != LT_NORMAL || caption || newpage;
86 /// horizontal line above
88 /// horizontal line below
90 /// Extra space between the top line and this row
92 /// Extra space between this row and the bottom line
94 /// Extra space between the bottom line and the next top line
95 string interline_space;
96 /// These are for longtabulars only
97 /// row type (head, foot, firsthead etc.)
101 /// row for a newpage
106 /// the numeric values are part of the file format!
110 /// A multicolumn cell. The number of columns is <tt>1 + number
111 /// of CELL_PART_OF_MULTICOLUMN cells</tt> that follow directly
112 CELL_BEGIN_OF_MULTICOLUMN,
113 /// This is a dummy cell (part of a multicolumn cell)
114 CELL_PART_OF_MULTICOLUMN,
116 CELL_BEGIN_OF_MULTIROW,
118 CELL_PART_OF_MULTIROW
124 CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
125 leftlines(0), rightlines(0), topline(false),
126 bottomline(false), rotate(0), mrxnum(0) {}
133 /// vertical cell alignment
135 /// number of lines on the left
137 /// number of lines on the right
139 /// do we have a line above?
141 /// do we have a line below?
143 /// how is the cell rotated?
145 /// width for multicolumn cells
147 /// special formatting for multicolumn cells
151 /// number of further multirows
159 ltType() : set(false), topDL(false), bottomDL(false), empty(false) {}
160 // we have this header type (is set in the getLT... functions)
162 // double borders on top
164 // double borders on bottom
166 // used for FirstHeader & LastFooter and if this is true
167 // all the rows marked as FirstHeader or LastFooter are
168 // ignored in the output and it is set to be empty!
173 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
174 inline char const * verbose_align(char c)
191 /// translate a vertical alignment (as stored in ColInfo and CellInfo) to LyX
192 inline char const * verbose_valign(char c)
194 // The default value for no special alignment is "top".
207 // stripped down from tabluar.C. We use it currently only for bools and
209 string const write_attribute(string const & name, bool const & b)
211 // we write only true attribute values so we remove a bit of the
212 // file format bloat for tabulars.
213 return b ? ' ' + name + "=\"true\"" : string();
217 string const write_attribute(string const & name, string const & s)
219 return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
223 string const write_attribute(string const & name, int const & i)
225 // we write only true attribute values so we remove a bit of the
226 // file format bloat for tabulars.
227 return i ? write_attribute(name, convert<string>(i)) : string();
231 /*! rather brutish way to code table structure in a string:
236 \multicolumn{2}{c}{4} & 5 //
242 gets "translated" to:
245 HLINE 1 TAB 2 TAB 3 HLINE HLINE LINE
246 \hline HLINE \multicolumn{2}{c}{4} TAB 5 HLINE HLINE LINE
247 HLINE 6 TAB 7 HLINE HLINE LINE
248 HLINE 8 HLINE \endhead HLINE LINE
252 char const TAB = '\001';
253 char const LINE = '\002';
254 char const HLINE = '\004';
258 * Move the information in leftlines, rightlines, align and valign to the
259 * special field. This is necessary if the special field is not empty,
260 * because LyX ignores leftlines > 1, rightlines > 1, align and valign in
263 void ci2special(ColInfo & ci)
265 if (ci.width.empty() && ci.align == 'n')
266 // The alignment setting is already in special, since
267 // handle_colalign() never stores ci with these settings
268 // and ensures that leftlines == 0 and rightlines == 0 in
272 if (ci.decimal_point != '\0') {
273 // we only support decimal point natively
274 // with 'l' alignment in or 'n' alignment
275 // with width in second row
276 if (ci.align != 'l' && ci.align != 'n') {
277 ci.decimal_point = '\0';
283 if (!ci.width.empty()) {
286 ci.special += ">{\\raggedright}";
289 ci.special += ">{\\raggedleft}";
292 ci.special += ">{\\centering}";
295 if (ci.valign == 'n')
298 ci.special += ci.valign;
299 ci.special += '{' + ci.width + '}';
302 ci.special += ci.align;
304 // LyX can only have one left and one right line.
305 for (int i = 1; i < ci.leftlines; ++i)
306 ci.special.insert(0, "|");
307 for (int i = 1; i < ci.rightlines; ++i)
309 ci.leftlines = min(ci.leftlines, 1);
310 ci.rightlines = min(ci.rightlines, 1);
317 * Handle column specifications for tabulars and multicolumns.
318 * The next token of the parser \p p must be an opening brace, and we read
319 * everything until the matching closing brace.
320 * The resulting column specifications are filled into \p colinfo. This is
321 * in an intermediate form. fix_colalign() makes it suitable for LyX output.
323 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
324 ColInfo const & start)
326 if (p.get_token().cat() != catBegin)
327 cerr << "Wrong syntax for table column alignment.\n"
328 "Expected '{', got '" << p.curr_token().asInput()
331 ColInfo next = start;
332 for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
335 cerr << "t: " << t << " c: '" << t.character() << "'\n";
338 // We cannot handle comments here
339 if (t.cat() == catComment) {
340 if (t.cs().empty()) {
344 cerr << "Ignoring comment: " << t.asInput();
348 switch (t.character()) {
350 // whitespace, ignore.
355 // new column, horizontal aligned
356 next.align = t.character();
357 if (!next.special.empty()) {
359 // handle decimal separator
360 if (next.decimal_point != '\0') {
361 if (!colinfo.empty() && colinfo.back().align == 'r') {
362 colinfo.back().align = 'd';
363 colinfo.back().decimal_point = next.decimal_point;
365 next.decimal_point = '\0';
368 colinfo.push_back(next);
374 // new column, vertical aligned box
375 next.valign = t.character();
376 next.width = p.verbatim_item();
377 if (!next.special.empty()) {
379 // handle decimal separator
380 if (next.decimal_point != '\0') {
381 if (!colinfo.empty() && colinfo.back().align == 'r') {
382 colinfo.back().align = 'd';
383 colinfo.back().decimal_point = next.decimal_point;
385 next.decimal_point = '\0';
388 colinfo.push_back(next);
393 if (colinfo.empty()) {
394 if (next.special.empty())
398 } else if (colinfo.back().special.empty())
399 ++colinfo.back().rightlines;
400 else if (next.special.empty())
403 colinfo.back().special += '|';
406 // text before the next column
407 string const s = trimSpaceAndEol(p.verbatim_item());
408 if (next.special.empty() &&
410 // Maybe this can be converted to a
411 // horizontal alignment setting for
412 // fixed width columns
413 if (s == "\\raggedleft")
415 else if (s == "\\raggedright")
417 else if (s == "\\centering")
420 next.special = ">{" + s + '}';
422 next.special += ">{" + s + '}';
426 // text after the last column
427 string const s = trimSpaceAndEol(p.verbatim_item());
429 // This is not possible in LaTeX.
430 cerr << "Ignoring separator '<{"
431 << s << "}'." << endl;
433 ColInfo & ci = colinfo.back();
435 ci.special += "<{" + s + '}';
440 if (p.next_token().character() != '{')
442 // *{n}{arg} means 'n' columns of type 'arg'
443 string const num = p.verbatim_item();
444 string const arg = p.verbatim_item();
445 size_t const n = convert<unsigned int>(num);
446 if (!arg.empty() && n > 0) {
448 for (size_t i = 0; i < n; ++i)
452 handle_colalign(p2, colinfo, next);
455 cerr << "Ignoring column specification"
456 " '*{" << num << "}{"
457 << arg << "}'." << endl;
462 // text instead of the column spacing
464 // text in addition to the column spacing
465 string const arg = p.verbatim_item();
466 next.special += t.character();
467 next.special += '{' + arg + '}';
468 string const sarg = arg.size() > 2 ? arg.substr(0, arg.size() - 1) : string();
469 if (t.character() == '@' && sarg == "\\extracolsep{0pt}")
470 next.decimal_point = arg.back();
474 // try user defined column types
475 // unknown column types (nargs == -1) are
476 // assumed to consume no arguments
478 next.special += t.character();
480 preamble.getSpecialTableColumnArguments(t.character());
481 for (int i = 0; i < nargs; ++i)
482 next.special += '{' +
483 p.verbatim_item() + '}';
484 colinfo.push_back(next);
491 // Maybe we have some column separators that need to be added to the
494 if (!next.special.empty()) {
495 ColInfo & ci = colinfo.back();
497 ci.special += next.special;
498 next.special.erase();
504 * Move the left and right lines and alignment settings of the column \p ci
505 * to the special field if necessary.
507 void fix_colalign(ColInfo & ci)
509 if (ci.leftlines > 1 || ci.rightlines > 1)
515 * LyX can't handle more than one vertical line at the left or right side
517 * This function moves the left and right lines and alignment settings of all
518 * columns in \p colinfo to the special field if necessary.
520 void fix_colalign(vector<ColInfo> & colinfo)
522 // Try to move extra leftlines to the previous column.
523 // We do this only if both special fields are empty, otherwise we
524 // can't tell wether the result will be the same.
525 for (size_t col = 0; col < colinfo.size(); ++col) {
526 if (colinfo[col].leftlines > 1 &&
527 colinfo[col].special.empty() && col > 0 &&
528 colinfo[col - 1].rightlines == 0 &&
529 colinfo[col - 1].special.empty()) {
530 ++colinfo[col - 1].rightlines;
531 --colinfo[col].leftlines;
534 // Try to move extra rightlines to the next column
535 for (size_t col = 0; col < colinfo.size(); ++col) {
536 if (colinfo[col].rightlines > 1 &&
537 colinfo[col].special.empty() &&
538 col < colinfo.size() - 1 &&
539 colinfo[col + 1].leftlines == 0 &&
540 colinfo[col + 1].special.empty()) {
541 ++colinfo[col + 1].leftlines;
542 --colinfo[col].rightlines;
545 // Move the lines and alignment settings to the special field if
547 for (size_t col = 0; col < colinfo.size(); ++col)
548 fix_colalign(colinfo[col]);
553 * Parse hlines and similar stuff.
554 * \returns wether the token \p t was parsed
556 bool parse_hlines(Parser & p, Token const & t, string & hlines,
557 bool is_long_tabular)
559 LASSERT(t.cat() == catEscape, return false);
561 if (t.cs() == "hline" || t.cs() == "toprule" || t.cs() == "midrule" ||
562 t.cs() == "bottomrule")
563 hlines += '\\' + t.cs();
565 else if (t.cs() == "cline")
566 hlines += "\\cline{" + p.verbatim_item() + '}';
568 else if (t.cs() == "cmidrule") {
569 // We cannot handle the \cmidrule(l){3-4} form
572 bool const hasParentheses(p.getFullArg('(', ')').first);
576 hlines += "\\cmidrule{" + p.verbatim_item() + '}';
579 else if (t.cs() == "addlinespace") {
582 bool const hasArgument(p.getFullArg('{', '}').first);
585 hlines += "\\addlinespace{" + p.verbatim_item() + '}';
587 hlines += "\\addlinespace";
590 else if (is_long_tabular && t.cs() == "newpage")
591 hlines += "\\newpage";
600 /// Position in a row
602 /// At the very beginning, before the first token
604 /// After the first token and before any column token
606 /// After the first column token. Comments and whitespace are only
607 /// treated as tokens in this position
609 /// After the first non-column token at the end
615 * Parse table structure.
616 * We parse tables in a two-pass process: This function extracts the table
617 * structure (rows, columns, hlines etc.), but does not change the cell
618 * content. The cell content is parsed in a second step in handle_tabular().
620 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
621 RowPosition & pos, unsigned flags)
623 // table structure commands such as \hline
626 // comments that occur at places where we can't handle them
630 Token const & t = p.get_token();
633 debugToken(cerr, t, flags);
636 // comments and whitespace in hlines
639 case IN_HLINES_START:
641 if (t.cat() == catComment) {
646 // We can't handle comments here,
647 // store them for later use
648 comments += t.asInput();
650 } else if (t.cat() == catSpace ||
651 t.cat() == catNewline) {
652 // whitespace is irrelevant here, we
653 // need to recognize hline stuff
662 // We need to handle structure stuff first in order to
663 // determine wether we need to output a HLINE separator
664 // before the row or not.
665 if (t.cat() == catEscape) {
666 if (parse_hlines(p, t, hlines, is_long_tabular)) {
669 pos = IN_HLINES_START;
674 case IN_HLINES_START:
681 else if (t.cs() == "tabularnewline" ||
685 cerr << "Warning: Converting TeX "
686 "'\\cr' to LaTeX '\\\\'."
688 // stuff before the line break
689 os << comments << HLINE << hlines << HLINE
691 //cerr << "hlines: " << hlines << endl;
698 else if (is_long_tabular &&
699 (t.cs() == "endhead" ||
700 t.cs() == "endfirsthead" ||
701 t.cs() == "endfoot" ||
702 t.cs() == "endlastfoot")) {
703 hlines += t.asInput();
707 // these commands are implicit line
709 os << comments << HLINE << hlines
716 pos = IN_HLINES_START;
718 case IN_HLINES_START:
725 // We need a HLINE separator if we either have no hline
726 // stuff at all and are just starting a row or if we just
727 // got the first non-hline token.
730 // no hline tokens exist, first token at row start
731 case IN_HLINES_START:
732 // hline tokens exist, first non-hline token at row
734 os << hlines << HLINE << comments;
740 // Oops, there is still cell content or unsupported
741 // booktabs commands after hline stuff. The latter are
742 // moved to the cell, and the first does not work in
743 // LaTeX, so we ignore the hlines.
746 if (support::contains(hlines, "\\hline") ||
747 support::contains(hlines, "\\cline") ||
748 support::contains(hlines, "\\newpage"))
749 cerr << "Ignoring '" << hlines
750 << "' in a cell" << endl;
760 // If we come here we have normal cell content
764 if (t.cat() == catMath) {
765 // we are inside some text mode thingy, so opening new math is allowed
766 Token const & n = p.get_token();
767 if (n.cat() == catMath) {
768 // TeX's $$...$$ syntax for displayed math
770 // This does only work because parse_math outputs TeX
771 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
773 p.get_token(); // skip the second '$' token
775 // simple $...$ stuff
778 // This does only work because parse_math outputs TeX
779 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
784 else if (t.cat() == catSpace
785 || t.cat() == catNewline
786 || t.cat() == catLetter
787 || t.cat() == catSuper
789 || t.cat() == catOther
790 || t.cat() == catActive
791 || t.cat() == catParameter)
794 else if (t.cat() == catBegin) {
796 parse_table(p, os, is_long_tabular, pos,
801 else if (t.cat() == catEnd) {
802 if (flags & FLAG_BRACE_LAST)
804 cerr << "unexpected '}'\n";
807 else if (t.cat() == catAlign) {
812 else if (t.cat() == catComment)
815 else if (t.cs() == "(") {
817 // This does only work because parse_math outputs TeX
818 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
822 else if (t.cs() == "[") {
824 // This does only work because parse_math outputs TeX
825 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
829 else if (t.cs() == "begin") {
830 string const name = p.getArg('{', '}');
831 active_environments.push_back(name);
832 os << "\\begin{" << name << '}';
833 // treat the nested environment as a block, don't
834 // parse &, \\ etc, because they don't belong to our
835 // table if they appear.
836 os << p.ertEnvironment(name);
837 os << "\\end{" << name << '}';
838 active_environments.pop_back();
841 else if (t.cs() == "end") {
842 if (flags & FLAG_END) {
843 // eat environment name
844 string const name = p.getArg('{', '}');
845 if (name != active_environment())
846 p.error("\\end{" + name + "} does not match \\begin{"
847 + active_environment() + "}");
850 p.error("found 'end' unexpectedly");
857 // We can have comments if the last line is incomplete
860 // We can have hline stuff if the last line is incomplete
861 if (!hlines.empty()) {
862 // this does not work in LaTeX, so we ignore it
863 cerr << "Ignoring '" << hlines << "' at end of tabular"
869 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
872 for (size_t col = 0; col < ci.size(); ++col)
873 ci[col].topline = true;
877 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
879 ri.bottomline = true;
880 for (size_t col = 0; col < ci.size(); ++col)
881 ci[col].bottomline = true;
885 } // anonymous namespace
888 void handle_tabular(Parser & p, ostream & os, string const & name,
889 string const & tabularwidth, string const & halign,
892 bool const is_long_tabular(name == "longtable");
893 bool booktabs = false;
894 string tabularvalignment("middle");
895 string posopts = p.getOpt();
896 if (!posopts.empty()) {
897 // FIXME: Convert this to ERT
899 cerr << "horizontal longtable positioning '"
900 << posopts << "' ignored\n";
901 else if (posopts == "[t]")
902 tabularvalignment = "top";
903 else if (posopts == "[b]")
904 tabularvalignment = "bottom";
906 cerr << "vertical tabular positioning '"
907 << posopts << "' ignored\n";
910 vector<ColInfo> colinfo;
912 // handle column formatting
913 handle_colalign(p, colinfo, ColInfo());
914 fix_colalign(colinfo);
916 // first scan of cells
917 // use table mode to keep it minimal-invasive
918 // not exactly what's TeX doing...
919 vector<string> lines;
921 RowPosition rowpos = ROW_START;
922 parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
923 split(ss.str(), lines, LINE);
925 vector< vector<CellInfo> > cellinfo(lines.size());
926 vector<RowInfo> rowinfo(lines.size());
933 //cerr << "// split into rows\n";
934 for (size_t row = 0; row < rowinfo.size();) {
937 cellinfo[row].resize(colinfo.size());
938 bool deletelastrow = false;
941 vector<string> dummy;
942 //cerr << "\n########### LINE: " << lines[row] << "########\n";
943 split(lines[row], dummy, HLINE);
945 // handle horizontal line fragments
946 // we do only expect this for a last line without '\\'
947 if (dummy.size() != 3) {
948 if ((dummy.size() != 1 && dummy.size() != 2) ||
949 row != rowinfo.size() - 1)
950 cerr << "unexpected dummy size: " << dummy.size()
951 << " content: " << lines[row] << "\n";
954 lines[row] = dummy[1];
956 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
957 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
958 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
960 for (int i = 0; i <= 2; i += 2) {
961 //cerr << " reading from line string '" << dummy[i] << "'\n";
964 Token t = p1.get_token();
965 //cerr << "read token: " << t << "\n";
966 if (t.cs() == "hline" || t.cs() == "toprule" ||
967 t.cs() == "midrule" ||
968 t.cs() == "bottomrule") {
969 if (t.cs() != "hline")
972 if (rowinfo[row].topline) {
973 if (row > 0) // extra bottomline above
974 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
976 cerr << "dropping extra "
978 //cerr << "below row: " << row-1 << endl;
980 handle_hline_above(rowinfo[row], cellinfo[row]);
981 //cerr << "above row: " << row << endl;
984 //cerr << "below row: " << row << endl;
985 handle_hline_below(rowinfo[row], cellinfo[row]);
987 } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
988 if (t.cs() == "cmidrule")
990 string arg = p1.verbatim_item();
991 //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
993 split(arg, cols, '-');
995 size_t from = convert<unsigned int>(cols[0]);
997 cerr << "Could not parse "
998 << t.cs() << " start column."
1001 // 1 based index -> 0 based
1003 if (from >= colinfo.size()) {
1004 cerr << t.cs() << " starts at "
1005 "non existing column "
1006 << (from + 1) << endl;
1007 from = colinfo.size() - 1;
1009 size_t to = convert<unsigned int>(cols[1]);
1011 cerr << "Could not parse "
1012 << t.cs() << " end column."
1015 // 1 based index -> 0 based
1017 if (to >= colinfo.size()) {
1018 cerr << t.cs() << " ends at "
1019 "non existing column "
1020 << (to + 1) << endl;
1021 to = colinfo.size() - 1;
1023 for (size_t col = from; col <= to; ++col) {
1024 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
1026 rowinfo[row].topline = true;
1027 cellinfo[row][col].topline = true;
1029 rowinfo[row].bottomline = true;
1030 cellinfo[row][col].bottomline = true;
1033 } else if (t.cs() == "addlinespace") {
1035 string const opt = p.next_token().cat() == catBegin ?
1036 p.verbatim_item() : string();
1039 rowinfo[row].top_space = "default";
1041 rowinfo[row].top_space = translate_len(opt);
1042 } else if (rowinfo[row].bottomline) {
1044 rowinfo[row].bottom_space = "default";
1046 rowinfo[row].bottom_space = translate_len(opt);
1049 rowinfo[row].interline_space = "default";
1051 rowinfo[row].interline_space = translate_len(opt);
1053 } else if (t.cs() == "endhead") {
1055 endhead.empty = true;
1057 rowinfo[row].type = LT_HEAD;
1058 for (int r = row - 1; r >= 0; --r) {
1059 if (rowinfo[r].type != LT_NORMAL)
1061 rowinfo[r].type = LT_HEAD;
1062 endhead.empty = false;
1065 } else if (t.cs() == "endfirsthead") {
1067 endfirsthead.empty = true;
1069 rowinfo[row].type = LT_FIRSTHEAD;
1070 for (int r = row - 1; r >= 0; --r) {
1071 if (rowinfo[r].type != LT_NORMAL)
1073 rowinfo[r].type = LT_FIRSTHEAD;
1074 endfirsthead.empty = false;
1076 endfirsthead.set = true;
1077 } else if (t.cs() == "endfoot") {
1079 endfoot.empty = true;
1081 rowinfo[row].type = LT_FOOT;
1082 for (int r = row - 1; r >= 0; --r) {
1083 if (rowinfo[r].type != LT_NORMAL)
1085 rowinfo[r].type = LT_FOOT;
1086 endfoot.empty = false;
1089 } else if (t.cs() == "endlastfoot") {
1091 endlastfoot.empty = true;
1093 rowinfo[row].type = LT_LASTFOOT;
1094 for (int r = row - 1; r >= 0; --r) {
1095 if (rowinfo[r].type != LT_NORMAL)
1097 rowinfo[r].type = LT_LASTFOOT;
1098 endlastfoot.empty = false;
1100 endlastfoot.set = true;
1101 } else if (t.cs() == "newpage") {
1104 rowinfo[row - 1].newpage = true;
1106 // This does not work in LaTeX
1112 rowinfo[row].newpage = true;
1114 cerr << "unexpected line token: " << t << endl;
1119 // LyX ends headers and footers always with \tabularnewline.
1120 // This causes one additional row in the output.
1121 // If the last row of a header/footer is empty, we can work
1122 // around that by removing it.
1124 RowInfo test = rowinfo[row-1];
1125 test.type = LT_NORMAL;
1126 if (lines[row-1].empty() && !test.special()) {
1127 switch (rowinfo[row-1].type) {
1129 if (rowinfo[row].type != LT_FIRSTHEAD &&
1130 rowinfo[row-2].type == LT_FIRSTHEAD)
1131 deletelastrow = true;
1134 if (rowinfo[row].type != LT_HEAD &&
1135 rowinfo[row-2].type == LT_HEAD)
1136 deletelastrow = true;
1139 if (rowinfo[row].type != LT_FOOT &&
1140 rowinfo[row-2].type == LT_FOOT)
1141 deletelastrow = true;
1144 if (rowinfo[row].type != LT_LASTFOOT &&
1145 rowinfo[row-2].type == LT_LASTFOOT)
1146 deletelastrow = true;
1154 if (deletelastrow) {
1155 lines.erase(lines.begin() + (row - 1));
1156 rowinfo.erase(rowinfo.begin() + (row - 1));
1157 cellinfo.erase(cellinfo.begin() + (row - 1));
1162 vector<string> cells;
1163 split(lines[row], cells, TAB);
1164 for (size_t col = 0, cell = 0; cell < cells.size();
1166 //cerr << "cell content: '" << cells[cell] << "'\n";
1167 if (col >= colinfo.size()) {
1168 // This does not work in LaTeX
1169 cerr << "Ignoring extra cell '"
1170 << cells[cell] << "'." << endl;
1173 string cellcont = cells[cell];
1174 // For decimal cells, ass the content of the second one to the first one
1176 if (colinfo[col].decimal_point != '\0' && colinfo[col].align == 'd' && cell < cells.size() - 1)
1177 cellcont += colinfo[col].decimal_point + cells[cell + 1];
1180 //cells[cell] << "'\n";
1181 if (p.next_token().cs() == "multirow") {
1182 // We do not support the vpos arg yet.
1184 string const vpos = p.getArg('[', ']');
1185 p.skip_spaces(true);
1186 cerr << "Ignoring multirow's vpos arg '"
1187 << vpos << "'!" << endl;
1191 size_t const ncells =
1192 convert<unsigned int>(p.verbatim_item());
1193 // We do not support the bigstrut arg yet.
1195 string const bs = p.getArg('[', ']');
1196 p.skip_spaces(true);
1197 cerr << "Ignoring multirow's bigstrut arg '"
1198 << bs << "'!" << endl;
1200 // the width argument
1201 string const width = p.getArg('{', '}');
1205 vmove = p.getArg('[', ']');
1206 p.skip_spaces(true);
1210 colinfo[col].width = width;
1212 cellinfo[row][col].mroffset = vmove;
1213 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTIROW;
1214 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1215 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1216 cellinfo[row][col].mrxnum = ncells - 1;
1219 parse_text_in_inset(p, os2, FLAG_ITEM, false, context);
1220 if (!cellinfo[row][col].content.empty()) {
1221 // This may or may not work in LaTeX,
1222 // but it does not work in LyX.
1223 // FIXME: Handle it correctly!
1224 cerr << "Moving cell content '"
1226 << "' into a multirow cell. "
1227 "This will probably not work."
1230 cellinfo[row][col].content += os2.str();
1231 } else if (p.next_token().cs() == "multicolumn") {
1234 size_t const ncells =
1235 convert<unsigned int>(p.verbatim_item());
1237 // special cell properties alignment
1239 handle_colalign(p, t, ColInfo());
1240 p.skip_spaces(true);
1241 ColInfo & ci = t.front();
1243 // The logic of LyX for multicolumn vertical
1244 // lines is too complicated to reproduce it
1245 // here (see LyXTabular::TeXCellPreamble()).
1246 // Therefore we simply put everything in the
1250 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1251 cellinfo[row][col].align = ci.align;
1252 cellinfo[row][col].special = ci.special;
1253 cellinfo[row][col].leftlines = ci.leftlines;
1254 cellinfo[row][col].rightlines = ci.rightlines;
1256 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1257 if (!cellinfo[row][col].content.empty()) {
1258 // This may or may not work in LaTeX,
1259 // but it does not work in LyX.
1260 // FIXME: Handle it correctly!
1261 cerr << "Moving cell content '"
1263 << "' into a multicolumn cell. "
1264 "This will probably not work."
1267 cellinfo[row][col].content += os.str();
1269 // add dummy cells for multicol
1270 for (size_t i = 0; i < ncells - 1; ++i) {
1272 if (col >= colinfo.size()) {
1273 cerr << "The cell '"
1276 << convert<string>(ncells)
1277 << " columns while the table has only "
1278 << convert<string>(colinfo.size())
1280 << " Therefore the surplus columns will be ignored."
1284 cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1285 cellinfo[row][col].align = 'c';
1288 } else if (col == 0 && colinfo.size() > 1 &&
1290 p.next_token().cs() == "caption") {
1291 // longtable caption support in LyX is a hack:
1292 // Captions require a row of their own with
1293 // the caption flag set to true, having only
1294 // one multicolumn cell. The contents of that
1295 // cell must contain exactly one caption inset
1296 // and nothing else.
1297 // Fortunately, the caption flag is only needed
1298 // for tables with more than one column.
1299 rowinfo[row].caption = true;
1300 for (size_t c = 1; c < cells.size(); ++c) {
1301 if (!cells[c].empty()) {
1302 cerr << "Moving cell content '"
1304 << "' into the caption cell. "
1305 "This will probably not work."
1307 cells[0] += cells[c];
1311 cellinfo[row][col].align = colinfo[col].align;
1312 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1314 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1315 cellinfo[row][col].content += os.str();
1316 // add dummy multicolumn cells
1317 for (size_t c = 1; c < colinfo.size(); ++c)
1318 cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
1322 if (p.next_token().cs() == "begin") {
1325 string const env = p.getArg('{', '}');
1326 if (env == "sideways" || env == "turn") {
1327 string angle = "90";
1328 if (env == "turn") {
1330 angle = p.getArg('{', '}');
1332 active_environments.push_back(env);
1333 p.ertEnvironment(env);
1334 active_environments.pop_back();
1336 if (!p.good() && support::isStrInt(angle))
1337 rotate = convert<int>(angle);
1341 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1342 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1343 cellinfo[row][col].align = colinfo[col].align;
1346 cellinfo[row][col].rotate = rotate;
1348 active_environments.push_back(p.getArg('{', '}'));
1351 parse_text_in_inset(p, os, FLAG_END, false, context);
1352 active_environments.pop_back();
1353 preamble.registerAutomaticallyLoadedPackage("rotating");
1355 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1357 cellinfo[row][col].content += os.str();
1361 //cerr << "// handle almost empty last row what we have\n";
1362 // handle almost empty last row
1363 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1364 //cerr << "remove empty last line\n";
1365 if (rowinfo[row].topline)
1366 rowinfo[row - 1].bottomline = true;
1367 for (size_t col = 0; col < colinfo.size(); ++col)
1368 if (cellinfo[row][col].topline)
1369 cellinfo[row - 1][col].bottomline = true;
1376 // Now we have the table structure and content in rowinfo, colinfo
1378 // Unfortunately LyX has some limitations that we need to work around.
1381 for (size_t row = 0; row < rowinfo.size(); ++row) {
1382 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1383 // Convert cells with special content to multicolumn cells
1384 // (LyX ignores the special field for non-multicolumn cells).
1385 if (cellinfo[row][col].multi == CELL_NORMAL &&
1386 !cellinfo[row][col].special.empty())
1387 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1388 // Add multirow dummy cells
1389 if (row > 1 && (cellinfo[row - 1][col].multi == CELL_PART_OF_MULTIROW
1390 || cellinfo[row - 1][col].multi == CELL_BEGIN_OF_MULTIROW)
1391 && cellinfo[row - 1][col].mrxnum > 0) {
1392 // add dummy cells for multirow
1393 cellinfo[row][col].multi = CELL_PART_OF_MULTIROW;
1394 cellinfo[row][col].align = 'c';
1395 cellinfo[row][col].mrxnum = cellinfo[row - 1][col].mrxnum - 1;
1400 // Distribute lines from rows/columns to cells
1401 // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py.
1402 // Each standard cell inherits the settings of the corresponding
1403 // rowinfo/colinfo. This works because all cells with individual
1404 // settings were converted to multicolumn cells above.
1405 // Each multicolumn cell inherits the settings of the rowinfo/colinfo
1406 // corresponding to the first column of the multicolumn cell (default
1407 // of the multicol package). This works because the special field
1408 // overrides the line fields.
1409 for (size_t row = 0; row < rowinfo.size(); ++row) {
1410 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1411 if (cellinfo[row][col].multi == CELL_NORMAL) {
1412 cellinfo[row][col].topline = rowinfo[row].topline;
1413 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1414 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1415 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1416 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) {
1418 while (s < cellinfo[row].size() &&
1419 cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN)
1421 if (s < cellinfo[row].size() &&
1422 cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN)
1423 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1424 if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
1425 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1426 } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTIROW) {
1428 while (s < rowinfo.size() &&
1429 cellinfo[s][col].multi == CELL_PART_OF_MULTIROW)
1431 if (s < cellinfo[row].size() &&
1432 cellinfo[s][col].multi != CELL_BEGIN_OF_MULTIROW)
1433 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1434 if (row > 0 && cellinfo[row - 1][col].multi == CELL_NORMAL)
1435 cellinfo[row][col].topline = rowinfo[row].topline;
1441 preamble.registerAutomaticallyLoadedPackage("booktabs");
1442 if (is_long_tabular)
1443 preamble.registerAutomaticallyLoadedPackage("longtable");
1445 //cerr << "// output what we have\n";
1446 // output what we have
1447 string const rotate = "0";
1448 size_type cols = colinfo.size();
1449 for (size_t col = 0; col < colinfo.size(); ++col) {
1450 if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
1453 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1454 << "\" columns=\"" << cols << "\">\n";
1456 << write_attribute("rotate", rotate)
1457 << write_attribute("booktabs", booktabs)
1458 << write_attribute("islongtable", is_long_tabular);
1459 if (is_long_tabular) {
1460 os << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1461 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1462 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1463 << write_attribute("headTopDL", endhead.topDL)
1464 << write_attribute("headBottomDL", endhead.bottomDL)
1465 << write_attribute("footTopDL", endfoot.topDL)
1466 << write_attribute("footBottomDL", endfoot.bottomDL)
1467 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1468 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1469 << write_attribute("lastFootEmpty", endlastfoot.empty);
1470 if (!halign.empty())
1471 os << write_attribute("longtabularalignment", halign);
1473 os << write_attribute("tabularvalignment", tabularvalignment)
1474 << write_attribute("tabularwidth", tabularwidth);
1477 //cerr << "// after header\n";
1478 for (size_t col = 0; col < colinfo.size(); ++col) {
1479 if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
1481 os << "<column alignment=\""
1482 << verbose_align(colinfo[col].align) << "\"";
1483 if (colinfo[col].decimal_point != '\0')
1484 os << " decimal_point=\"" << colinfo[col].decimal_point << "\"";
1485 os << " valignment=\""
1486 << verbose_valign(colinfo[col].valign) << "\""
1487 << write_attribute("width", translate_len(colinfo[col].width))
1488 << write_attribute("special", colinfo[col].special)
1491 //cerr << "// after cols\n";
1493 for (size_t row = 0; row < rowinfo.size(); ++row) {
1495 << write_attribute("topspace", rowinfo[row].top_space)
1496 << write_attribute("bottomspace", rowinfo[row].bottom_space)
1497 << write_attribute("interlinespace", rowinfo[row].interline_space)
1498 << write_attribute("endhead",
1499 rowinfo[row].type == LT_HEAD)
1500 << write_attribute("endfirsthead",
1501 rowinfo[row].type == LT_FIRSTHEAD)
1502 << write_attribute("endfoot",
1503 rowinfo[row].type == LT_FOOT)
1504 << write_attribute("endlastfoot",
1505 rowinfo[row].type == LT_LASTFOOT)
1506 << write_attribute("newpage", rowinfo[row].newpage)
1507 << write_attribute("caption", rowinfo[row].caption)
1509 for (size_t col = 0; col < colinfo.size(); ++col) {
1510 CellInfo const & cell = cellinfo[row][col];
1511 if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
1512 // These are the second columns in a salign pair. Skip.
1515 if ((cell.multi == CELL_BEGIN_OF_MULTICOLUMN
1516 || cell.multi == CELL_PART_OF_MULTICOLUMN)
1517 && colinfo[col].align != 'd')
1518 os << " multicolumn=\"" << cell.multi << "\"";
1519 if (cell.multi == CELL_BEGIN_OF_MULTIROW
1520 || cell.multi == CELL_PART_OF_MULTIROW)
1521 os << " multirow=\"" << cell.multi << "\"";
1522 os << " alignment=\"" << verbose_align(cell.align)
1524 << " valignment=\"" << verbose_valign(cell.valign)
1526 << write_attribute("topline", cell.topline)
1527 << write_attribute("bottomline", cell.bottomline)
1528 << write_attribute("leftline", cell.leftlines > 0)
1529 << write_attribute("rightline", cell.rightlines > 0)
1530 << write_attribute("rotate", cell.rotate)
1531 << write_attribute("mroffset", cell.mroffset);
1532 //cerr << "\nrow: " << row << " col: " << col;
1534 // cerr << " topline=\"true\"";
1535 //if (cell.bottomline)
1536 // cerr << " bottomline=\"true\"";
1537 os << " usebox=\"none\""
1538 << write_attribute("width", translate_len(cell.width));
1539 if (cell.multi != CELL_NORMAL)
1540 os << write_attribute("special", cell.special);
1542 << "\n\\begin_inset Text\n"
1544 << "\n\\end_inset\n"
1550 os << "</lyxtabular>\n";