While loading fancyvrb, siunitx and bigfoot, I got input stack size=5000, TeX STOPPED: fatal errors occurred

If I try to build the following document:

\documentclass{memoir}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{fancyvrb}
\usepackage{siunitx}
\usepackage{bigfoot}

\begin{document}

Test.

\end{document}


Latex just throws the error:

LaTeX Font Info:    External font cmex10' loaded for size
(Font)              <7> on input line 15.
LaTeX Font Info:    External font cmex10' loaded for size
(Font)              <5> on input line 15.
\c__siunitx_mathtt_int=\count327

input stack size=5000]
TeX STOPPED: fatal errors occurred. Check the TeX log file for details


Related questions:

Analysis

Compilation stops when the tokens in \AtBeginDocument are being evaluated. For historical reasons, bigfoot has

\def\FN@allmarks#1{\@elt{#1}%
\ifnum#1<\count266
\expandafter\FN@allmarks\expandafter{\number\numexpr#1+\@ne}%
\fi}


because etex.sty had

\count266=1 % globally allocates \marks classes 1, 2, ...


The bigfoot package has not been updated after LaTeX has incorporated much of etex.sty, including allocation of the extended registers (above 255), but the counter holding the last allocated marks register is not \count266 any longer.

What happens in your case is that \count266 is allocated by siunitx as part of the loading of expl3 and receives the value 16777216, which of course makes too long a loop when \FN@allmarks0 is executed.

Loading fancyvrb after siunitx doesn't trigger the error, but doesn't solve the issue either, because \count266 will hold a value that most likely has nothing to do with the number of allocated marks registers. The LaTeX kernel reserves \count256 for that.

Solution

Patch \FN@allmarks to use the right register.

\documentclass{memoir}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{fancyvrb}
\usepackage{siunitx}
\usepackage{bigfoot}
\usepackage{xpatch}

\makeatletter
\xpatchcmd{\FN@allmarks}{266}{256}{}{}
\makeatother

\begin{document}

Test.

\end{document}


\usepackage{siunitx}