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)
\subsecinstead of\subsection*in both the definition of theexerciseslist and for subsections in the body of the document. – murray Oct 03 '22 at 20:46\subsecdoes”. – egreg Oct 03 '22 at 20:46subsectosubsection*in definition ofexercisesand 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\subsecin 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\NewCommandCopykind of like the safe version of\let? – likethevegetable Oct 05 '22 at 02:27\letisn’t the correct tool (look forletltxmacrofor a previous implementation and use cases). – egreg Oct 05 '22 at 08:28