TransWikia.com

Configuring htlatex for nested minipages or minipages outside floating environments

TeX - LaTeX Asked on June 3, 2021

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.4textwidth}
        blindtext
    end{minipage}
    hfill
    begin{minipage}[t]{0.5textwidth}
        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.0textwidth}
        begin{minipage}[t]{0.4textwidth}
            blindtext
        end{minipage}
        hfill
        begin{minipage}[t]{0.5textwidth}
            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%;}

One Answer

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
defstriptextwidth#1textwidth{#1}
% we must refer to minipage from the css file, because tags are beeing
% written before we know dimensions
newcountmini@count
% save original minipage
letoldiimini@iiiminipage
% redefine minipage
def@iiiminipage#1#2[#3]#4{%
% calculate dimensions and save it to macro
edefminiwidth{strip@ptdimexpr(striptextwidth#4pt)*100relax%}
Css{#minipagethemini@count{width:miniwidth;}}%
oldiimini{#1}{#2}[#3]{#4}
}

ConfigureEnv{minipage}{advancemini@count by 1relaxifvmodeIgnoreParfiEndPHCode{<div class="minipage" align="center" id="minipagethemini@count" style="border:1px solid black;">}}
{ifvmodeIgnoreParfiEndPHCode{</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
edefminiwidth{strip@ptdimexpr(striptextwidth#4pt)*100relax%}
Css{#minipagethemini@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

Correct answer by michal.h21 on June 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP