The OP stipulates "let us say I have a document with only text and theorems".
I use a tokencycle
pseudo-environment named \shothms...\endshothms
, one instance of which is wrapped around the whole document, to achieve the result.
As currently EDITED, it will only execute instances of \chapter
& \section
(without optional arguments) and theorem
& proposition
environments. Note that optional arguments to these macros/environments can be dealt with via tokcycle
, but I did not feel like investing the time and code here. See Parse Argument by Character while Executing Embedded Macros, for an example where it is done.
The macro tests (if one wanted to add additional macros to capture) is performed by the \testmacros
macro
\newcommand\testmacros[1]{%
\ifx\chapter#1\addcytoks{#1}\gdef\addarg{T}\else
\ifx\section#1\addcytoks{#1}\gdef\addarg{T}\else
\gdef\addarg{F}\fi\fi
}
The environment tests (if more are needed) are performed by the \testenvs
macro, given as
\newcommand\testenvs[1]{%
\ifx\thmchk#1 1\else
\ifx\propchk#1 1\else
0\fi\fi
}
given the preamble definitions
\def\thmchk{theorem}
\def\propchk{proposition}
The MWE:
\documentclass{book}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{tokcycle}
\def\addarg{F}
\def\checktheorems{F}
\def\charson{F}
\def\thmchk{theorem}
\def\propchk{proposition}
\declaretheorem{theorem}
\declaretheorem{proposition}
\stripgroupingtrue
\tokcycleenvironment\shothms
{\if T\charson\addcytoks{##1}\fi}
{%
\if T\addarg\addcytoks{{##1}}\gdef\addarg{F}\fi
\if F\checktheorems
\if T\charson\addcytoks{{##1}}\fi
\else
\gdef\tmp{##1}%
\ifnum\testenvs{\tmp}=1\relax
\if B\checktheorems
\addcytoks{\begin{##1}}\gdef\charson{T}%
\else
\addcytoks{\end{##1}}\gdef\charson{F}%
\fi
\fi%
\fi
\gdef\checktheorems{F}
}
{%
\ifx\begin##1\gdef\checktheorems{B}\else
\ifx\end##1\gdef\checktheorems{E}\else
\gdef\checktheorems{F}%
\if T\charson\addcytoks{##1}\fi%
\fi
\fi
\testmacros{##1}%
}
{\if T\charson\addcytoks{##1}\fi}
\newcommand\testmacros[1]{%
\ifx\chapter#1\addcytoks{#1}\gdef\addarg{T}\else
\ifx\section#1\addcytoks{#1}\gdef\addarg{T}\else
\gdef\addarg{F}\fi\fi
}
\newcommand\testenvs[1]{%
\ifx\thmchk#1 1\else
\ifx\propchk#1 1\else
0\fi\fi
}
\begin{document}
\shothms
\chapter{My Chapter}
Chapter text
\section{One}
blah blah
\section{Two}
\begin{theorem}
Bloh \textbf{Blah} \today
\end{theorem}
blih blih \textit{blow}
more blah
\begin{proposition}
Blah$^2$
\end{proposition}
Finis
\endshothms
\end{document}

ADDENDUM
If one wanted less formatting of the sectioning, one could redefine \testmacros
as
\newcommand\testmacros[1]{%
\ifx\chapter#1\addcytoks{\stepcounter{chapter}\par\noindent Chapter
\thechapter:~}\gdef\addarg{T}\else
\ifx\section#1\addcytoks{\stepcounter{section}\par\noindent Section
\thesection:~}\gdef\addarg{T}\else
\gdef\addarg{F}\fi\fi
}
resulting in
