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


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?

  • \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. – Steven B. Segletes May 31 '17 at 15:33
  • @StevenB.Segletes you can't redefine ~ when it has catcode other. But I guess you actually know that :) – cgnieder May 31 '17 at 15:52

Just redefine ~ in a group

  <the other code for boxing the material

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

  • perfect. so simple... – umläute May 31 '17 at 18:03

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.