6

In my memoir document, all the subsections are unnumbered but are listed in the TOC. Is there some way to exploit cleveref (and hyperref) so as to cross-reference subsections that's more direct than the following?

\documentclass[12pt]{memoir}

\setsecnumdepth{subsection}
\settocdepth{subsection}
\setlength{\cftsubsectionindent}{5.25em}
\usepackage{lipsum}
\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[noabbrev,capitalize]{cleveref}
\crefname{subsection}{subsection}{subsection}    % not used?
\Crefname{subsection}{Subsection}{Subsection}
\crefname{page}{page}{page}

\begin{document}

\tableofcontents

\chapter{The Chapter}

\subsection*{A particular situation}%
\label{subsec:particular}
\addcontentsline{toc}{subsection}{\nameref{subsec:particular}}

\lipsum[1]

\newpage

\subsection*{Another subsection}
\label{subsec:another}
\addcontentsline{toc}{subsection}{\nameref{subsec:another}}

See subsection~``\nameref*{subsec:particular}'' (\cpageref{subsec:particular}).

\noindent See subsection~``\nameref{subsec:particular}'' (\cpageref{subsec:particular}).

\end{document}

From the output:

Table of contents page

page with references to subsection

Of course I can define a macro to do the \subsection*{...}\label{...}\addcontentsline... creation, and another to do the cross-referencing:

\documentclass[12pt]{memoir}

\setsecnumdepth{subsection}
\settocdepth{subsection}
\setlength{\cftsubsectionindent}{5.25em}
\usepackage{lipsum}
\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[noabbrev,capitalize]{cleveref}
\crefname{subsection}{subsection}{subsection}    % not used?
\Crefname{subsection}{Subsection}{Subsection}
\crefname{page}{page}{page}

\newcommand{\subsec}[2]{%
\subsection*{#1}\label{subsec:#2}%
\addcontentsline{toc}{subsection}{\nameref{subsec:#2}}%
}

\usepackage{suffix}

\newcommand{\subsecref}[1]{%
subsection~``\nameref{subsec:#1}'' (\cpageref{subsec:#1})%
}
\WithSuffix\newcommand\subsecref*[1]{%
subsection~``\nameref*{subsec:#1}'' (\cpageref{subsec:#1})%
}

\begin{document}

\tableofcontents

\chapter{The Chapter}

\section{This stuff}\label{sec:stuff}

\subsec{A particular situation}{particular}

\lipsum[1]

\newpage

\subsec{Another subsection}{another}

See \subsecref{another}.

\noindent See \subsecref*{another}.

\end{document}

(The output will be the same as before.)

Is there no way to use cleveref, or otherwise, to automatically include "subsection" in the cross-reference (just as a \cref would do for a section)?

3
  • Have you tried \subsection*{foo}\label[subsection]{subsec:foo}? But as there is no counter involved, \label is pretty useless
    – user31729
    Commented Dec 27, 2016 at 17:42
  • Evidently form my source & output, including a \label does allow cross-referencing. But I'd like LaTeX to "know" somehow that I'm referencing a subsection!
    – murray
    Commented Dec 27, 2016 at 17:50
  • Well, that is the problem: A \label needs a counter information, however, you don't use numbered subsections.
    – user31729
    Commented Dec 27, 2016 at 17:53

2 Answers 2

6

Define your own command:

\documentclass[12pt]{memoir}

\setsecnumdepth{subsection}
\settocdepth{subsection}
\setlength{\cftsubsectionindent}{5.25em}
\usepackage{lipsum}
\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[noabbrev,capitalize]{cleveref}

\crefname{page}{page}{page}
\crefformat{specialsubsection}{#2subsection~``#1''#3}

\newcounter{specialsubsection}
\makeatletter
\newcommand{\specialsubsection}[1]{%
  \subsection*{#1}%
  \addcontentsline{toc}{subsection}{#1}%
  \refstepcounter{specialsubsection}%
  \def\cref@currentlabel{[specialsubsection][\arabic{specialsubsection}][]#1}%
  \def\@currentlabelname{#1}%
}
\makeatother

\begin{document}

\tableofcontents

\chapter{The Chapter}

\section{Whatever}

\specialsubsection{A particular situation}%
\label{subsec:particular}

\lipsum[1]

\newpage

\specialsubsection{Another subsection}
\label{subsec:another}

See \cref*{subsec:particular} (\cpageref{subsec:particular}).

\noindent See \cref{subsec:particular} (\cpageref{subsec:particular}).

\noindent See \cref{subsec:another} (\cpageref{subsec:another}).

\end{document}

Contents

enter image description here

Text

enter image description here

1
  • Hard to choose between the answers of Christian Hupfer and egreg. I somewhat favor the latter because (1) it doesn't mess with low-level code from memoir, and (2) is a bit more flexible as to whether I can include or exclude the subsection page reference, too. I'll accept egreg's, though Hupfer's certainly solves the problem, too. Thanks both!
    – murray
    Commented Dec 29, 2016 at 4:05
3

Similar to egreg's solution, with special commands for the nameref and a 'slightly' modified \subsection command.

\documentclass[12pt]{memoir}


\setsecnumdepth{section}
\settocdepth{subsection}
\setlength{\cftsubsectionindent}{5.25em}
\usepackage{lipsum}
\usepackage{xparse}%
\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[noabbrev,capitalize]{cleveref}

\crefname{subsection}{subsection}{subsection}    % not used?
\Crefname{subsection}{Subsection}{Subsection}
\crefname{page}{page}{page}

\newcommand{\cnameref}[1]{%
  \namecref{#1}\ "\nameref{#1}" (\cpageref{#1})%
}

\newcommand{\Cnameref}[1]{%
  \nameCref{#1}\ "\nameref{#1}" (\cpageref{#1})%
}

\usepackage{letltxmacro}

\makeatletter
\LetLtxMacro\@@memoir@subsection\subsection

\RenewDocumentCommand{\subsection}{somo}{%
  \IfBooleanTF{#1}{%
    \@@memoir@subsection*{#3}%
  }{%
    \edef\@toctitle{#3}
    \IfValueT{#2}{%
      \edef\@toctitle{#2}
    }
    \refstepcounter{subsection}% Since it is unnumbered anyway ;-)
    \IfValueTF{#4}{%
      \@@memoir@subsection[\@toctitle]{#3}[#4]%
    }{%
      \@@memoir@subsection[\@toctitle]{#3}%
    }
    \edef\@currentlabelname{#3}% Set the \nameref information to #3
  }%
}

\makeatother

\setlength{\parindent}{0em}
\begin{document}

\tableofcontents

\chapter{The Chapter}

\section{This stuff}\label{sec:stuff}


\subsection{A particular situation}\label{subsec:particular}

\lipsum[1]

\newpage

\subsection{Another subsection}\label{subsec:another}

See \cnameref{subsec:particular} 

or 

\cnameref{subsec:another}

or

\Cnameref{subsec:another}

See \cnameref{sec:stuff}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .