3

I'm new to LaTeX and after spending a whole day of googling, I can't really find a solution to this problem. It might be simple, but I have no clue.

I'm using a class, not created by me, with the following code:

\pretocmd{\@chapter}{\begingroup\smallspacing}{}{}
\apptocmd{\@chapter}{\endgroup}{}{}

here's a basic example of a document using that class:

\chapter{Intro}
\label{ch:intro}
This is the Intro

\chapter{First}
This references \ref{ch:intro}

This outputs an empty reference. The problem is that the \label has to be placed between the \begingroup and \endgroup but I have no idea how to do this since the definitions are inside the class. Any ideas on how to do this?

Felipe
  • 165
  • 1
    What about \chapter{\label{ch:intro}Intro}? – siracusa Jun 17 '17 at 06:59
  • could you please provide a MWE? – naphaneal Jun 17 '17 at 07:04
  • @siracusa Thanks!!! That did it. I wasn't aware that I could define the label inside like that. Feel free to post it as an answer so I can accept it. Just in case, is there a way to fix the class itself instead of changing how labels are defined? This is just for curiosity since your answer is good enough for me. – Felipe Jun 17 '17 at 07:13
  • The tag labels is not meant for the \label command! –  Jun 17 '17 at 07:43
  • Of course, the patch done by your class is the wrong one; it should patch \@makechapterhead instead (and \@makeschapterhead). – egreg Jun 17 '17 at 19:34
  • @egreg so, I tried your approach, which fixed the referencing issue, but that also changed the formatting. The first paragraph used to be indented, and by using \@makechapterhead now it's not. – Felipe Jun 17 '17 at 19:35

1 Answers1

3

As mentioned in the comments, you can put the \label in the \chapter title so it will be inside of the local grouping:

\chapter{\label{ch:intro}Intro}
This is the Intro

If you still want to use \label the usual way, you can do that by a small modification of the \apptocmd hook. Here is a full example:

\documentclass{book}

\usepackage{etoolbox}

\makeatletter
\let\smallspacing\relax
\pretocmd{\@chapter}{\begingroup\smallspacing}{}{}
\apptocmd{\@chapter}{%
    \endgroup
    \if@mainmatter
        \addtocounter{chapter}{-1}%
        \refstepcounter{chapter}%
    \fi
}{}{}
\makeatother

\begin{document}
\chapter{Intro}
\label{ch:intro}
This is the Intro.

This references \ref{ch:intro}.
\end{document}

The problem with the local grouping is that it doesn't make the effect of \refstepcounter globally available, i.e. it removes the current internal label marker as soon as the group is closed so that it can't be used by the following \label command anymore. In the example above the \refstepcounter is duplicated after the closing of the group, so it can take effect globally. Note that the chapter counter is still incremented in global scope which makes the decrementation necessary there, too.

siracusa
  • 13,411
  • Thanks again for the fix. I have a follow-up question. I'm fixing the class but I was wondering if the same has to be applied for starred chapters since they also had the same problem. Then again, I have no idea how would somebody go referencing an un-numbered chapter/section... – Felipe Jun 17 '17 at 19:20
  • 1
    @Felipe: The internal command responsible for typesetting the starred chapter heading is \@schapter. But there is no label text to refer to via \label here, hence no extra handling is needed. If you still want to reference it, have a look at How to \label-\ref an un-numbered section for ideas. – siracusa Jun 17 '17 at 21:39