2

I am having trouble with \ref after redefining \section. I am working with a template given to me and previously they were changing the definition of \thesection to make the section headings appear as required. This in turn messed up equation numbering because I need "section.equation" and "section.subsection.equation" equation numbering (so the \thesection definition was coming through here). Instead, I returned \thesection to default defined in article.cls and set up the following for \section, \subsection, and \subsubsection.

\def\section{\@ifstar\@mysection\@@mysection}
\def\subsection{\@ifstar\@mysubsection\@@mysubsection}
\def\subsubsection{\@ifstar\@mysubsubsection\@@mysubsubsection}
\newcommand{\@@mysection}[1]{%
    \vskip 24pt%
    \hspace{-3.5ex \@plus -1ex \@minus -.2ex}%
    \stepcounter{section}%
    \if@app%
        \bgroup%
        \Large\bfseries\Alph{section}. #1 \newline
        \egroup
        \numberwithin{equation}{section}
        \renewcommand{\theequation}{\Alph{section}.\arabic{equation}}
    \else%
        \def\thesection{\@arabic\c@section}%
        \bgroup%
        \Large\bfseries\@arabic\c@section. #1 \newline
        \egroup
        \numberwithin{equation}{section}
        \renewcommand{\theequation}{\arabic{section}.\arabic{equation}}
    \fi
}
\newcommand{\@mysection}[1]{%
    \vskip 24pt%
    \hspace{-3.5ex \@plus -1ex \@minus -.2ex}%
    \bgroup%
    \Large\bfseries #1 \newline
    \egroup
}
\newcommand{\@@mysubsection}[1]{%
    \vskip 18pt%
    \hspace{-3.5ex \@plus -1ex \@minus -.2ex}%
    \stepcounter{subsection}%
    \if@app%
        \bgroup%
        \large\bfseries\Alph{section}.\arabic{subsection}. #1 \newline
        \egroup
        \setcounter{equation}{0}
        \numberwithin{equation}{subsection}
        \renewcommand{\theequation{\Alph{section}.\arabic{subsection}.\arabic{equation}}
    \else %
        \bgroup%
        \large\bfseries\@arabic\c@section.\@arabic\c@subsection. #1 \newline
        \egroup
        \setcounter{equation}{0}
        \numberwithin{equation}{subsection}
        \renewcommand{\theequation}{\arabic{section}.\arabic{subsection}.\arabic{equation}}
    \fi 
}
\newcommand{\@mysubsection}[1]{%
    \vskip 24pt%
    \hspace{-3.5ex \@plus -1ex \@minus -.2ex}%
    \bgroup%
    \large\bfseries #1 \newline
    \egroup
}
\newcommand{\@@mysubsubsection}[1]{%
    \vskip 18pt%
    \hspace{-3.5ex \@plus -1ex \@minus -.2ex}%
    \stepcounter{subsubsection}%
    \if@app%
        \bgroup%
        \normalsize\bfseries\Alph{section}.\arabic{subsection}.\arabic{subsubsection}. #1 \newline
        \egroup
        \setcounter{equation}{0}
        \numberwithin{equation}{subsubsection}
        \renewcommand{\theequation}{\Alph{section}.\arabic{subsection}.\arabic{subsubsection}.\arabic{equation}}
    \else %
        \bgroup%
        \normalsize\bfseries\@arabic\c@section.\@arabic\c@subsection.\@arabic\c@subsubsection. #1 \newline
        \egroup
        \setcounter{equation}{0}
        \numberwithin{equation}{subsubsection}
        \renewcommand{\theequation}{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\arabic{equation}}
    \fi 
}
\newcommand{\@mysubsubsection}[1]{%
    \vskip 24pt%
    \hspace{-3.5ex \@plus -1ex \@minus -.2ex}%
    \bgroup%
    \normalsize\bfseries #1 \newline
    \egroup
}

The trouble is that whenever I reference a section, subsection, or subsubsection with \ref, nothing is printed. If I use \hyperref[sec:4]{4} (for example) it works. What am I doing wrong?

  • 5
    Replace \stepcounter by \refstepcounter, but without seeing a compilable document, this is guessing only. The labelling mechanism 'relies' on \refstepcounter. The explicit \setcounter{equation} statements are strange. –  Dec 29 '17 at 22:00
  • @ChristianHupfer That fixed it. Thank you! I had no idea that \stepcounter wasn't what I was supposed to be using. – Jacob House Dec 29 '17 at 22:12
  • 1
    At this point all you have to do is fixed the incorrect bracing in your \@@mysubsection macro and change the three \stepcounters to \refstepcounter. That should straighten out things. Perhaps a better question would be how you came up with these definitions in the first place. Maybe something better can be done (there seems to be a lot of code duplication, even if it's just conditioning on whether or not you're in the appendix - \if@app). – Werner Dec 29 '17 at 22:13
  • 2
    @JacobHouse: Perhaps consider reading this: Understanding how references and labels work – Werner Dec 29 '17 at 22:14
  • 1
    @JacobHouse: Better drop that template ... it's weird (as almost all templates). I don't see any advantage over using the regular \section etc. from article –  Dec 29 '17 at 22:17
  • 1
    @ChristianHupfer I don't like it either; it's for someone else who had strict guidelines of what it was supposed to look like. Like I said, before they just redefined \thesection to achieve the desired effect but this impacted equation numbering so here we are... Before it's all said and done I may try to rework it entirely to reduce all the duplicate code as suggested by @Werner. – Jacob House Dec 29 '17 at 22:20
  • 2
    @JacobHouse: Changing \thesection has multiple side effects. Change \@seccntformat rather. –  Dec 29 '17 at 22:22
  • 2
  • 1
    All six \hspace{-3.5ex \@plus -1ex \@minus -.2ex}% instructions could (in fact: should) be replaced by \noindent... – Mico Dec 29 '17 at 22:32
  • @JacobHouse: Your \refstepcounter comes too early for the appendix code (\if@app), besides that, all that is inside a group, so a change of \thesection would not work outside –  Dec 29 '17 at 22:51
  • @JacobHouse: Place \if@app \renewcommand{\thesection}{\Alph{section}} \fi before \refstepcounter{section} in your \@@mysection macro –  Dec 29 '17 at 22:53

1 Answers1

3

As far as I can tell, your preamble is set to accomplish six things:

  • Change the font sizes used in section, subsection, and subsubsection headers. However, as the new font sizes -- \Large, \large, and \normalsize -- are the defaults for the article document class, the associated instructions may simply be omitted.

  • Change the numbering style of section-level headers from arabic numerals to uppercase letters for sections located in the appendix. This change is performed automatically if the \appendix macro is encountered in the body of the document.

  • Automatically indent the first line of a paragraph after sectioning headers. This is accomplished quite easily by loading the indentfirst package.

  • Change the numbering style for equations, to always prefix the section, subsection, and subsubsection numbers.

  • Change the spacing above section, subsection, and subsection headers from rubber lengths to fixed lengths of 24pt, 18pt, and 18pt, respectively.

  • Affix a "dot" to section, subsection, and subsection numbers in the associated headers.

These tasks could be achieved succinctly with the following code:

\documentclass[10pt]{article}
\usepackage{amsmath}
\usepackage{indentfirst} % indent first line after sectioning headers
\usepackage{etoolbox}    % for "\patchcmd" and "\pretocmd" macros
\usepackage[colorlinks]{hyperref} % hyperlinking of cross-references

%% Change equation numbering style
\pretocmd{\section}{\numberwithin{equation}{section}}{}{}
\pretocmd{\subsection}{\numberwithin{equation}{subsection}}{}{}
\pretocmd{\subsubsection}{\numberwithin{equation}{subsubsection}}{}{}

\makeatletter

%% Change spacing above sectioning headers
\patchcmd{\section}{-3.5ex \@plus -1ex \@minus -.2ex}{-24pt}{}{}
\patchcmd{\subsection}{-3.25ex\@plus -1ex \@minus -.2ex}{-18pt}{}{}
\patchcmd{\subsubsection}{-3.25ex\@plus -1ex \@minus -.2ex}{-18pt}{}{}

%% Affix "dot" to sectioning numbers
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
   {\csname the#1\endcsname\ }%       default
   {\csname #1@cntformat\endcsname}}% enable indiv. control
\newcommand\section@cntformat{\thesection.\ }
\newcommand\subsection@cntformat{\thesubsection.\ }
\newcommand\subsubsection@cntformat{\thesubsubsection.\ }

\makeatother

A full MWE (minimum working example):

enter image description here

\documentclass[10pt]{article}
\usepackage{amsmath}
\usepackage{indentfirst} % indent first line after sectioning headers
\usepackage{etoolbox} % for "\patchcmd" and "\pretocmd" macros
\usepackage[colorlinks]{hyperref}

%% Change equation numbering style
\pretocmd{\section}{\numberwithin{equation}{section}}{}{}
\pretocmd{\subsection}{\numberwithin{equation}{subsection}}{}{}
\pretocmd{\subsubsection}{\numberwithin{equation}{subsubsection}}{}{}

\makeatletter
%% Change spacing above sectioning headers
\patchcmd{\section}{-3.5ex \@plus -1ex \@minus -.2ex}{-24pt}{}{}
\patchcmd{\subsection}{-3.25ex\@plus -1ex \@minus -.2ex}{-18pt}{}{}
\patchcmd{\subsubsection}{-3.25ex\@plus -1ex \@minus -.2ex}{-18pt}{}{}

%% Affix "dot" to sectioning numbers
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
   {\csname the#1\endcsname\ }%       default
   {\csname #1@cntformat\endcsname}}% enable indiv. control
\newcommand\section@cntformat{\thesection.\ }
\newcommand\subsection@cntformat{\thesubsection.\ }
\newcommand\subsubsection@cntformat{\thesubsubsection.\ }

\makeatother

\begin{document}
\section{Hello} \label{sec:hello}
\begin{equation}\label{eq:first} 1+1=2 \end{equation}

\subsection{Good morning} \label{sec:morning}
\begin{equation}\label{eq:second} 1+1=2 \end{equation}

\subsubsection{Good afternoon} \label{sec:afternoon}
\begin{equation}\label{eq:third} 1+1=2 \end{equation}

\appendix % <-- change numbering style of section-level headers

\section{Goodbye} \label{sec:goodbye}
\begin{equation}\label{eq:fourth} 1+1=2 \end{equation}

% Generate cross-references to sectioning headers and to equations:
\ref{sec:hello}, \ref{sec:morning}, \ref{sec:afternoon}, \ref{sec:goodbye}

\eqref{eq:first}, \eqref{eq:second}, \eqref{eq:third}, \eqref{eq:fourth}
\end{document}
Mico
  • 506,678