[Tex/LaTex] Align the names in \listtheorems (ntheorem)

horizontal alignmentntheoremtable of contentstheorems

Question:

How do I align the names of theorems in ntheorem list of theorems?

Screenshot: Current output

Screenshot: Intentended output (photoshopped)

Context:

I am using the ntheorem package to typeset a list of theorems and definitions (\listtheorems) and have defined several custom theorem environments. The environment names have different word lengths which leads to a non aligned typesetting in the list.

I am writing in German language in which only one environment name is significantly shorter altough noticable minimal meanderings exist between the others as well.

Screenshot: image

Related Questions
I have found two related questions but both focus only on spacing.

TeX.SE Question: 53353 Titlespacing for \listtheorems in ntheorem

TeX.SE Question: 43166 Spacing numwidth control for \listtheorems

Minimal Example:

\documentclass{article}

\usepackage{ntheorem}

\theoremlisttype{allname}

\newtheorem{axiom}{Axiom}
\newtheorem{proposition}{Proposition}
\newtheorem{lemma}{Lemma}
\newtheorem{corollary}{Corollary}
\newtheorem{converse}{Converse}

\begin{document}
\listtheorems{axiom,proposition,lemma,corollary,converse}

\begin{axiom}[Theorem 1] \end{axiom}
\begin{proposition}[Theorem 2] \end{proposition}
\begin{lemma}[Theorem 3] \end{lemma}
\begin{corollary}[Theorem 4] \end{corollary}
\begin{converse}[Theorem 5] \end{converse}

\end{document}

Best Answer

To solve this we need to patch \thm@@thmline in ntheorem.

It's called \thm@@thmline@noname or \thm@@thmline@name depending on the called options all or opt or, respectively allname or optname.

There are two versions of these macros in ntheorem. Those with four parameters are used in non-hyperref documents. Those with five parameters are used in documents when hyperref is loaded.

Therefore you can either patch \thm@@thmline directly and force allname/optname behavior like in the first code snippet or you can patch \thm@@thmline@name as in the second code snippet, which will preserve the ability to use opt and all. Both snippets will work with and without hyperref.

Solution 1

\documentclass{article}

\usepackage{ntheorem}

%\usepackage{hyperref}
%\usepackage[hyperref]{ntheorem}

\makeatletter
\AtBeginDocument{
    \@ifpackageloaded{hyperref}
     {
      % hyperref version of the macro
      \renewcommand\thm@@thmline[5]{%
        \@dottedtocline {-2}{0em}{2.3em}{\hyper@linkstart{link}{#5}{\makebox[\widesttheorem][l]{#1 \protect \numberline {#2}}#3}\hyper@linkend}{#4}%
      }
     }
     {
      % standard, non-hyperref version of the macro
      \renewcommand\thm@@thmline[4]{%
        \@dottedtocline {-2}{0em}{2.3em}{\makebox[\widesttheorem][l]{#1 \protect \numberline {#2}}#3}{#4}%
     }

} \settowidth{\widesttheorem}{Proposition 10\quad} } \makeatother \newlength\widesttheorem

\theoremlisttype{allname}

\newtheorem{axiom}{Axiom}
\newtheorem{proposition}{Proposition}
\newtheorem{lemma}{Lemma}
\newtheorem{corollary}{Corollary}
\newtheorem{converse}{Converse}

\begin{document}
\listtheorems{axiom,proposition,lemma,corollary,converse}

\begin{axiom}[Theorem 1]
\end{axiom}
\begin{proposition}[Theorem 2]
\end{proposition}
\begin{lemma}[Theorem 3]
\end{lemma}
\begin{corollary}[Theorem 4]
\end{corollary}
\begin{converse}[Theorem 5]
\end{converse}

\end{document}

Solution 2

\documentclass{article}

\usepackage{ntheorem}

%\usepackage{hyperref}
%\usepackage[hyperref]{ntheorem}

\makeatletter
\def\thm@@thmline@name#1#2#3#4{%
        \@dottedtocline{-2}{0em}{2.3em}%
                   {\makebox[\widesttheorem][l]{#1 \protect\numberline{#2}}#3}%
                   {#4}}
\@ifpackageloaded{hyperref}{
\def\thm@@thmline@name#1#2#3#4#5{%
    \ifx\\#5\\%
        \@dottedtocline{-2}{0em}{2.3em}%
            {\makebox[\widesttheorem][l]{#1 \protect\numberline{#2}}#3}%
            {#4}
    \else
        \ifHy@linktocpage\relax\relax
            \@dottedtocline{-2}{0em}{2.3em}%
                {\makebox[\widesttheorem][l]{#1 \protect\numberline{#2}}#3}%
                {\hyper@linkstart{link}{#5}{#4}\hyper@linkend}%
        \else
            \@dottedtocline{-2}{0em}{2.3em}%
                {\hyper@linkstart{link}{#5}%
                  {\makebox[\widesttheorem][l]{#1 \protect\numberline{#2}}#3}\hyper@linkend}%
                    {#4}%
        \fi
    \fi}
}
\makeatother
\newlength\widesttheorem
\AtBeginDocument{
  \settowidth{\widesttheorem}{Proposition 10\quad}
}

\theoremlisttype{allname}

\newtheorem{axiom}{Axiom}
\newtheorem{proposition}{Proposition}
\newtheorem{lemma}{Lemma}
\newtheorem{corollary}{Corollary}
\newtheorem{converse}{Converse}

\begin{document}
\listtheorems{axiom,proposition,lemma,corollary,converse}

\begin{axiom}[Theorem 1]
\end{axiom}
\begin{proposition}[Theorem 2]
\end{proposition}
\begin{lemma}[Theorem 3]
\end{lemma}
\begin{corollary}[Theorem 4]
\end{corollary}
\begin{converse}[Theorem 5]
\end{converse}

\end{document}

In the argument to \settowidth you put the longest label, the biggest number and a suitable spacing.

enter image description here

Related Question