# Make \refstepcounter (\@currentlabel) visible outside its group

I defined my own list environment. See the following MWE: (The actual definition is much more complicated in case why you ask why I am not using the standard lists)

\documentclass{article}

\makeatletter

\newcounter{mylist}
\renewcommand*{\themylist}{\Alph{mylist}}
\newcommand*{\labelmylist}{\themylist}

\newcommand*{\makemylabel}[1]{%
\refstepcounter{mylist}%
\@mklab{\labelmylist}%
}

\newenvironment{mylist}{
\list{\@empty}{
\let\makelabel\makemylabel
\setcounter{mylist}{0}
}
}{
\endlist
}

\makeatother

\begin{document}

\section{Section Title}

\begin{mylist}
\item\label{foo} List entry 1
\end{mylist}

The reference are wrong: \ref{foo}.

\end{document}


The error is that \ļabel{foo} creates a reference to the subsection counter and thus \ref{foo} prints the wrong label.

I understand that \refstepcounter redefines \@currentlabel and \label ships \@currentlabel out to the aux file.

But \refstepcounter uses \edef (and not \xdef) and thus \@currentlabel is reset to its old definition after the \makemylabel commands ends. Thus \label{foo} uses the counter of \section.

How do I get \label{foo} to use the correct counter?

The not so minimimal MWE due to questions from @david-carlisle and @alan-munn

\documentclass{article}

\makeatletter

\newcounter{mylist}
\renewcommand*{\themylist}{FA-\Alph{mylist}}
\newcommand*{\labelmylist}{\themylist}

\newcommand*{\makemylabel}[1]{%
\refstepcounter{mylist}%
\@mklab{\labelmylist}%
}

\newenvironment{mylist}{
\list{\@empty}{
\let\makelabel\makemylabel
\setcounter{mylist}{0}
}
}{
\endlist
}

\makeatother

\begin{document}

\section{Section Title}

\begin{mylist}
\item\label{foo} List entry A
\item[5] List entry E
\item\label{bar} List entry F
\end{mylist}

The reference are wrong: \ref{foo} and \ref{bar}.

\end{document}

• the intended use is to use \usecounter{mylist} in your \list setup then setting to 0 and a \refstepcounter will be automatic at each \item – David Carlisle Nov 19 '17 at 18:35
• Unfortunately, this does not work for me. You cannot see that because of the MWE. In my case \item[n] does not use n as the label, but sets \setcounter{mylist}{n} and then use \themylist to produce the actual label. I did not want to redefine all the logic of \item from "The L A TEX 2ε Sources " so I tried to put that logic into \makemylabel. However all that is not shown in the MWE. – nagmat84 Nov 19 '17 at 18:42
• Then you need to explain exactly what you are trying to achieve since there may be a better way. – Alan Munn Nov 19 '17 at 18:46
• unrelated but \if#1 is wrong, it will be true if the first two letters of #1 are the same. try \item[111] List entry E – David Carlisle Nov 19 '17 at 22:20

I think it would be better to structure the code to use the first argument of \list that is there specifically to control the counter used for referencing, however starting from where you are, you can lift the definition of \@currentlabel out of the group using \aftergroup:

\documentclass{article}

\makeatletter

\newcounter{mylist}
\renewcommand*{\themylist}{FA-\Alph{mylist}}
\newcommand*{\labelmylist}{\themylist}

\newcommand*{\makemylabel}[1]{%
\if#1\@empty% you probably want \ifx\relax#1\relax
\refstepcounter{mylist}%
\protected@xdef\zzz{\def\noexpand\@currentlabel{\@currentlabel}}%
\aftergroup\zzz
\@mklab{\labelmylist}%
}

\newenvironment{mylist}{%%%%%
\list{}{%%%%%
\let\makelabel\makemylabel
\setcounter{mylist}{0}%%%%%
}
}{%%%%%%%%%%
\endlist
}

\makeatother

\begin{document}

\section{Section Title}

\begin{mylist}
\item\label{foo} List entry A
\item[5] List entry E
\item\label{bar} List entry F
\end{mylist}

The reference are wrong: \ref{foo} and \ref{bar}.

\end{document}