tex2lyx: support decimal alignment in tables

This commit is contained in:
Juergen Spitzmueller 2018-08-26 16:17:54 +02:00
parent a7ea98a30a
commit 8ef2558dc2
4 changed files with 115 additions and 225 deletions

View File

@ -28,7 +28,6 @@ Format LaTeX feature LyX feature
364 branch file name suffix \filename_suffix 364 branch file name suffix \filename_suffix
371 automatic mhchem loading \use_mhchem 371 automatic mhchem loading \use_mhchem
390 forward/reverse search \forward_search, \forward_macro 390 forward/reverse search \forward_search, \forward_macro
391 decimal alignment in tables InsetTabular
399 automatic mathdots loading \use_mathdots 399 automatic mathdots loading \use_mathdots
411 support for polyglossia \language_package (the cases of no package, of babel and of custom package is supported) 411 support for polyglossia \language_package (the cases of no package, of babel and of custom package is supported)
415 automatic undertilde loading \use_package undertilde 415 automatic undertilde loading \use_package undertilde

View File

@ -38,7 +38,8 @@ namespace {
class ColInfo { class ColInfo {
public: 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') {}
/// column alignment /// column alignment
char align; char align;
/// vertical alignment /// vertical alignment
@ -53,6 +54,8 @@ public:
int leftlines; int leftlines;
/// varwidth column /// varwidth column
bool varwidth; bool varwidth;
/// decimal separator
char decimal_point;
}; };
@ -180,6 +183,8 @@ inline char const * verbose_align(char c)
return "right"; return "right";
case 'l': case 'l':
return "left"; return "left";
case 'd':
return "decimal";
default: default:
return "none"; return "none";
} }
@ -267,6 +272,17 @@ void ci2special(ColInfo & ci)
// this case. // this case.
return; 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()) { if (!ci.width.empty()) {
string arraybackslash; string arraybackslash;
if (ci.varwidth) if (ci.varwidth)
@ -346,8 +362,17 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
case 'r': case 'r':
// new column, horizontal aligned // new column, horizontal aligned
next.align = t.character(); next.align = t.character();
if (!next.special.empty()) if (!next.special.empty()) {
ci2special(next); 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); colinfo.push_back(next);
next = ColInfo(); next = ColInfo();
break; break;
@ -365,8 +390,17 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
// new column, vertical aligned box // new column, vertical aligned box
next.valign = t.character(); next.valign = t.character();
next.width = p.verbatim_item(); next.width = p.verbatim_item();
if (!next.special.empty()) if (!next.special.empty()) {
ci2special(next); 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); colinfo.push_back(next);
next = ColInfo(); next = ColInfo();
break; break;
@ -442,11 +476,16 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
} }
case '@': case '@':
// text instead of the column spacing // text instead of the column spacing
case '!': case '!': {
// text in addition to the column spacing // text in addition to the column spacing
string const arg = p.verbatim_item();
next.special += t.character(); 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; break;
}
default: { default: {
// try user defined column types // try user defined column types
// unknown column types (nargs == -1) are // unknown column types (nargs == -1) are
@ -1143,7 +1182,12 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
<< cells[cell] << "'." << endl; << cells[cell] << "'." << endl;
continue; 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(); parse.skip_spaces();
//cells[cell] << "'\n"; //cells[cell] << "'\n";
if (parse.next_token().cs() == "multirow") { if (parse.next_token().cs() == "multirow") {
@ -1416,8 +1460,13 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
//cerr << "// output what we have\n"; //cerr << "// output what we have\n";
// output what we have // 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<lyxtabular version=\"3\" rows=\"" << rowinfo.size() os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
<< "\" columns=\"" << colinfo.size() << "\">\n"; << "\" columns=\"" << cols << "\">\n";
os << "<features" os << "<features"
<< write_attribute("rotate", context.tablerotation) << write_attribute("rotate", context.tablerotation)
<< write_attribute("booktabs", booktabs) << write_attribute("booktabs", booktabs)
@ -1442,9 +1491,13 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
//cerr << "// after header\n"; //cerr << "// after header\n";
for (size_t col = 0; col < colinfo.size(); ++col) { for (size_t col = 0; col < colinfo.size(); ++col) {
if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
continue;
os << "<column alignment=\"" os << "<column alignment=\""
<< verbose_align(colinfo[col].align) << "\"" << verbose_align(colinfo[col].align) << "\"";
<< " valignment=\"" if (colinfo[col].decimal_point != '\0')
os << " decimal_point=\"" << colinfo[col].decimal_point << "\"";
os << " valignment=\""
<< verbose_valign(colinfo[col].valign) << "\"" << verbose_valign(colinfo[col].valign) << "\""
<< write_attribute("width", translate_len(colinfo[col].width)) << write_attribute("width", translate_len(colinfo[col].width))
<< write_attribute("special", colinfo[col].special) << write_attribute("special", colinfo[col].special)
@ -1471,9 +1524,13 @@ void handle_tabular(Parser & p, ostream & os, string const & name,
<< ">\n"; << ">\n";
for (size_t col = 0; col < colinfo.size(); ++col) { for (size_t col = 0; col < colinfo.size(); ++col) {
CellInfo const & cell = cellinfo[row][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"; os << "<cell";
if (cell.multi == CELL_BEGIN_OF_MULTICOLUMN if ((cell.multi == CELL_BEGIN_OF_MULTICOLUMN
|| cell.multi == CELL_PART_OF_MULTICOLUMN) || cell.multi == CELL_PART_OF_MULTICOLUMN)
&& colinfo[col].align != 'd')
os << " multicolumn=\"" << cell.multi << "\""; os << " multicolumn=\"" << cell.multi << "\"";
if (cell.multi == CELL_BEGIN_OF_MULTIROW if (cell.multi == CELL_BEGIN_OF_MULTIROW
|| cell.multi == CELL_PART_OF_MULTIROW) || cell.multi == CELL_PART_OF_MULTIROW)

View File

@ -5027,16 +5027,13 @@ status open
\begin_layout Standard \begin_layout Standard
\begin_inset Tabular \begin_inset Tabular
<lyxtabular version="3" rows="4" columns="8"> <lyxtabular version="3" rows="4" columns="5">
<features tabularvalignment="middle" tabularwidth="0pt"> <features tabularvalignment="middle" tabularwidth="0pt">
<column alignment="center" valignment="top"> <column alignment="center" valignment="top">
<column alignment="center" valignment="top"> <column alignment="center" valignment="top">
<column alignment="right" valignment="top"> <column alignment="decimal" decimal_point="." valignment="top">
<column alignment="none" valignment="top" special="@{\extracolsep{0pt}.}l"> <column alignment="decimal" decimal_point="." valignment="top">
<column alignment="right" valignment="top"> <column alignment="decimal" decimal_point="." valignment="top">
<column alignment="none" valignment="top" special="@{\extracolsep{0pt}.}l">
<column alignment="right" valignment="top">
<column alignment="none" valignment="top" special="@{\extracolsep{0pt}.}l">
<row> <row>
<cell alignment="center" valignment="top" usebox="none"> <cell alignment="center" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
@ -5056,7 +5053,7 @@ Two
\end_inset \end_inset
</cell> </cell>
<cell multicolumn="1" alignment="none" valignment="top" usebox="none" special="c"> <cell alignment="none" valignment="top" usebox="none" special="c">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
@ -5065,12 +5062,7 @@ Three
\end_inset \end_inset
</cell> </cell>
<cell multicolumn="2" alignment="center" valignment="top" usebox="none"> <cell alignment="none" valignment="top" usebox="none" special="c">
\begin_inset Text
\end_inset
</cell>
<cell multicolumn="1" alignment="none" valignment="top" usebox="none" special="c">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
@ -5079,23 +5071,13 @@ Four
\end_inset \end_inset
</cell> </cell>
<cell multicolumn="2" alignment="center" valignment="top" usebox="none"> <cell alignment="none" valignment="top" usebox="none" special="c">
\begin_inset Text
\end_inset
</cell>
<cell multicolumn="1" alignment="none" valignment="top" usebox="none" special="c">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
Five Five
\end_layout \end_layout
\end_inset
</cell>
<cell multicolumn="2" alignment="center" valignment="top" usebox="none">
\begin_inset Text
\end_inset \end_inset
</cell> </cell>
</row> </row>
@ -5118,7 +5100,7 @@ two
\end_inset \end_inset
</cell> </cell>
<cell multicolumn="1" alignment="none" valignment="top" topline="true" usebox="none" special="c"> <cell alignment="none" valignment="top" topline="true" usebox="none" special="c">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
@ -5127,12 +5109,7 @@ three
\end_inset \end_inset
</cell> </cell>
<cell multicolumn="2" alignment="center" valignment="top" topline="true" usebox="none"> <cell alignment="none" valignment="top" topline="true" usebox="none" special="c">
\begin_inset Text
\end_inset
</cell>
<cell multicolumn="1" alignment="none" valignment="top" topline="true" usebox="none" special="c">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
@ -5141,23 +5118,13 @@ four
\end_inset \end_inset
</cell> </cell>
<cell multicolumn="2" alignment="center" valignment="top" topline="true" usebox="none"> <cell alignment="none" valignment="top" topline="true" usebox="none" special="c">
\begin_inset Text
\end_inset
</cell>
<cell multicolumn="1" alignment="none" valignment="top" topline="true" usebox="none" special="c">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
five five
\end_layout \end_layout
\end_inset
</cell>
<cell multicolumn="2" alignment="center" valignment="top" topline="true" usebox="none">
\begin_inset Text
\end_inset \end_inset
</cell> </cell>
</row> </row>
@ -5180,52 +5147,29 @@ He
\end_inset \end_inset
</cell> </cell>
<cell alignment="right" valignment="top" usebox="none"> <cell alignment="decimal" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
2 2.77234
\end_layout \end_layout
\end_inset \end_inset
</cell> </cell>
<cell alignment="none" valignment="top" usebox="none"> <cell alignment="decimal" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
77234 45672.
\end_layout \end_layout
\end_inset \end_inset
</cell> </cell>
<cell alignment="right" valignment="top" usebox="none"> <cell alignment="decimal" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
45672 0.69
\end_layout
\end_inset
</cell>
<cell alignment="none" valignment="top" usebox="none">
\begin_inset Text
\end_inset
</cell>
<cell alignment="right" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Standard
0
\end_layout
\end_inset
</cell>
<cell alignment="none" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Standard
69
\end_layout \end_layout
\end_inset \end_inset
@ -5250,56 +5194,29 @@ C
\end_inset \end_inset
</cell> </cell>
<cell alignment="right" valignment="top" usebox="none"> <cell alignment="decimal" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
12537 12537.64
\end_layout \end_layout
\end_inset \end_inset
</cell> </cell>
<cell alignment="none" valignment="top" usebox="none"> <cell alignment="decimal" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
64 37.66345
\end_layout \end_layout
\end_inset \end_inset
</cell> </cell>
<cell alignment="right" valignment="top" usebox="none"> <cell alignment="decimal" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
37 86.37
\end_layout
\end_inset
</cell>
<cell alignment="none" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Standard
66345
\end_layout
\end_inset
</cell>
<cell alignment="right" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Standard
86
\end_layout
\end_inset
</cell>
<cell alignment="none" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Standard
37
\end_layout \end_layout
\end_inset \end_inset

View File

@ -5421,16 +5421,13 @@ status open
\begin_layout Standard \begin_layout Standard
\begin_inset Tabular \begin_inset Tabular
<lyxtabular version="3" rows="4" columns="8"> <lyxtabular version="3" rows="4" columns="5">
<features tabularvalignment="middle" tabularwidth="0pt"> <features tabularvalignment="middle" tabularwidth="0pt">
<column alignment="center" valignment="top"> <column alignment="center" valignment="top">
<column alignment="center" valignment="top"> <column alignment="center" valignment="top">
<column alignment="right" valignment="top"> <column alignment="decimal" decimal_point="." valignment="top">
<column alignment="none" valignment="top" special="@{\extracolsep{0pt}.}l"> <column alignment="decimal" decimal_point="." valignment="top">
<column alignment="right" valignment="top"> <column alignment="decimal" decimal_point="." valignment="top">
<column alignment="none" valignment="top" special="@{\extracolsep{0pt}.}l">
<column alignment="right" valignment="top">
<column alignment="none" valignment="top" special="@{\extracolsep{0pt}.}l">
<row> <row>
<cell alignment="center" valignment="top" usebox="none"> <cell alignment="center" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
@ -5450,7 +5447,7 @@ Two
\end_inset \end_inset
</cell> </cell>
<cell multicolumn="1" alignment="none" valignment="top" usebox="none" special="c"> <cell alignment="none" valignment="top" usebox="none" special="c">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
@ -5459,12 +5456,7 @@ Three
\end_inset \end_inset
</cell> </cell>
<cell multicolumn="2" alignment="center" valignment="top" usebox="none"> <cell alignment="none" valignment="top" usebox="none" special="c">
\begin_inset Text
\end_inset
</cell>
<cell multicolumn="1" alignment="none" valignment="top" usebox="none" special="c">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
@ -5473,23 +5465,13 @@ Four
\end_inset \end_inset
</cell> </cell>
<cell multicolumn="2" alignment="center" valignment="top" usebox="none"> <cell alignment="none" valignment="top" usebox="none" special="c">
\begin_inset Text
\end_inset
</cell>
<cell multicolumn="1" alignment="none" valignment="top" usebox="none" special="c">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
Five Five
\end_layout \end_layout
\end_inset
</cell>
<cell multicolumn="2" alignment="center" valignment="top" usebox="none">
\begin_inset Text
\end_inset \end_inset
</cell> </cell>
</row> </row>
@ -5512,7 +5494,7 @@ two
\end_inset \end_inset
</cell> </cell>
<cell multicolumn="1" alignment="none" valignment="top" topline="true" usebox="none" special="c"> <cell alignment="none" valignment="top" topline="true" usebox="none" special="c">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
@ -5521,12 +5503,7 @@ three
\end_inset \end_inset
</cell> </cell>
<cell multicolumn="2" alignment="center" valignment="top" topline="true" usebox="none"> <cell alignment="none" valignment="top" topline="true" usebox="none" special="c">
\begin_inset Text
\end_inset
</cell>
<cell multicolumn="1" alignment="none" valignment="top" topline="true" usebox="none" special="c">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
@ -5535,23 +5512,13 @@ four
\end_inset \end_inset
</cell> </cell>
<cell multicolumn="2" alignment="center" valignment="top" topline="true" usebox="none"> <cell alignment="none" valignment="top" topline="true" usebox="none" special="c">
\begin_inset Text
\end_inset
</cell>
<cell multicolumn="1" alignment="none" valignment="top" topline="true" usebox="none" special="c">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
five five
\end_layout \end_layout
\end_inset
</cell>
<cell multicolumn="2" alignment="center" valignment="top" topline="true" usebox="none">
\begin_inset Text
\end_inset \end_inset
</cell> </cell>
</row> </row>
@ -5574,52 +5541,29 @@ He
\end_inset \end_inset
</cell> </cell>
<cell alignment="right" valignment="top" usebox="none"> <cell alignment="decimal" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
2 2.77234
\end_layout \end_layout
\end_inset \end_inset
</cell> </cell>
<cell alignment="none" valignment="top" usebox="none"> <cell alignment="decimal" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
77234 45672.
\end_layout \end_layout
\end_inset \end_inset
</cell> </cell>
<cell alignment="right" valignment="top" usebox="none"> <cell alignment="decimal" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
45672 0.69
\end_layout
\end_inset
</cell>
<cell alignment="none" valignment="top" usebox="none">
\begin_inset Text
\end_inset
</cell>
<cell alignment="right" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Standard
0
\end_layout
\end_inset
</cell>
<cell alignment="none" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Standard
69
\end_layout \end_layout
\end_inset \end_inset
@ -5644,56 +5588,29 @@ C
\end_inset \end_inset
</cell> </cell>
<cell alignment="right" valignment="top" usebox="none"> <cell alignment="decimal" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
12537 12537.64
\end_layout \end_layout
\end_inset \end_inset
</cell> </cell>
<cell alignment="none" valignment="top" usebox="none"> <cell alignment="decimal" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
64 37.66345
\end_layout \end_layout
\end_inset \end_inset
</cell> </cell>
<cell alignment="right" valignment="top" usebox="none"> <cell alignment="decimal" valignment="top" usebox="none">
\begin_inset Text \begin_inset Text
\begin_layout Standard \begin_layout Standard
37 86.37
\end_layout
\end_inset
</cell>
<cell alignment="none" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Standard
66345
\end_layout
\end_inset
</cell>
<cell alignment="right" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Standard
86
\end_layout
\end_inset
</cell>
<cell alignment="none" valignment="top" usebox="none">
\begin_inset Text
\begin_layout Standard
37
\end_layout \end_layout
\end_inset \end_inset