Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'd like to build an environment that would detect if it is right after a sectioning command (\section, \subsection or \subsubsection) and smart enough to detect if a float-type environment (say figure) has been inserted between the sectioning command and itself. Here is a sketch of what I have in mind (written for the article class, my real case is with the book class, but I don't think it matters that much).

\documentclass{article}
\newif\ifRightAfterASectioningCommand
\newenvironment{myenv}{%
  % Some detection mechanism
  % that sets RightAfterASectioningCommand
  % to true or false
  % ...
  \ifRightAfterASectioningCommand
    Right after a sectioning command
  \else
    Not right after a sectioning command
  \fi
  }{}
\begin{document}

\begin{myenv}
% RightAfterASectioningCommand is false
\end{myenv}

\section{Section}

\begin{myenv}
% RightAfterASectioningCommand is true
\end{myenv}

\begin{myenv}
% RightAfterASectioningCommand is false
\end{myenv}

\subsection{Subsection}

\begin{myenv}
% RightAfterASectioningCommand is true
\end{myenv}

\begin{myenv}
% RightAfterASectioningCommand is false
\end{myenv}

\subsubsection{Subsubsection}

\begin{myenv}
% RightAfterASectioningCommand is true
\end{myenv}

\begin{myenv}
% RightAfterASectioningCommand is false
\end{myenv}

\subsubsection{Subsubsection}

\begin{figure}
\end{figure}

\begin{myenv}
% RightAfterASectioningCommand is true
\end{myenv}

\begin{myenv}
% RightAfterASectioningCommand is false
\end{myenv}

\end{document}
share|improve this question
    
\ifRightAfterASectioningCommand==\if@nobreak –  egreg May 16 '13 at 10:31

1 Answer 1

up vote 10 down vote accepted

All sectional command globally set the internal conditional \if@nobreak to true. This conditional is reset to false as soon as a paragraph is started. However, floats would change it only if really typeset at the spot. The rationale is that a float appearing immediately after a sectional title counts as the first paragraph after the title.

You probably will be satisfied with

\makeatletter
\newenvironment{myenv}{%
  % Some detection mechanism
  % that sets RightAfterASectioningCommand
  % to true or false
  % ...
  \if@nobreak
    Right after a sectioning command
  \else
    Not right after a sectioning command
  \fi
  }{}
\makeatother
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.