# Customizing theorem list

I use theorems a lot, and make theorem lists. Here is a minimal theorem example with thmtools:

\documentclass[a4paper]{report}
\usepackage{amsthm,thmtools,amsmath,amssymb,amsfonts}
\newtheorem{teor}{Theorem}[section]

\begin{document}
\chapter{Measures}
\begin{teor}[name=$\sigma$-subadditivity of a measure]
If $(X,\mathcal{E},\mu)$ is a measure space, then for any countable collection of measurable sets $\{A_n\}_{n\in\mathbb{N}}$ we have:
$\mu\!\left(\bigcup_{n\in\mathbb{N}}A_n\right)\leq\!\sum_{n=1}^\infty\mu(A_n).$
\end{teor}

\listoftheorems
\end{document}

My problems with the default thmtools handling of theorems are the following:

1. I might want to change the counter sequence for the same theorem; for example, if I define teor as [section] (which seems reasonable since I most often have theorems in chapters with sections and no subsections) but then find myself in a chapter with no sections, as in the example, I'd want to remove the 0; if I find myself with subsections, I'd like to add the subsection number to the counter sequence;
2. If I need a new kind of theorem I can either use manual bold-italing font changes and manually write things to the appropriate file for the list, or go back to the start of the document to add a \newtheorem;
3. The structure of an item on the list.

Point #1 can easily be handled by fiddling with the counter through \counterwithin and similar. #2 can easily be handled by defining a new \xbegin macro which takes appropriate arguments, defines the theorem kind if undefined (btw is \newtheorem an \@onlypreamble command?), and then fires the \begin command. Let me explain #3, which is the main issue in the question, better. The list has the following structure for an item:

<counter sequence><spacing determined bu \thmt@listnumwidth><Theorem kind><space>%
<Theorem name in brackets><dots>

<Theorem kind><space><ctr seq><colon><space><theorem name>

I have devised a way to solve all three in one go, but it has oodles of conditionals and keeps giving me problems. Here is my MWE with things done my way:

\documentclass[a4paper]{report}
\usepackage{amsthm,thmtools,amsmath,amssymb,amsfonts,noindentafter}
\usepackage[thm=true,lang=english]{mworkxb} %Call to my own package for theorem handling; see code at link for.
\xtheorsetlist %See code at link. Sets \thmt@listnumwidth.
\listformat{#1} %See code. Removes brackets from \thmtformatoptarg.
%\newtheorem{teor}{Theorem}[section]

\begin{document}
\chapter{Measures}
\begin{teor}[$\sigma$-subadditivity of a measure]
If $(X,\mathcal{E},\mu)$ is a measure space, then for any countable collection of measurable sets $\{A_n\}_{n\in\mathbb{N}}$ we have:
$\mu\!\left(\bigcup_{n\in\mathbb{N}}A_n\right)\leq\!\sum_{n=1}^\infty\mu(A_n).$
\end{teor}

\listoftheorems
\end{document}

Here is a link to a gist of the relevant portions of the package:

The xtheor environment and its twins have messy comments, I know, but I don't have time to deal with them. The main problems it gives me are linked to the reference-getting commends, which often give incomprehensible errors probably due to reference sequences not being found and the \@get@label@… commands returning "oh dear I don't have enough arguments". I'm planning to try an iterative \@car/\@cdr approach for that, but before that, I wanted to know if there is a way to tamper with the list headings and get the structure I indicated without those oodles of conditionals and complex code in the link. Any ideas? Thx in advance.

Edit: to clarify, this question is just about getting the desired look for the items on the list of theorems. I linked the code just in case anyone wanted to see how I achieved this in my own not-very-efficient way. I do not want anyone to help me clear that code, I want to start over, go back to basic thmtools (or almost), but change the way the list of theorems is managed. Basically, I'd like to know how I can redefine the \listoftheorems command to have the theorem kind along with the counter sequence, for then it is easy to change \thmt@formatoptarg to remove the brackets. So if you look at the first list, I want to be able to alter the way the left column is produced, since I know how to alter the right column (with \thmt@formatoptarg). Hope this makes the question clear.

Expansion: to further elaborate on «just in case anyone wanted to see …», the main reason I put the code was to show the result in the second picture, which is my aimed result. I might as well remove the code altogether, but since this is a LaTeX forum I thought someone might have been curious to know how I managed that. I placed the long code outside the site to avoid overloading the question, then I thought a couple comments on the presentation of the code and why I wanted to abandon this approach were in order, which makes for the last bit of the original question. Also, if anyone wanted to compile the second MWE, the code in the gist was necessary, so here's another «just in case».

Reading guide for the long code: If anyone tries to read the 989 lines of code in the gist, well, there is one portion which is repeated three times, which is ll. 266-427, repeated in ll. 430-592 and ll. 596-747. The repetition is because I have three identical environments, to allow different theorems to go on different lists, since the theorems are all of one kind per environment: @thmattr, @thmattr2 and @thmattr3, matching xtheor, xtheor2 and xtheor3. On line 762 there is a \str_case:nnF which sets some strings for various languages. You are only interested in the {english} case, ll. 782-799. Also, ll. 184-223 are the lower-cased equivalents of ll. 158-183, so if you want to see the problems that are urging me towards giving up this approach, you can read the latter and skip the former, since the only difference is the lower-casing of the reference text. From l. 963 to the end, there are extra theorem-related things that aren't really relevant. I didn't try to minimize the code before gisting, sorry about that. Anyway, it is there just for completeness, and I don't mean anyone to read that mess. Unless they want to :).

Update: Since this question is on hold, I tried my luck with the source again. This question directed me to editing \thmt@mklistcmd. Indeed, I see:

\thmtlo@chaptervspacehack
\csname ll@\thmt@envname\endcsname
}%
}

