我需要用星号标记一些章节标题来表示特殊章节。我采用了titlesec
文档(第 6.7 节)。此外,我发现这个话题已经讨论过了这里一切都很好,除了定理环境(对我来说,它始终是证明环境)和带星号的部分标题之间的垂直空间比两个正常部分之间的空间稍大。
以下是 MWE:
\documentclass[a4paper,12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[amsmath,thmmarks]{ntheorem}
{
\theoremstyle{nonumberplain}
\theoremindent0em
\theoremrightindent0em
\theoremheaderfont{\bfseries}
\theorembodyfont{\normalfont}
\theoremsymbol{\ensuremath{\Box}}
\newtheorem{proof}{Proof.}
}
% \usepackage{amsthm}
% \renewcommand\qedsymbol{\ensuremath{\Box}}
\usepackage{titlesec}
\usepackage{titletoc}
\renewcommand{\thesubsection}{\arabic{subsection}}
\newcommand{\secmark}{}
\newcommand{\marktotoc}[1]{\renewcommand{\secmark}{#1}}
\newenvironment{advanced}{
\renewcommand{\secmark}{*}
\addtocontents{toc}{\protect\marktotoc{*}}
}{
\addtocontents{toc}{\protect\marktotoc{}}
}
\titleformat{\subsection}
{\bfseries\large}
{\llap{\secmark}\thesubsection}
{1em}
{}
\newcommand{\starsec}[1]{\begin{advanced}\subsection{#1}\end{advanced}}
\titlecontents{section}[6em]
{\bfseries\addvspace{1pc}}
{\contentslabel[Chapter \thecontentslabel]{6em}}
{Chapter }
{\titlerule*{}\bfseries\contentspage}
\titlecontents{subsection}[3.7em]
{}
{\contentslabel[\llap{\secmark}\thecontentslabel]{2.3em}}
{\hspace*{-2.3em}}
{\titlerule*[10pt]{.}\contentspage}
\begin{document}
\tableofcontents
\section{The first section}
\subsection{The first subsection}
This is the first subsection.
\begin{proof}
Let's skip the proof.
\end{proof}
\begin{advanced}
\subsection{The first asterisked subsection}
This is the first asterisked subsection.
\subsection{The second asterisked subsection}
This is the second asterisked subsection.
\end{advanced}
\subsection{Another subsection}
This is another subsection.
\subsection{Normal subsection}
This is normal.
\end{document}
如果我使用包提供的证明环境,问题仍然存在amsthm
。但是当我\addtocontents{toc}{...}
从环境的定义中删除时advanced
,多余的空格就消失了。所以问题一定与有关\addtocontents
。这似乎\addtocontents
是一条水平命令?当定理遇到它时,会放置一个额外的空格。额外的空间是\topsep
因为\theorempostskip{\topsep}
是中的默认设置ntheorem
。
对我来说,\addtocontents
是必要的,因为我希望目录能够显示某个部分是否带有星号。有谁知道如何消除多余的空间,同时正确打印目录?
答案1
使得发出的命令\addtocontents
无法“看到” 发出的命令。\addvspace
\subsection
\end{proof}
我建议采取一种不同的方法:
\documentclass[a4paper,12pt]{article}
\usepackage[margin=2.5cm,heightrounded]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{etoolbox}
\renewcommand{\thesubsection}{\secmark\arabic{subsection}}
\newrobustcmd{\secmark}{}
\newenvironment{advanced}{%
\renewcommand{\secmark}{\secasterisk}%
}{}
% normal behavior of \secasterisk is to print an asterisk
\newrobustcmd{\secasterisk}{*}
% in the toc and in section titles we want that the asterisk is 'llapp'ed
\newrobustcmd{\secasteriskzero}{\makebox[0pt][r]{*}}
\makeatletter
\patchcmd{\@starttoc}
{\begingroup}
{\begingroup\let\secasterisk\secasteriskzero}
{}{}
\makeatother
\titleformat{\subsection}
{\bfseries\large\let\secasterisk\secasteriskzero}
{\thesubsection}
{1em}
{}
\newcommand{\starsec}[1]{\begin{advanced}\subsection{#1}\end{advanced}}
\titlecontents{section}[6em]
{\bfseries\addvspace{1pc}}
{\contentslabel[Chapter \thecontentslabel]{6em}}
{Chapter }
{\titlerule*{}\bfseries\contentspage}
\titlecontents{subsection}[3.7em]
{}
{\contentslabel[\llap{\secmark}\thecontentslabel]{2.3em}}
{\hspace*{-2.3em}}
{\titlerule*[10pt]{.}\contentspage}
\begin{document}
\tableofcontents
\section{The first section}
References \ref{testa} and \ref{testb}.
\subsection{The first subsection}\label{testa}
This is the first subsection.
\begin{proof}
Let's skip the proof.
\end{proof}
\subsection{Another subsection}
This is a subsection to compare spaces.
\begin{proof}
Let's skip the proof.
\end{proof}
\begin{advanced}
\subsection{The first asterisked subsection}\label{testb}
This is the first asterisked subsection.
\subsection{The second asterisked subsection}
This is the second asterisked subsection.
\end{advanced}
\subsection{Another subsection}
This is another subsection.
\subsection{Normal subsection}
This is normal.
\end{document}
我使用了amsthm
。但是,这并不意味着ntheorem
如果你真的喜欢它,你就不能使用它。无论如何,我不明白为什么要使用支撑\newtheorem{proof}{Proof}
和相关设置。