[Tex/LaTex] How to eat / gobble only whitespace or (implicit) \par

paragraphsspacing

I created a command that prints material in an aux file and that's all. I can reasonably expect it to (also) be used in the following ways:

\precis{Some text}
The section goes on.

\precis{Some text}

The section goes on.

That is, with or without the % signs at the end of lines that would automatically take care of any spurious spaces.

In order to remove the trailing spaces the command is defined as:

\newcommand{\precis}[1]{%
    % Whatever the command does
    \ignorespaces%
}

However, as shown in my second example, there is still the case of a "spurious \par" which I don't know how to deal with.

I have tried using \@gobble instead, but indeed this only works if the next character is a space or blank line, and it breaks if %-signs were used (i.e. it swallows the first letter of the sentence).

So, I would like to be able to gobble only whitespace and the implicit par. Is that possible?

(I am working on a package, hence the attempt to be foolproof… I can go with the % signs for myself.)

Best Answer

You can do it with a font sectioning command:

\documentclass{article}

\usepackage{lipsum}

\makeatletter
\newcommand\precis{%
  \@startsection{precis}{1000}
    {\z@}% Left indentation
    {\z@}% Space above
    {-\fontdimen2\font plus -\fontdimen3\font minus -\fontdimen4\font}% space after label
    {\normalfont\normalsize\bfseries}% font for
}
\newcommand\precismark[1]{}
\makeatother

\begin{document}

\lipsum[2]

\precis{Some text}
The section goes on.

\precis{Some text}

The section goes on.

\precis{Some text}


The section goes on.

\lipsum[3]

\end{document}

enter image description here

Alternatively, use \@ifnextchar to gobble white space and check whether \par follows; in that case gobble it and restart the machinery.

\documentclass{article}

\usepackage{lipsum}

\makeatletter
\newcommand{\precis}[1]{%
  \par\noindent\textbf{#1}\space
  \@ifnextchar\par{\precis@gobblepar}{}%
}
\newcommand{\precis@gobblepar}[1]{%
  \@ifnextchar\par{\precis@gobblepar}{}%
}
\makeatother

\begin{document}

\lipsum[2]

\precis{Some text}
The section goes on.

\precis{Some text}

The section goes on.

\precis{Some text}


The section goes on.

\lipsum[3]

\end{document}
Related Question