# How to create a section heading sensitive macro or environment?

I have a \comments macro that inserts three centered asterisks (* * *) and changes font size and color. A corresponding \nocomments macro adds another break with centered asterisks and restores standard font size and color.

I would like the \comments macro to be section heading sensitive, that is, after \chapter, \section, \subsection and their friends I would like to suspend output of the tree asterisks and only change font size and color.

In other words, directly after a heading, the macro should change font size and color, but in all other cases also output the 'anonymous break' (* * *).

I have tried looking into core latex constructs such as \if@nobreak, but it seems that a solution to this is not that simple.

And yes, this could be done with a \newenvironment as well, but since I use pandoc as front-end, using simple macros bypasses pandoc's automatic detection of environments. Anyway, a solution for macros will probably work with environments too.

A not-so-well-working example:

\documentclass{article}

\usepackage{xcolor}

\newcommand{\comments}{%
\begingroup
\color{blue}

% The following line should be suspended if \begincomments' is used
% directly after a sectioning command such as \section' or
% \subsection'. This is just yet another of my attempts that does
% not work.
\if@noskipsec\else
\begingroup\centering * * *\par\endgroup
\fi

\small
\setlength{\parindent}{0mm}
\setlength{\parskip}{.5\baselineskip}
}
\newcommand{\nocomments}{%
\begingroup\centering * * *\par\endgroup

\endgroup
}

\begin{document}

\section{Section header}

\comments

Some comments about what will be written in this section, but is not
yet written. It should not start with three asterisks.

\nocomments

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

\comments

More comments. This should, however, start with three asterisks.

\nocomments

\end{document}

-
Welcome to TeX.SX! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Tobi Jan 4 '14 at 10:54

## 1 Answer

The conditional \if@nobreak is true after a section title (any level). You missed \makeatletter, probably.

\documentclass{article}

\usepackage{xcolor}

\makeatletter
\newcommand{\comments}{%
\begingroup
\if@nobreak\else
\begingroup\centering \textcolor{blue}{$*\ {*}\ *$}\par\endgroup
\fi
\small
\setlength{\parindent}{0mm}
\setlength{\parskip}{.5\baselineskip}
\everypar=\expandafter{\the\everypar\color{blue}}
}
\newcommand{\nocomments}{%
\par\begingroup\centering $*\ {*}\ *$\par\endgroup
\endgroup
}
\makeatother

\begin{document}

\section{Section header}

\comments
Some comments about what will be written in this section, but is not
yet written. It should not start with three asterisks.
\nocomments

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

\comments
More comments. This should, however, start with three asterisks.
\nocomments

\end{document}


I made some adjustments in order not to issue \color{blue} in vertical mode, which can have unexpected consequences.

This is the “environment” version, that, together with the comment package may be used for silently removing the comment text.

\documentclass{article}

\usepackage{xcolor}

\makeatletter
\newenvironment{comments}{%
\if@nobreak\else
\begingroup\centering \textcolor{blue}{$*\ {*}\ *$}\par\endgroup
\fi
\small
\setlength{\parindent}{0mm}
\setlength{\parskip}{.5\baselineskip}
\everypar=\expandafter{\the\everypar\color{blue}}
}
{%
\par\begingroup\centering $*\ {*}\ *$\par\endgroup
}
\makeatother

\begin{document}

\section{Section header}

\begin{comments}
Some comments about what will be written in this section, but is not
yet written. It should not start with three asterisks.
\end{comments}

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

\begin{comments}
More comments. This should, however, start with three asterisks.
\end{comments}

\end{document}

-
Thank you! I tried \if@nobreak as well, but forgot the \makeatletter. What a beginner's mistake! 😉 –  bluebirch Jan 4 '14 at 14:08
@bluebirch Finding missing % or \makeatletter is my main source for reputation here, someone would say. ;-)` –  egreg Jan 4 '14 at 14:09