2

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%;}
Rushi
  • 837

1 Answers1

2

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:

enter image description here

michal.h21
  • 50,697
  • 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 }. – Rushi Apr 05 '21 at 03: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}

    – Rushi Apr 05 '21 at 03:17
  • @Rushi I get this error with tikz code even without minipage. Maybe post it as another question? – michal.h21 Apr 05 '21 at 09: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! – Rushi Apr 05 '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. – Rushi Apr 05 '21 at 21:26
  • @Rushi the error is in the .cfg file, your TikZ code seems to use minipage internally and it breaks the CSS dimension calculation – michal.h21 Apr 05 '21 at 22:48
  • Ah ok, I did not know TikZ used minipages, not an easy fix then, I imagine? Apparently nested minipages break other things too. – Rushi Apr 05 '21 at 23:33
  • I found something odd. If I remove the text width=1.3cm option from my TikZ node, the error goes away. I updated my question with the new information. – Rushi Apr 06 '21 at 04:13
  • @Rushi I think that it uses minipage specifically when you use the text width option. – michal.h21 Apr 06 '21 at 08:07
  • Everything works as expected if I use a \parbox inside the node text. That is, \node [draw] (s) {\parbox[t]{1.3cm}{\centering system $y=u$}}; instead of \node [draw, text centered, text width=1.3cm] (s) {system $y=u$};. Unfortunately, this workaround involves changing too much of my existing LaTeX code (class notes, 100 pages, lots of TikZ pictures) to be useful. – Rushi Apr 06 '21 at 14:40
  • @Rushi I've posted my answer to the other question. I was able to fix the issue by correcting the CSS width calculation. – michal.h21 Apr 06 '21 at 17:53
  • Sweet! Thank you so much! – Rushi Apr 06 '21 at 18:22