3

This is related to tex4ht only and not pdf. This is a problem that existed in tex4ht forever.

I just found in HTML that by fixing the width of the whole table, then the column width will remain fixed per p specs.

None of the solutions given before in this forum to make fixed width columns in tex4ht worked well.

So if there is a way to tell tex4ht just to have the whole table of some given fixed width (using say px specs), then this will solve this problem finally.

First will show the problem. Here is MWE

\documentclass[12pt]{article}
\usepackage{hyperref}
\begin{document}
\begin{tabular}{|p{.5in}|p{2in}|p{2in}|p{2in}|p{2in}|}\hline 
\href{somelink}{1234}& second column stuff & third column stuff & 4th column stuff & 5th column stuff\\\hline 
\href{somelink}{1234}& second column stuff & third column stuff & 4th column stuff & 5th column stuff\\\hline 
\end{tabular}
\end{document}

Compiled using the command (notice using p-width)

 make4ht -ulm default -a debug  foo.tex "htm,p-width"

The problem is that, even though there is p specs on the columns, the column width changes when the browser window width is adjusted by the user.

I do not want this. I want the width of each column to remain fixed. If the user wants to see the whole table, they will have to widen the window.

Here is a small video showing what happens

enter image description here

You see how the columns width changes. Here is the generated HTML by tex4ht, I am only showing the part which needed to be changed to fix this problem

<!DOCTYPE html> 
<html lang='en-US' xml:lang='en-US'> 
<head><title></title> 
<meta charset='utf-8' /> 
<link href='foo.css' rel='stylesheet' type='text/css' /> 
<meta content='foo.tex' name='src' /> 
</head><body> 
<div class='tabular'> <table class='tabular' id='TBL-1'>
etc...
 
</body> 
</html>

I could fix this problem by manually editing the HTML file, so the above line

<div class='tabular'> <table class='tabular' id='TBL-1'>

becomes

<div class='tabular'> <table class='tabular' id='TBL-1' table-layout="fixed" width="800px">

Here is the effect now:

enter image description here

You can see that now column widths are fixed per the p specs. This is a side effect of making the whole table of fixed width.

The problem is, how to tell tex4ht to do this and give it the fixed width (which is "800px" in this example) to use? Obviously it is not practical to edit all the HTML files by hand each time and do these changes each time.

I would like to do this per specific table, and not for everything and for all files.

So this is something that needs to be passed to tex4ht from inside the Latex file itself per table at the point of creating the table, and not from my global .cfg file as this could be different for different tables as I do not know what TBL id that will be generated each time by tex4ht and the width can not be hardcoded in some .cfg file.

For example, using some imaginary command like the following

\ifdefined\HCode 
    \makeThisTableFixedWidth{800px} %command for tex4ht
\fi
\begin{tabular}{|p{.5in}|p{2in}|p{2in}|p{2in}|p{2in}|}\hline 
...
\end{tabular}

\ifdefined\HCode 
    \makeThisTableFixedWidth{600px} %command for tex4ht
\fi
\begin{tabular}{|p{.5in}|p{2in}|p{2in}|p{2in}|p{2in}|}\hline 
...
\end{tabular}
...

Is this possible? If this command is not given, then it defaults to how it is now. Basically just need a way to pass to tex4ht that the specific table should be fixed width and give it the actual width value. How this is done depends on which is the easiest way to do it.

TL 2022.

1 Answer 1

1

You need to use a custom environment for this special table:

\documentclass[12pt]{article}
\newenvironment{fixedtabular}[2]{\gdef\tablewidth{#2}\begin{tabular}{#1}}{\end{tabular}}
\usepackage{hyperref}
\begin{document}
\begin{fixedtabular}{|p{.5in}|p{2in}|p{2in}|p{2in}|p{2in}|}{800px}\hline 
\href{somelink}{1234}& second column stuff & third column stuff & 4th column stuff & 5th column stuff\\\hline 
\href{somelink}{1234}& second column stuff & third column stuff & 4th column stuff & 5th column stuff\\\hline 
\end{fixedtabular}
\end{document}

In this example, the fixedtabular environment just passes it's arguments to a regular tabular. But it enables you to use some CSS for this table:

\Preamble{xhtml}
\ConfigureEnv{fixedtabular}{}{
\Css{\#TBL-\TableNo{
table-layout: fixed;
width: \tablewidth;
}}
}{}{}
\begin{document}
\EndPreamble

Here we configure the environment to pass the CSS information for the current table. The \TableNo command contains the table number, used in the id attribute in the generated HTML. We need to use this command in the third argument to \ConfigureEnv, as the second is executed before tabular is placed to the document, the table number would be wrong here.

This is the result:

enter image description here

3
  • Thanks. But this had the hardcoded width in the ,cfg file. Which is common for all my builds. Is there a way to pass the table width (in this example 800px) to the environment at the time the table to created? I tried myself but can't figure the syntax. So something like \newenvironment{fixedtabular}[2]{\begin{tabular}{#1}{800px}}{\end{tabular}} and then the ,cfg will be \Preamble{xhtml} \ConfigureEnv{fixedtabular}{}{ \Css{\#TBL-\TableNo{ table-layout: fixed; width: #2; }} }{}{} \begin{document} \EndPreamble but this does not work.
    – Nasser
    Aug 11, 2022 at 19:08
  • @Nasser see the edit, it should work
    – michal.h21
    Aug 11, 2022 at 19:20
  • Great. Your edit worked very well. This way the same .cfg can now be used for different table width. Thanks.
    – Nasser
    Aug 11, 2022 at 19:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .