The following redefineds \subsection* in terms of a new command \subsec that provides unnumbered subsections yet still allows cross-referencing those subsections through hyperref/cleveref/crossreftools. (See https://tex.stackexchange.com/a/660430/13492.)
It works fine except when I also try to form both a short TOC that lists only to the depth of sections and a detailed TOC that lists to the depth of subsections. (See https://tex.stackexchange.com/a/512752/13492.)
Question: The short TOC is also listing subsections and so is identical to the detailed TOC. How can this be fixed?
\documentclass{memoir}
\usepackage{enumitem}
\usepackage[colorlinks,linkcolor=black]{hyperref}
\usepackage[nameinlink]{cleveref}
\usepackage{crossreftools}
% SECTIONING
\setsecnumdepth{subsection}
\settocdepth{subsection}
% Handle refs to subsections, which are unnumbered
% https://tex.stackexchange.com/a/660430/13492
\NewCommandCopy{\oldsubsection}{\subsection}
%
\newcounter{subsec}
%
\RenewDocumentCommand{\subsection}{s}{%
\IfBooleanTF{#1}{\subsec}{\oldsubsection}%
}
%
\makeatletter
\newcommand{\subsec}[1]{%
\refstepcounter{subsec}%
\def\cref@currentlabel{[subsec][\arabic{subsec}][]#1}%
\def@currentlabelname{#1}%
\oldsubsection*{#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})%
}
% SHORT & LONG TOCs
\newcommand{\longtocname}{Detailed Contents}
\newcommand{\shorttocname}{Short Contents}
%
% https://tex.stackexchange.com/a/512752/13492
\newif\ifSHORT
\newif\ifLONG
%
% credit for Ulrike Fischer for this idea, we need to have the
% conditionals defined inside the toc
\DeclareRobustCommand\activateif{%
\let\showShort\ifSHORT
\let\showLong\ifLONG
\let\stopShort\fi
\let\stopLong\fi
}
%
\usepackage{xpatch}
%
% next use patching to add a wrapper around the \addcontentsline
% that \tableofcontents generates. Since this is inside a group we
% don't having one patch messing up the other
\makeatletter
\newcommand\shortTOC{
\begingroup
\setcounter{tocdepth}{0}
\def\contentsname{\shorttocname}
\xpatchcmd{\mem@tableofcontents}%
{\addcontentsline{toc}{chapter}{\contentsname}}{%
\addtocontents{toc}{\protect\showShort}%
\addcontentsline{toc}{chapter}{\contentsname}
\addtocontents{toc}{\protect\stopShort}%
}{\typeout{ok}}{\typeout{failed}}
\LONGtrue
\tableofcontents
\endgroup
}
%
\newcommand\longTOC{
\begingroup
\setcounter{tocdepth}{3}
\def\contentsname{\longtocname}
\xpatchcmd{\mem@tableofcontents}%
{\addcontentsline{toc}{chapter}{\contentsname}}{%
\addtocontents{toc}{\protect\showLong}%
\addcontentsline{toc}{chapter}{\contentsname}
\addtocontents{toc}{\protect\stopLong}%
}{\typeout{ok}}{\typeout{failed}}
\SHORTtrue
\tableofcontents
\endgroup
}
%
% automatically add the activation to the toc
\AtBeginDocument{
\addtocontents{toc}{\activateif}
}
%
\makeatother
\begin{document}
\frontmatter
\shortTOC
\clearpage
\longTOC
\mainmatter
\chapter{Chapter 1}
\section{Section 1 of chapter 1}
Text.
\begin{itemize}
\item See: \cref{subsec:mysub}
\item See: \crefsubsec{subsec:mysub}
\end{itemize}
\subsection*{A subsection}\label{subsec:mysub}
\begin{exercises}
\item Do this.
\item Do that.
\end{exercises}
\section{Section 2 of chapter 1}
\subsection*{This subsection}
\begin{exercises}
\item Why?
\item Why not?
\end{exercises}
\chapter{Chapter 2}
\section{Section 1 of chapter 2}
\subsection*{First subsection of section 2 of chapter 2}
\subsection*{Second subsection of section 2 of chapter 2}\label{subsec:another}
\begin{exercises}
\item Another question.
\item Yet another exercise.
\end{exercises}
\end{document}

