I created an environment to shade some text I deem important. However, what I have within this environment can vary from a simple paragraph to an actual section or subsection. The problem is that the spacing before the text increases with the importance of the heading. I believe this is normal behavior, but in the book I'm writing it is too exaggerated for even subsubsection. Is there a way to reduce this space in a simple manner? I guess one could try to determine in the macro whether a new section, subsection, or just a paragraph is passed, and subtract vertical space with negative \vspace but I guess this would not be a simple solution. This is the MWE:
\documentclass{article}
\usepackage{xcolor}
\usepackage{amsthm}
\usepackage{lipsum}
\usepackage{framed}
\definecolor{shadecolor}{gray}{.85}%
\newenvironment{advanced}{\par \begin{shaded} \small}{\par\end{shaded}}
\begin{document}
\begin{advanced}
\section{Section}
\lipsum[1]
\end{advanced}
\begin{advanced}
\subsection{Subsection}
\lipsum[1]
\end{advanced}
\begin{advanced}
\lipsum[1]
\end{advanced}
\end{document}
which produces this:

\usepackage{titlesec}and\titlespacing*{\section}{0pt}{0pt}{0pt}in the preamble (but the spacing is also modified for non framed sections ; it's probably also better to provide rubber lengths as1ex plus 1ex minus 0.5exinstead of fixed lengths as1ex, in particular for vertical space after the title). Change\subsectionin the same way. – quark67 Feb 18 '23 at 00:45advancedenvironment I created. – aaragon Feb 18 '23 at 11:17