2

In this LaTeX code I attempt to replace the "(" in references, so that they would look like "x)", not "(x)":

\documentclass{article}

\usepackage{enumitem} \usepackage{xstring}

\begin{document}

\begin{enumerate}[label=(\alph*)] \item \label{cla:Item1} an apple \item \label{cla:Item2} a banana \item a carrot, see \StrSubstitute{\ref{cla:Item1}}{(}{} \item a durian, see \StrSubstitute{\ref{cla:Item2}}{(}{} \end{enumerate}

\end{document}

However, \StrSubstitute doesn't seem to work and all references are still printed as "(x)".

Why \StrSubstitute doesn't work? Is this a bug?

Are there any workarounds?

1 Answers1

3

It cannot work, because \ref produces what's needed to print the reference.

It's easier.

\documentclass{article}

\usepackage{enumitem}

\begin{document}

\begin{enumerate}[label=(\alph),ref=\alph)] \item \label{cla:Item1} an apple \item \label{cla:Item2} a banana \item a carrot, see \ref{cla:Item1} \item a durian, see \ref{cla:Item2} \end{enumerate}

\end{document}

enter image description here

You can do it with \StrSubstitute, but you have to use refcount that provides an expandable version of \ref and remove two pairs of braces (or use slow features of xstring that examine things hidden in braces), because the reference returned by \ref{cla:Itemi1} is {{(a)}}.

\documentclass{article}

\usepackage{enumitem} \usepackage{xstring} \usepackage{refcount}

\makeatletter \newcommand{\getenumitemref}[1]{% \expandafter\expandafter\expandafter@firstofone \expandafter@firstofone \expanded{\getrefbykeydefault{#1}{}{0}}% } \makeatother

\begin{document}

\begin{enumerate}[label=(\alph*)] \item \label{cla:Item1} an apple \item \label{cla:Item2} a banana \item a carrot, see \StrSubstitute{\getenumitemref{cla:Item1}}{(}{} \item a durian, see \StrSubstitute{\getenumitemref{cla:Item2}}{(}{} \end{enumerate}

\end{document}

egreg
  • 1,121,712