如何重新定义 \subsection*?

如何重新定义 \subsection*?

如何\subsection*重新定义(带星号的版本)以便能够做到\subsec下面所做的事情?

(请注意,我将不是使用联合国已加星标\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}

源文件显示的内容比问题直接询问的要多,但旨在表明所需版本的\subsection*使用方式。使用内置的、未修改的\subsection*不允许通过 、 和 的组合正确交叉引用子部分\hyperref\cleveref\crossreftools相关:带有自定义未编号 SUBsections 的复数 \cref

答案1

使用\NewDocumentCommand\NewCommandCopy\AddToHook以及最新版本的 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}

答案2

这有点不合时宜,但由于您没有使用无星号版本,因此您可以重新定义并让它忽略其第一个参数(星号)。然后使用与仅使用\subsection相同的定义,指定两个参数并将s 更改为s。\subsec\renewcommand#1#2

由于定义本身使用\subsection*,为了避免循环,您首先需要使用\let为原始命令分配不同的命令\subsection。这里我使用\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

相关内容