and \thmt@mklistcmd defines precisely that \csname ll@thmt@envname\endcsname, so if I tell it to place a line with the structure I want, everything should be fine. Trouble is, it isn't. With the following MWE:

\documentclass[a4paper]{report}
\usepackage{amsthm,thmtools,amsmath,amssymb,amsfonts}
\newtheorem{teor}{Theorem}[section]
\makeatletter
\renewcommand\thmt@mklistcmd{%
\@xa\protected@edef\csname l@\thmt@envname\endcsname{% CHECK: why p@edef?
\@nx\@dottedtocline{1}{1.5em}{\@nx\thmt@listnumwidth}{\thmt@thmname}{mu}%
}%
\ifthmt@isstarred
\@xa\def\csname ll@\thmt@envname\endcsname{%
\protect\numberline{\thmt@thmname\protect\let\protect\autodot\protect:}%
\ifx\@empty\thmt@shortoptarg\else\protect\thmtformatoptarg{\thmt@shortoptarg}\fi
}%
\else
\@xa\def\csname ll@\thmt@envname\endcsname{%
\thmt@thmname\ \csname the\thmt@envname\endcsname:\hfil%
\ifx\@empty\thmt@shortoptarg\else\protect\thmtformatoptarg{\thmt@shortoptarg}\fi
}%
\fi
\@xa\gdef\csname thmt@contentsline@\thmt@envname\endcsname{%
\thmt@contentslineShow% default:show
}%
}
\makeatother

\begin{document}
\chapter{Measures}
\begin{teor}[name=$\sigma$-subadditivity of a measure]
If $(X,\mathcal{E},\mu)$ is a measure space, then for any countable collection of measurable sets $\{A_n\}_{n\in\mathbb{N}}$ we have:
$\mu\!\left(\bigcup_{n\in\mathbb{N}}A_n\right)\leq\!\sum_{n=1}^\infty\mu(A_n).$
\end{teor}

\listoftheorems
\end{document}

I'd expect to see \@writefile{loe}{Theorem 1.0.1: \thmtformatoptarg{$\sigma$-subadditivity of a measure}}{1}}. But I don't. I keep seeing the old \@writefile{loe}{\contentsline {teor}{\numberline {1.0.1}Theorem\thmtformatoptarg {$\sigma$-subadditivity of a measure}}{1}}, and I have trashed the aux loe and co. files and recompiled many times. So what is happening?

Update: Manually redefining every single such control sequence (or redefining \newtheorem to do that automatically) solves the problem, which is very strange, or rather makes the fact the former option didn't work even more bizarre, since it was defining exactly the same control sequence in exactly the same way. But I guess I'd better ask another question about this strange thing, since it's more of a follow-up. Anyway, here's the working thing:

