1

I am using the make4ht configuration from this answer to extract width as a fraction of textwidth from LaTeX code and add it to the CSS file. The configuration works as expected until I add a TikZ picture anywhere in the document. The MWE from that question compiles correctly, but the following code generates an error (the error is there even if we delete the minipages entirely from the code below, I just left them in there to make sure any solutions proposed keep the minipage widths intact):

\documentclass{article}
\ifdefined\HCode
    \def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi 
\usepackage{tikz,amsmath,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}
    \begin{center}
        \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}
    \end{center}
\end{document}

The error message is:

[ERROR]   htlatex: ?    21       Argument of \striptextwidth has an extra }.
[ERROR]   htlatex: ?    21       Paragraph ended before \striptextwidth was complete.
[ERROR]   htlatex: ?    21       Missing number, treated as zero.
[ERROR]   htlatex: ?    21       Illegal unit of measure (pt inserted).
[ERROR]   htlatex: ?    21       Missing ) inserted for expression.

Any ideas on what the conflict is and how to fix it?

UPDATE: If I remove the text width = 1.3cm option from the node above, the error disappears and everything works as expected, but for some figures, I need multi line node text, so I need to restrict the text width.

2 Answers 2

1

This issue is caused by the minipage configuration in TeX4ht configuration file that I provided in my previous answer. It expects that minipage width is specified as a fraction of \textwidth. It was OK, as you used this format in your previous mwe. The problem is when minipage you don't provide width in relation to \textwidth, which happens when you use TikZ text width option.

To fix that, we need to do a better calculation of width. Fortunately, thanks to LaTeX 3, we can now easily do floating-point calculations, which simplifies the configuration file and fixes your issue.

Here is the updated cfg file:

\Preamble{xhtml}

\makeatletter

% we must refer to minipage from the css file, because tags are beeing
% written before we know dimensions
\newcount\mini@count
\ExplSyntaxOn
% save original minipage
\let\oldiimini\@iiiminipage
% redefine minipage
\def\@iiiminipage#1#2[#3]#4{%
  % calculate minipage dimensions and save it to the CSS file
  \Css{\#minipage\the\mini@count{width:\fp_eval:n{#4/\textwidth*100}\%;}}%
  \global\advance\mini@count by 1\relax%
  \oldiimini{#1}{#2}[#3]{#4}%
}

\ExplSyntaxOff

\ConfigureEnv{minipage}{\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

Minipage width is now written to the CSS file using the following code:

\Css{\#minipage\the\mini@count{width:\fp_eval:n{#4/\textwidth*100}\%;}}%

The #4 argument contains the width that is passed to minipage environment. It is then divided by \textwidth and multiplied by 100, to get the dimension in percents.

This is the result:

enter image description here

0

Not quite an answer, but a workaround. Everything works as expected if we use a \parbox inside the node text to set the text width instead of the text width option. The code

\documentclass{article}
\ifdefined\HCode
    \def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi 
\usepackage{tikz,amsmath,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}
    \begin{center}
        \usetikzlibrary{arrows,positioning} 
        \begin{tikzpicture}[>=latex']
            \node [draw] (s) {\parbox[t]{1.3cm}{\centering 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}
    \end{center}
\end{document}

compiles just fine with the config file from this answer:

\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

Not quite an answer, because it involves changing existing LaTeX code.

You must log in to answer this question.

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