2

I have a question to manipulate cross references to enumeration lists. To change the appearance of the list, I know the commands \theenumi to choose the type (roman, arabic, alph...), \labelenumi for the label in the list and \p@enumi as prefix for a cross reference. But is there also a macro for the postfix?

As an example I would like to realize the following:

\begin{enumerate}
    \item \label{item 1} Item
    \begin{enumerate}
        \item \label{subitem 1} Subitem
        \item Second Subitem
    \end{enumerate}
    \item Another item
\end{enumerate}

where a reference \ref{item1} gives (i) and \ref{subitem 1} gives (ia). I can define

\renewcommand*{\theenumi}{\roman{enumi}}
\renewcommand*{\theenumii}{\alph{enumii}}
\renewcommand*{\labelenumi}{(\theenumi)}
\renewcommand*{\labelenumii}{(\theenumii)}

which gives me (i) and (a) in the list itself. For the cross references I could try something like

\renewcommand*{\p@enumi}{(}
\renewcommand*{\p@enumii}{\p@enumi\theenumi}

which gives me the first bracket, resulting in (i and (ia. But how to close the bracket?

More academically than for real use I would like to know also a solution for three or four nested lists (although it would look ugly).

I suppose there are easy solution using additional packages. But my question is really about a possible macro for a postfix, which somehow I would expect to exist. Therefore an additional question: How can I (as newbie) find a list of defined macros?

  • 1
    Look at section 51.2 of source2e.pdf (type texdoc source2e at the terminal, then go to section 51.2). – GuM Mar 5 '16 at 22:46
1

For your elementary example it suffices to capture what follows the prefix using TeX's parameter text setup:

enter image description here

\documentclass{article}

\makeatletter
\renewcommand*{\theenumi}{\roman{enumi}}
\renewcommand*{\theenumii}{\alph{enumii}}
\renewcommand*{\labelenumi}{(\theenumi)}
\renewcommand*{\labelenumii}{(\theenumii)}

\def\p@enumi\csname #1\endcsname{(\theenumi)}% First level
\def\p@enumii\csname #1\endcsname{(\theenumi\theenumii)}% Second level
% \def\p@enumiii\csname #1\endcsname{(\theenumi\theenumii\theenumiii)}% Third level
%...
\makeatother

\begin{document}

\section{First}\label{section1}

\begin{enumerate}
  \item \label{item1} Item
  \begin{enumerate}
    \item \label{subitem1} Subitem
    \item Second Subitem
  \end{enumerate}
  \item Another item~\ref{subitem1}
\end{enumerate}

Those are references to items:~\ref{item1} and~\ref{subitem1}, but this reference here~\ref{section1} does not have parentheses. 

\end{document}
| improve this answer | |
2

It is in fact the \label command that writes a \newlabel{foo}{\@currentlabel}{\thepage} to the .aux file. Since \@currentlabel is already the expanded version of \p@... etc. it's too early to use something like (\p@...).

In my point of view the (...) pair has to be written to the .aux file by the \@currentlabel{...} and can't be added to \p@.... itself, due to \protected@edef and the involved expansion of the \@p... chain.

I've applied this strategy by adding some code after the start-up of enumerate, redefining \@currentlabel only slightly to have this (...) pair as wrapper around \p@...\the....

A redefinition of \refstepcounter to add the (...) is not sufficient -- this would be applied to any counter being refstepped. The following code checks first if the current environment is enumerate and applies the relevant setting then. (It might fail however for other counters that are \refstepped with enumerate, but not within a group.

Since all is in an (environment) group, the redefinition of \refstepcounter does not leak outside.

\documentclass{article}

\makeatletter

\renewcommand*{\theenumi}{\roman{enumi}}
\renewcommand*{\theenumii}{\alph{enumii}}
\renewcommand*{\labelenumi}{(\theenumi)}
\renewcommand*{\labelenumii}{(\theenumii)}



\newcommand{\@@labelprefix}{%
(%
}

\newcommand{\@@labelpostfix}{%
  )%
}


\let\@@latex@@refstepcounter\refstepcounter

\def\is@@enumerate{enumerate}

\def\refstepcounter#1{%
  \ifx\@currenvir\is@@enumerate
  \stepcounter{#1}%
  \protected@edef\@currentlabel{\@@labelprefix\csname p@#1\endcsname\csname the#1\endcsname\@@labelpostfix}
  \else
  \@@latex@@refstepcounter{#1}%
  \fi
}

\makeatother
\begin{document}
\section{First}\label{section1}
\begin{enumerate}
    \item \label{item1} Item
    \begin{enumerate}
        \item \label{subitem1} Subitem
        \item Second Subitem
          \begin{figure}
            \caption{foo}\label{firstfoo}
            \end{figure}

    \end{enumerate}
    \item Another item \ref{subitem1}
\end{enumerate}


Those are references to items: \ref{item1} and \ref{subitem1}, but this reference here \ref{section1} does not have parentheses. 

A reference for \ref{firstfoo}

\end{document}

It would be easier with enumitem package ;-)

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.