7

I created my own counter and an environment named problem. The counter increments on the level of a chapter, not section. When I label, e.g. \label{prb:3} and refer to it later in the text by \ref{prb:3}, instead of inserting the true problem number that was linked to that label, section number within which the problem lies gets inserted. That obiously makes a serious trouble. Although I checked some similar previous questions on this problem, I have not recognized something that helps my case. For example, using Problem~\hyperref[prb:3]{prb:3} prints Problem prb:3, while it should, for example, be Problem 5-8.

Essentially, my \label and \ref command should link to my problem number and not to the section number within which the problem lies.

I believe there are more than enough LaTeX experts that can help me set up and resolve this case, preferably without complicated definitions.

Here is some MWE:

\documentclass[a4paper,11pt, oneside]{book}
\usepackage[utf8]{inputenc}
%%% my custom counter `problem` and the environment, which increments on the chapter level
\newcounter{problem}
\newenvironment{problem}{
\vspace{10pt}\noindent\stepcounter{problem}\\
{\bf\thechapter-\theproblem}\hspace{10pt}
}{}

\begin{document}

\chapter{First Chapter}

\section{Section 11}
    \begin{problem}
            Problem~1-1. \label{prb 11}  % Problem 1-1
    \end{problem}

\section{Section 12}
    \begin{problem}
            Problem 1-2. \label{prb 12}  % Problem 1-2
    \end{problem}
    \begin{problem}
            Problem 1-3. \label{prb 13} \\  % Problem 1-3
    \end{problem}

\noindent Referring to Problem~1-3 by \ref{prb 13} incorrectly refers to Problem~1.2 because Problem~1-3 is in Section~\ref{prb 13} (that is: Section 1.1). \ \

\setcounter{problem}{0} \chapter{Second Chapter}

\section{Section 21}
    \begin{problem}
        Problem 2-1. \label{prb 21}  % Problem 2-1
    \end{problem}
    \begin{problem}
        Problem 2-2. \label{prb 22}  % Problem 2-2
    \end{problem}
    \begin{problem}
        Problem 2-3. \label{prb 23} \\  % Problem 2-3
    \end{problem}

\noindent Now referring to Problem~2-3 by \ref{prb 23} erroneously prints as Problem~2.1 because Problem~2-3 is within Section~\ref{prb 23} (that is: Section 2.1). \ \

So, not only is the number incorrect, but also the format. Instead of having chapter-problem# format, it has section# format. \

The question is: how can I make the \verb|\label| and \verb|\ref| not refer to a Section number, but to the Problem number within a particular chapter?

\end{document}

itc
  • 657
MSJ
  • 125

2 Answers2

12

There are at least three problems, or issues, with your problem-related code [pun intended].

  • The most serious one is that you're using \stepcounter instead of \refstepcounter to increment the counter called problem. The \label-\ref mechanism relies (with a few exceptions) on LaTeX associating the argument of \label with the counter that was most recently incremented via a \refstepcounter directive. \stepcounter doesn't cut it; that is why the \ref directives keep pointing to section numbers since, as you may have guessed by now, the section counter is incremented via a \refstepcounter directive whenever a \section command is executed.

  • Right now, you have to run \setcounter{problem}{0} at the start of each chapter. If you replace \newcounter{problem} with \newcounter{problem}[chapter], LaTeX resets the problem counter to 0 automatically each time a \chapter directive occurs.

  • Since you wish to display the "number" of the problem as "ChapterNum-ProblemNum", you might as well run

    \renewcommand\theproblem{\arabic{chapter}-\arabic{problem}}
    

    in the preamble


enter image description here

\documentclass[oneside]{book}
%% \usepackage[utf8]{inputenc} that's the default nowadays
\newcounter{problem}[chapter]
\renewcommand\theproblem{\arabic{chapter}-\arabic{problem}}
\newenvironment{problem}{%
  \par\vspace{10pt}\noindent\refstepcounter{problem}% % note "\refstepcounter"
  \textbf{\theproblem}\hspace{10pt}}%
  {\par}

