# Configuring htlatex for nested minipages or minipages outside floating environments

I am trying to do something similar to this post on configuring minipages with htlatex. However, my minipages are not inside a figure environment, so the strategy to apply a float: right css to div.minipage:last-child is no longer viable.

I am using the exact .cfg file from the answer above and my LaTeX document has a format like this:

\documentclass{article}
\usepackage{blindtext}
\begin{document}
\noindent Text before.\medskip

\noindent
\begin{minipage}[t]{0.4\textwidth}
\blindtext
\end{minipage}
\hfill
\begin{minipage}[t]{0.5\textwidth}
\blindtext
\end{minipage}

\medskip \noindent Text after
\end{document}


Naturally, the second minipage does not float right because it is not a child of any element in the DOM.

So, for the lack of a better idea, I tried nesting the minipages

\documentclass{article}
\usepackage{blindtext}
\begin{document}
\noindent Text before.\medskip

\noindent
\begin{minipage}[t]{1.0\textwidth}
\begin{minipage}[t]{0.4\textwidth}
\blindtext
\end{minipage}
\hfill
\begin{minipage}[t]{0.5\textwidth}
\blindtext
\end{minipage}
\end{minipage}

\medskip \noindent Text after
\end{document}


That almost worked as expected, except that in the generated CSS, minipage1, the outermost minipage, has the same width as minipage3, the second child minipage. The width should be 100%, but it is not.

#minipage2{width:39.99939%;}
#minipage3{width:50%;}
#minipage1{width:50%;}


Update: this version of the configuration file contains but, the fixed version can be found here.

Try this configuration file:

\Preamble{xhtml}
\makeatletter
% to strip fraction from \textwidth
\def\striptextwidth#1\textwidth{#1}
% we must refer to minipage from the css file, because tags are beeing
% written before we know dimensions
\newcount\mini@count
% save original minipage
\let\oldiimini\@iiiminipage
% redefine minipage
\def\@iiiminipage#1#2[#3]#4{%
% calculate dimensions and save it to macro
\edef\miniwidth{\strip@pt\dimexpr(\striptextwidth#4pt)*100\relax\%}
\Css{\#minipage\the\mini@count{width:\miniwidth;}}%
\oldiimini{#1}{#2}[#3]{#4}
}

\ConfigureEnv{minipage}{\advance\mini@count by 1\relax\ifvmode\IgnorePar\fi\EndP\HCode{<div class="minipage" align="center" id="minipage\the\mini@count" style="border:1px solid black;">}}
{\ifvmode\IgnorePar\fi\EndP\HCode{</div>\Hnewline}%
% we must write dimension here to the css file
}{}{}
\makeatother
\Css{div.minipage {
float: left;
}
}
\Css{div.minipage:last-child {
clear: none;
float: right;
}
}
\Css{ div.minipage + :not(.minipage) {clear:both;overflow:auto;width:100\%;}}
\begin{document}
\EndPreamble


I've changed only two things:

The dimensions of the current minipage are written to the CSS file earlier:

\def\@iiiminipage#1#2[#3]#4{%
% calculate dimensions and save it to macro
\edef\miniwidth{\strip@pt\dimexpr(\striptextwidth#4pt)*100\relax\%}
\Css{\#minipage\the\mini@count{width:\miniwidth;}}%
\oldiimini{#1}{#2}[#3]{#4}
}


The issue with the original version was that the \miniwidth command is global, so when you have nested minipages, the one that is defined as the last one overwrites value that should be used for the top minipage. This doesn't happen now, as it is used as soon as possible.

The CSS code that switches page layout from float to normal now uses the :not selector, so it is applied to all elements that follow minipage, as long as it isn't another minipage:

 \Css{ div.minipage + :not(.minipage) {clear:both;overflow:auto;width:100\%;}}


Here is the result:

• Thank you so much for the help @michal.h21! This solution does not seem to work if the minipage has a tikz picture inside of it. Any ideas why? I see an error that says [ERROR] htlatex: ? 15 Argument of \striptextwidth has an extra }. Apr 5 '21 at 3:14
• Just replace \blindtext in one of the minipages in the MWE by the following tikz code: \usetikzlibrary{arrows,positioning} \begin{tikzpicture}[>=latex'] \node [draw, text width=1.3cm, text centered] (s) {system $y=u$}; \draw [->] node[coordinate,left=1cm of s](u){} (u) -- node [above, near start] {$u$} (s); \draw [->] (s) node[coordinate,right=1cm of s](y){} -- node [above, near end] {$y$} (y); \end{tikzpicture} Apr 5 '21 at 3:17
• @Rushi I get this error with tikz code even without minipage. Maybe post it as another question? Apr 5 '21 at 9:00
• Yes, the error is there as soon as I add a TikZ picture, with or without a minipage. No issues if I add a picture using \includegraphics. I posted the question here. I could not think of a decent title though, feel free to edit! Apr 5 '21 at 17:09
• When htlatex says there is an error on line 15, like the error message above, which file is it talking about? I opened up all the temp files generated in the same directory as the .tex file to see if there was anything relevant on line 15 of any of those that will help me fix the configuration without bothering you, but it didn't look like the error message was referring to any of those files. It also has a ? where the file name should be according to the header. Apr 5 '21 at 21:26