Confusing bug in koma-script when using fontspec, \listoffigures, and cross-refs

cross-referencingfloatsfontspeckoma-script

I came across a weird bug in KOMA-script when using the fontspec package (either LuaLaTeX/XeLaTeX) and cross-referencing. Rather than give an in-depth description up front, here's an example of the code that triggers this:

\documentclass{scrreport}
\usepackage{fontspec}
%\setmainfont{Calibri}
\begin{document}
\listoffigures
\begin{figure}
\caption{Test}
\label{fig:test}
\end{figure}
\begin{figure}
\caption{Can't reference \ref{fig:test}}
\end{figure}
\end{document}

More or less, if you create a figure environment in a KOMA-script environment with a label, then try to cross-reference that figure within a caption environment of another figure, the environment will bug out if you use the fontspec package and call the command \listoffigures. Removing the cross-reference, the fontspec package, \listoffigures, or KOMA-script will fix the issue. The console error is:

<inserted text> 
                $
l.2 ...erence `fig:test' on page 2 undefined}}}{2}
                                                  {}%

So far I've been manually typing cross-refs in captions because I'm forced to include lists of figures/use Calibri in my lab reports and I have 0 clue how to fix this issue on my own, given that XeLaTeX appears to be trying to typeset a warning. I also like KOMA-script. I'm running MikTeX 21.8 and using TeXworks to compile my document with the XeLaTeX + MakeIndex + BibTeX option, though this bug happens when compiling with only the XeLaTeX option if one compiles the document twice after deleting the document's associated .lof file (the bug occurs on the second try; the first try compiles but puts "??" in place of the cross-ref).

I'd like to know if anyone has any ideas on how to fix this bug other than "stop using KOMA-script/don't use Calibri/don't cross-ref in captions/you don't need to list figures/etc etc". I've searched everywhere for info to try to fix this and have been unable to find much.

Best Answer

The error is not related to KOMA, you get the same error with the standard classes too.

When you have a reference to an unknown label in a caption:

\documentclass{article}
\usepackage{fontspec}
\begin{document}
\listoffigures
\begin{figure}
\caption{Can't reference \ref{fig:test}}
\end{figure}
\end{document}

then this is written to the .lof file (line breaks added by me):

\contentsline {figure}{\numberline {1}{
  \ignorespaces Can't reference \G@refundefinedtrue 
   {\mbox {\normalfont 
    \int _zero:N \l __fontspec_strong_int 
   \bfseries ??}}
   \GenericWarning { }{LaTeX Warning: Reference `fig:test' on page 1 undefined}}}{1}{}%

The \int _zero:N \l __fontspec_strong_int is fontspec specific and leds to the error, but even without fontspec the output is not good.

The correct solution is to protect the \ref:

\documentclass{article}
\usepackage{fontspec}
\begin{document}
\listoffigures
\begin{figure}
\caption{Can't reference \protect\ref{fig:test}}
\end{figure}
\end{document}