Change syntax of macro, to go inside braces

If you want to input, for instance

{\macro argument or whatever}

and want TeX to convert that to

\command{argument or whatever}

to get all the flexibility of an easily defined command with one argument, you can do

\def\macro{\aftergroup\command\aftergroup{}}
\def\command#1{Whatever you whant to do with the argument [#1].}

What does this do?

Your code {\macro argument} will open a group { and then expand \macro, which expands to \aftergroup\command\aftergroup{}. In turn, \aftergroup makes sure the next token is inserted exactly after closing the group, so \aftergroup\command ensures that when you close the group \command is there, and \aftergroup{ ensures that \command{ is there when you close a group. Then, you close the group with the following } leaving your code with \command{argument}.


The real solution depends on how \commandwitharg is defined. A general solution allowing

 <open group>\noarg text<close group>

is really very difficult to find, if at all possible.

Here's an alternative solution to Manuel's, which is slicker:

\documentclass{article}

\newcommand\commandwitharg[1]{---#1---}

\newcommand{\noarg}{%
  % close the group
  \egroup                  
  % the first \expandafter removes \iftrue
  % the second \expandafter removes \else
  \expandafter\expandafter\expandafter
  \commandwitharg\iftrue\expandafter{\else}\fi 
}

\begin{document}

\commandwitharg{ABC}

{\noarg ABC}

\end{document}

Don't hope to be able to have \begingroup\noarg text\endgroup: only {\noarg text} will work.

It is much better to use a text editor and simply do the change

{\noarg<space>

to

\commandwitharg{

Finally, knowing the real definition of \commandwitharg and its use cases, can lead to a better solution.

Tags:

Macros