# tex4ht: conflict between wrapfig and caption packages

MWE:

\documentclass[12pt]{book}

\usepackage{caption}
\usepackage{wrapfig}

\begin{document}
\begin{wrapfigure}{o}{1.25in}
\end{wrapfigure}
\end{document}


If I compile it with tex4ebook, or htlatex, then I come up with an error:

! Undefined control sequence.
\b:wrapfloat ->\endWrap

l.8 \end{wrapfigure}


Note that there is no error while we compile with pdflatex, and that if I change the order of including caption and wrapfig packages, then there won't be any errors.

I hope an improvement in these .4ht files.

(By the way, do anybody know how to configure tex4ebook so that it will pause compiling at errors, instead of ignoring them quietly, as before?)

Edit: As michal-h21 points out, in newer versions of tex4ebook, running with parameter -a debug suffices.

A sample code with caption:

\documentclass[12pt]{book}

\usepackage[labelformat=empty]{caption}
\usepackage{wrapfig}

\begin{document}
\begin{wrapfigure}{o}{1.25in}
\caption{Test}
\end{wrapfigure}
\end{document}

• you can use tex4ebook -a debug to stop on errors and show all terminal output. Dec 18 '19 at 17:32
• it seems that Caption redefines Wrapfig commands, but after they were patched by TeX4ht, so the confgurations are not used. I can see that half of Caption code just patches different packages in a way that is not useful for the HTML conversion. Dec 18 '19 at 21:49
• @michal.h21 By the way, I did not dig deeply but I found that if I set labelformat=empty in caption, tex4ht will still generate a colon as if there are texts before the colon. Dec 18 '19 at 22:07
• @michal.h21 Seemingly wrapfigure is ineffective for tex4ht. I modify it to figure temporarily (I check the effect in calibre. I don't know whether kindle even supports wrapping images, incidentally). Dec 18 '19 at 22:10

It seems that the Caption package redefines lot of other packages commands. It does that after TeX4ht already patched commands from these packages, which results in lose of the inserted TeX4ht configurations. I think the only safe solution is to prevent Caption from patching of all these commands. Try this version of usepackage.4ht:

% usepackage.4ht (2019-12-13-14:32), generated from tex4ht-4ht.tex
% Copyright 2003-2009 Eitan M. Gurari
% Copyright 2009-2019 TeX Users Group
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions
% of LaTeX version 2005/12/01 or later.
%
% This work has the LPPL maintenance status "maintained".
%
% The Current Maintainer of this work
% is the TeX4ht Project <http://tug.org/tex4ht>.
%
% If you modify this program, changing the
% version identification would be appreciated.
\immediate\write-1{version 2019-12-13-14:32}

\def\:temp{tex4ht}\ifx \:temp\@currname
\:warning{\string\usepackage{tex4ht} again?}
\def\:temp#1htex4ht.def,tex4ht.sty#2!*?: {\def\:temp{#2}}
\expandafter\:temp \@filelist htex4ht.def,tex4ht.sty!*?: %
\ifx \:temp\empty  \else
\:warning{if
\string\RequirePackage[tex4ht]{hyperref} or
\string\usepackage[tex4ht]{hyperref} was
\string\RequirePackage{hyperref} or
\string\usepackage{hyperref}}
\fi

\fi
\gdef\a:usepackage{\use:package xr,xr-hyper,eso-pic,expl3,savetrees,biblatex,xeCJK,polyglossia,fontspec,tikz,pdfbase,graphics,xcolor,imakeidx,minted,fancyhdr,caption,,!*?: }
\gdef\use:package#1,{%
\if :#1:\def\:temp##1!*?: {}\else
\def\:temp{#1}\ifx \@currname\:temp
\def\:temp##1!*?: {\input usepackage.4ht  }%
\else \let\:temp=\use:package \fi
\fi \:temp}
\def\:temp{xr}\ifx \@currname\:temp
\AtEndOfPackage{\let\XR:\XR@
\def\XR@[#1]#2{%
\Configure{AtBeginDocument}{\XR:[#1]{#2}}{}}%
}

\fi
\def\:temp{xr-hyper}\ifx \@currname\:temp
\AtEndOfPackage{\let\XR:\XR@
\def\XR@[#1][#2]#3{%
\AtBeginDocument{\XR:[#1][#2]{#3}}
}%
}

\fi
\def\:temp{eso-pic}\ifx \@currname\:temp
\:dontusepackage{eso-pic}
\fi
\def\:temp{expl3}\ifx \@currname\:temp
\ifdefined\XeTeXversion%
\xenunidelblock{Latin-expl3}%
\AtEndOfPackage{\xeuniuseblock{Latin-expl3}}
\fi
\fi
\def\:temp{savetrees}\ifx \@currname\:temp
\:dontusepackage{savetrees}
\fi
\def\:temp{biblatex}\ifx \@currname\:temp
\AtEndOfPackage{%
\def\blx@mknoautolang{%
\blx@lbxinput{\blx@languagename}%
{}{}%
}%
\def\blx@lbxinput@iii#1#2{%
\global\csundef{blx@lng@#2}%
\:IfFileExists{#1.lbx}
{\blx@lbxinput@iv{#2}{#1}{language '#2' -> '#1'}}
{\ifcsdef{blx@suffmaptried@#2}
{}
{\blx@warning@noline{%
Ignoring mapping '#2' -> '#1'}%
\global\cslet{blx@suffmaptried@#2}\@empty}%
\blx@lbxinput@iv{#2}{#2}{language '#2'}}}

}
\fi
\def\:temp{xeCJK}\ifx \@currname\:temp
\:dontusepackage{xeCJK}
\AtEndOfPackage{%
\RequirePackage{fontspec}
}
\DeclareDocumentCommand\setCJKmainfont{o m o}{}
\let\setCJKsansfont\setCJKmainfont
\let\setCJKmonofont\setCJKmainfont

\DeclareDocumentCommand\setCJKfamilyfont {m o m }{}
\DeclareDocumentCommand\newCJKfontfamily {o m o m}{\expandafter\gdef\csname #2\endcsname{\relax}}

\DeclareDocumentCommand\xeCJKsetup{m}{}
% }
\AtBeginDocument{%
\xeuniuseblock{CJK}
}
\fi
\def\:temp{polyglossia}\ifx \@currname\:temp
\ExplSyntaxOn
\AtEndOfPackage{

\cs_set_eq:NN\orig_polyglossia@keys_define_lang:n\polyglossia@keys_define_lang:n
\cs_set_protected:Npn \polyglossia@keys_define_lang:n #1 {
\orig_polyglossia@keys_define_lang:n{#1}
\keys_define:nn {polyglossia}{
#1 / direction
.  code:n = {}
}
}
\def\RequireBidi{}%
}
\ExplSyntaxOff
\fi
\def\:temp{fontspec}\ifx \@currname\:temp
\ifdefined\fontspec
\:dontusepackage{fontspec}
\else
\input usepackage-fontspec.4ht
\fi
\fi
\def\:temp{tikz}\ifx \@currname\:temp
\AtEndOfPackage{%
\let\use:tikzlibrary\usetikzlibrary
\def\find:externalize#1external#2\@nil{%
\if\relax#2\relax\else
\let\tikz:externalize\tikzexternalize
\renewcommand\tikzexternalize[1][]{\tikz:externalize[##1,mode=only graphics]}
\tikzset{%
tex4ht inc/.style={%
/pgf/images/include external/.code={%
\includegraphics[]{####1.pdf}%
}%
}
}
\tikzset{tex4ht inc}
\fi
}
\renewcommand\usetikzlibrary[1]{%
\use:tikzlibrary{#1}%
\find:externalize#1external\@nil%
}
}
\fi
\def\:temp{pdfbase}\ifx\@currname\:temp
\AtEndOfPackage{%
\let\old:outputpage\@outputpage
\def\@outputpage{\old:outputpage{}}
}
\fi
\def\:temp{graphics}\ifx\@currname\:temp
\ifdefined\XeTeXversion
\PassOptionsToPackage{dvips}{graphics}
\fi
\fi
\def\:temp{xcolor}\ifx\@currname\:temp
\ifdefined\XeTeXversion
\PassOptionsToPackage{dvips}{xcolor}
\fi
\fi
\def\:temp{imakeidx}\ifx\@currname\:temp
\PassOptionsToPackage{noautomatic}{imakeidx}
\fi
\def\:temp{minted}\ifx\@currname\:temp
\AtEndOfPackage{%
\define@booleankey{minted@opt@g}{breaklines}{}{}{}%
}
\fi
\def\:temp{fancyhdr}\ifx\@currname\:temp
\AtEndOfPackage{%
\def\ps@fancy{}%
}
\fi

\def\:temp{caption}\ifx\@currname\:temp
\AtEndOfPackage{%
}
\fi

\endinput


The important part is this:

\def\:temp{caption}\ifx\@currname\:temp
\AtEndOfPackage{%
}
\fi


This code is inserted when Caption is loaded using \usepackage the \AtEndOfPackage executes code after the package code is loaded. It redefines the command responsible for package patching to do nothing.

This is the resulting HTML from your sample:

<body>
<div class='wrapfig-o'>                       </div>

</body>

• Thanks. Is there any way to eliminate the extra colon properly under the option labelformat=empty (see my edited post)? Dec 19 '19 at 9:46
• @Yai0Phah I've added check for empty label to TeX4ht sources, it shouldn't print the spurious colon once it is updated in TL. Dec 19 '19 at 14:18