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}
dedded
  • 2,236
  • Someone should mention \everypar. It isn't what you want, but it could be useful. – John Kormylo Oct 01 '19 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. – Phelype Oleinik Oct 01 '19 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 02 '19 at 00: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 @ feature to reply to comments so people receive a notification. – Phelype Oleinik Oct 02 '19 at 09:26
  • Accepting @campa 's answer, since it answers the original question. But I don't understand @phelype-oleinik 's comment as much as I thought I did. Why doesn't this work?: \newcommand*\foo[1]{% \ifvmode% \expandafter\@gobble% \fi% {\testAchar{#1}}} The \@firstofone seems to be needed even with no \fi in the way. – dedded Oct 02 '19 at 23:07
  • @dedded The case for vertical mode is clear, \ifvmode is true and \@gobble consumes the {\testAchar{#1}}. When you're not in vertical mode, then all is left is {\testAchar{#1}}, and then the \testAchar macro 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 \@firstofone is 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
  • This would have been better as a new question. The "edit: here's a new problem" format makes this very hard to figure out what's been solved and what hasn't. – Teepeemm Nov 11 '22 at 00:42
  • @Teepeemm, sounds good: I can convert this to a new question tonight (or this weekend). – dedded Nov 11 '22 at 14:49
  • 1
    @Teepeemm, new question filed and linked above. – dedded Nov 11 '22 at 22:10

1 Answers1

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

campa
  • 31,130
  • 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 01 '19 at 23:49