Is there a way in (plain) (La)TeX to determine whether a subsection directly follows a section?
E.g. in the case
\section{Main level}
\subsection{Sublevel}
I want draw a line under the subsection, and in
\section{Main level}
some text
\subsection{Sublevel}
I want to draw a line under the section (not underlining the text, but a simple hsize-wide rule between heading and text).
To add some spice: I cannot include any external packages for they may interfer with my existing layout, therefore I need a rather low level solution to this problem. Is this even possible, at all?
Thanks to Ulrike Fischer's answer i ended up with something like
\documentclass{article}
\makeatletter
\newif\if@dolinesec\@dolinesecfalse
\def\section{\@dolinesectrue\@startsection{section}{1}{\z@}
{-2em\@plus-1ex\@minus-.2ex}
{1em\@plus.2ex}
{\normalsize\bfseries}}
\def\subsection{\@startsection{subsection}{2}{\z@}
{-1em\@plus-0.25ex\@minus-.2ex}
{1em\@plus.2ex}
{\normalsize\itshape\bfseries}}
\def\subsubsection{\@startsection{subsubsection}{3}{\z@}
{-3.25ex\@plus-1ex\@minus-0.2ex}
{1.5ex\@plus0.2ex}
{\normalfont\sffamily}}
\def\@afterheading{%
\@nobreaktrue
\everypar{%
\if@nobreak
\@nobreakfalse
\clubpenalty \@M
\if@afterindent \else
{\setbox\z@\lastbox
\if@dolinesec\rlap{\raise\baselineskip\hbox{\smash{\vrule width\hsize height.4\p@\relax}}}\fi%
}%
\fi
\@dolinesecfalse
\else
\clubpenalty \@clubpenalty
\everypar{}%
\fi}}
\makeatother
\begin{document}
\section{Test}
\subsection{Sub Test}
text text text text text text text text text text text text
text text text text text text text text text text text text
\subsubsection{Sub Test}\label{blabla}
text text text text text text text text text text text text
text text text text text text text text text text text text
\section{Test}
text text text text text text text text text text text text
text text text text text text text text text text text text
\subsection{Sub Test}
text text text text text text text text text text text text
text text text text text text text text text text text text
\subsubsection{Sub Test}\label{blabla}
text text text text text text text text text text text text
text text text text text text text text text text text text
\section{Test}\label{bla}
\subsection{Sub Test}\label{blabla}
\subsubsection{Sub Test}\label{blabla}
text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text text text text text text text
\end{document}
which gives me:

