1

I am using the solution given in How to vertically-center the text of the cells? by Jimi Oke, which works well for me:

If you also want to align all the cell contents toward the center in a horizontal sense

\begin{tabular}{ >{\centering\arraybackslash} m{4cm} >{\centering\arraybackslash} m{4cm} }
   ... & ... \\end{tabular}

In pdf, it comes out as expected. But when compiled to html using tex4ht, the columns are not centered. Here is MWE

\documentclass[12pt,titlepage]{article}

\usepackage{amsmath}
\usepackage{array}
\begin{document} 
\begin{center}
\begin{tabular}{| >{\centering\arraybackslash} m{2.5in}| >{\centering\arraybackslash} m{1in}|>{\centering\arraybackslash} m{2.5in}|  }\hline
Original ode in $x,y$ coordinates & Canonical coordinates transformation & ODE in canonical coordinates\\\hline 
$y'=\frac{y}{x}+x$ &
&
$S'=R$\\
text
&
{$\!\begin{aligned}
x& \to R\\
y& \to RS 
\end{aligned} 
$}
&
text\\\hline 
\end{tabular}
\end{center}

\end{document}

Compiled to pdf using lualatex on TL 2022 gives

enter image description here

Compiled to html using

make4ht -ulm default -a debug main.tex "mathjax,htm"

gives

enter image description here

Which is not centered. I know I can use c for column centering and that works in tex4ht, but I needed to use the solution given in the above question for same reasons given there, which is

How is it possible to vertically-center the text of the cells?

question is: Is it possible to make the output centered in HTML using same latex code?

The raw HTML generated by tex4ht is

<!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='main.css' rel='stylesheet' type='text/css' /> 
<meta content='main.tex' name='src' /> 
<script>window.MathJax = { tex: { tags: "ams", }, }; </script> 
 <script async='async' id='MathJax-script' src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js' type='text/javascript'></script>  
</head><body>
<div class='center'>
<!-- l. 60 --><p class='noindent'>
</p>
<div class='tabular'> <table class='tabular' id='TBL-1'><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><tr class='hline'><td></td><td></td><td></td></tr><tr id='TBL-1-1-' style='vertical-align:baseline;'><td class='td11' id='TBL-1-1-1' style='white-space:nowrap; text-align:left; vertical-align:middle;'> <!-- l. 62 --><p class='noindent'>Original ode in \(x,y\) coordinates           </p></td>
                                        <td class='td11' id='TBL-1-1-2' style='white-space:nowrap; text-align:left; vertical-align:middle;'> <!-- l. 62 --><p class='noindent'>Canonical 
coordinates 
transformation </p></td>
                                                          <td class='td11' id='TBL-1-1-3' style='white-space:nowrap; text-align:left; vertical-align:middle;'> <!-- l. 62 --><p class='noindent'>ODE in canonical coordinates        </p></td>
</tr><tr class='hline'><td></td><td></td><td></td></tr><tr id='TBL-1-2-' style='vertical-align:baseline;'><td class='td11' id='TBL-1-2-1' style='white-space:nowrap; text-align:left; vertical-align:middle;'> <!-- l. 63 --><p class='noindent'>\(y'=\frac {y}{x}+x\)                               </p></td>
<td class='td11' id='TBL-1-2-2' style='white-space:nowrap; text-align:left; vertical-align:middle;'> <!-- l. 64 --><p class='noindent'>             </p></td>
<td class='td11' id='TBL-1-2-3' style='white-space:nowrap; text-align:left; vertical-align:middle;'> <!-- l. 65 --><p class='noindent'>\(S'=R\)                              </p></td>
</tr><tr id='TBL-1-3-' style='vertical-align:baseline;'><td class='td11' id='TBL-1-3-1' style='white-space:nowrap; text-align:left; vertical-align:middle;'> <!-- l. 66 --><p class='noindent'>text                                           </p></td>
<td class='td11' id='TBL-1-3-2' style='white-space:nowrap; text-align:left; vertical-align:middle;'> <!-- l. 68 --><p class='noindent'>\(\!\begin {aligned} x&amp; \to R\\ y&amp; \to RS \end {aligned} \)             </p></td>
<td class='td11' id='TBL-1-3-3' style='white-space:nowrap; text-align:left; vertical-align:middle;'> <!-- l. 74 --><p class='noindent'>text                                          </p></td>
</tr><tr class='hline'><td></td><td></td><td></td></tr></table>                                                         </div></div>
 
</body> 
</html>

TL 2022.

1 Answer 1

1

You can simplify your document a bit:

\documentclass[12pt,titlepage]{article}

\usepackage{amsmath}
\usepackage{array}
\newcommand\centercell{\centering\arraybackslash}
\newcolumntype{C}{>{\centercell} m}
\begin{document} 
\begin{center}
\begin{tabular}{| C{2.5in}| C{1in}| C{2.5in}|  }\hline
Original ode in $x,y$ coordinates & Canonical coordinates transformation & ODE in canonical coordinates\\\hline 
$y'=\frac{y}{x}+x$ &
&
$S'=R$\\
text
&
{$\!\begin{aligned}
x& \to R\\
y& \to RS 
\end{aligned} 
$}
&
text\\\hline 
\end{tabular}
\end{center}

\end{document}

I've declared a new command, \centercell, and C column type:

\newcommand\centercell{\centering\arraybackslash}
\newcolumntype{C}{>{\centercell} m}

Thanks to this, you can declare the table in a much simpler way:

 \begin{tabular}{| C{2.5in}| C{1in}| C{2.5in}|  }\hline

Edit:

You can also try this configuration file:

\Preamble{xhtml}


\catcode`\:=11
\makeatletter
\Configure{halignTD} {}{}
   {m}{\HCode{ style="white-space:wrap; text-align:center; vertical-align:middle;"}\Protect\a:HColWidth}
   {}

  \Configure{HColWidth}
     {\ifnum\HRow=1% output CSS only on the first row, to prevent duplication
      \tmp:dim=\HColWidth \divide\tmp:dim by \f@size\relax
      \Css{\#TBL-\TableNo\space td:nth-child(\HCol)% CSS selects ID of the current table column
          {width: \strip@pt\tmp:dim em;}}
     \fi}


\makeatother
\catcode`\:=12

\begin{document}
\EndPreamble

It declares columns with the m specifier to be aligned to the center, and also saves the column width. This is the result:

enter image description here


Original answer:

You can then redefine \centercell command in TeX4ht to insert CSS instructions for the alignment change:

\Preamble{xhtml}
\renewcommand\centercell{%
\Css{\#TBL-\TableNo-\HRow-\HCol, \#TBL-\TableNo-\HRow-\HCol p{text-align: center !important;}}%
\centering\arraybackslash}

\begin{document}
\EndPreamble

The important bit is this:

 \Css{\#TBL-\TableNo-\HRow-\HCol, \#TBL-\TableNo-\HRow-\HCol p{text-align: center !important;}}%

With \#TBL-\TableNo-\HRow-\HCol you get unique ID of each table cell, and then you can define the alignment. We need to use !important, because all table cells have alignment set already.

This is the result:

enter image description here

You must log in to answer this question.

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