Converting XHTML table with rowspan and colspan to LaTeX
Asked Answered
G

0

6

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}|}&#10;</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() &lt;=$c][.=0])]"/>
  <xsl:if test=".=0">
   <xsl:if test="$td/@rowspan[. &gt; 1]">
    <xsl:text>\multirow{</xsl:text>
    <xsl:value-of select="$td/@rowspan"/>
    <xsl:text>}{*}{</xsl:text>
   </xsl:if>
   <xsl:if test="$td/@colspan[. &gt; 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[. &gt; 1]">}</xsl:if>
   <xsl:if test="$td/@rowspan[. &gt; 1]">}</xsl:if>
  </xsl:if>
  <xsl:choose>
   <xsl:when test=". &gt;1 and position()=last()">&amp;\\\hline&#10;</xsl:when>
   <xsl:when test="position()=last()">\\\hline&#10;</xsl:when>
   <xsl:otherwise>&amp;</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() &lt;=$c][.=0])]/(@rowspan,1)[1]
   -1)"/>
 </xsl:apply-templates>
</xsl:template>

</xsl:stylesheet>

The resulting LaTeX code contains following problems:

  1. 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.
  2. In the second line there is an extra & (ampersand) symbol appearing at the end just before \\\hline.
  3. In the last row, third column (content "a") is missing.

Is there any solution for this problem.

Grallatorial answered 22/2, 2016 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.