# tex4ht: How to create fixed width table columns

I used tex4ht.

I need to create longtable with fixed width of column. However specified width is ignored.

Example:

\documentclass[9pt,oneside,a4paper,english,notitlepage]{book}
\usepackage{longtable}
\usepackage{array}

\begin{document}
\begin{longtable}{|l|p{10cm}|m{5cm}} \\\hline
First & Second text cell with width of 10 cm& Third cell with width of 5 cm\\ \hline
\end{longtable}
\end{document}

What is wrong? Could anyone help me?

• Do you get this issue in your html? Please show a screenshot. When compiling this with pdfLaTeX, it works just fine. Please note that you are provoking an error in your last column (tex.stackexchange.com/q/68732). At least, I guess you do not want this behaviour. – LaRiFaRi Jun 8 '15 at 14:07
• I added screenshot. When I compile this table for pdf, it works well. However for html I get a trouble. – Yulia Spryn Jun 9 '15 at 7:13

You can use p-width command line option to save width of p columns:

htlatex filename "xhtml,p-width"

This doesn't support m columns by default, but you can add support for them easily with this configuration file, my.cfg:

\Preamble{xhtml,p-width}
\catcode\:=11
\Configure{halignTD} {}{}
{<}{\HCode{ style="white-space:nowrap; text-align:left;"}}
{-}{\HCode{ style="white-space:nowrap; text-align:center;"}}
{>}{\HCode{ style="white-space:nowrap; text-align:right;"}}
{^}{\HCode{ style="vertical-align:top; white-space:nowrap;"}}
{=}{\HCode{ style="vertical-align:baseline; white-space:nowrap;"}}
{||}{\HCode{ style="vertical-align:middle; white-space:nowrap;"}}
{_}{\HCode{ style="vertical-align:bottom; white-space:nowrap;"}}
{p}{\HCode{ style="white-space:wrap; text-align:left;"}\Protect\a:HColWidth}
{m}{\HCode{ style="white-space:wrap; text-align:left; vertical-align:middle;"}\Protect\a:HColWidth}
{b}{\HCode{ style="white-space:wrap; text-align:left; vertical-align:baseline;"}}
{}
\catcode\:=12
\begin{document}

\EndPreamble

the magic is in \Protect\a:HColWidth. Because p-width option is inserted in the \Preamble command, you don't need to specify it on the command line anymore. Compile with

htlatex filename my

the result:

Because tex4ht redefines lot of things, it seems that we don't have access to dimensions of p columns. The only way to style this is using custom .cfg file and CSS styles. Your sample creates such HTML table:

<table id="TBL-1" class="longtable"
><colgroup id="TBL-1-1g"><col
id="TBL-1-1" /></colgroup><colgroup id="TBL-1-2g"><col
id="TBL-1-2" /></colgroup><colgroup id="TBL-1-3g"><col
id="TBL-1-3" /></colgroup>
...

important are <col id="TBL-1-2" /> elements, because you can use them for styling columns. The id is construed as TBL + number of table + number of column. So this is second column of first table in the document. Now you can easily style that with such .cfg file (like hello.cfg):

\Preamble{xhtml}
\Css{\#TBL-1-2{width:10cm;}}
\Css{\#TBL-1-3{width:5cm;}}
\begin{document}
\EndPreamble

compile it with command:

htlatex filename hello

and the result:

note that you should style tables after your document is done, because if you add new table before some other table which does have styling already, the style would be used for another table!

• This way is not good for me, because content of my document is not stable, and I cannot know how many tables I will have. Maybe there is a way to redefine something in tex4ht before compilation? – Yulia Spryn Jun 9 '15 at 14:50
• How compatible would this answer be with odt compilation as opposed to xhtml? Adding the p-width to the preamble and the block in between the catcodes didn't work for me, but maybe there's just something simple missing. Is this actually more complex? Should this be it's own question? Thanks! – EngBIRD Feb 10 '19 at 21:57
• @EngBIRD I don't think the support is here out of the box. ODT doesn't use CSS, so some other mechanism needs to be used. I would need to investigate that. – michal.h21 Feb 10 '19 at 23:04