6

I'd like to create a command (\newcommand) that does some decoration around a text (basically drawing a box around the text), for technical reports:

Eg like:

  Use \pdobj{frob}{noz zle} for achievers

renders like:

enter image description here

Now the argument text (in this case frob and noz zle) very commonly uses the tilde-symbol ~. Since tilde is obviously special, I currently have to use

And \pdobj{slurp$\sim$}{} they did.

to achieve:

enter image description here

Now, what I would really like to do is to simply write

And \pdobj{slurp~}{} they did.

for the same effect. As far as I understand this would require to re-define the ~ command, with something like

\def~{$\sim$}

While this seems to work, it also makes the traditional use of ~ as a non-breaking space (outside of my command) impossible.

However, the \url command somehow does the trick to render ~ as some special glyphs (e.g. \url{~T}) without affecting the NBSP-use outside its scope.

I had a looked at url.sty, but frankly, this is way beyond my TeX-foo.

So, is there are (simple/easy/straightforward) way to make ~ do something completely different when encountered inside an argument to my command vs outside?

2
  • \catcode`~=12 will then allow you to \def~{$\sim$}. Note that it will render other uses of ~ defunct, unless you limit its scope. The key, however, is to redefine the catcode before you absorb the argument containing the tilde. Commented May 31, 2017 at 15:33
  • @StevenB.Segletes you can't redefine ~ when it has catcode other. But I guess you actually know that :)
    – cgnieder
    Commented May 31, 2017 at 15:52

1 Answer 1

9

Just redefine ~ in a group

\newcommand{\pdobj}[2]{%
  \begingroup\def~{$\sim$}%
  <the other code for boxing the material
  \endgroup
}

With the current definition of \pdobj, the code can be better optimized.

1
  • perfect. so simple...
    – umläute
    Commented May 31, 2017 at 18:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .