I today saw very interesting case when subsection started by shaded environment
\documentclass{amsbook}%{amsart}
\usepackage{xcolor}
\usepackage{framed}
\newtheorem{theorem}{Theorem}[section]
\newenvironment{Shaded}{%
\def\FrameCommand{\fboxsep1pt \colorbox{shadecolor}}%
\MakeFramed {\FrameRestore}}%
{\endMakeFramed}
\newenvironment{ShadedTheorem}
{
\definecolor{shadecolor}{rgb}{.94,.96,.94}%
\begin{Shaded}%
\begin{theorem}%
}{%
\end{theorem}%
\end{Shaded}%
}
\begin{document}
\subsection{Bad Subsection}
\begin{ShadedTheorem}
Bad Theorem: is moved
\end{ShadedTheorem}
\subsection{Okay without shading}
\begin{theorem}
Good theorem
\end{theorem}
\subsection{Subsection with text first}
Okay if shaded theorem is not the first object
\begin{ShadedTheorem}
Theorem Statement
\end{ShadedTheorem}
\subsection{Subsection with space first OK}~
\begin{ShadedTheorem}
Theorem Statement
\end{ShadedTheorem}
\subsection{Subsection with \texttt{\textbackslash par} first: not okay}\par
\begin{ShadedTheorem}
Theorem Statement
\end{ShadedTheorem}
\subsection{Subsection, leave vertical mode first, then enter vertical mode again.}\leavevmode\par
\begin{ShadedTheorem}
Theorem Statement
\end{ShadedTheorem}
\end{document}
In all cases the shaded theorem should appear after the section heading. But that's not what the output shows.
In general as you can see there is way around. However somebody may see similar case and need some solution. Also may be there are solution different from what I found

\subsection. – Willie Wong May 19 '21 at 12:18\subsectionis meant to be run in. Thus it is assuming that the theorem statement is an implicit "argument" of\subsection. If that is indeed the case, adding\leavevmodeor$ $before the theorem should correct the order. (I will be offline for a few hours, and don't have time to check this now.) – barbara beeton May 19 '21 at 21:04\leavevmodeis something I've taken on faith, not understanding how it works well enough to explain. But after poking around, I like the solution I proposed here: Proof environment - line break after the “Proof.” It avoids a possible break after the heading at the end of a page. – barbara beeton May 20 '21 at 14:34