1

I need to mark some section titles with asterisks to represent special sections. I adopted the method provided by titlesec document (sec. 6.7). Also, I found this topic had been discussed here. Everything is ok except that the vertical space between a theorem environment (for me, it's always proof environment) and the asterisked section title is a bit larger than the space between two normal sections.

enter image description here

Here is 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}

The problem still exists if I use the proof environment provided by amsthm package. But when I remove \addtocontents{toc}{...} from the definition of advanced environment, the extra space disappears. So the problem must be concerned with \addtocontents. It seems that \addtocontents is a horizontal command? When a theorem encounters it, an extra space will be placed. The extra space is \topsep because \theorempostskip{\topsep} is default setting in ntheorem.

For me, \addtocontents is needed because I want table of contents to demonstrate whether a section is asterisked. Does anyone know how to eliminate the extra space and meanwhile have table of contents printed correctly?

3
  • The ‘whatsit’ introduced by \addtocontents is exactly the problem: the \addvspace command issued by \subsection cannot see the trailing \addvspace after proof as “immediately preceding”.
    – egreg
    Commented Sep 4, 2019 at 9:44
  • Do you need to do references to the subsection numbers?
    – egreg
    Commented Sep 4, 2019 at 11:20
  • The definition of advanced has some extra spaces produced by "unprotected" line endings. Adding % at those places would be a good idea. Commented Sep 4, 2019 at 17:14

1 Answer 1

2

The \addtocontents makes impossible for the \addvspace command issued by \subsection to “see” the one issued by \end{proof}.

I propose a different approach:

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

enter image description here

I used amsthm. However, this doesn't mean you can't use ntheorem if you really prefer it. Anyway, I can't see why bracing \newtheorem{proof}{Proof} and the related settings.

2
  • Nice solution! Thanks! BTW, I braced them because I actually have different settings for other theorem environments and I just copied the part of proof environment into MWE.
    – North
    Commented Sep 4, 2019 at 12:04
  • One more question. I notice that the asterisk is part of label of asterisked subsections. When I reference an asterisked subsection, the asterisk appears. Can this be avoid? I hope to obtain only the number when referencing a subsection:)
    – North
    Commented Sep 4, 2019 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .