表格列对齐到 HTML

表格列对齐到 HTML

如何在转换后的 HTML 中保留 LaTeX 表格单元格对齐方式科尔元素?

例如:

\begin{tabular}{**lcr**} 

应转换为

<col align="**left**" ..><col align="**centre**" ..><col align="**right**" ..>

答案1

尝试这个配置文件:

\Preamble{xhtml}

\Configure{halignTD}
{}{}
{^}{\HCode{style="vertical-align:top"}}
{=}{\HCode{style="vertical-align:baseline"}}
{|}{\HCode{style="vertical-align:middle"}}
{_}{\HCode{style="vertical-align:bottom"}}
{<}{\HCode{align="left"}}
{-}{\HCode{align="center"}}
{>}{\HCode{align="right"}}
{p}{\HCode{align="left"}}
{}

\catcode`\:=11
\Configure{VBorder}
   {\let\VBorder\empty \let\AllColMargins\empty
    \global\let\GROUPS\empty \HAssign\NewGroup = 0
    \gHAdvance\Next:TableNo by 1 \global\let\TableNo=\Next:TableNo }
   {\xdef\VBorder{\VBorder\ifnum \NewGroup> 0 </colgroup>\fi}
    \HAssign\NewGroup = 0 % \gdef\GROUPS{rules="groups"}}
    % put vertical border for a column
    \ifnum\ar:cnt > 0%
      % in all other cases, we should put right border
      \Css{\#TBL-\TableNo-\ar:cnt {border-right:1px solid black;}}%
    \else%
      % columns are numbered from 1. if \ar:cnt == 0 then it is the left border
      \Css{\#TBL-\TableNo-1{border-left: 1px solid black;}}%
    \fi%
   }
   {\Advance:\NewGroup by 1
    \ifnum \NewGroup=1 \xdef\VBorder{\VBorder<colgroup
        id="TBL-\TableNo-\ar:cnt g">}\fi
    \bgroup\def\HCode##1{##1} % we need to redefine \HCode locally, otherwise we would get spurious characters inside the HTML tag
    \xdef\VBorder{\VBorder<col\Hnewline id="TBL-\TableNo
       -\ar:cnt" \HColAlign \xml:empty>}
   \egroup
   \xdef\AllColMargins{\AllColMargins1}}
   {\xdef\AllColMargins{\AllColMargins 0}}
\catcode`\:=12
\begin{document}
\EndPreamble

align首先,它定义了应使用属性而不是 CSS 样式来指定表格列对齐方式。这是在 中完成的\Configure{halignTD}。您可以在此处添加所有可能的表格说明符。

但这样可以确保align属性仅插入到特定的表格单元格中,而不会插入到<col>您想要的元素中。为此,我们需要将命令放入\HColAlign代码中\Configure{VBorder}

对于此 MWE:

\documentclass{article}
\begin{document}
\begin{tabular}{|l|c|r|}
  hello & world & right \\
  longer & words should & show something
\end{tabular}
\end{document}

结果如下:

在此处输入图片描述

<div class='tabular'> 
<table class='tabular' id='TBL-1'>
<colgroup id='TBL-1-1g'><col align='left' id='TBL-1-1' /></colgroup>
<colgroup id='TBL-1-2g'><col align='center' id='TBL-1-2' /></colgroup>
<colgroup id='TBL-1-3g'><col align='right' id='TBL-1-3' /></colgroup>
<tr id='TBL-1-1-' style='vertical-align:baseline;'><td align='left' class='td11' id='TBL-1-1-1'>hello </td><td align='center' class='td11' id='TBL-1-1-2'>   world     </td><td align='right' class='td11' id='TBL-1-1-3'>         right</td></tr>
<tr id='TBL-1-2-' style='vertical-align:baseline;'><td align='left' class='td11' id='TBL-1-2-1'>longer</td> <td align='center' class='td11' id='TBL-1-2-2'>words should</td> <td align='right' class='td11' id='TBL-1-2-3'>show something</td> </tr>
</table>                                                
</div>

相关内容