5

I'd like to configure tex4ht to set the width of a minipage div class based on the width specified in the LaTeX source. Here's what I have so far:

The tex file is:

\documentclass{article}
\begin{document}
\begin{minipage}[c]{0.6\textwidth}
  Some text
\end{minipage}
\hfill
\begin{minipage}[c]{0.3\textwidth}
  some more text
\end{minipage}
\end{document}

and my cfg file (with inspiration from https://www.mail-archive.com/[email protected]/msg00764.html) is:

\Preamble{html}
\Configure{HtmlPar}
{\EndP\Tg<p>}
{\EndP\Tg<p>}
{\HCode{</p>\Hnewline}}
{\HCode{</p>\Hnewline}}
\ConfigureEnv{minipage}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="minipage" align="center" style="width:INSERT WIDTH HERE;">}}{\ifvmode\IgnorePar\fi\EndP\HCode{</div>\Hnewline}}{}{}
\Css{div.minipage {
 float: left; 
 margin: 1ex 1em 1ex 0;
 } 
}
\Css{div.minipage:last-child {
 clear: none; 
 float: right; 
 margin: 1ex 0 1ex 1em;
 }
}
\begin{document}
\EndPreamble

For instance, I'd like the first minipage in the above tex example (with width of 0.6\textwidth) to result in a corresponding div with width 60%. So essentially, referencing this example, I'm looking for a way to:

  • extract only the "0.6" from \begin{minipage}[c]{0.6\textwidth}
  • multiply 0.6*100 = 60
  • insert 60, along with a % sign, into the width of the div

I may be a bit out of my element with this; I've been frantically trying to learn tex4ht and feel I have only a tenuous grasp of most aspects. This community's questions and answers have been tremendously helpful to me, and I'll sincerely appreciate any guidance on this issue.

1 Answer 1

3

This is little bit complicated, because minipage is not usual environment, but system of several macros dealing with optional parameters. This is modified cfg file:

\Preamble{html}
\Configure{HtmlPar}
{\EndP\Tg<p>}
{\EndP\Tg<p>}
{\HCode{</p>\Hnewline}}
{\HCode{</p>\Hnewline}}
\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
\xdef\miniwidth{\strip@pt\dimexpr(\striptextwidth#4pt)*100\relax\%}
\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
\Css{\#minipage\the\mini@count{width:\miniwidth;}}%
}{}{}
\makeatother
\Css{div.minipage {
 float: left; 
 margin: 1ex 1em 1ex 0;
 } 
}
\Css{div.minipage:last-child {
 clear: none; 
 float: right; 
 margin: 1ex 0 1ex 1em;
 }
}
\begin{document}
\EndPreamble

we can get all minipage parameters from \@iiiminipage macro, so we have to redefine it, strip \textwidth from parameter with width, calculate percents and save it to a macro so we can use it later.

Unfortunately, we can't use this calculated value directly in opening tag, because these are written before @iiiminipage is called, so we must generate unique id attribute for each minipage and write width to the css file.

Result with borders:

enter image description here

3
  • This is terrific, thank you very much, the solution is much more sophisticated than I thought and was beyond my current capabilities. I was hoping to avoid writing to the css file (because in practice I use a different, global css file) but it's just as well because I encountered a similar situation (reconciling the tex4ht-generated css file with my own css file) in the context of the tabular environment. Thus this solution has helped me solve not one but two issues; thanks again.
    – user59959
    Aug 7, 2014 at 16:36
  • @user59959 your welcome, I am happy that this helped you. you can use multiple css files with tex4ht, one generated by tex4ht and the other dealing with design and typography, for example
    – michal.h21
    Aug 7, 2014 at 17:34
  • @michal.h21 I have the problem that with this cfg, the text after the minipage does not go to a new line, but start on a third column. How do I prevent this? please, check my question tex.stackexchange.com/questions/229905/…
    – simona
    Feb 24, 2015 at 14:14

You must log in to answer this question.

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