我正在尝试使用 XSLT(版本 1.0)将表格从 XHTML 转换为 LaTeX,但该表格不适合文档的有用区域(宽度 - 16cm)。
没有 colspan 的表格恰好适合文档的有用区域,但有 colspan 的表格超出了这个区域。
我不知道我做错了什么。
例子
XHMTL 表
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2"> <strong>LOREM</strong></td>
<td colspan="10"><strong>IPSUM DOLOR SIT AMET</strong></td>
</tr>
<tr>
<td colspan="2"></td>
<td><strong>consectetuer</strong></td>
<td><strong>adipiscing</strong></td>
<td><strong>elit</strong></td>
<td><strong>Aenean</strong></td>
<td><strong>commodo</strong></td>
<td><strong>ligula</strong></td>
<td><strong>eget</strong></td>
<td><strong>dolor</strong></td>
<td><strong>Aenean</strong></td>
<td><strong>massa</strong></td>
</tr>
<tr>
<td><strong>Cum</strong></td>
<td>sociis</td>
<td>natoque</td>
<td>penatibus</td>
<td>magnis</td>
<td>parturient</td>
<td>montes</td>
<td>nascetur</td>
<td>ridiculus</td>
<td>Donec</td>
<td>felis</td>
<td><strong>ultricies</strong></td>
</tr>
</table>
xsl代码
<xsl:template match="xhtml:table">
<xsl:text>\begin{center} </xsl:text>
<xsl:text>\begin{tabulary}{\linewidth}{</xsl:text>
<xsl:variable name="total_columns">
<xsl:value-of select="sum(xhtml:tr[1]/*/@colspan) + count(xhtml:tr[1]/*[not(@colspan)])"/>
</xsl:variable>
<xsl:text>*{</xsl:text>
<xsl:value-of select="$total_columns"/>
<xsl:text>}{C}} </xsl:text>
<xsl:text>\toprule </xsl:text>
<xsl:for-each select="xhtml:tr">
<xsl:if test="position() != 1">
<xsl:text>\midrule </xsl:text>
</xsl:if>
<xsl:if test="position() = 2">
<xsl:text>\midrule </xsl:text>
</xsl:if>
<xsl:for-each select="xhtml:td|xhtml:th">
<xsl:if test="self::xhtml:th">\bfseries</xsl:if>
<xsl:choose>
<xsl:when test="@colspan">
<xsl:if test="current()/@colspan>=0">
\multicolumn{<xsl:value-of select="current()/@colspan"/>}{
>{\centering\arraybackslash}m{
<xsl:value-of select="((16 div $total_columns) * current()/@colspan)"/>cm}}{
</xsl:if>
</xsl:when>
</xsl:choose>
<xsl:apply-templates/>
<xsl:choose>
<xsl:when test="@colspan">
<xsl:if test="current()/@colspan>=0">}</xsl:if>
</xsl:when>
</xsl:choose>
<xsl:if test="position() != last()">
<xsl:text>&</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:if test="position()!=last()">\\ </xsl:if>
</xsl:for-each>
<xsl:text>\\ \bottomrule</xsl:text>
<xsl:text> \end{tabulary} </xsl:text>
<xsl:text>\end{center}</xsl:text>
</xsl:template>
最佳答案
I'm not sure what is wrong with the LaTeX, the table is generated, but even with the width limited to 16cm, the table exceed the area. Then, this is not necessarily an XSLT problem. Not exactly on topic, but you might want to use TeXML (getfo.org/texml) for tranforming XHTML>LaTeX with XSLT (where TeXML is intermediate step).
关于xml - XSL 转换后 LaTeX 表不适合线宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20121194/