I have been looking for a suitable solution for conversion of an HTML table to LaTeX. I have found the following questions which is similar to my requirement:
But both of these were not 100% helpful with my current requirement, as it does not involve in conversion of content with both colspan
and rowspan
.
The input HTML would look like:
<html>
<head>
</head>
<body>
<table border="1">
<tr>
<td>This</td>
<td>is</td>
<td>a</td>
<td>test</td>
</tr>
<tr>
<td colspan="2">This is</td>
<td>a</td>
<td>test</td>
</tr>
<tr>
<td>This</td>
<td>is</td>
<td colspan="2">a test</td>
</tr>
<tr>
<td rowspan="2">This</td>
<td colspan="2">is a</td>
<td rowspan="2">test</td>
</tr>
<tr>
<td>is</td>
<td>a</td>
</tr>
</table>
</body>
</html>
The LaTeX code I expect as the output is:
\documentclass{standalone}
\usepackage{multirow}
\begin{document}
\begin{tabular}{*{4}{|l}|}
\hline
This & is & a & test\\
\hline
\multicolumn{2}{|l|}{This is} & a & test \\
\hline
This & is & \multicolumn{2}{|l|}{a test} \\
\hline
\multirow{2}*{This} & \multicolumn{2}{|l|}{is a} & \multirow{2}*{test} \\
\cline{2-3}
& is & a & \\
\hline
\end{tabular}
\end{document}
I took the solution given by @DavidCarlisle in Converting XHTML table to LaTeX using XSLT and modified as given below:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"/>
<xsl:template match="/">
\documentclass{standalone}
\usepackage{multirow}
\begin{document}
<xsl:apply-templates/>
\end{document}
</xsl:template>
<xsl:template match="table">
<xsl:variable name="noc" select="max(tr/sum(td/(@colspan/number(.),1)[1]))"/>
<xsl:text>\begin{tabular}{*{</xsl:text>
<xsl:value-of select="$noc"/>
<xsl:text>}{|l}|} </xsl:text>
<xsl:apply-templates select="tr[1]">
<xsl:with-param name="rspans" select="for $i in 1 to xs:integer($noc) return 0"/>
</xsl:apply-templates>
<xsl:text>\end{tabular}</xsl:text>
</xsl:template>
<xsl:template match="tr">
<xsl:param name="rspans"/>
<xsl:text/>% [<xsl:value-of select="$rspans"/>]
<xsl:variable name="tr" select="."/>
<xsl:for-each select="$rspans">
<xsl:variable name="c" select="position()"/>
<xsl:variable name="td" select="$tr/td[count($rspans[position() <=$c][.=0])]"/>
<xsl:if test=".=0">
<xsl:if test="$td/@rowspan[. > 1]">
<xsl:text>\multirow{</xsl:text>
<xsl:value-of select="$td/@rowspan"/>
<xsl:text>}{*}{</xsl:text>
</xsl:if>
<xsl:if test="$td/@colspan[. > 1]">
<xsl:text>\multicolumn{</xsl:text>
<xsl:value-of select="$td/@colspan"/>
<xsl:text>}{|l|}{</xsl:text>
</xsl:if>
<xsl:apply-templates select="$td"/>
<xsl:if test="$td/@colspan[. > 1]">}</xsl:if>
<xsl:if test="$td/@rowspan[. > 1]">}</xsl:if>
</xsl:if>
<xsl:choose>
<xsl:when test=". >1 and position()=last()">&\\\hline </xsl:when>
<xsl:when test="position()=last()">\\\hline </xsl:when>
<xsl:otherwise>&</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates select="following-sibling::tr[1]">
<xsl:with-param name="rspans" select="for $c in 1 to count($rspans)
return
($rspans[$c] +
td[count($rspans[position() <=$c][.=0])]/(@rowspan,1)[1]
-1)"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
The resulting LaTeX code contains following problems:
- I am not able to put the
\cline{...}
command properly.\cline{..}
is reqired if there is a row spanned in the current row. Otherwise\hline
is required. - In the second line there is an extra & (ampersand) symbol appearing at the end just before
\\\hline
. - In the last row, third column (content "a") is missing.
Is there any solution for this problem.