This code crashes because I'm including the macro tmp into the label command. Reading this forum, especially What is the difference between Fragile and Robust commands? , I thought that the \protect I put in front of \tmp should fix the problem, but it doesn't. Could somebody please explain how to get it to work? Thanks very much for any suggestions
\documentclass{amsart}
\usepackage{soul}
\usepackage{xstring}
\begin{document}
\def\tmp{fig:\StrBefore{file.png}{.}}
\tmp
\begin{figure}
\caption{nothing}
\label{\protect\tmp}
%\label{\tmp}
\end{figure}
\end{document}


filen#{.ex}is a valid filename with filename-basefilen#{and filename-extensionex}in some file-systems. ... It is also good to know whether expansion of macros shall take place when obtaining tokens that form file-paths/filenames. – Ulrich Diez Apr 04 '20 at 20:19