6

Is it possible to have htlatex generate side-by-side subfigures? The mwe tex file

\documentclass{article}

\ifdefined\HCode
\usepackage[compatibility=false]{caption}
\def\pgfsysdriver{pgfsys-tex4ht.def}
\else
\usepackage[]{caption}
\fi
\usepackage{subcaption}
\usepackage[]{graphicx}

\begin{document}

\begin{figure}[htpb]
  \centering
  \begin{subfigure}{0.45\textwidth}
    \centering
    \includegraphics[width=\textwidth]{image1.png}
    \caption{Image 1}
  \end{subfigure}
  \begin{subfigure}{0.45\textwidth}
    \centering
    \includegraphics[width=\textwidth]{image2.png}
    \caption{Image 2}
  \end{subfigure}
  \caption{Main Caption}
\end{figure}

\end{document}

together with the config file

\Preamble{html}

\Configure{@HEAD}{\HCode{<link href="basic.css" rel="stylesheet" type="text/css" />\Hnewline}} 

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

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

\begin{document}

\makeatletter
% Various helper functions
% default font size
\newcommand\emwidth{16}
\let\emwidth\f@size
% convert pt to rem
\newcommand\CalcRem[1]{\strip@pt\dimexpr(#1)/\emwidth}

\Configure{graphics*}
{png}
{% we must add the image to the list of output files
  \special{t4ht+@File: \csname Gin@base\endcsname.png}
  \Picture[pict]{\csname Gin@base\endcsname\PictExt
  \space style="width:\CalcRem{\Gin@req@width}em;"
  }%  
}
\makeatother

\EndPreamble

generates

output

I have read this threads:

But, I cannot piece together how I might achieve the desired result. I have included the graphics configuration in the .cfg file because it is important that I be able to scale the images.

7

Try this config file:

\Preamble{xhtml}

\Configure{@HEAD}{\HCode{<link href="basic.css" rel="stylesheet" type="text/css" />\Hnewline}} 
\DeclareGraphicsRule{.png}{bmp}{.xbb}{}

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

\ConfigureEnv{subfigure}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="subfigure">}}{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}{}{}
\Css{.subfigure{display: inline-block;}}
\Css{.subfigure img{display:block;}}

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

\begin{document}

\makeatletter
% Various helper functions
% default font size
\newcommand\emwidth{16}
\let\emwidth\f@size
% convert pt to rem
\newcommand\CalcRem[1]{\strip@pt\dimexpr(#1)/\emwidth}

\Configure{graphics*}
{png}
{% we must add the image to the list of output files
  \special{t4ht+@File: \csname Gin@base\endcsname.png}
  \Picture[pict]{\csname Gin@base\endcsname\PictExt
  \space style="width:\CalcRem{\Gin@req@width}em;"
  }%  
}
\makeatother

\EndPreamble

The important changes are:

\DeclareGraphicsRule{.png}{bmp}{.xbb}{}

this will enable to use correct bounding box for the images. See this answer for details.

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

I've added \ifvmode\IgnorePar\fi\EndP for correct handling of paragraphs. See Control the <p> tags inserted by tex4ht for details.

Now the actual code for subfigure:

\ConfigureEnv{subfigure}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="subfigure">}}{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}{}{}
\Css{.subfigure{display: inline-block;}}
\Css{.subfigure img{display:block;}}

This add <div class="subfigure"> element and needed CSS configurations. The first one will make subfigures float next themselves, the second will put caption bellow the image.

The result:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.