How does tex4ebook work with images (png, jpg)?

After finish writing a book in LaTeX, I learned the many online books are published in the ePub format. Then I decided to learn a little bit of how to convert .tex (or .pdf) into .epub. The book has equations (including align), figures (.png,.eps), tables and TikZ. I use TeXstudio as editor and MikTeX on Windows.

First I tried Pandoc. Quickly I learned that .pdf -> .epub does not seem to be a viable route. Than I concentrated my effort into .tex -> (some route) -> .epub. I did not find any route or combination of flags to make Pandoc work properly.

I've seen and tested (at least tried) some options:

1. latexml: Use LaTeX to produce Epub -- it seemed to be and still is a bit confusing to me;
2. htlatex: How to Convert a LaTeX ebook to ePUB (LaTeX Advanced Tutorial-22) -- it worked well, (bonus for "User Command" in TeXstudio), but the ePub through Calibre will not work if the .html file has some issue.
3. tex4ebook: The tex4ebook package -- it achieved the best results.

The base command: tex4ebook %.tex, where % is the name of the file (suitable for quick user command in TeXstudio), worked quite well.

tex4ebook worked (incredible better than the first options I tried) for the following features:

1. Equation, including the math operators outside its environment: "$...$", "(...)" and "[ ... ]";

2. Tables;

3. Hyperref/url -- all links are working;

4. TikZ with the sintax

\ifdefined\HCode
\def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi


derived from tex4ebook: nodes vanishes for TikZ? Alternative TikZ drivers for tex4ht with the .css file from FriendsOfEpub/Blitz;

1. So far so good until I tried to test figures.

I can insert .eps figures, but I have no ideia of how I can insert other types such as .jpg and .png.

How are figures inserted in the ePub through tex4ebook?

Which also includes:

1. Does graphics and graphicx make any difference?;
2. Should I always insert the extension of the files? For pdflatex it is not necessary;
3. Is there any preferable type of figure file to ePub?

I tried to follow the steps in

Compiling the .tex with some options did not helped:

tex4ebook -l -t -c config_file.cfg %.tex

1. -l: LuaTeX just showed me messages more clear;
2. -t: Tidy, I did not understand its usage, and how to call it from anyfolder;
3. -c: that's a point that intrigues me.

I found some config files but I cannot say exactly what they are doing. I do not know if I'm missing some file, such as the .css files at the command \Configure in some examples; or something related to make4ht; or if the problem derives from the usage or not of LuaTeX; or the usage of Tidy.

Sometimes I receive the following message (one for each figure in different coordinates):

dvipng warning: at (31,156) unimplemented \special{em: graph any.jpg,72bp,72bp}


independent of using a .cfg or not. I have noticed that a .png file is created of some kinds of objects such as equation, math symbols and the .eps figures, but to the .jpg and .png the figures are always empty. I would speculate that it might have some relation of the option width or how it is understood in tex4ebook, but the examples I searched didn't give me any hint.

Below follows a MWE with some options I've tried so far and the resulting ePub. Note: "any", "some" and "fine" are generic filenames.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{epstopdf}
% \usepackage{tex4ebook} % Doesn't seem to make any difference for png jpg
\begin{document}

1 -- includegraphics alone.

\includegraphics{any.jpg}

2 -- includegraphics alone, but with parameters, jpg.

\includegraphics[width=1\linewidth]{any.jpg}

3 -- includegraphics png must have the explicit extension to work ate the \texttt{tex4ht} and with width.

\includegraphics[width=1\linewidth]{some.png}

\includegraphics[width=0.5\linewidth]{some.png}

4 -- Inside figure environment

\begin{figure}[!htb]
\centering
\includegraphics[width=0.8\linewidth]{any.jpg}
\caption{Figure inside figure environment.}
\end{figure}

4.1 -- Inside figure environment

\begin{figure}[!htb]
\centering
\includegraphics[width=600pt]{any.jpg}
\caption{Figure inside figure environment width pt.}
\end{figure}

5 -- eps figure

\includegraphics[width=1\linewidth]{fine.eps}

5.1 -- eps figure

