7

I'd like to find a way to define a command which would work like this

\mycommand{This +is+ a +simple example+.}

and give

This is a simple example.

i.e a command which looks for words surrounded by a particular symbol (here a +) and changes the formatting (here in bold) of these words.

Also, is there any references about "advanced" macros in TeX?

7

This is easy:

\def\mycommand{\begingroup\setupplus\mycommandaux}
\def\setupplus{%
  \catcode`+=\active
  \begingroup\lccode`~=`+\lowercase{\endgroup\def~##1~}{\textbf{##1}}%
}
\newcommand\mycommandaux[1]{#1\endgroup}

You open a group and in this group you set up + to be active; its action is to gather everything up to the next + and to print it in boldface. The important thing is to gather the argument after the setup has been performed. So the action is split into two and \mycommandaux gathers the argument, so that TeX can process it, and closes the group started by \mycommand.

Don't use such shortcuts; they'll bite you, some day or the other.

Caution: this \mycommand cannot be used in the argument of another command. If you need it there, then a slightly more complicated strategy is needed:

\protected\def\mycommand{\begingroup\setupplus\mycommandaux}
\def\setupplus{%
  \catcode`+=\active
  \begingroup\lccode`~=`+\lowercase{\endgroup\def~##1~}{\textbf{##1}}%
}
\newcommand\mycommandaux[1]{\scantokens{#1\noexpand}\endgroup}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.