Problem
I would like to use an expl3 function inside a \label{...}.
%!TEX program=xelatex
\documentclass{article}
\usepackage{l3regex}
\ExplSyntaxOn
\tl_new:N \l_slugify_tl
\cs_new:Npn \slugify #1 {
\tl_set:Nf \l_slugify_tl { \tl_lower_case:n { #1 } } %lower case
\regex_replace_all:nnN { [^a-z]+ }{ - }\l_slugify_tl % remove non-letters
\regex_replace_all:nnN { (^-|-$$$$) }{ } \l_slugify_tl % clean up leading/trailing dashes
\tl_use:N \l_slugify_tl
}
\ExplSyntaxOff
\begin{document}
\subsection{The Explanation}
\label{wiki:\slugify{The Explanation}}
\end{document}
When typesetting the document, I get a
) (./test-regex-slug.aux
! Undefined control sequence.
<argument> r@wiki:\tl
_set:Nf {the explanation}\regex _replace_all:nnN {[^a-...
l.3 ...x_replace_all:nnN {(^-|-$$$$)}{}}{{0.1}{1}}
Apparently, the contents of \slugify are not expanded and the expl3-code is written directly to the aux file. How can I prevent that and make my code work?
Background
I'd like to simplify the usage of cross-references inside my (huge) document. I would type something like
... e.g. using a \linkR{Time of Flight} detector.
That \linkR{TOF} would ...
% ...
\section{\anchorR{Time of Flight} Detector (\aliasR{TOF})}
% ...
instead of
...e.g. using a \hyperref[time-of-flight]{Time of Flight} detector.
That \hyperref[time-of-flight]{TOF} would...
% ...
\section{Time of Flight Detector (TOF)} \label{time-of-flight} \label{tof}
% ...
\slugifyoutside of\label, too.\tl_use:Vseems to be the problem (undefined). If you remove the bottom most line from your macro it runs without an error (outside of\label). – Skillmon Jul 04 '17 at 23:23\label. – Henri Menke Jul 04 '17 at 23:33\labeland\ref. – Henri Menke Jul 04 '17 at 23:44\label{}in\section{}i.e.\section{ \label{} }. That will end in tears, even setting the expansion issues mentioned by @HenriMenke aside. – cfr Jul 05 '17 at 01:03\cs_new. If you want something like\slugify, use\NewDocumentCommandor whatever as a wrapper for the lower level function\ralfix_slugify:n. – cfr Jul 05 '17 at 01:05glossaries. – Ulrike Fischer Jul 05 '17 at 06:40\tl_use:N. Changed it. – Peater de Xel Jul 05 '17 at 09:22\labelinside\section... https://tex.stackexchange.com/a/32327 – Peater de Xel Jul 05 '17 at 10:12\captionis not the same as putting it inside\section, as the comments on that answer point out. – cfr Jul 05 '17 at 13:27