\begin{figure}[!htb]
\centering
\includegraphics[width=1\linewidth]{fine.eps}
\caption{Figure eps inside figure environment width.}
\end{figure}

\end{document}


• you are presumably using latex, dvips internally, which basically only supports eps, but you can use imagemagic convert or similar to convert any png or jpg to eps before you start so that should not be an issue. – David Carlisle Jan 24 '20 at 1:11
• I think this might be related to this issue: tex.stackexchange.com/a/517901/2891 – michal.h21 Jan 24 '20 at 8:05
• michal.h21 (You are the genius behind tex4ebook, it is a pleasure to talk with you), I have good and bad news. I tested and the 'graphic.4ht' enables png and jpg into epub but, it blocks eps. Without 'graphic.4ht' eps are in and png/jpg are out. Is this the "You cannot have both" kind of situation? – FHZ Jan 24 '20 at 15:24
• David Carlisle, I converted the png and jpg into eps, it worked at the example (without graphic.4ht), but the figures fo the book didn't appear on its epub. I will verify if there is any other mistake or detail. – FHZ Jan 24 '20 at 15:31
• thanks for your kind words :) both png end eps should work. maybe try to run tex4eboo with -a debug option, it should report report errors. eps file needs to be converted to something that can be included in Epub first (png, svg), so maybe the conversion fails on your system. There should be some info near the end of the compilation. – michal.h21 Jan 24 '20 at 15:49

It seems that MikTeX version of TeX4ht is not up-to date. It can happen as TeX4ht is installed directly to TeX Live, the updates are not installed on CTAN, so MikTeX maintainer are not notified of updates. I've made a request on MikTeX issue tracker for the update, so these issues will be hopefully fixed soon.

In the meantime, we can fix your issues using local configurations. You've already managed to fix the issues with bitmap pictures, so we just need to fix the EPS support.

Try the following configuration file:

\Preamble{xhtml}
\begin{document}
\makeatletter
\Configure{graphics*}
{eps}
{\openin15=\csname Gin@base\endcsname\PictExt\relax%
\ifeof15%
\Needs{\csname a:EpsConvert\endcsname}%
\fi%
\closein15%
{\Configure{Needs}{File: \Gin@base\PictExt}\Needs{}}%
\Picture[\a:GraphicsAlt]{{\Gin@base\PictExt} \csname a:Gin-dim\endcsname
}}
\NewConfigure{EpsConvert}{1}
\Configure{EpsConvert}{"rungs -dSAFER -dBATCH    -dNOPAUSE    -dEPSCrop    -r120  -sDEVICE=pngalpha -sOutputFile="\Gin@base.png" "\Gin@base.eps" "}
\makeatother
\EndPreamble


It is basically EPS support code from TeX4ht sources. \Configure{graphics*}{eps} contains code for inclusion of the EPS files. It checks for the existence of a bitmap version of the included file, in order to prevent unnecessary conversions when it already exists. The \Needs command executes the command for the conversion. It is Ghostscript in our case. It should be available in both MikTeX and TeX Live as rungs command.

This is the result:

• It worked. I had to run an update in MikTeX Console (My last one was in Nov 2019), because there was no rungs.exe on my PC. After the update, the MWE created a correct epub with jpg, png and eps figures with the config file you suggested. After the first successful compilation, I tried it without the cfg, it worked. I deleted all other files, except the previous graphics.4ht, and it worked without the cfg. The conclusion leads to the issue in the missing rungs.exe instead of the need of your cfg workaround. What do you think? Next step is to apply all of this to the whole book. – FHZ Jan 24 '20 at 23:27
• It is possible that you had obsolete files in your Miktex distro and the update installed the correct ones. There were other issues than just missing rungs in your post. – michal.h21 Jan 24 '20 at 23:41
• I see. So it would probably be impossible to convert LaTeX -> ePub two months ago? I mean, because the features i used without knowing any restriction of the ePub format. Or at least, it would be necessary a lot more workarounds. Once again thank you @michal.h21. – FHZ Jan 24 '20 at 23:48
• @FHZ no it was possible, just the image conversion was broken because of the update of LaTeX core – michal.h21 Jan 26 '20 at 12:20