A “packageless” implementation:
\documentclass{article}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\refstartswithoneTF}{mmm}
{% #1 = label, #2 = true text, #3 = false text
\mgy_ref_starts:nnn { #1 } { #2 } { #3 }
}
\cs_new:Nn \mgy_ref_number:n
{
\cs_if_exist:cTF { r@#1 }
{ \exp_last_unbraced:Nf \use_i:nn { \use:c { r@#1 } } }
{ 0 }
}
\cs_new:Nn \mgy_ref_starts:nnn
{
\str_if_eq:eeTF { \str_head:e { \mgy_ref_number:n { #1 } } } { 1 } { #2 } { #3 }
}
\cs_generate_variant:Nn \str_head:n { e }
\ExplSyntaxOff
\begin{document}
\begin{equation}
\label{eq:a}
a=b
\end{equation}
\begin{equation}
\label{eq:b}
a=b
\end{equation}
\setcounter{equation}{9}
\begin{equation}
\label{eq:c}
a=b
\end{equation}
The reference to the label \texttt{eq:a},
which is actually \ref{eq:a},
\refstartswithoneTF{eq:a}{starts}{does not start} with 1.
The reference to the label \texttt{eq:b},
which is actually \ref{eq:b},
\refstartswithoneTF{eq:b}{starts}{does not start} with 1.
The reference to the label \texttt{eq:c},
which is actually \ref{eq:c},
\refstartswithoneTF{eq:c}{starts}{does not start} with 1.
\end{document}
What does the incantation
\exp_last_unbraced:Nf \use_i:nn { \use:c { r@#1 } }
do?
The first instruction tell TeX to jump over the next token, that is, \use_i:nn
, and to recursively expand the contents of the braced group, leaving the unbraced result in the input stream.
Say that #1
is eq:c
for the sake of the example. Then \use:c{r@eq:c}
first produces \r@eq:c
(a token that cannot be ordinarily written) which is expanded. Such expansion is always of the form
{<ref>}{<pageref>}
in the present case {10}{1}
. Now TeX will see
\use_i:nn {10}{1}
and this just leaves 10
in the input stream. This is used in \mgy_ref_starts:nnn
. Note that \str_head:e
fully expands its argument, so 10
becomes 1
, and we eventually get
\str_if_eq:nnTF { 1 } { 1 } {starts} {does not start}
which returns the true branch.
With eq:b
we eventually get
\str_if_eq:nnTF { 2 } { 1 } {starts} {does not start}
which returns the false branch.
hyperref
. Is that also the case for your real document? Hyperref changes the definition and output of\ref
, which would make a significant difference for answering this question, therefore it would be good to know if you indeed want a solution without hyperref.hyperref
. Even more I will usepolyglossia
. I change the mwe with a more realistic setup.