# why adding \pagestyle{fancy} gives compile error with tex4ht?

This MWE

\documentclass[12pt]{article}%
\usepackage{fancyhdr}
\pagestyle{fancy}

\begin{document}
\section{$\cos \left(x\right)$}
test
\end{document}


Gives this error

make4ht foo1.tex
........
(/usr/local/texlive/2019/texmf-dist/tex/generic/tex4ht/html4-math.4ht)
(/usr/local/texlive/2019/texmf-dist/tex/generic/tex4ht/html5.4ht)) (./foo1.aux)
! Argument of \im:g has an extra }.
<inserted text>
\par
l.6 \section{$\cos \left(x\right)$}

?


commenting out \pagestyle{fancy} the error goes away.

Also, which is more interesting, changing \section{$\cos \left(x\right)$} to \section{$\cos x$} the error goes away! i.e.

\documentclass[12pt]{article}%
\usepackage{fancyhdr}
\pagestyle{fancy}

\begin{document}
\section{$\cos x$}
test
\end{document}


No error. So it looks like it is the use of \left and \right is what caused the problem for text4ht when using fancyhrd

I will change my code so to avoid loading fancyhdr when in tex4ht mode to bypass this.

But the question is, why make4ht fails like this? Should make4ht handle this better?

TL 2019 with latest updates as of 2 days ago.

• I am quite sure it is not a good idea to use fancyhdr with tex4ht. I may add some code to suppress it's loading, but for the moment it is best to use the conditional loading – michal.h21 Oct 7 '19 at 21:31
• @michal.h21 I agree with you. I found this by accident. I do not need fancyhrd in tex4ht. But I thought the error itself could indicate some other issue somewhere. I already changed by code to avoid loading this package when using tex4ht. But it will be good if tex4ht could detect such problems internally and bypass them. – Nasser Oct 7 '19 at 21:48
• I guess it changes the sectioning commands somehow. I will look at this issue tomorrow. – michal.h21 Oct 7 '19 at 21:53

Here is definition of the fancy page style:

\def\ps@fancy{%
\f@nch@ifundefined{@chapapp}{\let\@chapapp\chaptername}{}% for amsbook
\f@nch@ifundefined{MakeUppercase}{\def\MakeUppercase{\uppercase}}{}%
\f@nch@ifundefined{chapter}%
{\def\sectionmark##1{\markboth{\MakeUppercase{\ifnum \c@secnumdepth>\z@
\thesection\hskip 1em\relax
\fi ##1}}{}}%
\def\subsectionmark##1{\markright {\ifnum \c@secnumdepth >\@ne
\thesubsection\hskip 1em\relax \fi ##1}}}%
{\def\chaptermark##1{\markboth {\MakeUppercase{\ifnum
\c@secnumdepth>\m@ne \@chapapp\ \thechapter. \ \fi ##1}}{}}%
\def\sectionmark##1{\markright{\MakeUppercase{\ifnum \c@secnumdepth >\z@
\thesection. \ \fi ##1}}}%
}
\ps@@fancy
\gdef\ps@fancy{\@fancyplainfalse\ps@@fancy}%
\fi}


As you can see, it redefines \sectionmark and other section related commands. It seems that it clashes with tex4ht. As the functionality provided by fancyhdr doesn't make sense for the HTML output, I think it is best to just redefine the \ps@fancy command to do nothing. The only catch is that because the \pagestyle command can be used in the document preamble, the patch cannot be incorporated using the fancyhdr.4ht file. We must use the tex4ht mechanism for patching packages in \usepackage. Here is the full updated usepackage.4ht:

% usepackage.4ht (2019-06-24-13:58), 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-06-24-13:58}

\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,fancyhdr,xeCJK,polyglossia,fontspec,tikz,pdfbase,graphics,xcolor,imakeidx,minted,,!*?: }
\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
\def\eat:optional[#1]{}
\AtEndOfPackage{\let\XR:\XR@
\def\XR@[#1]#2{%
\Configure{AtBeginDocument}{\XR:[#1]{#2}}{}\@ifnextchar[\eat:optional{}}%
}

\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
\AtEndOfPackage{
\let\orig@xpg@define@keys\xpg@define@keys
\def\xpg@define@keys#1{%
\orig@xpg@define@keys{#1}%
\define@choicekey+[xpg@setup]{#1}{direction}[\val\nr]{LR,RL}[LR]%
{
\togglefalse{#1@RL}%
\ifcase\nr\relax%
\csgappto{init@extras@#1}{%
a:PolyglossiaRtl%
}
\or
\csgappto{init@extras@#1}{%
\b:PolyglossiaRtl%
}
\fi
}%
{\xpg@warning{#1: Invalid value \val\space for key "direction"}}%
}}

\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

\endinput


The part that handles with fancyhdr is small:

\def\:temp{fancyhdr}\ifx\@currname\:temp
\AtEndOfPackage{%
\def\ps@fancy{}
}
\fi
`

This is the result: