Skip to content

Instantly share code, notes, and snippets.

@michal-h21
Created April 2, 2022 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michal-h21/03cf7696f7e82f28d60d0ccd3b170ae2 to your computer and use it in GitHub Desktop.
Save michal-h21/03cf7696f7e82f28d60d0ccd3b170ae2 to your computer and use it in GitHub Desktop.
Configuration for TeX4ht that uses <th> tags for table header
\Preamble{xhtml}
\catcode`\:=11
% configuration for tabular with support of change of table cell tag
% this code comes from TeX4ht sources
% default table cell tag:
\def\tablecelltype{td}
\Configure{tabular}
{\ifvmode \IgnorePar\fi \EndP \halignTB{tabular}\putVBorder }
{\HCode{</table>}}
{\HCode{<tr \Hnewline}\halignTR
\HCode{ id="TBL-\TableNo-\HRow-">}}
{\r:HA}
{\HCode{<\tablecelltype \ifnum \HMultispan>1 colspan="\HMultispan"\fi}%
\halignTD \HCode{ id="TBL-\TableNo-\HRow-\HCol"
\Hnewline class="td}\NoFonts
\bgroup
\ifx \ttfamily\:UnDef \else \ttfamily\fi
\ColMargins
\egroup
\EndNoFonts
%
\HCode{">}\SaveEndP
%
\par \ShowPar
\PushStack\Table:Stck\TableNo}
{\IgnorePar \EndP
\PopStack\Table:Stck\TableNo \d:HA}
\def\d:HA{\ifvmode \IgnorePar \fi \EndP\HCode{</\tablecelltype>}\RecallEndP}
\catcode`\:=12
% change
\ConfigureEnv{tablewithheader}{%
\gdef\tablecelltype{th}%
\AddToHookNext{cmd/hline/after}{\noalign{\gdef\tablecelltype{td}}}%
}{}{}{}
\begin{document}
\EndPreamble
<!DOCTYPE html>
<html lang='en-US' xml:lang='en-US'>
<head> <title></title>
<meta charset='utf-8' />
<meta content='TeX4ht (https://tug.org/tex4ht/)' name='generator' />
<meta content='width=device-width,initial-scale=1' name='viewport' />
<link href='sample.css' rel='stylesheet' type='text/css' />
<meta content='sample.tex' name='src' />
</head><body>
<div class='tabular'> <table class='tabular' id='TBL-1'><colgroup id='TBL-1-1g'><col id='TBL-1-1' /><col id='TBL-1-2' /></colgroup><tr id='TBL-1-1-' style='vertical-align:baseline;'><th class='td11' id='TBL-1-1-1' style='white-space:nowrap; text-align:left;'>first </th><th class='td11' id='TBL-1-1-2' style='white-space:nowrap; text-align:left;'>second</th></tr><tr class='hline'><td></td><td></td></tr><tr id='TBL-01-4-' style='vertical-align:baseline;'><td class='td11' id='TBL-01-4-1' style='white-space:nowrap; text-align:left;'>third</td><td class='td11' id='TBL-01-4-2' style='white-space:nowrap; text-align:left;'>fourth </td>
</tr><tr id='TBL-01-5-' style='vertical-align:baseline;'><td class='td11' id='TBL-01-5-1' style='white-space:nowrap; text-align:left;'>fifth </td><td class='td11' id='TBL-01-5-2' style='white-space:nowrap; text-align:left;'>sixth </td> </tr></table></div>
</body>
</html>
\documentclass{article}
\newenvironment{tablewithheader}[1]{%
\begin{tabular}{#1}%
}{\end{tabular}}
\begin{document}
\begin{tablewithheader}{ll}
first & second\\
\hline
third & fourth\\
fifth & sixth
\end{tablewithheader}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment