# Setting image size using em units instead of pt

I have found this question very helpful in my configuration of images. Inspired by tex4ht's capabilities (it's so much more powerful than I thought!) I'm wondering if I could have image width configured in em units instead of pt units.

Here are examples of my tex and cfg files:

mwe.tex:

\documentclass{article}
\usepackage[]{graphicx}
\usepackage[]{amsmath}
\begin{document}
EPS file (0.3 textwidth)
\begin{center}
\includegraphics[width=0.3\textwidth]{plot.eps}
\end{center}
PNG file: (0.5 textwidth)
\begin{center}
\includegraphics[width=0.5\textwidth]{plot.png}
\end{center}
\end{document}


my_mwe.cfg:

\Preamble{html}
\Configure{graphics*}
{eps}
{%
\Picture[pict]{\csname Gin@base\endcsname\PictExt
\space width="\the\dimexpr \expandafter\the\csname Gin@req@width\endcsname *3/2"
}%
}
\Configure{graphics*}
{png}
{%
\Picture[pict]{\csname Gin@base\endcsname .png
\space width="\the\dimexpr \expandafter\the\csname Gin@req@width\endcsname *3/2"
}%
}
\begin{document}
\EndPreamble


Running

htlatex mwe.tex "my_mwe.cfg"


produces images with widths of 155pt and 258pt, respectively, which look great in my pc browser but not a tablet or phone browser: I'd much rather have the width in em units or percentage. (As a follow up to a comment on the linked question, I found that "...\endcsname *1.5" did not work, but "...\endcsname *3/2" does.)

Being originally drawn to tex4ht in order to produce both html and pdf documents, I'd like to be able to take advantage of the responsiveness of html.

Is there hope that this is configurable? Thanks.

I've created simple macro for calculating em value, you just need to divide the dimension by fontsize:

% default font size
\let\emwidth\f@size
% convert pt to rem
\newcommand\CalcRem[1]{\strip@pt\dimexpr(#1)/\emwidth}


\emwidth is set to be the fontsize, parameter for \CalcRem is dimension, but it may be also an expression. \strip@pt is used to output the value and strip pt suffix, which would be added if we used \the to output the value.

Now you can change configurations for images:

\Preamble{html}
\begin{document}
\makeatletter
% Various helper functions
% default font size
\newcommand\emwidth{16}
\let\emwidth\f@size
% convert pt to rem
\newcommand\CalcRem[1]{\strip@pt\dimexpr(#1)/\emwidth}

\Configure{graphics*}
{png}
{% we must add the image to the list of output files
\special{t4ht+@File: \csname Gin@base\endcsname.png}
\Picture[pict]{\csname Gin@base\endcsname\PictExt
\space style="width:\CalcRem{\Gin@req@width}em;"
}%
}
\Configure{graphics*}
{eps}
{% you need to coonvert eps to png first
\Needs{"convert \csname Gin@base\endcsname.eps
\csname Gin@base\endcsname.png"}%
\special{t4ht+@File: \csname Gin@base\endcsname.png}
\Picture[pict]{\csname Gin@base\endcsname .png
\space style="width:\CalcRem{\Gin@req@width}em;"
}%
}
\makeatother
\EndPreamble


as you can see, css value width is used instead of width attribute, because it support only values in pixels.

The result (I used some dummy picture I found in my computer):

<!--l. 7--><p class="noindent" ><img
src="demo_carenv.png" alt="pict"
style="width:10.35011em;" ></div>
<!--l. 9--><p class="noindent" >PNG file: (0.5 textwidth)
<div class="center"
>
<!--l. 10--><p class="noindent" >
<!--l. 11--><p class="noindent" ><img
src="demo_carenv.png" alt="pict"
style="width:17.25em;" ></div>


• Thank you, I'm so pleased it can be done. I suspected there would be a way, but I had no knowledge of commands such as strip@pt, although I see now I could have learned about it at tex.stackexchange.com/questions/2810/…. I also now realize my question could have been better asked, as it was not hard for me to learn about \the\dimexp and Gin@req@width. So thanks for your patience and for the solution. – user59959 Aug 10 '14 at 23:54
• There are still some issues with the solution. The main one is that it picks the scale as width. I compiled \includegraphics[scale=0.7]{d1.png} and it seems to have took the 0.7 above as the width, so the resulting image in HTML was very small, with "width:4.21573em;" I did not really want this solution to affect scale since I have not yet changed all my documents to use width and now all my images are coming up very small in HTML. Another issue that should be mentioned in the solution above, is that this works for 12pt only due to the extra decimal point (per our chat). Thank you. – Nasser Aug 12 '14 at 23:27
• What I mean is that the solution above works well when using \includegraphics[width=0.7\paperwidth]{d1.png} but when using \includegraphics[scale=0.7]{d1.png} then it should leave this alone and not apply the 0.7 above as the width. For some reason this causes the image to come out too small in HTML. (btw, this solution is really useful and should be actually integrated in Tex4ht itself.) – Nasser Aug 12 '14 at 23:37
• @Nasser this has nothing to do with calculation, latex doesn't report image size in dvi mode, which is mode tex4ht uses. You can generate bouding box info file with ebb imagename command and use \usepackage[dvipdfm]{graphicx} for using this info file – michal.h21 Aug 13 '14 at 12:33
• Why is it that this works only when font size is 12pt? I learned the hard way about the extra decimal point, but I don't understand it. At the moment I'll use 11pt for pdflatex and 12pt for tex4ht, but is there a way of making the solution work for other font sizes? – user59959 Sep 18 '14 at 12:24