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 Answers1

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> 
michal.h21
  • 50,697
  • 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. – Alex Watson Sep 19 '20 at 13:33
  • @AlexWatson good suggestion, I've added rounding to my answer – michal.h21 Sep 19 '20 at 14:03
  • Hi @michal.h21, your answer has been extremely helpful (and upvoted long ago!) when I wrote getstart-html.cfg for the FlightGear manual (PDF, HTML), but I found a little bug in this: \Configure{Gin-dim}{style="width:\fp_eval:n{round(\Gin@req@width/\textwidth*100,2)}\%"}. I suggest using \char_generate:nn { `\% } { 12 } instead of \%, because... – frougon Jan 28 '22 at 11:56
  • ... in some cases (e.g., with babel-spanish), \% may have been redefined to include TeX typesetting commands, which web browsers dont appreciate very much (e.g., with \%, one can obtain an HTML attribute like style='width:60\protect \unhbox \voidb@x \unskip $\mathsurround (...)') :-). – frougon Jan 28 '22 at 11:57
  • 1
    @frougon thanks for your comment, it seems useful. I've updated my answer with this code. – michal.h21 Jan 28 '22 at 12:22
  • I wish this would be the default behaviour as this seems the natural logic to implement. – qarabala Oct 22 '22 at 19:44
  • 1
    @CebişMellim I can add is as a command line option – michal.h21 Oct 22 '22 at 20:43
  • That would be truly great! Also considering (1) how many views this question got and that (2) the above gives complains in the .log file. – qarabala Oct 22 '22 at 21:18
  • @michal.h21 the code that you provided works very well. I am wondering though why is LaTeX telling me that I am in trouble:
    l.7 --- TeX4ht warning --- \Configure{Gin-dim}? ---
    ! LaTeX Error: Missing \begin{document} in `...'.                        
    l.7 \Configure{Gin-dim}{style="width:\fp_eval:n{round(\Gin@req@width/\textwi...
    You're in trouble here.
    ! Undefined control sequence.
    <argument> \Gin@req@width 
    l.7 ...l:n{round(\Gin@req@width/\textwidth*100,2)}\char_generate:nn { `\% } ...
    The control sequence at the end of the top line
    of your error message was never \def'ed.
    
    – qarabala Oct 23 '22 at 14:33
  • 1
    @CebişMellim I think I will need a MWE for this error, I cannot reproduce it – michal.h21 Oct 23 '22 at 15:13
  • 1
    @CebişMellim I've just added this to TeX4ht sources, it is available using the Gin-percent option. – michal.h21 Oct 23 '22 at 18:56