2

I came across this question that was posted before and have been using the solution for a while. However, I can't seem to get the same thing to work with starred environments.

I am aware that long theorem titles have been a long-standing bug with ntheorem. However, some pointers as to how I can workaround this, if there exists one such workaround out there, is appreciated.

The following is a MWE using the tufte class, just to stay consistent with my usual setup.

\documentclass{tufte-book}
\usepackage{etoolbox}
\usepackage{ntheorem}

% Workaround for long titles in ntheorem
\makeatletter
\let\nobreakitem\item
\let\@nobreakitem\@item
\patchcmd{\nobreakitem}{\@item}{\@nobreakitem}{}{}
\patchcmd{\nobreakitem}{\@item}{\@nobreakitem}{}{}
\patchcmd{\@nobreakitem}{\@itempenalty}{\@M}{}{}
\patchcmd{\@xthm}{\ignorespaces}{\nobreak\ignorespaces}{}{}
\patchcmd{\@ythm}{\ignorespaces}{\nobreak\ignorespaces}{}{}

\renewtheoremstyle{break}%
  {\item{\theorem@headerfont
          ##1\ ##2\theorem@separator}\hskip\labelsep\relax\nobreakitem}%
  {\item{\theorem@headerfont
          ##1\ ##2\ (##3)\theorem@separator}\hskip\labelsep\relax\nobreakitem}
\makeatother

\theoremstyle{break}
\theoremprework{\textcolor{magenta}{\hrule height 2pt width \textwidth}}
\theoremheaderfont{\color{magenta}\normalfont\bfseries}
\theorempostwork{\textcolor{magenta}{\hrule height 2pt width \textwidth}}
\theoremindent10pt
\newtheorem{theorem}{Theorem}

\theoremprework{\textcolor{magenta}{\hrule height 2pt width \textwidth}}
\theoremheaderfont{\color{magenta}\normalfont\bfseries}
\theorempostwork{\textcolor{magenta}{\hrule height 2pt width \textwidth}}
\theoremindent10pt
\newtheorem*{theoremn}{Theorem}

\begin{document}
\begin{theorem}[Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem]
  This is a numbered theorem.
\end{theorem}

\begin{theoremn}[Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem]
  This is an unnumbered theorem.
\end{theoremn}
\end{document}

The following is an image of how it looks like: Numbered theorem vs Unnumbered theorem

For those who intend to recommend amsthm, note that I cannot achieve the styling of theorem environments that I can do with ntheorem, which is why I would prefer to find a solution with ntheorem, rather than dropping it. Also, the use of frames (e.g. mdframed) is a no-go, since I am also using the tufte class, and I greatly rely on having ample side notes and margin notes in my documents.

Japorized
  • 158
  • 7
  • Was not sure how helpful it would be, especially since it is very similar to the linked problem, but I have added an MWE anyway. – Japorized May 14 '19 at 22:33

1 Answers1

2

You can add

\def\thm@nonumberbreak{%
  \def\@begintheorem##1##2{%
    \item
    {\theorem@headerfont##1\theorem@separator}
    \hskip\labelsep\relax\nobreakitem 
  }%
  \def\@opargbegintheorem##1##2##3{%
    \item 
    {\theorem@headerfont##1\ (##3)\theorem@separator}%
    \hskip\labelsep\relax\nobreakitem 
  }%
}

before \makeatother. More simply, use regexpatch instead of etoolbox.

\documentclass{tufte-book}
\usepackage{regexpatch}
\usepackage{ntheorem}

% Workaround for long titles in ntheorem
\makeatletter
\let\nobreakitem\item
\let\@nobreakitem\@item
\xpatchcmd{\nobreakitem}{\@item}{\@nobreakitem}{}{}
\xpatchcmd{\nobreakitem}{\@item}{\@nobreakitem}{}{}
\xpatchcmd{\@nobreakitem}{\@itempenalty}{\@M}{}{}
\xpatchcmd{\@xthm}{\ignorespaces}{\nobreak\ignorespaces}{}{}
\xpatchcmd{\@ythm}{\ignorespaces}{\nobreak\ignorespaces}{}{}

\renewtheoremstyle{break}%
  {\item{\theorem@headerfont
          ##1\ ##2\theorem@separator}\hskip\labelsep\relax\nobreakitem}%
  {\item{\theorem@headerfont
          ##1\ ##2\ (##3)\theorem@separator}\hskip\labelsep\relax\nobreakitem}
% make \th@nonumberbreak the same as \th@break, but remove "\ ##2"
\let\th@nonumberbreak\th@break
\xpatchcmd*{\th@nonumberbreak}{\ ##2}{}{}{}
\makeatother

\theoremstyle{break}
\theoremprework{\textcolor{magenta}{\hrule height 2pt width \textwidth}}
\theoremheaderfont{\color{magenta}\normalfont\bfseries}
\theorempostwork{\textcolor{magenta}{\hrule height 2pt width \textwidth}}
\theoremindent10pt
\newtheorem{theorem}{Theorem}

\theoremprework{\textcolor{magenta}{\hrule height 2pt width \textwidth}}
\theoremheaderfont{\color{magenta}\normalfont\bfseries}
\theorempostwork{\textcolor{magenta}{\hrule height 2pt width \textwidth}}
\theoremindent10pt
\newtheorem*{theoremn}{Theorem}

\begin{document}
\begin{theorem}[Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem]
  This is a numbered theorem.
\end{theorem}

\begin{theoremn}[Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem Theorem]
  This is an unnumbered theorem.
\end{theoremn}
\end{document}

enter image description here

egreg
  • 1,121,712
  • Are the theorem headings meant to be ragged right? – barbara beeton May 15 '19 at 01:18
  • Thank you. How does one go about learning how to tackle these problems on their own? Mind sharing some of your insight to help a learning fellow? I am aware that this is not the best place for these sort of questions but I am out of my wit. – Japorized May 15 '19 at 02:31
  • 1
    @Japorized I just noticed a bunch of \vbox and \hbox in the definition of \th@nonumberbreak; then I looked at \th@break and thought that it ought to be similar. I never use ntheorem because it's rather inflexible. – egreg May 15 '19 at 08:46