\documentclass[a4paper]{report}
\usepackage{amsthm,thmtools,amsmath,amssymb,amsfonts,letltxmacro}
\newtheorem{teor}{Theorem}[section]
\makeatletter
\renewcommand\ll@teor{%
\thmt@thmname\ \csname the\thmt@envname\endcsname:\hfil%
\ifx\@empty\thmt@shortoptarg\else\protect\thmtformatoptarg{\thmt@shortoptarg}\fi
}
\LetLtxMacro{\original@newtheorem}{\newtheorem}
\renewcommand{\newtheorem}[1]{\original@newtheorem #1 \@xa\def\csname ll@\thmt@envname\endcsname{\thmt@thmname\ \csname the\thmt@envname\endcsname:\hfil%
\ifx\@empty\thmt@shortoptarg\else\protect\thmtformatoptarg{\thmt@shortoptarg}\fi
}}
\newtheorem{{defi}{Definizione}[section]}
\newtheorem{*{mapu}{Mapu}}
\makeatother

\begin{document}
\chapter{Measures}
\begin{teor}[name=$\sigma$-subadditivity of a measure]
If $(X,\mathcal{E},\mu)$ is a measure space, then for any countable collection of measurable sets $\{A_n\}_{n\in\mathbb{N}}$ we have:
$\mu\!\left(\bigcup_{n\in\mathbb{N}}A_n\right)\leq\!\sum_{n=1}^\infty\mu(A_n).$
\end{teor}
\begin{defi}[name=Una definizione a caso]
Eccheccavoloneso!
\end{defi}
\begin{mapu}[name=mapu]
Mapu mapu mapu mapu.
\end{mapu}

\listoftheorems
\end{document}

If you reopen, I can self-answer, and then link to the follow-up post. If you think I'd do well using this post also for the follow-up, do tell me. By the way, this question gave me the idea of manually redefining every \csname ll@\thmt@envname\endcsname. Even making the definitions \global in the redefinition of \thmt@mklistcmd is useless, which I write here for the mere sake of completeness.

• You're basically asking “How do I clear my messy code” without even telling where this code is: I see no link. This is “too broad” anyway. – egreg Jan 3 '15 at 14:48
• @egreg it says "here is a link" with the link underneath. And I'm asking if an altogether different approach allows for the same results in the theorem list, so I wouldn't term it as "clear my messy code". – MickG Jan 3 '15 at 15:40
• Sorry, I couldn't see the link. But do you really think anybody would like to read 989 lines of code? Or trying to solve a badly stated problem? – egreg Jan 3 '15 at 15:48
• @egreg I hope the edit clarifies the question. – MickG Jan 3 '15 at 16:09
• Everybody see the update. – MickG Jan 4 '15 at 15:05

The answer is already in the question, since at the time I discovered it thanks to this question the question this answer answers was closed and with 4 reopen votes. Now I find out it has been reopen so I post a partial answer to accept it, bump the question off the unanswered list, and finally finish its dreadful history. What I need to do is one of the following:

• Redefine \thmt@mklistcmd, the command telling thmtools how to define all macros below and therefore how to make the list, if I want the change to apply to all theorems;
• Redefine the single \ll@<envname> for the theorem kind I want to change the \listoftheorems appearance of; e.g. if I have a theorem called defi and want to change the appearance of that kind of theorems in the list, I redefine \ll@defi.

How to redefine it obviously depends on what I want. For example, if I want the header to be

<theorem kind><space><counters><colon><space><theorem title><dots>

like:

Definizione 2.1.4: Spazio Metrico

I will do something like:

\renewcommand\ll@defi{\thmt@thmname\ \csname the\thmt@envname\endcsname:\ \ifx\@empty\thmt@shortoptarg\else\protect\thmt@formatoptarg{\thmt@shortoptarg}

which naturally requires to redefine \thmt@formatoptarg to avoid the brackets, i.e. doing:

\renewcommand\thmt@formatoptarg[1]{#1}

I will, of course, study a way to get \dottedtocline back in, as to have the dots, if necessary. I will also study how to align the theorem titles. I finally note that, as pointed out by Ulrike Fischer's comment, the redefinition of \thmt@mklistcmd have to be made before declaring a theorem, and I guess redefining (or defining) \ll@defi before declaring the theorem defi is useless.