7

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.)

5
  • 2
    You should define \precis to be an inline heading using \@startsection or a list \item both of which have built in code to do exactly this. Jul 15 '15 at 8:41
  • Thanks! I thought of that but didn't dare to try as this really is neither… I will give \@startsection a try whenever I have time.
    – ienissei
    Jul 15 '15 at 8:45
  • 1
    @DavidCarlisle I wondered about this myself, still, even if \@startsection or \item work, an explanation would be welcome.
    – Manuel
    Jul 15 '15 at 8:52
  • @DavidCarlisle Yes, what is the code that does this and how does it work?
    – ienissei
    Jul 15 '15 at 8:53
  • maybe later, only had time for a comment just now:-) Jul 15 '15 at 9:06
3

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}
1
  • Thank you! I will implement it right away using \@ifnextchar – it does seem obvious now that you said it, but this is what makes you a wizard. It is more semantic than adding a sectioning level in my case.
    – ienissei
    Sep 28 '15 at 19:43
7

As previously pointed out, what you want to do is more elegantly achieved by building on LaTeX's existing sectioning mechanism. However, if you want an actual answer to your question, here it is:

\makeatletter
\def\precis#1{
  % Whatever the command does
  \@ifnextchar\par\@gobble\relax}
\makeatother

Note that \@ifnextchar already eats whitespace, so you only need to compare the next character to \par, gobble it if it matches, and do nothing otherwise.

3
  • Thank you very much! I accepted egreg's answer for completeness' sake and also because in my reckoning his recursive loop should be more robust, but your answer is appreciated all the same :)
    – ienissei
    Sep 28 '15 at 19:46
  • I don't see why you want that recursive loop. Multiple newlines are still only one \par. So the difference with the recursive loop is that if the user types \par\par it eats both pars. But at that point the user probably wants a new paragraph. Anyway, you can still bypass this by typing \relax\par, so you still aren't forcing the user not to have a paragraph, just making it harder to figure out what is going on without reading the source. Sep 28 '15 at 20:08
  • Yes, but with a recursion the code also survives a comment between two blank lines, i.e. with new lines instead \par%...\par. In the context of a package, this looks like a reasonable usage of TeX. The user does not want a new paragraph there in my context, as I explained in the question, because the command is used mid-line in the output although a break in the code is more suitable.
    – ienissei
    Sep 28 '15 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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