\usepackage[colorlinks]{hyperref} % optional

\begin{document}

\chapter{First Chapter} \section{Section A} \begin{problem} Problem~1-1. \label{prb 11} % Problem 1-1 \end{problem} \section{Section B} \begin{problem} Problem 1-2. \label{prb 12} % Problem 1-2 \end{problem} \begin{problem} Problem 1-3. \label{prb 13} % Problem 1-3 \end{problem}

\bigskip\noindent Now cross-referencing Problem~1-3 as \ref{prb 13}, \emph{not} as [section] 1.2.

\chapter{Second Chapter} \section{Section C} \begin{problem} Problem 2-1. \label{prb 21} % Problem 2-1 \end{problem} \begin{problem} Problem 2-2. \label{prb 22} % Problem 2-2 \end{problem} \begin{problem} Problem 2-3. \label{prb 23} % Problem 2-3 \end{problem}

\bigskip\noindent Now cross-referencing Problem~2-3 as \ref{prb 23}, \emph{not} as [section] 2.1.

\end{document}


In response to @Mico, this is the new MWE explaining how \ref works when referring to \label that was defined in the main file as opposed to the \label defined in the outer file.

\documentclass[oneside]{book}

\newcounter{problem}[chapter] \renewcommand\theproblem{\arabic{chapter}-\arabic{problem}} \newenvironment{problem}{% \par\vspace{10pt}\noindent\refstepcounter{problem}% \textbf{\theproblem}\hspace{10pt}}% {\par} %\renewcommand\theproblem{\arabic{chapter}-\arabic{problem}}

\usepackage[colorlinks]{hyperref} % optional

\begin{document}

\chapter{Chapter Title} \section{Section Title} \begin{problem} Problem 1-1. \label{prb_11} \end{problem} \begin{problem} Problem 1-2. \label{prb_12} \end{problem}

    \input{prb_13}

\bigskip\noindent When the \verb|\label| is used within the main file, the cross-referencing works well and \verb|\ref{prb_11}| gives \ref{prb_11} and \verb|\ref{prb_12}| gives \ref{prb_12}. Correct!

\medskip\noindent In contrast, when the label is defined within the outer file named \verb|prb_13| and included using the \verb|\input| command, the cross-referencing does not work well and \verb|\ref{prb_13}| gives \ref{prb_13}, which is not the problem number, but the section number.

\end{document}

