带有自定义 \subsection* 的短/长目录

带有自定义 \subsection* 的短/长目录

以下内容\subsection*根据新命令重新定义,该命令提供未编号的子节,但仍允许通过/ /\subsec交叉引用这些子节。(见hyperrefcleverefcrossreftoolshttps://tex.stackexchange.com/a/660430/13492。

它工作正常除了当我尝试同时形成一个简短的目录(仅列出章节的深度)和一个详细的目录(列出子章节的深度)时。(见https://tex.stackexchange.com/a/512752/13492。

问题:简短目录也列出了子部分,因此与详细目录相同。如何修复此问题?

\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}

简短的目录也包括子部分

答案1

删除(第 8 行和第 9 行)

\setsecnumdepth{subsection}
\settocdepth{section}

并更改\setcounter{tocdepth}{1}\shortTOC

\newcommand\shortTOC{
    \begingroup
    \setcounter{tocdepth}{1} %<<<<<<<<<<<<<<<<<<<<<< changed 
    \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
}

A

相关内容