Followup to Split the reference counter for any numbered item (like with subequations) : I was trying to make a generic subanything macro, and I fail miserably - see MWE below for what I get as error at, basically, \setcounter{parent#1}{\value{#1}}:
\documentclass{article} \usepackage{amsmath} \usepackage{listings} \usepackage{xcolor} % \pagecolor \usepackage{hyperref} \pagecolor{yellow!15} \usepackage{trace} % modded from {subequations}, `kpsewhich amsmath.sty` \newcounter{parentlstlisting}% Counter for ``parent lstlisting''; set up manually for all that may be used in subanything \makeatletter \newenvironment{subanything}[1][]{% \traceon \xdef\subanyin{#1}% \xdef\dosubany{1}% \typeout{subanything: inside '#1'}% \ifx#1\undefined% \xdef\dosubany{}% \typeout{subanything: Didn't get argument: '#1'; skipping start}% \else% \ifx\subanyin\empty% \xdef\dosubany{}% \typeout{subanything: Didn't get argument: '#1'; skipping start}% \else% \refstepcounter{#1}% \typeout{subanything: A}% \edef\tcsa{theparent\subanyin}% \edef\tcsb{the\subanyin}% \expandafter\protected@edef\expandafter\csname\tcsa\endcsname{\expandafter\csname\tcsb\endcsname}% \typeout{subanything: B}% \edef\tcsc{parent#1}% \makeatletter% % problem here with setcounter: % \csname theparentlstlisting\endcsname ->1 % ! Use of \csname doesn't match its definition. % <argument> c % @parentlstlisting % \@ifundefined #1->\expandafter \ifx \csname #1 % \endcsname \relax \expandafter... \expandafter\setcounter\expandafter{\tcsc}{\value{#1}}% \setcounter{#1}{0}% \typeout{subanything: C}% \expandafter\def\csname the\subanyin\endcsname{\expandafter\csname theparent\subanyin\endcsname\alph{#1}}% \ignorespaces% \fi% \fi% }{% \ifx\dosubany\empty% ifx #1: ! Illegal parameter number in definition of \endsubanything.: 1 \typeout{subanything: skipping end}% \else% \setcounter{\subanyin}{\value{parent\subanyin}}% \ignorespacesafterend% \fi% } \makeatother \begin{document} Hello... \begin{subequations}\label{eq:ex1} \begin{minipage}{0.45\textwidth} \begin{align} \label{eq:ex1a} a = b + c \end{align} \end{minipage} \begin{minipage}{0.45\textwidth} \begin{align} \label{eq:ex1b} x = y + z \end{align} \end{minipage} \end{subequations} \vspace{\baselineskip} Ref'ing: master eq.~\ref{eq:ex1}; inner a eq.~\ref{eq:ex1a}, inner b eq.~\ref{eq:ex1b} ... \begin{subanything}[lstlisting] \label{lst:ex1} \noindent\begin{minipage}[t]{.325\textwidth} \begin{lstlisting}[basicstyle=\scriptsize\ttfamily, caption={[short] Some instructions here; the font here is \texttt{\ttdefault}.}, escapechar=!, showlines=true, label=lst:ex1a, columns=fixed, frame=tlrb] 080484c4 <list>: 80484c4: cmd one 80484c7: cmd two 80484ca: cmd three, four 80484cf: cmd five 80484d6: cmd six, seven 80484dd: cmd more than enough 80484e0: cmd not_even_joking \end{lstlisting} \end{minipage} \hspace{1cm} \noindent\begin{minipage}[t]{.325\textwidth} \begin{lstlisting}[basicstyle=\scriptsize\ttfamily, caption={[short] Some instructions here; the font here is \texttt{\ttdefault}.}, escapechar=!, showlines=true, label=lst:ex1b, columns=fullflexible, % basewidth=\tlen, frame=tlrb] 080484c4 <list>: 80484c4: cmd one 80484c7: cmd two 80484ca: cmd three, four 80484cf: cmd five 80484d6: cmd six, seven 80484dd: cmd more than enough 80484e0: cmd not_even_joking \end{lstlisting} \end{minipage} \end{subanything} Ref'ing: inner a listing~\ref{lst:ex1a}, inner b listing~\ref{lst:ex1b} ... \end{document} It looks like it's breaking on the at @ in c@parentlstlisting - which should be absurd, given we're explicitly inside a \makeatletter?! What am I missing here?

\expandafter\protected@edef\expandafter\csname\tcsa\endcsname{\expandafter\csname\tcsb\endcsname}%, line number 29, and it runs at least syntactically correct if I comment it out\protected@edef\theparent#1{\the#1}, but it was giving me problems like that, so I tried to\expandafter-it; and I thought it was solved, seeing the error show on\setcounterline... will need to take a look again. Cheers!