I am involved in the later stages of a big project, in which we use the following \tocless command to exclude specific subsections from appearing in the table of contents:
\newcommand{\nocontentsline}[3]{}
\newcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup}
However, I recently found out that this messes up the references as can be seen in the enclosed MWE. Is there a way to change the definition of \tocless to correct this behaviour?
\documentclass[12pt,a4paper]{memoir}
\setcounter{tocdepth}{2}
\usepackage[usehighlevels]{alnumsec}
\alnumsectionlevels{1}{section,subsection,subsubsection,paragraph}
\alnumsecstyle{aaaa}
\surroundarabic[{.}][]{}{}
\otherseparators{1}
\alnumsecstyle{aaaa}
\newcommand{\nocontentsline}[3]{}
\newcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup}
\begin{document}
\tableofcontents
\section{this the first section}
\subsection{this is a normal subsection}\label{subsec:AA}
\tocless\subsection{this is a subsection not in TOC}\label{subsec:AB}
\section{this the second section}
\tocless\subsection{this is another subsection not in TOC}\label{subsec:BA}
\begin{itemize}
\item referring to 1.1: \ref{subsec:AA}
\item referring to 1.2: \ref{subsec:AB}
\item referring to 2.1: \ref{subsec:BA}
\end{itemize}
\end{document}





\tocless\subsection, which one might as well replace by a single macro (say)\toclesssubsection. This will not work as expected if you're using\tocless\section. – Werner Mar 11 '16 at 18:27