2

How can \subsection* (the starred version) be redefined so as to do exactly what \subsec does in the following?

(Note that I will not be using the unstarred \subsection.)

\documentclass{memoir}
\usepackage{enumitem}
\usepackage{suffix}

% SECTIONING \setsecnumdepth{subsection} \settocdepth{subsection}

% ---> How redefine \subsection* to do what \subsec below does? % Handle refs to subsections, which are unnumbered \newcounter{subsec} \makeatletter \newcommand{\subsec}[1]{% \refstepcounter{subsec}%{\ss} \def\cref@currentlabel{[subsec][\arabic{subsec}][]#1}% \def@currentlabelname{#1}% \subsection*{#1}% \addcontentsline{toc}{subsection}{#1}% } \makeatother

% EXERCISES (as list in a subsec): \newlist{exercises}{enumerate}{1} \setlist[exercises]{ label=\bfseries\sffamily\arabic., ref=\thesection.\arabic*, before={\subsec{Exercises for section \thesection}}, resume % to continue numbering across a chapter's sections } % To restart exercise numbering at 1 in each chapter: \let\oldchapter\chapter \renewcommand{\chapter}{\restartlist{exercises}\oldchapter}

% CROSS-REFERENCING \usepackage{hyperref} \usepackage{cleveref} \usepackage{crossreftools} % \crefname{subsec}{subsection}{subsections} \crefformat{subsec}{#2subsection~``#1''#3} % for the unnumbered subsections \newcommand{\crefsubsec}[1]{\cref{#1} (\cpageref{#1})} \WithSuffix\newcommand\crefsubsec[1]{\cref*{#1} (\cpageref{#1})}

\begin{document}

\frontmatter \tableofcontents

\mainmatter

\chapter{Chapter 1} \section{Section 1 of chapter 1}

Text. See \crefsubsec*{subsec:mysub}

\subsec{A subsection}\label{subsec:mysub}

Text.

\begin{exercises} \item Do this. \item Do that. \end{exercises}

\section{Section 2 of chapter 1}

\subsec{This subsection}

\begin{exercises} \item Why? \item Why not? \end{exercises}

\chapter{Chapter 2}

\section{Section 1 of chapter 2}

\subsec{First subsection of section 2 of chapter 2}

\subsec{Second subsection of section 2 of chapter 2}

\begin{exercises} \item Another question. \item Yet another exercise. \end{exercises}

\end{document}

The source shows more than the question directly asks about but is intended to indicate the way the desired version of \subsection* will be used. Using the built-in, unmodified \subsection* does not allow proper cross-references to the subsections through the combination of \hyperref, \cleveref, and \crossreftools. (Related: Plural \cref with custom unnumbered SUBsections)

murray
  • 7,944

2 Answers2

3

Use \NewDocumentCommand, \NewCommandCopy and \AddToHook, with a recent version of LaTeX.

\documentclass{memoir}
\usepackage{enumitem}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{crossreftools}

% SECTIONING \setsecnumdepth{subsection} \settocdepth{subsection}

% ---> How redefine \subsection* to do what \subsec below does? % Handle refs to subsections, which are unnumbered \NewCommandCopy{\memoirsubsection}{\subsection}

\newcounter{subsec} \RenewDocumentCommand{\subsection}{s}{% \IfBooleanTF{#1}{\subsec}{\memoirsubsection}% }

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

% EXERCISES (as list in a subsec): \newlist{exercises}{enumerate}{1} \setlist[exercises]{ label=\bfseries\sffamily\arabic., ref=\thesection.\arabic*, before={\subsec{Exercises for section \thesection}}, resume % to continue numbering across a chapter's sections } % To restart exercise numbering at 1 in each chapter: \AddToHook{cmd/chapter/before}{\restartlist{exercises}}

% \crefname{subsec}{subsection}{subsections} \crefformat{subsec}{#2subsection~``#1''#3} % for the unnumbered subsections \NewDocumentCommand{\crefsubsec}{sm}{% \IfBooleanTF{#1}{\cref*{#2}}{\cref{#2}}~(\cpageref{#2})% }

\begin{document}

\frontmatter \tableofcontents*

\mainmatter

\chapter{Chapter 1} \section{Section 1 of chapter 1}

Text. See \crefsubsec*{subsec:mysub}

\subsec{A subsection}\label{subsec:mysub}

Text.\crefsubsec{subsec:mysub}

\begin{exercises} \item Do this. \item Do that. \end{exercises}

\section{Section 2 of chapter 1}

\subsec{This subsection}

\begin{exercises} \item Why? \item Why not? \end{exercises}

\chapter{Chapter 2}

\section{Section 1 of chapter 2}

\subsec{First subsection of section 2 of chapter 2}

\subsec{Second subsection of section 2 of chapter 2}

\begin{exercises} \item Another question. \item Yet another exercise. \end{exercises}

\end{document}

egreg
  • 1,121,712
  • That still uses \subsec instead of \subsection* in both the definition of the exercises list and for subsections in the body of the document. – murray Oct 03 '22 at 20:46
  • 1
    @murray So what? You say “as to do exactly what \subsec does”. – egreg Oct 03 '22 at 20:46
  • If I change subsec to subsection* in definition of exercises and for subsections in the document body, it does work! Now I'm bothered that I still ahve to use \crefname{subsec}, etc., instead of \crefname{subsection}. In a sense, \subsection* has not really been redefined but just made as a alternate name for \subsed; I'd like to get rid of \subsec. – murray Oct 03 '22 at 20:52
  • 1
    @murray Sorry, I don't understand. Nobody forces you to use \subsec in the document. It's often convenient to bury complicated code in auxiliary commands. Anyway, referring to unnumbered objects by number doesn't seem a good idea. – egreg Oct 03 '22 at 21:07
  • the only reason for the numbering reference to the unnumbered objects seems to be to allow proper operation of the cross-referencing commands -- but let me think about that! – murray Oct 03 '22 at 21:40
  • 1
    @murray Referring to something non-existent is not a good service to your readers, is it? – egreg Oct 03 '22 at 21:41
  • is \NewCommandCopy kind of like the safe version of \let? – likethevegetable Oct 05 '22 at 02:27
  • 1
    @likethevegetable Yes, it knows how to cope with the cases when \let isn’t the correct tool (look for letltxmacro for a previous implementation and use cases). – egreg Oct 05 '22 at 08:28
  • @egreg: This method somehow interferes with including both a detailed TOC (down to susection legel) and a short TOC going only down to section level. See: https://tex.stackexchange.com/questions/660858/short-long-tocs-with-custom-subsection – murray Oct 07 '22 at 14:58
2

It's a bit hacky, but since you're not using the unstarred version, you could redefine \subsection and have it ignore its first argument (the star). Then use the same definition as you had for \subsec only using \renewcommand, specify two arguments and change the #1s to #2s.

Since the definition itself uses \subsection*, to avoid circularity you'll first need to use \let to assign a different command to the original \subsection. Here I use \oldsubsection:

\newcounter{subsec}
\let\oldsubsection=\subsection
\makeatletter
\renewcommand{\subsection}[2]{%
  \refstepcounter{subsec}%{\ss}
  \def\cref@currentlabel{[subsec][\arabic{subsec}][]#2}%
  \def\@currentlabelname{#2}%
  \oldsubsection*{#2}%
  \addcontentsline{toc}{subsection}{#2}%
}
\makeatother
frabjous
  • 41,473
  • I agree that's rather "hacky." I keep thinking I need somehow to use \WithSuffix but don't see how. – murray Oct 03 '22 at 19:57