Text FormattingList of specific Theorem Environments

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
ProbablyNotYou
Posts: 3
Joined: Mon Jul 08, 2024 8:44 am

List of specific Theorem Environments

Post by ProbablyNotYou »

Hello Everyone!

I am looking for a way to automatically create a list of specific theorem environments, like for example a list of all definitions in the document, and found this code in a conversation from tex.stackexchange (https://tex.stackexchange.com/questions ... vironments) from 12 years ago:

Code: Select all

\documentclass[a4paper]{book}
\usepackage{amsthm,thmtools}
\usepackage{xcolor}
\usepackage{framed}
\colorlet{shadecolor}{lightgray!25}

\newtheorem{thm}{Theorem}[chapter]

\newtheoremstyle{definitionsty}{3pt}{3pt}{\slshape}{}{\bfseries}{.}{.5em}{}
\theoremstyle{definitionsty}
\newtheorem{tdefn}{Definition}[chapter]
\newenvironment{defn}
  {\begin{shaded}\begin{tdefn}}
  {\end{tdefn}\end{shaded}}

\usepackage{etoolbox}
\makeatletter
\patchcmd\thmtlo@chaptervspacehack
  {\addtocontents{loe}{\protect\addvspace{10\p@}}}
  {\addtocontents{loe}{\protect\thmlopatch@endchapter\protect\thmlopatch@chapter{\thechapter}}}
  {}{}
\AtEndDocument{\addtocontents{loe}{\protect\thmlopatch@endchapter}}
\long\def\thmlopatch@chapter#1#2\thmlopatch@endchapter{%
  \setbox\z@=\vbox{#2}%
  \ifdim\ht\z@>\z@
    \hbox{\bfseries\chaptername\ #1}\nobreak
    #2
    \addvspace{10\p@}
  \fi
}
\def\thmlopatch@endchapter{}

\makeatother
\renewcommand{\thmtformatoptarg}[1]{ -- #1}
\renewcommand{\listtheoremname}{List of definitions}


\begin{document}

\frontmatter
\listoftheorems[ignoreall,show={tdefn}]

\mainmatter
\chapter{X}

\begin{defn}[My hilarious definition]
bla bla
\end{defn}

\chapter{Y}

\begin{thm}
b
\end{thm}

\chapter{Z}

\begin{defn}[My hilarious definition 2]
bla bla
\end{defn}

\begin{thm}
a
\end{thm}

\end{document}
This creates a very good solution, but once the chapter counter goes from 1 to 2 the alignment is wrong, as the "Definition - ... " is static and doesn't adjust with the width of the theorem counter.
Another thing I don't get right is the from "egreg" proposed costumization of creating the output "Definition X.X -- Name" instead of "X.X Definition -- Name".

I'm just not able to understand this complicated code and it would be really nice, if someone could help me get it right :).
Thanks in advance

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Post Reply