Mico
  • 506,678
  • Thank you for your timely response and a very efficient remedy. That's what I was hoping for! – MSJ Aug 17 '22 at 05:06
  • The only glitch I find with your \renecommand suggestion is that is incorrectly then prints the problem number in my problem-environment, e.g. instead of 5-8, it prints 5-5-8. – MSJ Aug 17 '22 at 05:56
  • @MSJ - Please provide more information about how you "print the problem number in the problem-environment". – Mico Aug 17 '22 at 06:07
  • Perhaps my word print I used was misleading. What I referred to was the automatic text (i.e. the label) that is typeset before the text of some problem. It is included in the above code as is, constituted by the syntax \begin{problem} some text \end{problem} That block then produces the problem numbers in the sequence 1-1, 1-2 etc, as indicated above. Using the syntax you suggested at the end as \renewcommand\theproblem actually read 1-1-1 instead of 1-1. I see in your snippet it reads well, but I got it inadequate in the text. I'll test it again and report. – MSJ Aug 17 '22 at 09:13
  • @MSJ - The reason I asked you to follow up was that code I posted generates 1-1, 1-2, etc (a claim that may be verified by consulting the screenshot) and not 1-1-1, 1-1-2, etc – Mico Aug 17 '22 at 09:51
  • I know, but I believe I figured out now what the case was. – MSJ Aug 17 '22 at 13:13
  • I know, but I believe I figured out now what the case was. In my true manuscript, I insert codes from outer files and:
    • When the \verb|\label| is used within the main file (as was in the MWE), the cross-referencing works well and \verb|\ref{prb_12}| CORRECTLY gives 1-2.

    • In contrast, when the label is defined within the outer file named \verb|prb_13| and included using the \verb|\input| command, the cross-referencing does not work well and \verb|\ref{prb_13}| gives 1.1, which is not the problem number, but the section number.

    No room here to include that example.

    – MSJ Aug 17 '22 at 13:20
  • 1
    I just explanded the original post with this new MWE. You can take a look if the admin approves it soon. Thanks! – MSJ Aug 17 '22 at 13:24
  • Your newly added code attempts to input a file called prb_13.tex. Unfortunately, we don't know what may be in it. Please advise. – Mico Aug 17 '22 at 14:24
  • 1
    Thanks for your interest. The file prb_13.tex, for the sake of this MWE, just contains the following: \label{prb_13} \begin{problem} Problem 1-3 % some sample text \end{problem} – MSJ Aug 17 '22 at 18:53
  • @MSJ - Do look at your two "in file" problem environments: In both cases, \label comes between\begin{problem} and \end{problem} -- and cross-referencing works just fine. Why are you doing things differently when you place the self-contained problem environment in an external file? I.e., why do you suddenly place \label before \begin{problem}? For sure, if you place \label{prb_13} after instead of before \begin{problem}, everything will be just fine in terms of cross-referencing capabilities. – Mico Aug 17 '22 at 19:05
  • I am sorry, I didn't mean to withhold any relevant info. Just not apt enough with this 5-min editing policy and MWE. So, my main file has like: \chapter{One} \section{One} \input{file1}

    while file1 contains: `\begin{problem} \input{prb_1} \end{problem}

    \begin{solution} % another environment w/out counter \input{sol_1} \end{solution}`

    where some line in sol_1 has \ref{prb_1}. I had \label as the 1st line of prb_1 - didn't help. I now tried to have \label within the \begin{problem}. The outcome is the same: correct problem number, but w/out chapter num.

    – MSJ Aug 17 '22 at 21:25
  • 1
    Great news at last. With what I described just above, your previously suggested line reading: \renewcommand\theproblem{\arabic{chapter}-\arabic{problem}} now did make a difference (for whatever the reason, there was no difference before). I did not necessarily need to explicitly place label within \begin{problem} block. It sufficed to have it within the file that is input within the \begin{problem} block. Thanks a lot for not giving up on me! :) – MSJ Aug 17 '22 at 21:37
  • @MSJ - Your issue is utterly unrelated to the instructions residing in an external file that is loaded via \input. If \label occurs before \begin{problem} in file1.tex, then LaTeX has no choice but to associate the argument of \label with the counter that was most recently incremented via \refstepcounter; by your description, this happens to be the section counter. You must place \label between \begin{problem} and \end{problem} if you want LaTeX to associate the argument of \label with the problem environment. – Mico Aug 17 '22 at 21:38
  • My understanding was that having \input{filename}, which contains \label{prb}, within \begin{problem} environment had the same effect as explicitly defining \label next to \input{filename} when within \begin{problem}. I started celebrating a bit too soon. The \renewcommand\theproblem fixed referencing of the problem, but caused the incorrect displaying of the problem number where the problem is originally typed with, like 5-5-8. I mentioned that earlier. I cannot quite explain why my attempt with \newcommand{\pref}[1]{\thechapter-\ref{#1}} reconciles both goals, but it works. Thx! – MSJ Aug 17 '22 at 22:20
  • @MSJ - I must confess that I've lost track of what precisely you're doing in the external files that are loaded via \input statements. You do seem to strongly resist the suggestion to just place the \label statements inside the problem environments; I honestly don't understand this apparent reluctance to follow a particular suggestion. If the formatting issues persist, do feel free to post a new query, in which you explain in full detail what you're doing and what you're trying to achieve. – Mico Aug 17 '22 at 22:54
  • Sorry if there is a misunderstanding. I was not reluctant to place \label inside the problem environment. I tested it tonight after your recent comment. But the outcome was no different from what I had prior to that. Look, you've been very committed to help me resolve my issue and I am very greatful for your effort. Thanks a lot. I don't mean to occupy you with this any longer. I understand that. I made a progress. And not only the progress. I seem to have resolved it using a combo of what you suggested me and my desperate line of \newcommand{\pref} that works after all. Thanks again! – MSJ Aug 17 '22 at 23:00
1

Edit: See the comment and the answer of David Carlisle and Mico respectively (and "respect-worthy")

You can do it with custom labels like here:

https://tex.stackexchange.com/a/18192/120578

\documentclass[a4paper,11pt, oneside]{book}
\usepackage[utf8]{inputenc}
%%% my custom counter `problem` and the environment, which increments on the chapter level
\newcounter{problem}
\newenvironment{problem}{
\vspace{10pt}\noindent\stepcounter{problem}\\
{\bf\thechapter-\theproblem}\hspace{10pt}
}{}

\makeatletter \newcommand{\clabel}[1]{% \protected@write @auxout {}{\string \newlabel {#1}{{\thechapter-\theproblem}{}}}} \makeatother

\begin{document}

\chapter{First Chapter}

\section{Section 11}
    \begin{problem}
            Problem~1-1. \clabel{prb11}  % Problem 1-1
    \end{problem}

\section{Section 12}
    \begin{problem}
            Problem 1-2. \clabel{prb12}  % Problem 1-2
    \end{problem}
    \begin{problem}
            Problem 1-3. \clabel{prb13}  % Problem 1-3
    \end{problem}

\noindent Referring to Problem~1-3 by \ref{prb13} correctly refers to Problem~1.2 [because Problem~1-3 is in Section~\ref{prb13} (that is: Section 1.1). ]

\setcounter{problem}{0} \chapter{Second Chapter}

\section{Section 21}
    \begin{problem}
        Problem 2-1. \clabel{prb21}  % Problem 2-1
    \end{problem}
    \begin{problem}
        Problem 2-2. \clabel{prb22}  % Problem 2-2
    \end{problem}
    \begin{problem}
        Problem 2-3. \clabel{prb23} \\  % Problem 2-3
    \end{problem}

\noindent Now referring to Problem~2-3 by \ref{prb23} [prints as Problem~2.1 because Problem~2-3 is within Section~\ref{prb23} (that is: Section 2.1). ]

So, not only is the number incorrect, but also the format. Instead of having chapter-problem# format, it has section# format.

The question is: how can I make the \verb|\label| and \verb|\ref| not refer to a Section number, but to the Problem number within a particular chapter?

\end{document}

koleygr
  • 20,105
  • 3
    no sorry this is bad advice, there is no reason not to use a standard \label here. – David Carlisle Aug 16 '22 at 18:20
  • Thanks @DavidCarlisle... I will check your answer soon – koleygr Aug 16 '22 at 18:24
  • Thanks to everybody in any case. I go through all the suggested edits. I found the reply by @Mico a bit more elegant, except that using \ref by his suggested \renewcommand\theproblem' form spoil printing of the format in my problem-environment, while securing a correct form within the text, where\refis used. For now, I work around it by introducing another form of\refas\newcommand{\pref}[1]{\thechapter-\ref{#1}}', which serves just for referencing the problem numbers and it works well, except that I am not too happy for having another form of \ref command. – MSJ Aug 17 '22 at 05:44
  • @MSJ Should that have been a comment on Mico's answer? The typo in the formatting is not helping, but I don't think you want to combined content with \ref. If you're still having trouble printing something, can you clarify what you're wanting? It might entail asking a new question. – Teepeemm Aug 17 '22 at 14:35