7

When I introduce graphics with \includegraphics[width=0.5\textwidth]{fig}, tex4ht processes this into some number of pixels (I'm not sure how - presumably there's a ppi setting I've not encountered?) and produces a <img src="..." width="276" height="276"> for example, but what would seem natural for me would be, say, <img src="..." style="width: 50%;">.

There was a sort of similar question previously -- in this answer, michal.h21 gives an example where these widths appear as "276pt" (or similar), but
(1) I don't see this when I compile a test -- I just see numbers, which I understand should be in px -- though of course that answer was 6 years go;
(2) this doesn't appear to be legal html anyway; and
(3) neither px nor pt values scale with the viewport (or, perhaps more pertinently, the styled size of the surrounding figure).

So this leads me to the question: is there a way to directly use relative image sizes with tex4ht?

1 Answer 1

7

Edit: The code bellow is now built-in to TeX4ht sources, you can require it using the Gin-percent option. It may take some time to be included in TeX Live though.

That linked answer is really old and obsolete. You can use \Configure{Gin-dim} command to change the way how image dimensions are calculated now. By default, TeX4ht relies on information about image dimensions provided by the Graphics package. So if you use explicit dimensions (like width=0.5\textwidth), the actual dimension calculated by TeX is used.

One problem is that if you set only one dimension, for example width, the other dimension will be set to the same value. You usually don't want this, unless your image is a square. To get the correct value for all dimensions, Graphics uses a .xbb file for image. It can be created using the following command:

ebb -x *.jpg

Run analogous command for every other supported image format you use. This will ensure that correct values are used for implicitly calculated dimensions.

Now, to your actual question. Thanks to the LaTeX 3 project, we can now use the l3fp package to calculate the image dimensions in percents. It is much easier to use than \dimexpr command, which has lot of limitations.

Try the following .cfg file:

\Preamble{xhtml}
\makeatletter
\ExplSyntaxOn
\Configure{Gin-dim}{style="width:\fp_eval:n{round(\Gin@req@width/\textwidth*100,2)}\char_generate:nn { `\% } { 12 }"}
\ExplSyntaxOff
\makeatother
\begin{document}
\EndPreamble

In this configuration, we divide the image width provided by Graphics by text width. This is then multiplied to get the correct percent value.

For this sample file:

% https://tex.stackexchange.com/q/563276/2891
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics[width=0.5\textwidth]{example-image.png}
\end{document}

You get this HTML code:

<p class='noindent'><img style='width:50%' alt='PIC' src='example-image.png' /> </p> 
11
  • 1
    This works very well, and thanks for the clarification about image sizes, too -- I did wonder about the dimensions being identical. One change I might suggest to this answer is to round -- I got width:60.00061035156249% when I tried it on an example with 0.6\textwidth. Rounding to 2 decimal places: round ( \Gin@req@width/\textwidth*100 , 2 ) makes this a bit more legible. Commented Sep 19, 2020 at 13:33
  • 1
    @frougon thanks for your comment, it seems useful. I've updated my answer with this code.
    – michal.h21
    Commented Jan 28, 2022 at 12:22
  • 1
    @CebişMellim I can add is as a command line option
    – michal.h21
    Commented Oct 22, 2022 at 20:43
  • 1
    @CebişMellim I think I will need a MWE for this error, I cannot reproduce it
    – michal.h21
    Commented Oct 23, 2022 at 15:13
  • 1
    @CebişMellim I've just added this to TeX4ht sources, it is available using the Gin-percent option.
    – michal.h21
    Commented Oct 23, 2022 at 18:56

You must log in to answer this question.

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