Aesthetic caveat: I am actually using small-caps with hanging figures and not lining. However, in creating a minimal example, I have succumbed to the defaults and created a typographical monstrosity.
I am trying to create an environment with something like the following syntax:
\begin{<environment name>}{<single letter>}[<options>}
\item <stuff>
\end{<environment name>}
I want the list to be enumerated and I want each item to be labelled with \textsc{<single letter>}\arabic{<number of item>}.
This is straightforward with enumitem. For example, I can do this:
\NewDocumentEnvironment{letters}{ O {} m }{%
\begin{enumerate}[label=\textsc{#2}\arabic*., ref=\textsc{#2}\arabic*, #1]
}{%
\end{enumerate}%
}
and then
\begin{letters}{a}
\item first
\item second
\end{letters}
will produce
However, when I reference an item, I would like the format of the reference to be \textsc{<letter>}\arabic{<value>} (without a dot). Again, this is fine and the ref in enumitem above takes care of this nicely.
But I would also like to be able to capitalise the letter when the reference occurs at the start of a sentence, for example. Both fancyref and cleveref offer this kind of facility, but the use of small-caps for lower case makes this problematic.
So I started to investigate enumitem and started messing around with the \AddEnumerateCounter(*). As far as I can tell, this command does nothing of the sort. Instead, it seems to add a counter format so it is for adding an analogue to \arabic rather than analogue to c@page, for example. That said, I'm not at all sure I understand it in any way.
I did manage to reproduce my existing result with the following code
\makeatletter
\def\csimplelettered#1{\expandafter\@csimplelettered\csname c@#1\endcsname}
\def\@csimplelettered#1{\@arabic#1}
\makeatother
\AddEnumerateCounter{\csimplelettered}{\@csimplelettered}{2}
\NewDocumentEnvironment{simplelettered}{ O {} m }{%
\begin{enumerate}[label=\textsc{#2}\csimplelettered*., ref=\textsc{#2}\csimplelettered*, #1]
}{%
\end{enumerate}%
}
which lets me write
\begin{simplelettered}{b}
\item first
\item second
\end{simplelettered}
to produce
So then I tried to create a new counter format on-the-fly within the environment definition. Here's one (surely laughable) attempt
\makeatletter
\NewDocumentEnvironment{lettered}{ O {} m }{%
\expandafter\def\csname clettered#2\endcsname ##1{\expandafter\expandafter\expandafter\csname @clettered#2\endcsname\csname c@##1\endcsname}%
\expandafter\def\csname @clettered#2\endcsname ##1{\@arabic##1}%
\AddEnumerateCounter*{\expandafter\csname clettered#2\endcsname}{\expandafter\csname @clettered#2\endcsname}{2}%
\begin{enumerate}[label=\textsc{#2}\arabic*., ref=\textsc{#2}\arabic*, #1]
}{%
\end{enumerate}%
}
\makeatother
Clearly my default strategy of throwing \expandafters randomly in arbitrary places is not proving especially fruitful in this particular case.
And now I'm more interested in this problem than I am in the original one (which I can solve in another way). How can I make the lettered environment work so that it creates a new counter format and uses it in the enumeration?
Complete NMWE:
\documentclass{article}
\usepackage{xparse,enumitem}
\NewDocumentEnvironment{letters}{ O {} m }{%
\begin{enumerate}[label=\textsc{#2}\arabic*., ref=\textsc{#2}\arabic*, #1]
}{%
\end{enumerate}%
}
\makeatletter
\def\csimplelettered#1{\expandafter\@csimplelettered\csname c@#1\endcsname}
\def\@csimplelettered#1{\@arabic#1}
\makeatother
\AddEnumerateCounter{\csimplelettered}{\@csimplelettered}{2}
\NewDocumentEnvironment{simplelettered}{ O {} m }{%
\begin{enumerate}[label=\textsc{#2}\csimplelettered*., ref=\textsc{#2}\csimplelettered*, #1]
}{%
\end{enumerate}%
}
\makeatletter
\NewDocumentEnvironment{lettered}{ O {} m }{%
\expandafter\def\csname clettered#2\endcsname ##1{\expandafter\expandafter\expandafter\csname @clettered#2\endcsname\csname c@##1\endcsname}%
\expandafter\def\csname @clettered#2\endcsname ##1{\@arabic##1}%
\AddEnumerateCounter*{\expandafter\csname clettered#2\endcsname}{\expandafter\csname @clettered#2\endcsname}{2}%
\begin{enumerate}[label=\textsc{#2}\arabic*., ref=\textsc{#2}\arabic*, #1]
% want to use something like:
%\begin{enumerate}[label=\textsc{\csname clettered#2\endcsname*}., ref=\csname clettered#2\endcsname*, #1]
}{%
\end{enumerate}%
}
\makeatother
\begin{document}
\begin{letters}{a}
\item first
\item second
\end{letters}
\begin{simplelettered}{b}
\item first
\item second
\end{simplelettered}
\begin{lettered}{c}
\item first
\item second
\end{lettered}
\end{document}


\expandafterin\expandafter\csname foo\endcsnamedoes nothing; and you can put\expandafters at the end of csnames, like\csname @clettered#2\expandafter\endcsname\csname c@##1\endcsname. Not necessarily relevant, just information. – Manuel Apr 06 '16 at 00:26