# How to make cleveref compress different-level theorems?

Whenever you have differently deep sections in your document and wish to number your theorems (as well as notes, lemmas, propositions, ...) according to the currently deepest level, you tend to write code such as

\documentclass[USenglish]{lipics-v2018}%%% loads amsthm and changes it a bit. Available from https://www.dagstuhl.de/en/publications/lipics/instructions-for-authors/
%\documentclass{article}\usepackage{amsthm}%%% Same outcome up to formatting
\usepackage{cleveref}
\newtheorem{thmS}{Theorem}[section]
\newtheorem{thmSS}{Theorem}[subsection]
\newtheorem{thmSSS}{Theorem}[subsubsection]
\crefname{thmS}{Thm.}{Thms.}%%% to be used on the section level
\crefname{thmSS}{Thm.}{Thms.}%%% to be used on the subsection level
\crefname{thmSSS}{Thm.}{Thms.}%%% to be used on the subsubsection level
%%% Similar junk for noteS, noteSS, noteSSS, lemmaS, lemmaSS, lemmaSSS, propS, propSS, propSSS, etc.
\begin{document}
\section{Section 1}
\begin{thmS}\label{thm:in1}
My great theorem.
\end{thmS}
\section{Section 2}
\subsection{Subsection 2.1}
\begin{thmSS}\label{thm:in21}
Another great theorem.
\end{thmSS}
\section{Section 3}
\subsection{Subsection 3.1}
\subsubsection{Subsection 3.1.1}
\begin{thmSSS}\label{thm:in311}
One more great theorem.
\end{thmSSS}
\section{Section 4}
The following result follows from \cref{thm:in1,thm:in21,thm:in311}: \dots
\end{document}


