2

I'm using tex4ebook to generate an epub. I want subfigures to be placed side-by-side with subcaptions below the subfigures. I came across a couple of issues and I'd appreciate any hints for how to resolve them.

How do I get side-by-side subfigures?

I used recommendations in the answers to these questions to configure and provide css for figures and subfigures: htlatex and subfigures

htlatex and subfigures Using subfigure with tex4ht

This is part of the result I got. The other part is a pair of subfigures that are identical except for the captions.

mwe.cfg

\Preamble{xhtml}

\ConfigureEnv{figure}
   {\ifvmode\IgnorePar\fi\EndP\HCode{<div class="figure">}%
    \bgroup \Configure{float}{\ShowPar}{}{}%
   }
   {\egroup
    \ifvmode\IgnorePar\fi\EndP\HCode{</div>}\ShowPar
\par}
{}{}

\Css{div.caption{text-align:center;font-size:83\%;text-indent:0em;margin-left:2em;margin-right:2em;}}
\Css{div.figure{text-align:center;clear:both;overflow:auto;width:100\%;margin-bottom:1em;}}

\ConfigureEnv{subfigure}
    {\ifvmode\IgnorePar\fi\EndP\HCode
    {<div class="subfigure"\Hnewline>}}
    {\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}
{}{}

\Css{div.subfigure {text-align:center;display:inline-block; max-width:45\%;}}
\Css{.subfigure img{display:block; margin-left:auto;margin-right:auto;}}

\ConfigureEnv{subfigurecaption}
    {\ifvmode\IgnorePar\fi\EndP\HCode{<div class="subfigurecaption">}}
    {\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}
{}{}

\Css{.subfigcaption {margin-top:1em;margin-left:1em;}}

\makeatletter 
\DeclareGraphicsExtensions{.svg,.png}
\ExplSyntaxOn
\Configure{Gin-dim}
    {style="width:\fp_eval:n{round(\Gin@req@width/\textwidth*100,2)}\%;max-height:\fp_eval:n{round(\Gin@req@height/\textwidth*100,2)}\%;"}
\ExplSyntaxOff
\makeatother 

\begin{document}

\EndPreamble

mwe.tex

\documentclass{book}

\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}

\include{included}

\begin{figure}[htbp]
\caption{A pair of frogs}
\centering
\begin{subfigure}[b]{0.45\textwidth}
\caption{Third Frog}
\label{fig:frogone}
\includegraphics[width=0.36\textwidth, keepaspectratio]{./images/simplefrog.png}
\end{subfigure}
\begin{subfigure}[b]{0.45\textwidth}
\caption{Fourth Frog}
\label{fig:frogtwo}
\includegraphics[width=0.36\textwidth, keepaspectratio]{./images/simplefrog.png}
\end{subfigure}
\end{figure}

included.tex

\begin{figure}[htbp]
\caption{Included frogs}
\centering
\begin{subfigure}[b]{0.45\textwidth}
    \centering
    \includegraphics[height=0.36\textwidth, keepaspectratio]{./images/simplefrog.svg}
    \caption{First Frog}
\end{subfigure}
\begin{subfigure}[b]{0.45\textwidth}
    \centering
    \includegraphics[height=0.36\textwidth, keepaspectratio]{./images/simplefrog.svg}
    \caption{Second Frog}
\end{subfigure}
\end{figure}

When I commented out \Css{.subfigure img{display:block; margin-left:auto;margin-right:auto;}} the figures are scaled correctly, but the styling when the subfigures are inserted in the top-level .tex file is different from the styling when they're inserted with \include (the subcaption is beside the subfigure instead of being under it).

That leads me to my second question:

What can I do ensure the styling of subfigures doesn't change when they're inserted with \include?

Here's what the .epub looks like when I comment out the line I mentioned (css for subfigure img) in the .cfg:

enter image description here

mwe.html

<?xml version='1.0' encoding='utf-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns='http://www.w3.org/1999/xhtml'> 
<head>
   <title></title> 
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> 
<meta name='generator' content='TeX4ht (https://tug.org/tex4ht/)' /> 
<meta name='originator' content='TeX4ht (https://tug.org/tex4ht/)' /> 
<!--  charset=utf-8,epub,uni-html4,html,xhtml  --> 
<meta name='src' content='mwe.tex' /> 
<link type='text/css' rel='stylesheet' href='mwe.css' /> 
</head><body>

   <div class='figure'>


