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?

  • \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 :) – clemens May 31 '17 at 15:52
up vote 7 down vote accepted

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.

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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