From d984f7dd21863fc01a1a6ede0878d3dd14af157a Mon Sep 17 00:00:00 2001 From: albert-github Date: Wed, 30 May 2018 19:33:17 +0200 Subject: Problems and some enhancements for LaTeX tables - Possibility to honor also the cell alignment when using Markdown tables for other formats (markdown.cpp, latexdocvisitor.cpp, lines: 1146, 1157, 1173) latexdocvisitor.cpp: - Possibility to nest tables to a further level when using LaTeX. It was only possible till level 2 (i.e. a longtabu followed buy a tabularx table). By placing tabularx in an extra set of brackets this is fixed (lines 938 and 951) - tablarx environment cannot handle the headers as used for the longtabu table. A header line is in case of tabularx implemented by mimicking the header line. - longtabu needs multiple times the header line (first and following), tabularx we only need 1 (line 998) - no longtabu special headers (line 1092) - tabularx cannot handle rowcolor, coloring done only with columncolor(line 1024, 1134, 1188) - no necessity for longtabu to define column with for spanned columns / rows(line 1048, 1134, 1171) --- src/docparser.cpp | 26 +++++++++++++++++++++++--- src/latexdocvisitor.cpp | 46 +++++++++++++++++++++++++--------------------- 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/src/docparser.cpp b/src/docparser.cpp index 29c6ca7..205e818 100644 --- a/src/docparser.cpp +++ b/src/docparser.cpp @@ -3505,14 +3505,34 @@ DocHtmlCell::Alignment DocHtmlCell::alignment() const { HtmlAttribList attrs = attribs(); uint i; - for (i=0; iname.lower()=="align") { - if (attrs.at(i)->value.lower()=="center") + if (attrs.at(i)->value.lower()=="center") + return Center; + else if (attrs.at(i)->value.lower()=="right") + return Right; + else return Left; + } + else if (attrs.at(i)->name.lower()=="class") + { + if (attrs.at(i)->value.lower()=="markdowntableheadcenter") + return Center; + else if (attrs.at(i)->value.lower()=="markdowntableheadright") + return Right; + else if (attrs.at(i)->value.lower()=="markdowntableheadleft") + return Left; + else if (attrs.at(i)->value.lower()=="markdowntableheadnone") + return Center; + else if (attrs.at(i)->value.lower()=="markdowntablebodycenter") return Center; - else if (attrs.at(i)->value.lower()=="right") + else if (attrs.at(i)->value.lower()=="markdowntablebodyright") return Right; + else if (attrs.at(i)->value.lower()=="markdowntablebodyleft") + return Left; + else if (attrs.at(i)->value.lower()=="markdowntablebodynone") + return Left; else return Left; } } diff --git a/src/latexdocvisitor.cpp b/src/latexdocvisitor.cpp index 86ceade..e268006 100644 --- a/src/latexdocvisitor.cpp +++ b/src/latexdocvisitor.cpp @@ -935,7 +935,7 @@ static void writeStartTableCommand(FTextStream &t,const DocNode *n,int cols) { if (tableIsNested(n)) { - t << "\\begin{tabularx}{\\linewidth}{|*{" << cols << "}{>{\\raggedright\\arraybackslash}X|}}"; + t << "{\\begin{tabularx}{\\linewidth}{|*{" << cols << "}{>{\\raggedright\\arraybackslash}X|}}"; } else { @@ -948,7 +948,7 @@ static void writeEndTableCommand(FTextStream &t,const DocNode *n) { if (tableIsNested(n)) { - t << "\\end{tabularx}\n"; + t << "\\end{tabularx}}\n"; } else { @@ -995,7 +995,8 @@ void LatexDocVisitor::visitPre(DocHtmlTable *t) if (firstRow && firstRow->isHeading()) { setFirstRow(TRUE); - firstRow->accept(this); + DocNode *n = t->parent(); + if (!tableIsNested(n)) firstRow->accept(this); setFirstRow(FALSE); } } @@ -1021,13 +1022,14 @@ void LatexDocVisitor::visitPost(DocHtmlCaption *c) void LatexDocVisitor::visitPre(DocHtmlRow *r) { setCurrentColumn(0); - if (r->isHeading()) m_t << "\\rowcolor{\\tableheadbgcolor}"; } void LatexDocVisitor::visitPost(DocHtmlRow *row) { if (m_hide) return; + DocNode *n = row->parent() ->parent(); + int c=currentColumn(); while (c<=numCols()) // end of row while inside a row span? { @@ -1045,10 +1047,7 @@ void LatexDocVisitor::visitPost(DocHtmlRow *row) if (span->colSpan>1) // row span is also part of a column span { m_t << "\\multicolumn{" << span->colSpan << "}{"; - m_t << "p{(\\linewidth-\\tabcolsep*" - << numCols() << "-\\arrayrulewidth*" - << row->visibleCells() << ")*" - << span->colSpan <<"/"<< numCols() << "}|}{}"; + m_t << "}|}{}"; } else // solitary row span { @@ -1089,7 +1088,8 @@ void LatexDocVisitor::visitPost(DocHtmlRow *row) m_t << "\n"; - if (row->isHeading() && row->rowIndex()==1) + + if (row->isHeading() && row->rowIndex()==1 && !tableIsNested(n)) { if (firstRow()) { @@ -1131,10 +1131,7 @@ void LatexDocVisitor::visitPre(DocHtmlCell *c) { m_t << "|"; } - m_t << "p{(\\linewidth-\\tabcolsep*" - << numCols() << "-\\arrayrulewidth*" - << row->visibleCells() << ")*" - << span->colSpan <<"/"<< numCols() << "}|}{}"; + m_t << "l|}{" << (c->isHeading()? "\\columncolor{\\tableheadbgcolor}" : "") << "}"; // alignment not relevant, empty column setCurrentColumn(currentColumn()+span->colSpan); } else @@ -1146,6 +1143,7 @@ void LatexDocVisitor::visitPre(DocHtmlCell *c) } int cs = c->colSpan(); + int a = c->alignment(); if (cs>1 && row) { setInColSpan(TRUE); @@ -1154,11 +1152,18 @@ void LatexDocVisitor::visitPre(DocHtmlCell *c) { m_t << "|"; } - m_t << "p{(\\linewidth-\\tabcolsep*" - << numCols() << "-\\arrayrulewidth*" - << row->visibleCells() << ")*" - << cs <<"/"<< numCols() << "}|}{"; - if (c->isHeading()) m_t << "\\cellcolor{\\tableheadbgcolor}"; + switch (a) + { + case DocHtmlCell::Right: + m_t << "r|}{"; + break; + case DocHtmlCell::Center: + break; + m_t << "c|}{"; + default: + m_t << "l|}{"; + break; + } } int rs = c->rowSpan(); if (rs>0) @@ -1168,9 +1173,8 @@ void LatexDocVisitor::visitPre(DocHtmlCell *c) // c->rowIndex(),c->columnIndex(),c->rowSpan(),c->colSpan(), // currentColumn()); addRowSpan(new ActiveRowSpan(c,rs,cs,currentColumn())); - m_t << "\\multirow{" << rs << "}{\\linewidth}{"; + m_t << "\\multirow{" << rs << "}{*}{"; } - int a = c->alignment(); if (a==DocHtmlCell::Center) { m_t << "\\PBS\\centering "; @@ -1181,7 +1185,7 @@ void LatexDocVisitor::visitPre(DocHtmlCell *c) } if (c->isHeading()) { - m_t << "\\textbf{ "; + m_t << "\\cellcolor{\\tableheadbgcolor}\\textbf{ "; } if (cs>1) { -- cgit v0.12