3

Edit (Nov. 2022): I've hit a snag with the answer I accepted below. The \ifvmode command returns true when I'm not starting a new paragraph after a {verse} environment. I was asked to make this a new question, which I've done. It can be found here.

I would like to write a macro that behaves differently at the start of a paragraph. How can I do this?

(Ultimately I would like this macro to take an argument, but am assuming that's not relevant to the MWE.)

Non-working example:

\documentclass{article}
\newcommand\foo{%
  \ifAtParagraphBeginning% How to do this?
    Hello
  \else
    (hello)
}
\begin{document}

\foo{} % This should print "Hello"
says the quick brown fox jumping over the lazy dog.

The lazy dog says bark \foo{} % This should print "(hello)"
to the brown fox.
\end{document}

Edit: Here's a new example, where I hit a problem. I can't get the "@ifnextchar A" to work when it's in the else clause of the \if (is @ifnextchar looking at "\fi" instead of the character that follows?):

\newcommand*\foo[1]{%
  \ifvmode
  \else
    \@ifnextchar A%
      {\textsuperscript{#1}\kern -0.15em}%
      {\textsuperscript{#1}\kern 0pt}%
  \fi}
9
  • Someone should mention \everypar. It isn't what you want, but it could be useful. Oct 1, 2019 at 23:31
  • Regarding your edit: the way the code is structured, the \@ifnextchar test will always yield false because it will not see the next character (that you expect it to see), but the \fi (as you concluded yourself in another edit). To make that work you need to take the \fi out of the way: \newcommand*\foo[1]{% \ifvmode \expandafter\@gobble \else \expandafter\@firstofone \fi \testAchar} where \testAchar contains the actual test you want to do. Oct 1, 2019 at 23:56
  • OK, maybe I got it. I really have \testAchar{#1}, and it seems to work if I group it: {\testAchar{#1}}.
    – dedded
    Oct 2, 2019 at 0:11
  • @dedded Yes, here TeX expects a single macro argument, which is either a single token (like \testAchar or \fi or a) or a {-}-balanced list of tokens, like {\testAchar{#1}}. P.S.: In comments use the @<name> feature to reply to comments so people receive a notification. Oct 2, 2019 at 9:26
  • 1
    @Teepeemm, new question filed and linked above.
    – dedded
    Nov 11, 2022 at 22:10

1 Answer 1

6

At the start of a paragraph TeX is in vertical mode, so you can use the \ifvmode primitive. Be careful about trailing spaces.

\documentclass{article}

\newcommand*{\foo}[1]{%
  \ifvmode
    Hello #1%
  \else
    (hello #1)%
  \fi  
}

\begin{document}
\foo{baz} says the quick brown fox jumping over the lazy dog.

The lazy dog says bark \foo{baz} to the brown fox.
\end{document}

enter image description here

1
  • This is so close (and works for the example I gave). I tried it in my application, however, and hit a snag. I'll update the question with a second example.
    – dedded
    Oct 1, 2019 at 23:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .