Here is your solution where the assumptions are:
- You are using
article
class.
- You are not going to change the font of the sections.
- By sectioning commands, you mean
\section
as well as \subsection
and \subsubsection
. If this is otherwise, please comment out the lines
for \subsection
and \subsubsection
.
\documentclass{article}
\pagenumbering{gobble}
\usepackage{xspace}
\usepackage{etoolbox}
\newtoggle{insidesection}
\togglefalse{insidesection}
\patchcmd{\section}{\bfseries}{\bfseries\toggletrue{insidesection}}{}{}
\patchcmd{\subsection}{\bfseries}{\bfseries\toggletrue{insidesection}}{}{}
\patchcmd{\subsubsection}{\bfseries}{\bfseries\toggletrue{insidesection}}{}{}
\def\insideoroutside{\iftoggle{insidesection}{I AM INSIDE\xspace}{I am outside\xspace}}
\begin{document}
This is before any section: \insideoroutside
\section{\insideoroutside At Start Section}
\insideoroutside
\subsection{A Subsection \insideoroutside}
\insideoroutside
\section{At Mid \insideoroutside Section}
\insideoroutside
\subsection{Another Subsection}
\insideoroutside
\subsubsection{And a Subsubsection \insideoroutside}
\insideoroutside
\section{At End Section \insideoroutside}
\insideoroutside
\section*{What if There is a Star? \insideoroutside}
\end{document}

How It Works?
If we take a look at the definition of the sectioning commands in article.cls
, we find
that the last (6th actually or #6) argument which is being sent to \@startsection
is something in the line of, \normalfont\Large\bfseries
.
The above effectively changes the font of the section text. We exploit this by putting
a \toggletrue{insidesection}
just after \bfseries
.
Here, \patchcmd{\section}{\bfseries}{\bfseries\toggletrue{insidesection}}{}{}
replaces \bfseries
with \bfseries\toggletrue{insidesection}
, thus setting
the flag to true.
Our sense of logic would tell us to reset the flag to false at the end
of the sectioning text, but that does not become necessary.
As mentioned above, all the sectioning commands call \@startsection
to
render the section. And inside the definition of this macro, the
scoping around argument #6
makes the flag setting local and hence
it returns to its original value of false once the section name text
has been rendered.
To learn more about
\patchcmd
, read this excellent answer.
\section
or includes\subsection
and\subsubsection
as well? – Masroor Aug 28 '17 at 16:35