2

Well, I am trying to write a short summary about my template and I have come to a crossroads. In my class definition, I redefined the \section{} command to \section*{} to maintain a certain stylization of my template, but now I want to reference the sections independently, but keep the property that if I write \section{A random title} the title will remain unnumbered.

Let me explain this in more detail:

  1. I redefined the \section{} command with these lines of code:

    \let\oldsection\section                             
    \renewcommand{\section}[1]{%                        
       \oldsection*{#1}                                
       \phantomsection                                
       \addcontentsline{toc}{section}{#1}              
    }
    
  2. When I use \ref{A label given to the section} in two different sections, I get the following result:

enter image description here

  1. I do not need the \section{} command to be numbered because I have created a mini-summary that contains the name of each section within the chapter as follows:

enter image description here

I think it is not necessary to include the code of this mini-summary, so as not to overload the post with information.

Essentially, I would like help just to figure out how to make this \ref{} work.

Mico
  • 506,678
Jimeens
  • 617
  • 1
    LaTeX's \label-\ref mechanism works by (a) \label{<arg>} creating an association between <arg> and the most recently incremented LaTeX counter (specifically, the counter has to be incremented via a \refstepcounter instruction), and (b) \ref{<arg>} resolving to the value of the counter at the time it got associated with <arg>. By using \section* instead of \section, you are going out of your way not to increment the counter called section. What do you think \label should be associated with? If \ref{<arg>} resolved to "2", what are your readers supposed to make of it? – Mico Mar 04 '23 at 10:26
  • I wanted this \label{} to associate with the respective section number, but not print it when I used the \section{}, like a "ghost number". – Jimeens Mar 04 '23 at 10:33
  • 1
    By using \section* instead of \section, you're not only not printing the section number, i.e., the value of the counter called section, you are also making sure that the section counter isn't incremented to begin with. After all, what would it even mean for a number be associated with a sectioning header that, by design, doesn't have a number associated with it? I've noticed that you've set the tag "hyperref". Why aren't you using that package's \hypertarget/\hyperlink mechanism to create cross-references to unnumbered objects? – Mico Mar 04 '23 at 10:42
  • 1
    For an example of how to use the \hypertarget/\hyperlink mechanism to create cross-rereferences to unnumbered "objects", please see the posting How to cross-reference an unnumbered theorem using hyperref and cleveref. – Mico Mar 04 '23 at 10:52
  • you should always add a small compilable example as this makes it much easier to test your issue (and possible solutions). Apart from this: what should a reader do with a reference to a "ghost number"? Count the section by hand to find the one you mean? @mico There is no need for a \hypertarget, there is a \phantomsection (and hyperref adds targets to unnumbered sections anyway). So hyperlinks should simply work. – Ulrike Fischer Mar 04 '23 at 10:57
  • @UlrikeFischer - Thanks. What I happen to like about \hypertarget is its generality: The mechanism works equally well with unnumbered sections, unnumbered theorems, unnumbered equations (presumably, with \hypertarget in the argument of a cleverly placed \tag or \tag* directive...), etc. You're right that this extra bit of generality isn't needed when the job at hand is to cross-reference a section-level header. – Mico Mar 04 '23 at 11:04
  • @Mico If you add a target manually you have to think about the location. If you put it after the section, if will be too low, if you put it before it can be separated by a page break or affect spacing etc. Using the hyperref targets are often safer. – Ulrike Fischer Mar 04 '23 at 11:14
  • @Jimeens - If the objective is to make sure that all section-level headers (as well as, presumably, subsection-level headers, subsubsection-level headers, etc.) are unnumbered, don't redefine the \section macro. Instead, just execute \setcounter{secnumdepth}{0} in the preamble. – Mico Mar 04 '23 at 11:23
  • Maybe this could be a problem because the section title is customized with titleformat{section} and I don't know if rearranging the code would look the same, but I'll give it a try, thanks @Mico – Jimeens Mar 04 '23 at 11:38

1 Answers1

1

There is possibility to make \ref{} work as you want. You would need to redefine \@currentlabel in place before next \label. In your case, that would be inside a redefined \section.

Since you load hyperref, you don't need \phantomsection because anchors are already added by each starred section. It's also recommended to use \NewCommandCopy instead of \let.

The following example produces:

enter image description here

I slightly expanded your macro in order for \ref{} to use a shorter section title if specified. I also assume after colons are page numbers because section number in this case would be meaningless.

\documentclass{report}
\usepackage[colorlinks]{hyperref}
\usepackage{blindtext}

\title{The Title} \author{First Last} \date{}

\makeatletter \NewCommandCopy\oldsection\section \RenewDocumentCommand\section{O{#2}m}{% \oldsection*{#2}\addcontentsline{toc}{section}{\numberline{}#2}% \edef@currentlabel{(titled as "#1")}} \makeatother

\begin{document} \maketitle \tableofcontents

\chapter{Chapter one} \section{Section: Aaa}\label{sec:aaa} \Blindtext

\chapter{Chapter two} \section[Bbb]{Section: Bbb}\label{sec:bbb} \Blindtext

\chapter{Chapter three} Referring to the section 1 \ref{sec:aaa}: \pageref{sec:aaa}.\* Referring to the section 2 \ref{sec:bbb}: \pageref{sec:bbb}. \end{document}

Celdor
  • 9,058