4

The answer in Cross-reference to unnumbered SUBsections shows how to define a custom \subsec command so as to allow cross-referencing with cleveref.

But how can I modify things to allow a plural form of \cref, referencing two such subsections.

Sample source:

\documentclass[12pt]{memoir}
\usepackage{hyperref}
\usepackage{cleveref}    

\setsecnumdepth{subsection}
%    subsections:
%
% Handle refs to subsections, which are unnumbered 
\newcounter{subsec}
\makeatletter
\newcommand{\subsec}[1]{%
  \subsection*{#1}%
  \addcontentsline{toc}{subsection}{#1}%
  \refstepcounter{subsec}%
  \def\cref@currentlabel{[subsec][\arabic{subsec}][]#1}%
  \def\@currentlabelname{#1}%
}
\makeatother

\crefname{subsection}{subsection}{subsections}    % not used?
\Crefname{subsection}{Subsection}{Subsections}  % not used?
\crefformat{subsec}{#2subsection~``#1''#3} % for unnumbered subsections
\Crefformat{subsec}{#2Subsection~``#1''#3} % ditto, caps

\begin{document}

In \cref{sec:one,sec:other} we outline.  [OK, as expected]

\section{The section}\label{sec:one}

\section{The other section}\label{sec:other}

\begin{itemize}

   \item In \cref{subsec:this} and \cref{subsec:another} we ponder. [OK]

   \item In \cref{subsec:this,subsec:another} we analyze. [Does \textbf{NOT} work.]

   \emph{Want output there to be:} subsections ``This one'' and ``Another one''

\end{itemize}

\subsec{This one}\label{subsec:this}

   This is in a subsection.

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

   Here is another subsection.

\end{document}

Defective output: How get plural \cref for unnumbered sections?

  • 1
    Your counter is subsec, not subsection, so use \crefname{subsec}{subsection}{subsections} and similarly for \Crefname, not \crefname{subsection}{...}{...} which is already pre-defined by cleveref – user31729 Jul 27 '17 at 19:36
  • Sorry about that: a leftover from a now-abandoned design choice that had had numbered subsections with the standard subsection label. – murray Jul 27 '17 at 20:58
  • By the way, if your document creates a table of contents (ToC) and if you wish items of level subsec to show up in the ToC, you ought to issue the instruction \settocdepth{subsection} -- or, equivalently, \setcounter{tocdepth}{2}. If, however, you do not wish unnumbered subsection headers to ever show up in the ToC, you might as well omit the instruction \addcontentsline{toc}{subsection}{#1} from the definition of the \subsec macro. – Mico Jul 27 '17 at 21:30
3

The counter being labeled there is subsec, so \crefname should use subsec and not subsection in order to provide the correct cross-reference information.

The same holds for \Crefname then.

\crefname{subsection}{subsection}{subsections} etc. is done by cleveref already.

\documentclass[12pt]{memoir}
\usepackage{hyperref}
\usepackage{cleveref}    

\setsecnumdepth{subsection}
%    subsections:
%
% Handle refs to subsections, which are unnumbered 
\newcounter{subsec}
\makeatletter
\newcommand{\subsec}[1]{%
  \subsection*{#1}%
  \addcontentsline{toc}{subsection}{#1}%
  \refstepcounter{subsec}%
  \def\cref@currentlabel{[subsec][\arabic{subsec}][]#1}%
  \def\@currentlabelname{#1}%
}
\makeatother

\crefname{subsec}{subsection}{subsections}    % not used?
\Crefname{subsec}{Subsection}{Subsections}  % not used?
\crefformat{subsec}{#2subsection~``#1''#3} % for unnumbered subsections
\Crefformat{subsec}{#2Subsection~``#1''#3} % ditto, caps

\crefmultiformat{subsec}{subsections~``#2#1#3''}{ and~``#2#1#3''}{,~``#2#1#3''}{ and~``#2#1#3''}% As Mico proposed ;-)


\begin{document}

In \cref{sec:one,sec:other} we outline.  [OK, as expected]

\section{The section}\label{sec:one}

\section{The other section}\label{sec:other}

\begin{itemize}

   \item In \cref{subsec:this} and \cref{subsec:another} we ponder. [OK]

   \item In \cref{subsec:this,subsec:another} we analyze. [Does \textbf{NOT} work.]

   \emph{Want output there to be:} subsections ``This one'' and ``Another one''

\end{itemize}

\subsec{This one}\label{subsec:this}

   This is in a subsection.

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

   Here is another subsection.

\end{document}
  • 1
    +1. To fully (re)produce the OP's desired cross-reference format, you may also want to provide the instruction \crefmultiformat{subsec}{subsections~``#2#1#3''}{ and~``#2#1#3''}{,~``#2#1#3''}{ and~``#2#1#3''}. – Mico Jul 27 '17 at 19:52
  • 1
    @Mico: Thanks, I've forgotten that command actually. I added it according to your proposition, with a reference to you :D – user31729 Jul 27 '17 at 20:05
  • @Mico: Yes, to get the quotes around both references in the plural situation, \crefmultiformat does it where as just \crefformat (with the corrected \crefname) does not. Lovely! – murray Jul 27 '17 at 20:57
  • 1
    @murray - Incidentally, if you want the cross-reference labels to subsec-type items to be hyperlinks and if you want clicking on one of the links to take you to the line of the header itself (rather than to the next line), you should place the instructions \subsection*{#1}% and \addcontentsline{toc}{subsection}{#1}% at the end, rather than at the beginning, of the definition of the \subsec macro. – Mico Jul 27 '17 at 21:18
  • @Mico: a nice improvement. – murray Jul 29 '17 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.