#include "tex2lyx.h"
+#include "Context.h"
#include "Preamble.h"
#include "support/lassert.h"
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
int leftlines;
/// varwidth column
bool varwidth;
+ /// decimal separator
+ char decimal_point;
+ /// V column type
+ bool vcolumn;
};
public:
CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
leftlines(0), rightlines(0), topline(false),
- bottomline(false), rotate(0), mrxnum(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
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
return "right";
case 'l':
return "left";
+ case 'd':
+ return "decimal";
default:
return "none";
}
// 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)
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';
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;
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;
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 += '|';
}
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
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{<with>}r{<width>})
+ 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") {
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";
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<string> cols;
split(arg, cols, '-');
cols.resize(2);
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") {
<< 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() == "multirow") {
//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<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
- << "\" columns=\"" << colinfo.size() << "\">\n";
+ << "\" columns=\"" << cols << "\">\n";
os << "<features"
- << write_attribute("rotate", rotate)
+ << write_attribute("rotate", context.tablerotation)
<< write_attribute("booktabs", booktabs)
<< write_attribute("islongtable", is_long_tabular);
if (is_long_tabular) {
//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 << "<column alignment=\""
- << verbose_align(colinfo[col].align) << "\""
- << " valignment=\""
+ << verbose_align(colinfo[col].align) << "\"";
+ if (colinfo[col].decimal_point != '\0')
+ os << " decimal_point=\"" << colinfo[col].decimal_point << "\"";
+ os << " valignment=\""
<< verbose_valign(colinfo[col].valign) << "\""
<< write_attribute("width", translate_len(colinfo[col].width))
<< write_attribute("special", colinfo[col].special)
<< ">\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 << "<cell";
- if (cell.multi == CELL_BEGIN_OF_MULTICOLUMN
- || cell.multi == CELL_PART_OF_MULTICOLUMN)
+ if ((cell.multi == CELL_BEGIN_OF_MULTICOLUMN
+ || cell.multi == CELL_PART_OF_MULTICOLUMN)
+ && colinfo[col].align != 'd')
os << " multicolumn=\"" << cell.multi << "\"";
if (cell.multi == CELL_BEGIN_OF_MULTIROW
|| cell.multi == CELL_PART_OF_MULTIROW)
<< " valignment=\"" << verbose_valign(cell.valign)
<< "\""
<< write_attribute("topline", cell.topline)
+ << write_attribute("toplineltrim", cell.topline_ltrim)
+ << write_attribute("toplinertrim", cell.topline_rtrim)
<< write_attribute("bottomline", cell.bottomline)
+ << write_attribute("bottomlineltrim", cell.bottomline_ltrim)
+ << write_attribute("bottomlinertrim", cell.bottomline_rtrim)
<< write_attribute("leftline", cell.leftlines > 0)
<< write_attribute("rightline", cell.rightlines > 0)
<< write_attribute("rotate", cell.rotate)