3

I have two counters in my document, counterA and counterB.

I would like the references \ref{} in my document to be printed as 1.a) or 2., etc. where the digit refers to counterA and the letter to counterB.

Is it possible? How can I do it?

\documentclass{article}
\begin{document}

\newcounter{counterA} 
\setcounter{counterA}{0}

\newcounter{counterB}
\setcounter{counterB}{0}


\refstepcounter{counterA}
\refstepcounter{counterB}
\label{First_Label}

I get: \ref{Second_Label}. I would like \textbf{2.}\,b)

Vestibulum lectus metus, tincidunt at fermentum non, pellentesque at lorem. Vivamus nisl sem, tempor ac mi et, elementum feugiat justo. Pellentesque tristique consequat molestie.


\bigskip
\refstepcounter{counterA}
\refstepcounter{counterB}
\label{Second_Label}

I get: \ref{First_Label}. I would like \textbf{1.}\,a)

Morbi nec nibh nulla. Cras posuere erat vitae lacus convallis, ut consequat urna dignissim. 
\end{document}

enter image description here

Colas
  • 6,772
  • 4
  • 46
  • 96

2 Answers2

5

You can use the following command:

\renewcommand{\thecounterB}{\textbf{\thecounterA}.\alph{counterB})}

Here's a complete MWE:

% arara: pdflatex
\documentclass{article}

\newcounter{counterA} 
\setcounter{counterA}{0}

\newcounter{counterB}
\setcounter{counterB}{0}

\renewcommand{\thecounterB}{\textbf{\thecounterA}.\alph{counterB})}
\begin{document}

\refstepcounter{counterA}
\refstepcounter{counterB}
\label{First_Label}

I get: \ref{Second_Label}. I would like \textbf{2.}\,b)

Vestibulum lectus metus, tincidunt at fermentum non, pellentesque at lorem. Vivamus nisl sem, tempor ac mi et, elementum feugiat justo. Pellentesque tristique consequat molestie.


\bigskip
\refstepcounter{counterA}
\refstepcounter{counterB}
\label{Second_Label}

I get: \ref{First_Label}. I would like \textbf{1.}\,a)

Morbi nec nibh nulla. Cras posuere erat vitae lacus convallis, ut consequat urna dignissim. 
\end{document}
cmhughes
  • 100,947
2

Every time one creates a counter variable named, say, counterB, with a \newcounter instruction, LaTeX sets up a macro called \p@counterB that can be used to "prefix" some other information to the label for the sake of creating a cross-reference. I.e., you could type

\makeatletter
\renewcommand\p@counterB{\thecounterA.}
\makeatother

to prefix LaTeX's representation of the current value of counterA plus a . to LaTeX's representation of the value of counterB.

A full MWE:

enter image description here

\documentclass{article}
\newcounter{counterA}
\newcounter{counterB}
\renewcommand\thecounterA{\arabic{counterA}} % arabic numbering
\renewcommand\thecounterB{\alph{counterB})}  % alphabetic numbering
\makeatletter
\renewcommand\p@counterB{\thecounterA.}
\makeatother
\begin{document}
% need to increment the counters via \refstepcounter
\refstepcounter{counterA} \label{refA}
\refstepcounter{counterB} \label{refB}
Here's a cross-reference to item \ref{refB}.
\end{document}
Mico
  • 506,678
  • Thanks! The best would be to redefine both \thecounterA and \thecounterB, so that if \ref{}refers to one or to the other, the result will be the same. – Colas Mar 18 '14 at 20:14
  • I understood your setup to be as follows: counterA is the upper-level or primary variable, and counterB is the secondary or subordinate counter. Is that not the case? By the way, you're free to define \thecounterA and \thecounterB (the "looks" of the individual counters) any way you wish. The MWE simply mimicked the numbering styles you indicated in your query. – Mico Mar 18 '14 at 20:19