X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ftex2lyx%2Ftable.cpp;h=411fd5b6ab5534f929684b38458cc727856d2f6a;hb=a2b21e3cd4bbfd42e59161143eba6e7681aaa93f;hp=6f2563231dd6a55acfab99bd3233d2900f4b3f1a;hpb=f38816bf16fa27504e40e33beb9b20d8095c988a;p=lyx.git
diff --git a/src/tex2lyx/table.cpp b/src/tex2lyx/table.cpp
index 6f2563231d..411fd5b6ab 100644
--- a/src/tex2lyx/table.cpp
+++ b/src/tex2lyx/table.cpp
@@ -16,6 +16,7 @@
#include "tex2lyx.h"
+#include "Context.h"
#include "Preamble.h"
#include "support/lassert.h"
@@ -37,7 +38,8 @@ namespace {
class ColInfo {
public:
- ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0), varwidth(false) {}
+ ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0),
+ varwidth(false), decimal_point('\0'), vcolumn(false) {}
/// column alignment
char align;
/// vertical alignment
@@ -52,6 +54,10 @@ public:
int leftlines;
/// varwidth column
bool varwidth;
+ /// decimal separator
+ char decimal_point;
+ /// V column type
+ bool vcolumn;
};
@@ -103,14 +109,18 @@ public:
/// the numeric values are part of the file format!
-enum Multicolumn {
+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 = 1,
+ CELL_BEGIN_OF_MULTICOLUMN,
/// This is a dummy cell (part of a multicolumn cell)
- CELL_PART_OF_MULTICOLUMN = 2
+ CELL_PART_OF_MULTICOLUMN,
+ ///
+ CELL_BEGIN_OF_MULTIROW,
+ ///
+ CELL_PART_OF_MULTIROW
};
@@ -118,11 +128,13 @@ class CellInfo {
public:
CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
leftlines(0), rightlines(0), topline(false),
- bottomline(false), rotate(0) {}
+ 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
@@ -135,12 +147,24 @@ public:
bool topline;
/// do we have a line below?
bool bottomline;
+ /// 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;
};
@@ -171,6 +195,8 @@ inline char const * verbose_align(char c)
return "right";
case 'l':
return "left";
+ case 'd':
+ return "decimal";
default:
return "none";
}
@@ -258,6 +284,17 @@ 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)
@@ -273,7 +310,9 @@ void ci2special(ColInfo & ci)
ci.special += ">{\\centering" + arraybackslash + "}";
break;
}
- if (ci.varwidth)
+ if (ci.vcolumn)
+ ci.special += 'V';
+ else if (ci.varwidth)
ci.special += 'X';
else if (ci.valign == 'n')
ci.special += 'p';
@@ -337,8 +376,17 @@ void handle_colalign(Parser & p, vector & colinfo,
case 'r':
// new column, horizontal aligned
next.align = t.character();
- 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;
@@ -350,14 +398,38 @@ void handle_colalign(Parser & p, vector & colinfo,
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;
@@ -370,7 +442,7 @@ 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 += '|';
@@ -410,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();
@@ -431,11 +505,16 @@ 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: {
// try user defined column types
// unknown column types (nargs == -1) are
@@ -532,14 +611,21 @@ bool parse_hlines(Parser & p, Token const & t, string & hlines,
hlines += "\\cline{" + p.verbatim_item() + '}';
else if (t.cs() == "cmidrule") {
- // We cannot handle the \cmidrule(l){3-4} form
p.pushPosition();
p.skip_spaces(true);
- bool const hasParentheses(p.getFullArg('(', ')').first);
- p.popPosition();
- if (hasParentheses)
+ // We do not support the optional height argument
+ if (p.hasOpt())
return false;
- hlines += "\\cmidrule{" + p.verbatim_item() + '}';
+ // 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") {
@@ -921,7 +1007,7 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
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";
@@ -947,10 +1033,13 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
handle_hline_below(rowinfo[row], cellinfo[row]);
}
} else if (t.cs() == "cline" || t.cs() == "cmidrule") {
- if (t.cs() == "cmidrule")
+ string trim;
+ if (t.cs() == "cmidrule") {
booktabs = true;
+ trim = p1.getFullParentheseArg();
+ }
string arg = p1.verbatim_item();
- //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
+ //cerr << "read " << t.cs() << " arg: '" << arg << "', trim: '" << trim << "'\n";
vector cols;
split(arg, cols, '-');
cols.resize(2);
@@ -987,9 +1076,25 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
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") {
@@ -1132,10 +1237,65 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
<< cells[cell] << "'." << endl;
continue;
}
- Parser parse(cells[cell]);
+ 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 (parse.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?
+ parse.get_token();
+ size_t const ncells =
+ 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 =
@@ -1284,13 +1444,23 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
// 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;
+ }
}
}
@@ -1320,6 +1490,16 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
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;
}
}
}
@@ -1335,11 +1515,15 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
//cerr << "// output what we have\n";
// output what we have
- string const rotate = "0";
+ 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";
+ 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 << "\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\"";
|