# how to cross-reference a manually incremented counter

How to make the command \ref{pb:p1} work.

\documentclass[onecolumn,a4paper,10pt]{article}
\usepackage{tcolorbox}
\usepackage{geometry}
\geometry{left=3.18cm,right=3.18cm,top=2.54cm,bottom=2.54cm}
\usepackage{hyperref}

\newcounter{problem}[section]
\newcommand{\rproblem}[1]
{
\stepcounter{problem}
\vspace{1em}
\begin{tcolorbox}[colback=black!20!white,colframe=black!50!white,title=\textbf{Problem \arabic{problem}}]
#1
\end{tcolorbox}
}

\begin{document}

\rproblem{\label{pb:p1}
My problem xxx.
}

\end{document}

• Use \refstepcounter instead of \stepcounter. – Heiko Oberdiek Aug 27 '17 at 11:41
• Make it a \refstepcounter instead of \stepcounter. But maybe you should let tcolorbox manage counters (auto counter and then in the title a \thetcbcounter). – TeXnician Aug 27 '17 at 11:41
• @TeXnician Would you mind to add an answer? – aban Aug 27 '17 at 11:44
• Is there any reason why not using tcolorbox mechanism for numbered boxes with labels and adding it to a list of ...? – user31729 Aug 27 '17 at 12:01

I suggest to use either the \newtcolorbox macro in order to create the environment rproblem or \newtcbox to generate a box macro that behaves basically as the \rproblem macro by the O.P.

The counter is used from the definition as problem with use counter=problem and the list of=toc adds the box to the usual ToC, as subsection entries, since \l@problem was defined as being \l@subsection.

The label is given within the optional argument of \rproblem with label=....

I provided both methods: With \newtcbox and \newtcolorbox

\documentclass[onecolumn,a4paper,10pt]{article}
\usepackage{tcolorbox}
\usepackage{geometry}
\geometry{left=3.18cm,right=3.18cm,top=2.54cm,bottom=2.54cm}
\usepackage{hyperref}

\newcounter{problem}[section]

\makeatletter
\let\l@problem\l@subsection
\makeatother

\tcbset{rproblem/.style={colback=black!20!white,
colframe=black!50!white,
title=\textbf{Problem \theproblem},
before skip=1em}
}

\newtcbox[use counter=problem,number within={section},list inside=toc]{\rproblem}[1][]{
rproblem,
#1
}

\newtcolorbox[use counter=problem,number within={section},list inside=toc]{rproblemenv}[1][]{
rproblem,
#1
}

\begin{document}
\tableofcontents

\section{Problems}

\rproblem[label={pb:p1}]{%
My problem xxx.
}

\begin{rproblemenv}[label={pb:p2}]
My problem xxx.
\end{rproblemenv}

This is answer \ref{pb:p1} or \ref{pb:p2}

\end{document}


• Great thanks. how to add the \vspace{1em}? – aban Aug 27 '17 at 12:23
• @aban: Add before skip=1em to the options of the boxes, but do you really mean 1em and not 1ex? – user31729 Aug 27 '17 at 12:27
• How to make the first \rproblem width as the second one. i.e. let 1.1 being \linewidth? – aban Aug 27 '17 at 12:47
• @aban: You can't since a \tcbox uses basically only the width that the content inside have. Only the environment version uses the fixed width of \linewidth (or any other one that has been specified before). The environment approach is the better one, in my point of view! – user31729 Aug 27 '17 at 12:49

If you do not want to use the tcolorbox approach you need to replace \stepcounter with \refstepcounter.

The reason for that is that \label uses \@currentlabel to find out what to refer to. And basically \refstepcounter sets this macro and increments the counter value.