How to vertically center an image in table, using p{} and without using m for tex4ht?

Ok, I gave up on this. This is the problem. I'd like to center an image in table columns.

There are solutions for this, for example here but this does not work when I use p for column specifications. Another solution I know that works is using m for all the columns, as shown here but this uses m and I need to use p because I need to use tex4ht to also generate these tables. I just tried makecell package. I can get it to center the image horizontally, (which also works with tex4ht) but do not know how to use this package to center vertically also. Package documentation not clear to me.

The problem is tex4ht does not support m and it ignores the column width. So have to use p but I do not know how center the image using p

So I am looking for solution to center images in columns, but without using m and must be able to use p. This is what I tried (MWE)

\documentclass{article}

\usepackage{mwe}
\usepackage{longtable}
\usepackage{hyperref}
\usepackage{array}
\usepackage{makecell} %for makecell
%\usepackage{blindtext}
\newcommand{\mytext}{bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla}
\begin{document}

\begin{longtable}{|p{1in}|p{1in}|p{2in}|}\hline
\mytext&\mytext&
\raisebox{-.5\height}{\includegraphics[scale=0.25]{example-image}}
\\\hline
\end{longtable}

\begin{longtable}{|p{1in}|p{1in}|p{2in}|}\hline
\mytext&\mytext&
\makecell[cc]{\includegraphics[scale=0.25]{example-image}}
%centered ok, but only horizonatlly, how to make it center vertically?
%\makecell[{{m{2in}}}]{\includegraphics[scale=0.25]{example-image}} %not working
\\\hline
\end{longtable}

\begin{longtable}{|m{1in}|m{1in}|m{2in}|}\hline
\mytext&\mytext&
\includegraphics[scale=0.25]{example-image}
\\\hline
\end{longtable}
\end{document}


pdflatex output

So the m solution works in pdflatex (but not with tex4ht). The \raisebox{-.5\height} works if one does not use p but I need to use p else table will be too wide.

Here is the output from tex4ht (must use p-width option, else tex4ht will ignore p)

 htlatex foo.tex "htm,p-width"


One can see the p worked, but the image is not aligned in center. WIth makecell it is aligned ok, but only horizontally, and With m the image is aligned, but lost the width specification. (isn't Latex fun?)

• Did you think of using the makecell package. Its \makecell defines, by default, cell whose content is horizontally and vertically centred. I don't know if it' compatible with tex4ht, but you might give a try. – Bernard Jun 14 '14 at 22:14
• @Bernard thanks for the sugestion. Never used this. But tried it. and I can only get it to center align horizontally, but not vertically. I'll add what I tried above, may be I am not doing something right. – Nasser Jun 14 '14 at 22:34
• Do you mean it's not vertically centred even in the .pdf? In your table, use the c specifier for the image column, and just type \makecell{\includegraphics{…}}. – Bernard Jun 14 '14 at 22:58
• @Bernard yes. The first image above is the PDF output as you can see. It is only horizontally centered. I used \makecell[cc]{image} but the doc is bit confusing, may be it needs something more.... \makecell{\includegraphics{…}} also only center it horizontally. Not vertically. Vertically centered is more important for me actually. – Nasser Jun 14 '14 at 23:01

I found a solution that works for both tex4ht and pdflatex to vertically align an image in a table cell.

For pdf, I'll use m since it works with little extra effort. For tex4ht I had to resort to using \Css configuration after much trial and error.

Then in the latex file, I check if I am running tex4ht or pdflatex and set things accordingly. Here is the output from the same latex file for pdf and html

Here is the code

\documentclass[11pt]{article}
\usepackage{mwe}
\usepackage{longtable}
\usepackage{array}
\newcommand{\mytext}{bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla}
\usepackage{hyperref}

\begin{document}
\ifdefined\HCode
\Css{td img {display:block; margin: 0 auto;}}
\Css{td {vertical-align: middle;}}
\begin{longtable}{|p{1in}|p{1in}|p{2in}|}\hline
\else
\begin{longtable}{|m{1in}|m{1in}|m{2in}|}\hline
\fi
\mytext &\mytext &
\includegraphics[scale=0.25]{example-image}%
\\\hline
\end{longtable}

\end{document}


To run with tex4ht, the command is

  pdflatex foo.tex


texlive 2013, Linux mint

Here are two solutions – at least to produce a pdf. The first one uses the \makecell and \thead commands from the makecell package, the second one uses the \raisebox command and requires manual adjustment:

\documentclass[11pt]{article}

\usepackage{mwe}
\usepackage{longtable}
\usepackage{array, mathtools}
\usepackage{multirow}
\newcolumntype{P}{>{\centering\arraybackslash}p{2in}}
\usepackage{makecell} %for makecell
\renewcommand{\cellalign}{{p{1in}}c}

\newcommand{\mytext}{bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla}

\usepackage{hyperref}

\begin{document}

\begin{longtable}{|p{1in}|p{1in}|P|}
\caption{Mytable}\\
\hline
\\
\hline
\end{longtable}

\begin{longtable}{|p{1in}|p{1in}|P|}
\caption{Mytable}\\
\hline
{\mytext }&{ \mytext} &\raisebox{-1.6\totalheight}[0\height][\height]{\includegraphics[scale=0.25]{example-image}}
\\
\hline
\end{longtable}

\end{document}


• Thanks. But this did not work with tex4ht which is what I wanted. This is what I get from htlatex foo.tex "htm,p-width" !Mathematica graphics – Nasser Jun 15 '14 at 2:51
• I guess it's due to the limitations of tex4ht. I've updated my answer with a second solution, that requires manual adjustment, but uses \raisebox and might work with tex4ht. – Bernard Jun 15 '14 at 10:45
• No, it still does not center it in tex4ht !Mathematica graphics You can try that yourself, tex4ht is part of standard latex distribution, so you should have it along with pdflatex in your distribution? – Nasser Jun 15 '14 at 10:56
• I've just tested it. I'm afraid the only solution would be to use a pdf to html converter. You can do that online. – Bernard Jun 15 '14 at 11:27