1

Consider the following MWE:

\documentclass{article}

\newcommand{\foo}{foo}

\begin{document}

test \foo bar

test {\foo} bar

test {\foo}.

\end{document}

enter image description here

My goal is to define \foo in such a way that it behaves as if it was braced in text {\foo} in order to maintain the space before the next word or ignore it in case of punctuation. Using \bgroup..\egroup in declaration doesn't seem to work. Bulk check for the next character via #1 for \foo is not desired because it's not versatile.

I'm curious about both plain tex and latex3 approach.

antshar
  • 4,238
  • 9
  • 30
  • 1
    you can not do that. the space is removed parsing the name so is gone before any definition of \foo is consulted. But see the xspace package which can usually add a space back (although I don't recommend it) – David Carlisle Apr 30 '23 at 09:50
  • 1
    note in general \foo{} is better than {\foo} unless you know \foo is not affected by being in a group. – David Carlisle Apr 30 '23 at 09:52
  • https://tex.stackexchange.com/a/86620/1090 – David Carlisle Apr 30 '23 at 09:53
  • @DavidCarlisle there should be definitely a way by making space an active character – antshar Apr 30 '23 at 10:51
  • oh but making space globally active without completely breaking everything is not at all trivial (several people have tried, I don't think anyone ever succeeded) – David Carlisle Apr 30 '23 at 10:54
  • you would have to make newline active as well so code "blank line ends paragraph" by hand, while making sure multiple consecutive spaces expand to a single space – David Carlisle Apr 30 '23 at 10:59

0 Answers0