2

I recently updated my Ubuntu environment from 18.04 to 22.04 and I noticed that the default behavior of tex4ht (or htlatex) with respect to included graphics significantly changed. Consider the basic example ex.tex

\documentclass{article}
\usepackage{graphicx}

\begin{document}
Lorem ipsum dolor sit amet, consectetur $2+3=5$:
\[a=b+c\]
\begin{centering}
\includegraphics[width=0.5\textwidth]{myimage.eps}
\end{centering}
\end{document}

Now the result of the basic conversion command:

htlatex ex.tex "xhtml, charset=utf-8, pic-m, gif", "-cunihtf -utf8"

is the creation of two gif files: ex0x.gif, ex1x.gif (corresponding to formulas) and one png file:myimage.png which is a converted graphics.

However, in Ubuntu 18.04 the result of the same command was completely different - it produced three gifs: ex0x.gif, ex1x.gif, ex2x.gif with the third gif corresponding to included graphics.

This change of default behavior leads to the following problems in my working environment:

  1. There is no longer a common pattern for the names, and event types (gif+png) of graphics generated by htlatex. This complicates the further post-processing of the generated files.
  2. In the old behavior, the image generated form the included graphics has the correct pixel dimension (at least the width). That is the width produced by tex4ht in the <img> tag was the same as the pixel width of the generated file. In the current behavior this is not the case, as the png is generated with fixed density relative to the bounding box. In my case it sometimes is too small, and sometimes is too large.

So I have the following question: is it possible to mimic the old behavior of htlatex, that is to force it to convert the included eps graphics in the same way as other pictures generated by math formulas?


Some more info:

  • I used to generate gif's, but it is ok for me to switch to png's, so the gif option is not important.
  • I do see how tex4ht generates png files from eps graphics - its the following fragment in the source code (tex4ht-html4.tex)
\Configure{graphics*}
   {eps}
   {\openin15=\csname Gin@base\endcsname\PictExt\relax%
   \ifeof15% 
   \Needs{\a:EpsConvert}% 
   \fi%
   \closein15%
   {\Configure{Needs}{File: \Gin@base\PictExt}\Needs{}}%
   \Picture[\a:GraphicsAlt]{{\Gin@base\PictExt} |<graphics dim|>}}

|<graphics default extensions|>
....
\Configure{EpsConvert}{"\a:Ghostscript\space -dSAFER    -dBATCH    -dNOPAUSE    -dEPSCrop    -r\gr:density\space -sDEVICE=pngalpha -sOutputFile="\[email protected]" "\[email protected]" "}

but I somehow can not find the old source code for this conversion (with old behavior).

  • I am not completely sure if this is really a problem with tex4ht or with latex itself (or some of its packages, graphicx?)
  • I can manage the problem with the lack of the common pattern for the generated png files, but the problem with wrong pixel dimensions (widths) of the generated files is really annoying.

1 Answer 1

1

This change happened four years ago. The images included by \includegraphics keep their names in general. As EPS files are not supported on the web, they are converted to PNG or SVG. This conversion happens only once thanks to this condition. It executes the conversion command only when the destination image doesn't exist. This can significantly speed up the conversion process:

\Configure{graphics*}
   {eps}
   {\openin15=\csname Gin@base\endcsname\PictExt\relax%
   \ifeof15% 
   \Needs{\a:EpsConvert}% 
   \fi%
   \closein15%

Regarding image sizes, we get the image dimensions passed from the \includegraphics command, so when you use width=0.5\textwidth, this width is used. There are several ways how to change that, see this how-to.

If you really want to have EPS images converted to files named after the TeX file, you can use the following configuration file:

\Preamble{xhtml}

\makeatletter
\catcode`\:=11
\Configure{graphics*}
   {eps}
   {%
   \gif:nm{}%
   \Needs{"\a:Ghostscript\space -dSAFER    -dBATCH    -dNOPAUSE    -dEPSCrop    -r\gr:density\space -sDEVICE=pngalpha -sOutputFile="\PictureFile" "\[email protected]" "}
   {\Configure{Needs}{File: \PictureFile}\Needs{}}%
   \Picture[\a:GraphicsAlt]{{\PictureFile} \csname a:Gin-dim\endcsname}}
\Configure{Gin-dim}{} 
\Css{img { 
    max-width: 100\%; 
    height: auto; 
}} 
\catcode`\:=12
\makeatother
\begin{document}
\EndPreamble

The \gif:nm declares a new image name, which is then saved in the \PictureFile command. We can use this command in the conversion command and in declaration of the image.

We also removed image dimensions, but we need to declare maximal width of the image using CSS, otherwise the image could overflow the page.

This is the result:

<!-- l. 5 --><p class='noindent'>Lorem ipsum dolor sit amet, consectetur <img alt='2+ 3 = 5  ' class='math' src='sample0x.gif' />:
</p>
   <div class='math-display'>
<img alt='a = b + c
' class='math-display' src='sample1x.gif' /></div>
<!-- l. 6 --><p class='nopar'>  <img alt='PIC' src='sample2x.gif' />
</p>
    

enter image description here

1
  • Thank you very much for a quick response. The solution works perfectly for me. Tex4ht is an extraordinary useful tool and I use it since 2008. I was always amazed that it worked perfectly for so many years with my initial configuration. It was probably the first time I needed to dig deeper into the config files of the system.
    – wynlatex
    Jun 22, 2023 at 11:06

You must log in to answer this question.

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