tex4ht gives \catcode`\^^ error when loading mathtools

Edit:

I've found the source of this issue. TeX4ht redefines \@begindocumenthook and executes some code before and after it. \@begindocumenthook contained code that is added using \AtBeginDocument. It was deprecated by the new LaTeX hook system. Because it is not used anymore, TeX4ht code that handled \AtBeginDocument is not executed, which results in this catcode clash.

This issue can be fixed by using the hook system to execute the TeX4ht handling of \AtBeginDocument. I've fixed it in TeX4ht sources already. In the meantime, you can emulate it using the following .cfg file:

\Preamble{xhtml}
\AddToHook{begindocument/before}[tex4ht]{\csname a:AtBeginDocument\endcsname}
\AddToHook{begindocument/end}[tex4ht]{\csname b:AtBeginDocument\endcsname}
\begin{document}
\EndPreamble

Original answer:

Mathtools loads the graphicx package in \AtBeginDocument. It is loaded after package .4ht files had been processed and there is some catcode clash, it seems.

The simplest fix is to load graphicx package in your document, as Egreg showed. Fix on the TeX4ht side is to load graphicx in mathtools.4ht:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% mathtools.4ht                         2009-05-21-09:32 %
% Copyright (C) 2007--2009       Eitan M. Gurari         %
%                                                        %
% This work may be distributed and/or modified under the %
% conditions of the LaTeX Project Public License, either %
% version 1.3c of this license or (at your option) any   %
% later version. The latest version of this license is   %
% in                                                     %
%   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".%
%                                                        %
% This Current Maintainer of this work                   %
% is Eitan M. Gurari.                                    %
%                                                        %
% If you modify this program your changing its signature %
% with a directive of the following form will be         %
% appreciated.                                           %
%            \message{signature}                         %
%                                                        %
%                             [email protected]  %
%                 http://www.cse.ohio-state.edu/~gurari  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\immediate\write-1{version 2009-05-21-09:32}

\@ifpackageloaded{graphicx}{}{\RequirePackage{graphicx}}

\ifx \o:endgathered:\:UnDef\else
   \HRestore\endgathered
\fi
\def\shortintertext{\intertext}
\def\:tempc#1#2{{\csname a:prescript\endcsname}\sp{#1}\sb{#2}}
\expandafter\HLet\csname prescript \endcsname=\:tempc
\NewConfigure{prescript}{1}
\Configure{prescript}{\HCode{<mrow class="prescript"></mrow>}}
\renewenvironment{dcases}[1][c]{%
  \RIfM@\else
      \nonmatherr@{\begin{\@currenvir}}%
  \fi
  \left\lbrace
  \vcenter \bgroup
      \Let@ \chardef\dspbrk@context\@ne \restore@math@cr
      \spread@equation
      \ialign\bgroup
        \strut@$\m@th\displaystyle{##}$\hfil&\quad$\m@th\displaystyle{##}$\hfil\strut@
        \crcr
}
{\endaligned\right.}
                                    \catcode`\#13 \catcode`\!6
\def\reg:dcases[!1]{%
  \RIfM@\else
      \nonmatherr@{\begin{dcases}}%
  \fi
  \vcenter\bgroup
    \Let@ \chardef\dspbrk@context\@ne \restore@math@cr
    \SaveMkHalignConf:g{dcases}%
    \MkHalign#{$\m@th\displaystyle{\HCode{}}#$&\quad$\m@th\displaystyle{\HCode{}}#$}%
}
                                    \catcode`\#=6 \catcode`\!=12
\def\:tempc{\crcr\EndMkHalign
    \RecallMkHalignConfig \egroup\b:dcases \right.}
\HLet\enddcases\:tempc
\def\:temp{\left\lbrace   \pic:MkHalign{dcases}}
\expandafter\HLet\csname \string\dcases\endcsname\:temp
\NewConfigure{dcases}{6}
\renewenvironment{dcases*}[1][c]{%
  \RIfM@\else
      \nonmatherr@{\begin{\@currenvir}}%
  \fi
  \left\lbrace
  \vcenter \bgroup
      \Let@ \chardef\dspbrk@context\@ne \restore@math@cr
      \spread@equation
      \ialign\bgroup
        \strut@$\m@th\displaystyle{##}$\hfil&\quad{##}\hfil\strut@
        \crcr
}
{\endaligned\right.}
                                    \catcode`\#13 \catcode`\!6
\expandafter\def\csname reg:dcases*\endcsname[!1]{%
  \RIfM@\else
      \nonmatherr@{\begin{dcases*}}%
  \fi
  \vcenter\bgroup
    \Let@ \chardef\dspbrk@context\@ne \restore@math@cr
    \SaveMkHalignConf:g{dcases*}%
    \MkHalign#{$\m@th\displaystyle{\HCode{}}#$\quad&#}%
}
                                    \catcode`\#=6 \catcode`\!=12
\def\:tempc{\crcr\EndMkHalign
    \RecallMkHalignConfig \egroup\csname b:dcases*\endcsname \right.}
\expandafter\HLet\csname enddcases*\endcsname\:tempc
\def\:temp{\left\lbrace   \pic:MkHalign{dcases*}}
\expandafter\HLet\csname \string\dcases*\endcsname\:temp
\NewConfigure{dcases*}{6}

\edef\cat:underscore{%
   \noexpand\catcode`\noexpand\_=\the\catcode`\_ }
\catcode`\_=11
\def\MT_gathered_pre:{}
      \def\MT_gathered_post:{}
      \def\MT_gathered_env_end:{}
                                    \catcode`\#13 \catcode`\!6
\def\reg:MT_gathered_env[!1]{%
  \RIfM@\else
      \nonmatherr@{\begin{MT_gathered_env}}%
  \fi
  \null  \vcenter\bgroup
    \Let@ \chardef\dspbrk@context\@ne \restore@math@cr
    \SaveMkHalignConf:g{MT_gathered_env}%
\MkHalign#{$\m@th\displaystyle{\HCode{}}#$}%
}
                                    \catcode`\#=6 \catcode`\!=12
\def\:tempc{\crcr\EndMkHalign
    \RecallMkHalignConfig \egroup\b:MT_gathered_env}
\HLet\endMT_gathered_env\:tempc
\def\:temp{\pic:MkHalign{MT_gathered_env}}
\expandafter\HLet\csname \string\MT_gathered_env\endcsname\:temp
\NewConfigure{MT_gathered_env}{6}
\def\:tempc#1#2{{\m@th#1{#2}}}
\HLet\MT_cramped_internal:Nn\:tempc

\cat:underscore
\Hinput{mathtools}
\endinput

The document is processed without errors with this fix.


The error happens during loading of graphics.sty. I'm not sure why this happens, but a workaround is to load graphicx before mathtools.

\documentclass{article}
\usepackage{graphicx}
\usepackage{mathtools}

\begin{document}
test
\begin{align*} a&=b\\c&=d\end{align*}
\end{document}

What's the problem? That mathtools does

\AtBeginDocument{\RequirePackage{graphicx}...}

which is incorrect and should be

\RequirePackage{graphicx}
\AtBeginDocument{...}

On the other hand tex4ht makes ^ into a math active character, hence with category code 12. Loading a package with “strange” category codes can lead to unexpected results.

So, in my opinion, mathtools and tex4ht are linked in a conspiracy and both should fix the respective problems; mathtools should load the package earlier and tex4ht should change category codes as late as possible.