Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 7 down vote accepted

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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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