X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ftex2lyx%2Ftable.cpp;h=bd7225db0f25dcf20414cf2dd9bf7095498ea213;hb=361d4d43e31c749d5e087192a1bd2e58f13046b9;hp=d23db7088a1bd700bc85be7a7ca5db4b4c671c59;hpb=337c6d157754e2f613ddbc3bca072ca658282edf;p=lyx.git diff --git a/src/tex2lyx/table.cpp b/src/tex2lyx/table.cpp index d23db7088a..bd7225db0f 100644 --- a/src/tex2lyx/table.cpp +++ b/src/tex2lyx/table.cpp @@ -16,10 +16,14 @@ #include "tex2lyx.h" +#include "Context.h" +#include "Preamble.h" + #include "support/lassert.h" #include "support/convert.h" #include "support/lstrings.h" +#include #include #include #include @@ -29,15 +33,13 @@ using namespace std; namespace lyx { -// filled in preamble.cpp -map special_columns; - namespace { class ColInfo { public: - ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0) {} + ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0), + varwidth(false), decimal_point('\0'), vcolumn(false) {} /// column alignment char align; /// vertical alignment @@ -50,6 +52,12 @@ public: int rightlines; /// number of lines on the left int leftlines; + /// varwidth column + bool varwidth; + /// decimal separator + char decimal_point; + /// V column type + bool vcolumn; }; @@ -72,27 +80,47 @@ enum LTRowType class RowInfo { public: RowInfo() : topline(false), bottomline(false), type(LT_NORMAL), - newpage(false) {} + caption(false), newpage(false) {} + /// Does this row have any special setting? + bool special() const + { + return topline || bottomline || !top_space.empty() || + !bottom_space.empty() || !interline_space.empty() || + type != LT_NORMAL || caption || newpage; + } /// horizontal line above bool topline; /// horizontal line below bool bottomline; + /// Extra space between the top line and this row + string top_space; + /// Extra space between this row and the bottom line + string bottom_space; + /// Extra space between the bottom line and the next top line + string interline_space; /// These are for longtabulars only /// row type (head, foot, firsthead etc.) LTRowType type; + /// row for a caption + bool caption; /// row for a newpage bool newpage; }; -enum Multicolumn { +/// the numeric values are part of the file format! +enum Multi { /// A normal cell CELL_NORMAL = 0, /// A multicolumn cell. The number of columns is 1 + number /// of CELL_PART_OF_MULTICOLUMN cells that follow directly CELL_BEGIN_OF_MULTICOLUMN, /// This is a dummy cell (part of a multicolumn cell) - CELL_PART_OF_MULTICOLUMN + CELL_PART_OF_MULTICOLUMN, + /// + CELL_BEGIN_OF_MULTIROW, + /// + CELL_PART_OF_MULTIROW }; @@ -100,11 +128,13 @@ class CellInfo { public: CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'), leftlines(0), rightlines(0), topline(false), - bottomline(false), rotate(false) {} + bottomline(false), topline_ltrim(false), + topline_rtrim(false), bottomline_ltrim(false), + bottomline_rtrim(false), rotate(0), mrxnum(0) {} /// cell content string content; /// multicolumn flag - Multicolumn multi; + Multi multi; /// cell alignment char align; /// vertical cell alignment @@ -117,12 +147,41 @@ public: bool topline; /// do we have a line below? bool bottomline; - /// is the cell rotated? - bool rotate; + /// Left trimming of top line + bool topline_ltrim; + /// Right trimming of top line + bool topline_rtrim; + /// Left trimming of bottom line + bool bottomline_ltrim; + /// Right trimming of top line + bool bottomline_rtrim; + /// how is the cell rotated? + int rotate; /// width for multicolumn cells string width; /// special formatting for multicolumn cells string special; + /// multirow offset + string mroffset; + /// number of further multirows + int mrxnum; +}; + + +class ltType { +public: + // constructor + ltType() : set(false), topDL(false), bottomDL(false), empty(false) {} + // we have this header type (is set in the getLT... functions) + bool set; + // double borders on top + bool topDL; + // double borders on bottom + bool bottomDL; + // used for FirstHeader & LastFooter and if this is true + // all the rows marked as FirstHeader or LastFooter are + // ignored in the output and it is set to be empty! + bool empty; }; @@ -136,6 +195,8 @@ inline char const * verbose_align(char c) return "right"; case 'l': return "left"; + case 'd': + return "decimal"; default: return "none"; } @@ -174,6 +235,14 @@ string const write_attribute(string const & name, string const & s) } +string const write_attribute(string const & name, int const & i) +{ + // we write only true attribute values so we remove a bit of the + // file format bloat for tabulars. + return i ? write_attribute(name, convert(i)) : string(); +} + + /*! rather brutish way to code table structure in a string: \verbatim @@ -203,7 +272,8 @@ char const HLINE = '\004'; /*! * Move the information in leftlines, rightlines, align and valign to the * special field. This is necessary if the special field is not empty, - * because LyX ignores leftlines, rightlines, align and valign in this case. + * because LyX ignores leftlines > 1, rightlines > 1, align and valign in + * this case. */ void ci2special(ColInfo & ci) { @@ -214,19 +284,37 @@ void ci2special(ColInfo & ci) // this case. return; + if (ci.decimal_point != '\0') { + // we only support decimal point natively + // with 'l' alignment in or 'n' alignment + // with width in second row + if (ci.align != 'l' && ci.align != 'n') { + ci.decimal_point = '\0'; + return; + } else + ci.special.clear(); + } + if (!ci.width.empty()) { + string arraybackslash; + if (ci.varwidth) + arraybackslash = "\\arraybackslash"; switch (ci.align) { case 'l': - ci.special += ">{\\raggedright}"; + ci.special += ">{\\raggedright" + arraybackslash + "}"; break; case 'r': - ci.special += ">{\\raggedleft}"; + ci.special += ">{\\raggedleft" + arraybackslash + "}"; break; case 'c': - ci.special += ">{\\centering}"; + ci.special += ">{\\centering" + arraybackslash + "}"; break; } - if (ci.valign == 'n') + if (ci.vcolumn) + ci.special += 'V'; + else if (ci.varwidth) + ci.special += 'X'; + else if (ci.valign == 'n') ci.special += 'p'; else ci.special += ci.valign; @@ -235,12 +323,13 @@ void ci2special(ColInfo & ci) } else ci.special += ci.align; - for (int i = 0; i < ci.leftlines; ++i) + // LyX can only have one left and one right line. + for (int i = 1; i < ci.leftlines; ++i) ci.special.insert(0, "|"); - for (int i = 0; i < ci.rightlines; ++i) + for (int i = 1; i < ci.rightlines; ++i) ci.special += '|'; - ci.leftlines = 0; - ci.rightlines = 0; + ci.leftlines = min(ci.leftlines, 1); + ci.rightlines = min(ci.rightlines, 1); ci.align = 'n'; ci.valign = 'n'; } @@ -279,24 +368,68 @@ void handle_colalign(Parser & p, vector & colinfo, } switch (t.character()) { + case ' ': + // whitespace, ignore. + break; case 'c': case 'l': case 'r': // new column, horizontal aligned next.align = t.character(); + if (!next.special.empty()) { + ci2special(next); + // handle decimal separator + if (next.decimal_point != '\0') { + if (!colinfo.empty() && colinfo.back().align == 'r') { + colinfo.back().align = 'd'; + colinfo.back().decimal_point = next.decimal_point; + } else + next.decimal_point = '\0'; + } + } + colinfo.push_back(next); + next = ColInfo(); + break; + case 'X': + // varwidth column + next.varwidth = true; if (!next.special.empty()) ci2special(next); colinfo.push_back(next); next = ColInfo(); break; + case 'V': { + // V column type (varwidth package) + string const s = trimSpaceAndEol(p.verbatim_item()); + // V{\linewidth} is treated as a normal column + // (which allows for line breaks). The V type is + // automatically set by LyX in this case + if (s != "\\linewidth" || !next.special.empty()) { + next.vcolumn = true; + next.width = s; + ci2special(next); + } + colinfo.push_back(next); + next = ColInfo(); + break; + } case 'p': case 'b': case 'm': // new column, vertical aligned box next.valign = t.character(); next.width = p.verbatim_item(); - if (!next.special.empty()) + if (!next.special.empty()) { ci2special(next); + // handle decimal separator + if (next.decimal_point != '\0') { + if (!colinfo.empty() && colinfo.back().align == 'r') { + colinfo.back().align = 'd'; + colinfo.back().decimal_point = next.decimal_point; + } else + next.decimal_point = '\0'; + } + } colinfo.push_back(next); next = ColInfo(); break; @@ -309,24 +442,24 @@ void handle_colalign(Parser & p, vector & colinfo, next.special += '|'; } else if (colinfo.back().special.empty()) ++colinfo.back().rightlines; - else if (next.special.empty()) + else if (next.special.empty() && p.next_token().cat() != catEnd) ++next.leftlines; else colinfo.back().special += '|'; break; case '>': { // text before the next column - string const s = trim(p.verbatim_item()); + string const s = trimSpaceAndEol(p.verbatim_item()); if (next.special.empty() && next.align == 'n') { // Maybe this can be converted to a // horizontal alignment setting for // fixed width columns - if (s == "\\raggedleft") + if (s == "\\raggedleft" || s == "\\raggedleft\\arraybackslash") next.align = 'r'; - else if (s == "\\raggedright") + else if (s == "\\raggedright" || s == "\\raggedright\\arraybackslash") next.align = 'l'; - else if (s == "\\centering") + else if (s == "\\centering" || s == "\\centering\\arraybackslash") next.align = 'c'; else next.special = ">{" + s + '}'; @@ -336,7 +469,7 @@ void handle_colalign(Parser & p, vector & colinfo, } case '<': { // text after the last column - string const s = trim(p.verbatim_item()); + string const s = trimSpaceAndEol(p.verbatim_item()); if (colinfo.empty()) // This is not possible in LaTeX. cerr << "Ignoring separator '<{" @@ -349,6 +482,8 @@ void handle_colalign(Parser & p, vector & colinfo, break; } case '*': { + if (p.next_token().character() != '{') + continue; // *{n}{arg} means 'n' columns of type 'arg' string const num = p.verbatim_item(); string const arg = p.verbatim_item(); @@ -370,30 +505,32 @@ void handle_colalign(Parser & p, vector & colinfo, } case '@': // text instead of the column spacing - case '!': + case '!': { // text in addition to the column spacing + string const arg = p.verbatim_item(); next.special += t.character(); - next.special += '{' + p.verbatim_item() + '}'; + next.special += '{' + arg + '}'; + string const sarg = arg.size() > 2 ? arg.substr(0, arg.size() - 1) : string(); + if (t.character() == '@' && sarg == "\\extracolsep{0pt}") + next.decimal_point = arg.back(); break; - default: + } + default: { // try user defined column types - if (special_columns.find(t.character()) != - special_columns.end()) { - ci2special(next); - next.special += t.character(); - int const nargs = - special_columns[t.character()]; - for (int i = 0; i < nargs; ++i) - next.special += '{' + - p.verbatim_item() + - '}'; - colinfo.push_back(next); - next = ColInfo(); - } else - cerr << "Ignoring column specification" - " '" << t << "'." << endl; + // unknown column types (nargs == -1) are + // assumed to consume no arguments + ci2special(next); + next.special += t.character(); + int const nargs = + preamble.getSpecialTableColumnArguments(t.character()); + for (int i = 0; i < nargs; ++i) + next.special += '{' + + p.verbatim_item() + '}'; + colinfo.push_back(next); + next = ColInfo(); break; } + } } // Maybe we have some column separators that need to be added to the @@ -429,7 +566,7 @@ void fix_colalign(vector & colinfo) { // Try to move extra leftlines to the previous column. // We do this only if both special fields are empty, otherwise we - // can't tell wether the result will be the same. + // can't tell whether the result will be the same. for (size_t col = 0; col < colinfo.size(); ++col) { if (colinfo[col].leftlines > 1 && colinfo[col].special.empty() && col > 0 && @@ -459,19 +596,49 @@ void fix_colalign(vector & colinfo) /*! * Parse hlines and similar stuff. - * \returns wether the token \p t was parsed + * \returns whether the token \p t was parsed */ bool parse_hlines(Parser & p, Token const & t, string & hlines, bool is_long_tabular) { LASSERT(t.cat() == catEscape, return false); - if (t.cs() == "hline") - hlines += "\\hline"; + if (t.cs() == "hline" || t.cs() == "toprule" || t.cs() == "midrule" || + t.cs() == "bottomrule") + hlines += '\\' + t.cs(); else if (t.cs() == "cline") hlines += "\\cline{" + p.verbatim_item() + '}'; + else if (t.cs() == "cmidrule") { + p.pushPosition(); + p.skip_spaces(true); + // We do not support the optional height argument + if (p.hasOpt()) + return false; + // We support the \cmidrule(l){3-4} form but + // not the trim length parameters (l{}r{}) + string const trim = p.getFullParentheseArg(); + string const range = p.verbatim_item(); + if (!trim.empty()) { + if (support::contains(trim, "{")) + return false; + hlines += "\\cmidrule" + trim + "{" + range + "}"; + } else + hlines += "\\cmidrule{" + range + '}'; + } + + else if (t.cs() == "addlinespace") { + p.pushPosition(); + p.skip_spaces(true); + bool const hasArgument(p.getFullArg('{', '}').first); + p.popPosition(); + if (hasArgument) + hlines += "\\addlinespace{" + p.verbatim_item() + '}'; + else + hlines += "\\addlinespace"; + } + else if (is_long_tabular && t.cs() == "newpage") hlines += "\\newpage"; @@ -515,7 +682,7 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular, Token const & t = p.get_token(); #ifdef FILEDEBUG - cerr << "t: " << t << " flags: " << flags << "\n"; + debugToken(cerr, t, flags); #endif // comments and whitespace in hlines @@ -545,7 +712,7 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular, } // We need to handle structure stuff first in order to - // determine wether we need to output a HLINE separator + // determine whether we need to output a HLINE separator // before the row or not. if (t.cat() == catEscape) { if (parse_hlines(p, t, hlines, is_long_tabular)) { @@ -605,7 +772,6 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular, } continue; } - } // We need a HLINE separator if we either have no hline @@ -623,14 +789,20 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular, pos = IN_COLUMNS; break; case IN_HLINES_END: - // Oops, there is still cell content after hline - // stuff. This does not work in LaTeX, so we ignore - // the hlines. - cerr << "Ignoring '" << hlines << "' in a cell" - << endl; + // Oops, there is still cell content or unsupported + // booktabs commands after hline stuff. The latter are + // moved to the cell, and the first does not work in + // LaTeX, so we ignore the hlines. os << comments; - hlines.erase(); comments.erase(); + if (support::contains(hlines, "\\hline") || + support::contains(hlines, "\\cline") || + support::contains(hlines, "\\newpage")) + cerr << "Ignoring '" << hlines + << "' in a cell" << endl; + else + os << hlines; + hlines.erase(); pos = IN_COLUMNS; break; case IN_COLUMNS: @@ -661,13 +833,13 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular, } } - else if (t.cat() == catSpace + else if (t.cat() == catSpace || t.cat() == catNewline - || t.cat() == catLetter - || t.cat() == catSuper - || t.cat() == catSub - || t.cat() == catOther - || t.cat() == catActive + || t.cat() == catLetter + || t.cat() == catSuper + || t.cat() == catSub + || t.cat() == catOther + || t.cat() == catActive || t.cat() == catParameter) os << t.cs(); @@ -713,7 +885,7 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular, // treat the nested environment as a block, don't // parse &, \\ etc, because they don't belong to our // table if they appear. - os << p.verbatimEnvironment(name); + os << p.ertEnvironment(name); os << "\\end{" << name << '}'; active_environments.pop_back(); } @@ -765,17 +937,22 @@ void handle_hline_below(RowInfo & ri, vector & ci) } // anonymous namespace -void handle_tabular(Parser & p, ostream & os, bool is_long_tabular, +void handle_tabular(Parser & p, ostream & os, string const & name, + string const & tabularwidth, string const & halign, Context & context) { + bool const is_long_tabular(name == "longtable" || name == "xltabular"); + bool booktabs = false; + string tabularvalignment("middle"); string posopts = p.getOpt(); if (!posopts.empty()) { - // FIXME: Convert this to ERT - if (is_long_tabular) - cerr << "horizontal longtable"; + if (posopts == "[t]") + tabularvalignment = "top"; + else if (posopts == "[b]") + tabularvalignment = "bottom"; else - cerr << "vertical tabular"; - cerr << " positioning '" << posopts << "' ignored\n"; + cerr << "vertical tabular positioning '" + << posopts << "' ignored\n"; } vector colinfo; @@ -795,13 +972,18 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular, vector< vector > cellinfo(lines.size()); vector rowinfo(lines.size()); + ltType endfirsthead; + ltType endhead; + ltType endfoot; + ltType endlastfoot; // split into rows //cerr << "// split into rows\n"; - for (size_t row = 0; row < rowinfo.size(); ++row) { + for (size_t row = 0; row < rowinfo.size();) { // init row cellinfo[row].resize(colinfo.size()); + bool deletelastrow = false; // split row vector dummy; @@ -825,17 +1007,22 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular, for (int i = 0; i <= 2; i += 2) { //cerr << " reading from line string '" << dummy[i] << "'\n"; - Parser p1(dummy[i]); + Parser p1(dummy[size_type(i)]); while (p1.good()) { Token t = p1.get_token(); //cerr << "read token: " << t << "\n"; - if (t.cs() == "hline") { + if (t.cs() == "hline" || t.cs() == "toprule" || + t.cs() == "midrule" || + t.cs() == "bottomrule") { + if (t.cs() != "hline") + booktabs = true; if (i == 0) { if (rowinfo[row].topline) { if (row > 0) // extra bottomline above handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]); else - cerr << "dropping extra hline\n"; + cerr << "dropping extra " + << t.cs() << '\n'; //cerr << "below row: " << row-1 << endl; } else { handle_hline_above(rowinfo[row], cellinfo[row]); @@ -845,37 +1032,42 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular, //cerr << "below row: " << row << endl; handle_hline_below(rowinfo[row], cellinfo[row]); } - } else if (t.cs() == "cline") { + } else if (t.cs() == "cline" || t.cs() == "cmidrule") { + string trim; + if (t.cs() == "cmidrule") { + booktabs = true; + trim = p1.getFullParentheseArg(); + } string arg = p1.verbatim_item(); - //cerr << "read cline arg: '" << arg << "'\n"; - vector t; - split(arg, t, '-'); - t.resize(2); - size_t from = convert(t[0]); + //cerr << "read " << t.cs() << " arg: '" << arg << "', trim: '" << trim << "'\n"; + vector cols; + split(arg, cols, '-'); + cols.resize(2); + size_t from = convert(cols[0]); if (from == 0) cerr << "Could not parse " - "cline start column." + << t.cs() << " start column." << endl; else // 1 based index -> 0 based --from; if (from >= colinfo.size()) { - cerr << "cline starts at non " - "existing column " + cerr << t.cs() << " starts at " + "non existing column " << (from + 1) << endl; from = colinfo.size() - 1; } - size_t to = convert(t[1]); + size_t to = convert(cols[1]); if (to == 0) cerr << "Could not parse " - "cline end column." + << t.cs() << " end column." << endl; else // 1 based index -> 0 based --to; if (to >= colinfo.size()) { - cerr << "cline ends at non " - "existing column " + cerr << t.cs() << " ends at " + "non existing column " << (to + 1) << endl; to = colinfo.size() - 1; } @@ -884,43 +1076,95 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular, if (i == 0) { rowinfo[row].topline = true; cellinfo[row][col].topline = true; + if (support::contains(trim, 'l') && col == from) { + //rowinfo[row].topline_ltrim = true; + cellinfo[row][col].topline_ltrim = true; + } + else if (support::contains(trim, 'r') && col == to) { + //rowinfo[row].topline_rtrim = true; + cellinfo[row][col].topline_rtrim = true; + } } else { rowinfo[row].bottomline = true; cellinfo[row][col].bottomline = true; + if (support::contains(trim, 'l') && col == from) { + //rowinfo[row].bottomline_ltrim = true; + cellinfo[row][col].bottomline_ltrim = true; + } + else if (support::contains(trim, 'r') && col == to) { + //rowinfo[row].bottomline_rtrim = true; + cellinfo[row][col].bottomline_rtrim = true; + } } } + } else if (t.cs() == "addlinespace") { + booktabs = true; + string const opt = p.next_token().cat() == catBegin ? + p.verbatim_item() : string(); + if (i == 0) { + if (opt.empty()) + rowinfo[row].top_space = "default"; + else + rowinfo[row].top_space = translate_len(opt); + } else if (rowinfo[row].bottomline) { + if (opt.empty()) + rowinfo[row].bottom_space = "default"; + else + rowinfo[row].bottom_space = translate_len(opt); + } else { + if (opt.empty()) + rowinfo[row].interline_space = "default"; + else + rowinfo[row].interline_space = translate_len(opt); + } } else if (t.cs() == "endhead") { - if (i > 0) + if (i == 0) + endhead.empty = true; + else rowinfo[row].type = LT_HEAD; for (int r = row - 1; r >= 0; --r) { if (rowinfo[r].type != LT_NORMAL) break; rowinfo[r].type = LT_HEAD; + endhead.empty = false; } + endhead.set = true; } else if (t.cs() == "endfirsthead") { - if (i > 0) + if (i == 0) + endfirsthead.empty = true; + else rowinfo[row].type = LT_FIRSTHEAD; for (int r = row - 1; r >= 0; --r) { if (rowinfo[r].type != LT_NORMAL) break; rowinfo[r].type = LT_FIRSTHEAD; + endfirsthead.empty = false; } + endfirsthead.set = true; } else if (t.cs() == "endfoot") { - if (i > 0) + if (i == 0) + endfoot.empty = true; + else rowinfo[row].type = LT_FOOT; for (int r = row - 1; r >= 0; --r) { if (rowinfo[r].type != LT_NORMAL) break; rowinfo[r].type = LT_FOOT; + endfoot.empty = false; } + endfoot.set = true; } else if (t.cs() == "endlastfoot") { - if (i > 0) + if (i == 0) + endlastfoot.empty = true; + else rowinfo[row].type = LT_LASTFOOT; for (int r = row - 1; r >= 0; --r) { if (rowinfo[r].type != LT_NORMAL) break; rowinfo[r].type = LT_LASTFOOT; + endlastfoot.empty = false; } + endlastfoot.set = true; } else if (t.cs() == "newpage") { if (i == 0) { if (row > 0) @@ -939,6 +1183,48 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular, } } + // LyX ends headers and footers always with \tabularnewline. + // This causes one additional row in the output. + // If the last row of a header/footer is empty, we can work + // around that by removing it. + if (row > 1) { + RowInfo test = rowinfo[row-1]; + test.type = LT_NORMAL; + if (lines[row-1].empty() && !test.special()) { + switch (rowinfo[row-1].type) { + case LT_FIRSTHEAD: + if (rowinfo[row].type != LT_FIRSTHEAD && + rowinfo[row-2].type == LT_FIRSTHEAD) + deletelastrow = true; + break; + case LT_HEAD: + if (rowinfo[row].type != LT_HEAD && + rowinfo[row-2].type == LT_HEAD) + deletelastrow = true; + break; + case LT_FOOT: + if (rowinfo[row].type != LT_FOOT && + rowinfo[row-2].type == LT_FOOT) + deletelastrow = true; + break; + case LT_LASTFOOT: + if (rowinfo[row].type != LT_LASTFOOT && + rowinfo[row-2].type == LT_LASTFOOT) + deletelastrow = true; + break; + case LT_NORMAL: + break; + } + } + } + + if (deletelastrow) { + lines.erase(lines.begin() + (row - 1)); + rowinfo.erase(rowinfo.begin() + (row - 1)); + cellinfo.erase(cellinfo.begin() + (row - 1)); + continue; + } + // split into cells vector cells; split(lines[row], cells, TAB); @@ -951,18 +1237,74 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular, << cells[cell] << "'." << endl; continue; } - Parser p(cells[cell]); - p.skip_spaces(); + string cellcont = cells[cell]; + // For decimal cells, ass the content of the second one to the first one + // of a pair. + if (colinfo[col].decimal_point != '\0' && colinfo[col].align == 'd' && cell < cells.size() - 1) + cellcont += colinfo[col].decimal_point + cells[cell + 1]; + Parser parse(cellcont); + parse.skip_spaces(); //cells[cell] << "'\n"; - if (p.next_token().cs() == "multicolumn") { + if (parse.next_token().cs() == "multirow") { + // We do not support the vpos arg yet. + if (parse.hasOpt()) { + string const vpos = parse.getArg('[', ']'); + parse.skip_spaces(true); + cerr << "Ignoring multirow's vpos arg '" + << vpos << "'!" << endl; + } // how many cells? - p.get_token(); + parse.get_token(); size_t const ncells = - convert(p.verbatim_item()); + convert(parse.verbatim_item()); + // We do not support the bigstrut arg yet. + if (parse.hasOpt()) { + string const bs = parse.getArg('[', ']'); + parse.skip_spaces(true); + cerr << "Ignoring multirow's bigstrut arg '" + << bs << "'!" << endl; + } + // the width argument + string const width = parse.getArg('{', '}'); + // the vmove arg + string vmove; + if (parse.hasOpt()) { + vmove = parse.getArg('[', ']'); + parse.skip_spaces(true); + } + + if (width != "*") + colinfo[col].width = width; + if (!vmove.empty()) + cellinfo[row][col].mroffset = vmove; + cellinfo[row][col].multi = CELL_BEGIN_OF_MULTIROW; + cellinfo[row][col].leftlines = colinfo[col].leftlines; + cellinfo[row][col].rightlines = colinfo[col].rightlines; + cellinfo[row][col].mrxnum = ncells - 1; + + ostringstream os2; + parse_text_in_inset(parse, os2, FLAG_ITEM, false, context); + if (!cellinfo[row][col].content.empty()) { + // This may or may not work in LaTeX, + // but it does not work in LyX. + // FIXME: Handle it correctly! + cerr << "Moving cell content '" + << cells[cell] + << "' into a multirow cell. " + "This will probably not work." + << endl; + } + cellinfo[row][col].content += os2.str(); + } else if (parse.next_token().cs() == "multicolumn") { + // how many cells? + parse.get_token(); + size_t const ncells = + convert(parse.verbatim_item()); // special cell properties alignment vector t; - handle_colalign(p, t, ColInfo()); + handle_colalign(parse, t, ColInfo()); + parse.skip_spaces(true); ColInfo & ci = t.front(); // The logic of LyX for multicolumn vertical @@ -977,8 +1319,8 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular, cellinfo[row][col].special = ci.special; cellinfo[row][col].leftlines = ci.leftlines; cellinfo[row][col].rightlines = ci.rightlines; - ostringstream os; - parse_text_in_inset(p, os, FLAG_ITEM, false, context); + ostringstream os2; + parse_text_in_inset(parse, os2, FLAG_ITEM, false, context); if (!cellinfo[row][col].content.empty()) { // This may or may not work in LaTeX, // but it does not work in LyX. @@ -989,22 +1331,97 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular, "This will probably not work." << endl; } - cellinfo[row][col].content += os.str(); + cellinfo[row][col].content += os2.str(); // add dummy cells for multicol - for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) { + for (size_t i = 0; i < ncells - 1; ++i) { ++col; + if (col >= colinfo.size()) { + cerr << "The cell '" + << cells[cell] + << "' specifies " + << convert(ncells) + << " columns while the table has only " + << convert(colinfo.size()) + << " columns!" + << " Therefore the surplus columns will be ignored." + << endl; + break; + } cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN; cellinfo[row][col].align = 'c'; } + } else if (col == 0 && colinfo.size() > 1 && + is_long_tabular && + parse.next_token().cs() == "caption") { + // longtable caption support in LyX is a hack: + // Captions require a row of their own with + // the caption flag set to true, having only + // one multicolumn cell. The contents of that + // cell must contain exactly one caption inset + // and nothing else. + // Fortunately, the caption flag is only needed + // for tables with more than one column. + rowinfo[row].caption = true; + for (size_t c = 1; c < cells.size(); ++c) { + if (!cells[c].empty()) { + cerr << "Moving cell content '" + << cells[c] + << "' into the caption cell. " + "This will probably not work." + << endl; + cells[0] += cells[c]; + } + } + cells.resize(1); + cellinfo[row][col].align = colinfo[col].align; + cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN; + ostringstream os2; + parse_text_in_inset(parse, os2, FLAG_CELL, false, context); + cellinfo[row][col].content += os2.str(); + // add dummy multicolumn cells + for (size_t c = 1; c < colinfo.size(); ++c) + cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN; } else { + bool turn = false; + int rotate = 0; + if (parse.next_token().cs() == "begin") { + parse.pushPosition(); + parse.get_token(); + string const env = parse.getArg('{', '}'); + if (env == "sideways" || env == "turn") { + string angle = "90"; + if (env == "turn") { + turn = true; + angle = parse.getArg('{', '}'); + } + active_environments.push_back(env); + parse.ertEnvironment(env); + active_environments.pop_back(); + parse.skip_spaces(); + if (!parse.good() && support::isStrInt(angle)) + rotate = convert(angle); + } + parse.popPosition(); + } cellinfo[row][col].leftlines = colinfo[col].leftlines; cellinfo[row][col].rightlines = colinfo[col].rightlines; cellinfo[row][col].align = colinfo[col].align; - ostringstream os; - parse_text_in_inset(p, os, FLAG_CELL, false, context); - cellinfo[row][col].content += os.str(); + ostringstream os2; + if (rotate != 0) { + cellinfo[row][col].rotate = rotate; + parse.get_token(); + active_environments.push_back(parse.getArg('{', '}')); + if (turn) + parse.getArg('{', '}'); + parse_text_in_inset(parse, os2, FLAG_END, false, context); + active_environments.pop_back(); + preamble.registerAutomaticallyLoadedPackage("rotating"); + } else { + parse_text_in_inset(parse, os2, FLAG_CELL, false, context); + } + cellinfo[row][col].content += os2.str(); } } @@ -1019,49 +1436,136 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular, cellinfo[row - 1][col].bottomline = true; rowinfo.pop_back(); } + + ++row; } // Now we have the table structure and content in rowinfo, colinfo // and cellinfo. // Unfortunately LyX has some limitations that we need to work around. - // Convert cells with special content to multicolumn cells - // (LyX ignores the special field for non-multicolumn cells). + // Some post work for (size_t row = 0; row < rowinfo.size(); ++row) { for (size_t col = 0; col < cellinfo[row].size(); ++col) { + // Convert cells with special content to multicolumn cells + // (LyX ignores the special field for non-multicolumn cells). if (cellinfo[row][col].multi == CELL_NORMAL && !cellinfo[row][col].special.empty()) cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN; + // Add multirow dummy cells + if (row > 1 && (cellinfo[row - 1][col].multi == CELL_PART_OF_MULTIROW + || cellinfo[row - 1][col].multi == CELL_BEGIN_OF_MULTIROW) + && cellinfo[row - 1][col].mrxnum > 0) { + // add dummy cells for multirow + cellinfo[row][col].multi = CELL_PART_OF_MULTIROW; + cellinfo[row][col].align = 'c'; + cellinfo[row][col].mrxnum = cellinfo[row - 1][col].mrxnum - 1; + } } } + // Distribute lines from rows/columns to cells + // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py. + // Each standard cell inherits the settings of the corresponding + // rowinfo/colinfo. This works because all cells with individual + // settings were converted to multicolumn cells above. + // Each multicolumn cell inherits the settings of the rowinfo/colinfo + // corresponding to the first column of the multicolumn cell (default + // of the multicol package). This works because the special field + // overrides the line fields. + for (size_t row = 0; row < rowinfo.size(); ++row) { + for (size_t col = 0; col < cellinfo[row].size(); ++col) { + if (cellinfo[row][col].multi == CELL_NORMAL) { + cellinfo[row][col].topline = rowinfo[row].topline; + cellinfo[row][col].bottomline = rowinfo[row].bottomline; + cellinfo[row][col].leftlines = colinfo[col].leftlines; + cellinfo[row][col].rightlines = colinfo[col].rightlines; + } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) { + size_t s = col + 1; + while (s < cellinfo[row].size() && + cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN) + s++; + if (s < cellinfo[row].size() && + cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN) + cellinfo[row][col].rightlines = colinfo[col].rightlines; + if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL) + cellinfo[row][col].leftlines = colinfo[col].leftlines; + } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTIROW) { + size_t s = row + 1; + while (s < rowinfo.size() && + cellinfo[s][col].multi == CELL_PART_OF_MULTIROW) + s++; + if (s < cellinfo[row].size() && + cellinfo[s][col].multi != CELL_BEGIN_OF_MULTIROW) + cellinfo[row][col].bottomline = rowinfo[row].bottomline; + if (row > 0 && cellinfo[row - 1][col].multi == CELL_NORMAL) + cellinfo[row][col].topline = rowinfo[row].topline; + } + } + } + + if (booktabs) + preamble.registerAutomaticallyLoadedPackage("booktabs"); + if (name == "longtable") + preamble.registerAutomaticallyLoadedPackage("longtable"); + else if (name == "xltabular") + preamble.registerAutomaticallyLoadedPackage("xltabular"); + else if (name == "tabularx") + preamble.registerAutomaticallyLoadedPackage("tabularx"); + //cerr << "// output what we have\n"; // output what we have + size_type cols = colinfo.size(); + for (size_t col = 0; col < colinfo.size(); ++col) { + if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd') + --cols; + } os << "\n\n"; + << "\" columns=\"" << cols << "\">\n"; os << "\n"; + << write_attribute("rotate", context.tablerotation) + << write_attribute("booktabs", booktabs) + << write_attribute("islongtable", is_long_tabular); + if (is_long_tabular) { + os << write_attribute("firstHeadTopDL", endfirsthead.topDL) + << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL) + << write_attribute("firstHeadEmpty", endfirsthead.empty) + << write_attribute("headTopDL", endhead.topDL) + << write_attribute("headBottomDL", endhead.bottomDL) + << write_attribute("footTopDL", endfoot.topDL) + << write_attribute("footBottomDL", endfoot.bottomDL) + << write_attribute("lastFootTopDL", endlastfoot.topDL) + << write_attribute("lastFootBottomDL", endlastfoot.bottomDL) + << write_attribute("lastFootEmpty", endlastfoot.empty); + if (!halign.empty()) + os << write_attribute("longtabularalignment", halign); + } else + os << write_attribute("tabularvalignment", tabularvalignment); + + os << write_attribute("tabularwidth", tabularwidth) << ">\n"; //cerr << "// after header\n"; for (size_t col = 0; col < colinfo.size(); ++col) { + if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd') + continue; os << " 0) - << write_attribute("rightline", colinfo[col].rightlines > 0) << write_attribute("width", translate_len(colinfo[col].width)) << write_attribute("special", colinfo[col].special) + << write_attribute("varwidth", colinfo[col].varwidth) << ">\n"; } //cerr << "// after cols\n"; for (size_t row = 0; row < rowinfo.size(); ++row) { os << "\n"; for (size_t col = 0; col < colinfo.size(); ++col) { CellInfo const & cell = cellinfo[row][col]; + if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd') + // These are the second columns in a salign pair. Skip. + continue; os << " 0) << write_attribute("rightline", cell.rightlines > 0) - << write_attribute("rotate", cell.rotate); + << write_attribute("rotate", cell.rotate) + << write_attribute("mroffset", cell.mroffset); //cerr << "\nrow: " << row << " col: " << col; //if (cell.topline) // cerr << " topline=\"true\"";