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.
\protect\ref