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}

\@ifnextchartest 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\fiout of the way:\newcommand*\foo[1]{% \ifvmode \expandafter\@gobble \else \expandafter\@firstofone \fi \testAchar}where\testAcharcontains the actual test you want to do. – Phelype Oleinik Oct 01 '19 at 23:56\testAcharor\fiora) or a{-}-balanced list of tokens, like{\testAchar{#1}}. P.S.: In comments use the @\newcommand*\foo[1]{% \ifvmode% \expandafter\@gobble% \fi% {\testAchar{#1}}}The\@firstofoneseems to be needed even with no\fiin the way. – dedded Oct 02 '19 at 23:07\ifvmodeis true and\@gobbleconsumes the{\testAchar{#1}}. When you're not in vertical mode, then all is left is{\testAchar{#1}}, and then the\testAcharmacro will look at the next token, which is a}and will expand to false (or throw an error, depending on how it's defined). The\@firstofoneis there to grab the{\testAchar{#1}}and leave\testAchar{#1}: it removes a layer of braces. That's why you need it. – Phelype Oleinik Oct 02 '19 at 23:34