An answer to one of my previous questions provided this code for referencing a range of lines in which a line labels were placed at the beginning and end of the line range.
\makeatletter
\newcommand{\reflines}[1]{%
\begingroup
\def\temp@a{\getrefnumber{start:#1}}%
\def\temp@b{\getrefnumber{end:#1}}%
\ifnum\temp@a = \temp@b
line \temp@a%
\else
lines \temp@a--\temp@b%
\fi
\endgroup
}
\makeatother
In the MWE, if we change one instance of \reflines
to occur within \edef
, there is a compilation error:
\documentclass{memoir}
\usepackage{refcount}
\usepackage{lineno}
\newcommand{\labellines}[2]{%
\linelabel{start:#1}%
#2%
\linelabel{end:#1}%
}
\makeatletter
\newcommand{\reflines}[1]{%
\begingroup
\edef\temp@a{\getrefnumber{start:#1}}%
\edef\temp@b{\getrefnumber{end:#1}}%
\ifnum\temp@a = \temp@b
line \temp@a%
\else
lines \temp@a\ and \temp@b%
\fi
\endgroup
}
\makeatother
\begin{document}
\runninglinenumbers*
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam lacinia posuere
magna ut imperdiet. \labellines{nunc-pellentesque}{Nunc pellentesque} velit quis
leo interdum ullamcorper. Ut fringilla sapien ut sem viverra, et rhoncus enim
lobortis. Morbi diam eros, tristique nec feugiat eu, consequat ac dui. Duis
molestie, sem id efficitur dignissim, velit metus auctor orci, sit amet mattis
metus purus in nibh. Etiam mattis, magna ut imperdiet molestie, ex leo viverra
velit, ac posuere augue est maximus risus. Etiam ac pellentesque nulla. Morbi
quis pharetra purus. Proin porta, turpis et scelerisque hendrerit, elit eros
malesuada mi, a imperdiet massa est id nibh. Class aptent taciti sociosqu ad
litora torquent per conubia nostra, per inceptos himenaeos.
\labellines{nunc-pharetra}{Nunc pharetra vehicula metus, eu tincidunt arcu
aliquet et. Aenean ac odio nunc. Etiam ultricies purus non dolor tincidunt, et
ultricies turpis consequat. Integer vitae ullamcorper eros. Nullam pulvinar
dictum nunc. Morbi ac justo mi.}
% \reflines{nunc-pellentesque} should print "line 2"
``Nunc pellentesque\ldots'' was on \reflines{nunc-pellentesque}.
\edef\nuncref{\reflines{nunc-pharetra}}
% \reflines{nunc-pharetra} should print "lines 10--13"
``Nunc pharetra\ldots'' was on \nuncref.
\end{document}
Why is this?
\def
, by definition, is unexpandable because it is evaluated at time of execution. But I'm sure a fuller explanation will occur.\def
have to be defined using\DeclareRobustCommand
?