<a id='x1-2r1'></a>
<a id='x1-3'></a>
<br />                                                                  <div class='caption'><span class='id'>
Figure 1: </span><span class='content'>Included frogs
</span></div><!-- tex4ht:label?: x1-2r  -->
<div class='subfigure'><!-- l. 7 --><p class='noindent'><img alt='PIC' src='./images/simplefrog.png' style='width:36%;max-height:36%;' /> <a id='x1-4r1'></a>
<span class='cmr-9'>(a) First Frog</span>
</p></div> <div class='subfigure'><img alt='PIC' src='./images/simplefrog.png' style='width:36%;max-height:36%;' /> <a id='x1-5r2'></a>
<span class='cmr-9'>(b) Second Frog</span>
</div>

   </div>


   <div class='figure'>


<a id='x1-6r2'></a>
<a id='x1-7'></a>
<br />                                                                  <div class='caption'><span class='id'>
Figure 2: </span><span class='content'>A pair of frogs
</span></div><!-- tex4ht:label?: x1-6r  -->
<div class='subfigure'><a id='x1-8r1'></a>
<!-- l. 14 --><p class='noindent'><span class='cmr-9'>(a) Third Frog</span>
</p><!-- l. 16 --><p class='noindent'><img alt='PIC' src='./images/simplefrog.png' style='width:36%;max-height:36%;' />
</p></div> <div class='subfigure'><a id='x1-9r2'></a>
<span class='cmr-9'>(b) Fourth Frog</span>
<!-- l. 21 --><p class='noindent'><img alt='PIC' src='./images/simplefrog.png' style='width:36%;max-height:36%;' />
</p></div>

   </div>   
 
</body></html>

6
  • 1
    I've tried your TeX and configuration files and everything looks correctly in Firefox. Sunfigures are displayed one next to other.
    – michal.h21
    Feb 14, 2021 at 10:06
  • I tried my .tex and config file again and the scaling is correct, but the figures are still stacked rather than side-by-side, and the captions of the subfigures inserted with \include are still beside the subfigures rather than appearing beneath them. I took a look at the html file in firefox and the epub and zathura and the positioning of the subfigures and subcaptions is the same in both.
    – avren
    Feb 14, 2021 at 21:06
  • 1
    the captions and pictures are in the order you put them. in the included file you have captions after graphics, in the main tex file you have captions and then graphics.
    – michal.h21
    Feb 14, 2021 at 23:27
  • Thanks for catching that. I corrected my included.tex file and the subcaptions are positioned correctly, but the subfigures are still not side-by-side. I compiled the document using the config file you suggested below. Although when I compiled with the config and tex files I included in my question, for some reason now they're producing the correct output. Perhaps there's something else I'm overlooking.
    – avren
    Feb 15, 2021 at 1:51
  • 1
    I've updated my answer with another version of the .cfg file. I hope it works now,.
    – michal.h21
    Feb 15, 2021 at 11:56

1 Answer 1

1

Your configuration file works for me. I would change it a bit though. First of all, we can add a configuration file for the subcaption package, subcaption.4ht:

\NewConfigure{subfigure}{2}
\ConfigureEnv{subfigure}{\a:subfigure}{\b:subfigure}{}{}
\ConfigureEnv{subtable}{\a:subfigure}{\b:subfigure}{}{}
\Hinput{subcaption}

It defines new configuration hooks subfigure. You can modify the .cfg file to use that:

\Preamble{xhtml}

\ConfigureEnv{figure}
   {\ifvmode\IgnorePar\fi\EndP\HCode{<div class="figure">}%
    \bgroup \Configure{float}{\ShowPar}{}{}%
   }
   {\egroup
    \ifvmode\IgnorePar\fi\EndP\HCode{</div>}\ShowPar
\par}
{}{}

\Configure{subfigure}
   {\ifvmode\IgnorePar\fi\EndP%
     \HCode{<div class="subfigure">}\par\ShowPar
}
   {\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}

\Css{figure.float, div.figure {margin-left: auto; margin-right: auto; text-align:center;}}
\Css{figure.float img, figure.figure img {display: block; margin-left: auto; margin-right: auto;}}
\Css{figure.figure {text-align:center;}}
\Css{figcaption.caption, div.caption  {text-indent:-2em; margin-left:3em; margin-right:1em; text-align:center;}}
\Css{figcaption.caption span.id{font-weight: bold; white-space: nowrap; }}
\Css{div.subfigure {text-align:center;display:inline-block; max-width:45\%;}}

\makeatletter 
\ExplSyntaxOn
 \Configure{Gin-dim}
     {style="width:\fp_eval:n{round(\Gin@req@width/\textwidth*100,2)}\%;max-height:\fp_eval:n{round(\Gin@req@height/\textwidth*100,2)}\%;"}
 \ExplSyntaxOff
\makeatother 

\begin{document}

\EndPreamble

This is the result:

enter image description here

1
  • Unfortunately I'm still getting the same output (stacked subfigures and subcaptions for first set of subfigures appear to the side of the figures).
    – avren
    Feb 14, 2021 at 21:04

You must log in to answer this question.

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