(Above, I'm primarily interested in the lipics class from https://www.dagstuhl.de/en/publications/lipics/instructions-for-authors/, but the same question also applies to the combination article+amsthm.) The output is great:

(An aside: once could say that an alternative way to achieve the same output would be defining \Crefname rather than \crefname and using the capitalize class option for cleveref. My question does not apply to this alternative, since for enumi, for example, I wish to have both capital and noncapital versions: \crefname{enumi}{part}{parts} \Crefname{enumi}{Part}{Parts}.)

There is just one issue: the space in the last line of the output is a bit wasted, since you could express the same information with a shorter

The following result follows from Thms. 1.1, 2.1.1, and 3.1.1.1: ...

How to make \cref{...} produce such an output automatically? Can one write some other macro (say, \magiccref{...}) doing this kind of automation?

I don't actually think a separate theorem environment for every depth is the right way to go about this, even if you're not using cleveref. It would be more effective to instead define just a single theorem environment and make the formatting of the theorem number depend on the current depth.

The following code produces the same output, except for the fact that cleveref now regonises that these are all theorem environments of the same type (because they are). It therefore prints "Thms." instead of repeating "Thm." three times.

%\documentclass[USenglish]{lipics-v2018} %% <- also works
\documentclass{article}

%% Redefined these to leave out any zeros
\renewcommand\thesubsection{\thesection\ifnum\value{subsection}>0.\arabic{subsection}\fi}
\renewcommand\thesubsubsection{\thesubsection\ifnum\value{subsubsection}>0.\arabic{subsubsection}\fi}

\usepackage{amsthm}
\usepackage{cleveref}
\newtheorem{thm}{Theorem}[subsubsection]
\crefname{thm}{Thm.}{Thms.}

\begin{document} %% I only replaced thmS, thmSS and thmSSS by thm below
\section{Section 1}
\begin{thm}\label{thm:in1}
My great theorem.
\end{thm}
\section{Section 2}
\subsection{Subsection 2.1}
\begin{thm}\label{thm:in21}
Another great theorem.
\end{thm}
\section{Section 3}
\subsection{Subsection 3.1}
\subsubsection{Subsection 3.1.1}
\begin{thm}\label{thm:in311}
One more great theorem.
\end{thm}
\section{Section 4}
The following result follows from \cref{thm:in1,thm:in21,thm:in311}: \dots
\end{document}


(The output below was created by uncommenting the lipics-v2018 line, but it works with article and amsthm as well.)

## Explanation:

I've defined just a single theorem-like environment called thm, whose numbering is subordinate to that of subsubsections. This means that the thm counter will be reset automatically to 0 whenever the section, subsection or subsubsection counter is either incremented or are reset to 0. (Incrementing the section counter — generally achieved by a \section directive — resets the subsection counter and hence also the subsubsection counter to 0 automatically. Likewise, a \subsection directive resets the subsubsection counter to 0 automatically.)

The formatting of the number assigned to a thm environment is determined by the macro \thethm. This macro is defined as

\thesubsubsection.\arabic{thm}


since we run the instruction \newtheorem{thm}{Theorem}[subsubsection]. So it takes the formatted subsubsection counter and affixes a period and the current value of the thm counter, expressed in arabic numerals.

The macro \thesubsubsection determines the formatting of the number assigned to a subsubsection-level header. In many document classes, including both article and lipics-v2018, \thesubsubsection is defined by default as

\thesubsection.\arabic{subsubsection}


The macro \thesubsection, in turn, is defined by default as \thesection.\arabic{subsection}, and \thesection generally just expands to \arabic{section}.

What the two \renewcommand instructions above do is redefine \thesubsection and \thesubsubsection so that they affix .\arabic{subsection} and .\arabic{subsubsection} only if these counters have a positive value. The result will be that all zeros in the formatted thm numbers are suppressed, both when they're stated and when they're cross-referenced, say, via a \cref instruction.

This method also lets you change formatted equation numbers in the same way, by adding \numberwithin{equation}{subsubsection} to your preamble. (\numberwithin is provided by the amsmath package.)

Here is a version that adapts to earlier redefinitions of \thesubsection and \thesubsubsection. Below, \thesubsection is redefined to check the value of the relevant counter and use either \thesection (if it is zero) or the original definition of \thesubsection (if it is non-zero). The new definition of \thesubsubsection works the same way.

I redefined section numbering to look like 1:1a instead of 1.1.1 for demonstration purposes.

%\documentclass[USenglish]{lipics-v2018} %% <- also works
\documentclass{article}

\usepackage{amsthm}
\usepackage{cleveref}
\newtheorem{thm}{Theorem}[subsubsection]
\crefname{thm}{Thm.}{Thms.}

%% Prior redefinition of \thesubsection and \thesubsubsection:
\renewcommand\thesubsection{\thesection:\arabic{subsection}}
\renewcommand\thesubsubsection{\thesubsection\alph{subsubsection}}

%% Redefine \thesubsection and \thesubsubsection to omit counters with value 0:
\edef\thesubsection{%                          %% <- edef to expand \thesubsection (once)
\noexpand\ifnum\noexpand\value{subsection}=0 %% <-if subsubsection=0
\noexpand\thesection                       %% <- use \thesection
\noexpand\else                               %% <- otherwise
\unexpanded\expandafter{\thesubsection}%   %% <- use whatever \thesubsection was before
\noexpand\fi
}
\edef\thesubsubsection{%
\noexpand\ifnum\noexpand\value{subsubsection}=0
\noexpand\thesubsection
\noexpand\else
\unexpanded\expandafter{\thesubsubsection}%
\noexpand\fi
}

\begin{document} %% I only replaced thmS, thmSS and thmSSS by thm below

\section{Section 1}
\begin{thm}\label{thm:in1}
My great theorem.
\end{thm}
\section{Section 2}
\subsection{Subsection 2.1}
\begin{thm}\label{thm:in21}
Another great theorem.
\end{thm}
\section{Section 3}
\subsection{Subsection 3.1}
\subsubsection{Subsection 3.1.1}
\begin{thm}\label{thm:in311}
One more great theorem.
\end{thm}
\section{Section 4}
The following result follows from \cref{thm:in1,thm:in21,thm:in311}: \dots

\end{document}


You could accomplish the same thing by redefining \thethm instead of \thesubsection and \thesubsubsection. You should then insert

\renewcommand\thethm{\thesection
\ifnum\value{subsection}>0.\arabic{subsection}\fi
\ifnum\value{subsubsection}>0.\arabic{subsubsection}\fi
.\arabic{thm}}


after the \newtheorem line, and remove the two other \renewcommand lines in the code block at the top. You could then do the same thing for \theequation. There is little benefit to doing it this way, unless the fact that \thesubsubsection prints zeros when you are not in a subsubsection is somehow relevant to you.

• +1 for showing how to set up thm, note, prop, and other theorem-like environments (and, if so desired, equation environments as well!) with "numbers" whose formats vary automatically with the sectioning depth. – Mico Jul 27 '18 at 5:53
• I've take the liberty of eding your explanations a bit, mainly to provide additional information about what some of the example code does. Feel free to revert. – Mico Jul 27 '18 at 9:44
• @mico No problem, I think these are all improvements. I had quite deliberately used the parenthesised word "(effectively)" though because the actual definitions of \thethm etc. use \@arabic{\c@...} instead of \arabic{...} and I didn't want to claim something that is technically incorrect. (So I will reinsert that.) – Circumscribe Jul 27 '18 at 9:59
• @mico On second thought, I'll leave it as it is. Being clear is probably more important than not being "technically wrong". I am going to change your -- into a proper em-dash though. – Circumscribe Jul 27 '18 at 10:12
• @user49915 - The only notable "unexpected" effect caused by the dynamic redefinition of \thesubsubsection might arise if a given \section contains \subsubsection directives but no \subsection directives. However, skipping a level of sectioning headers is a typographic no-no; your document shouldn't have such issues to begin with. (If it does such issues, you should tackle them ASAP.) – Mico Jul 27 '18 at 15:41