5

I want to add subsubsections with alternative numbering (A1..A3, B1..B3, etc.) to my document. These subsubsections should be added to the ToC, but the native numbering should be suppressed, the alternative numbering should come onto the numberline.

I managed to do this with the titlesec package, but the breaks the \nameref{} command. A fix was suggested in this answer, but that makes the starred section also show up in the ToC, with original section-numbering.

How can I use the \nameref{} command for starred sections using the titlesec-package?

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{titlesec} 

\titleformat{\subsubsection}[hang]{\normalfont\bfseries}{}{0pt}{\tempSectNumber\quad}

\newcommand{\mySubSubSection}[1]{\def\tempSectNumber{\thesubsubsection}\subsubsection{#1}\label{s:#1}}
\newcommand{\myStarredSubSubSection}[1]{\def\tempSectNumber{X1}\subsubsection*{#1}\label{s:#1}\addcontentsline{toc}{subsubsection}{\protect{\numberline{X1}}#1}}
\begin{document}
\tableofcontents
\setlength{\parindent}{0pt}
\mySubSubSection{Normal}
\myStarredSubSubSection{Special}

Reference to Normal: \autoref{s:Normal}/\nameref{s:Normal}\par
Reference to Special: \autoref{s:Special}/\nameref{s:Special}\par
\end{document}

Desired outcome:

Contents
0.0.1 Normal  . . . . . . . . . . . . . . . . . . . . . . . . . . . .   1
X1    Special . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   1

0.0.1  Normal
X1  Special

Reference to Normal: subsubsection 0.0.1/Normal
Reference to Special: subsubsection 0.0.1/Special
Gauwain
  • 131

1 Answers1

3

Step the subsubsection counter in order to trigger the \label-ref mechanism, define \@currentlabelname and \@currentlabel to the desired values.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{titlesec} 
\usepackage{hyperref}

\titleformat{\subsubsection}[hang]
  {\normalfont\bfseries}
  {}
  {0pt}
  {\tempSectNumber\quad}

\newcommand{\mySubSubSection}[1]{%
  \def\tempSectNumber{\thesubsubsection}%
  \subsubsection{#1}%
  \label{s:#1}%
}
\makeatletter
\newcommand{\myStarredSubSubSection}[1]{%
  \def\tempSectNumber{X1}%
  \refstepcounter{subsubsection}%
  \subsubsection*{#1}%
  \addtocounter{subsubsection}{-1}%
  \def\@currentlabelname{#1}%
  \def\@currentlabel{\thesubsubsection}%
  \label{s:#1}%
  \addcontentsline{toc}{subsubsection}{\protect{\numberline{X1}}#1}%
}
\makeatother


\begin{document}
\tableofcontents
\setlength{\parindent}{0pt}

\mySubSubSection{Normal}
\myStarredSubSubSection{Special}

Reference to Normal: \autoref{s:Normal}/\nameref{s:Normal}\par
Reference to Special: \autoref{s:Special}/\nameref{s:Special}\par

\end{document}

enter image description here

I discourage from setting labels this way. If your title contains special characters you're doomed. And labels should be mnemonics.

egreg
  • 1,121,712
  • That did it, thank you! I set the \myStarredSubSubSection to a two variable command, giving in the section number manually and set that as label. That prevents doom when using special characters in the title.

    \makeatletter \newcommand{\myStarredSubSubSection}[2]{% \def\tempSectNumber{#2}% \subsubsection*{#1}% \addtocounter{subsubsection}{-1}% \def\@currentlabelname{#1}% \def\@currentlabel{\thesubsubsection}% \label{#2} \addcontentsline{toc}{subsubsection}{\protect{\numberline{#2}}#1}% } \makeatother

    – Gauwain Dec 28 '15 at 13:47