Как можно отформатировать ссылки на несколько счетчиков с помощью \ref{}?

Как можно отформатировать ссылки на несколько счетчиков с помощью \ref{}?

У меня в документе есть два счетчика: counterAи counterB.

Я бы хотел, чтобы ссылки \ref{}в моем документе были напечатаны как 1.a)или 2.и т. д., где цифра относится к counterA, а буква к counterB.

Возможно ли это? Как это сделать?

\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}

введите описание изображения здесь

решение1

Каждый раз, когда создается переменная счетчика с именем, скажем, counterB, с \newcounterинструкцией, LaTeX устанавливает макрос с именем \p@counterB, который может быть использован для «префикса» некоторой другой информации к метке с целью создания перекрестной ссылки. То есть, вы можете ввести

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

для добавления префикса LaTeX-представления текущего значения counterAплюс a .к LaTeX-представлению значения counterB.

Полная версия MWE:

введите описание изображения здесь

\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}

решение2

Вы можете использовать следующую команду:

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

Вот полный 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}

Связанный контент