I want to escape TeX codes on a range of text; this is for a macro providing slightly neater URL formatting. The code I have now is,


where the first arg is the protocol (http) and second is rest of the link. While the link is generated correctly for the PDF, the tilde characters are not escaped (they turn into whitespace), making the print version incorrect. I want something like \verb without the formatting.

5 Answers 5


While, like Joseph, I would ordinarily choose the more straightforward path that \url provides, I can see that that approach doesn't solve your stated problem. In which case, should you really want to to typeset your URLs the way that you proposed, this code should get you there:



% -OR- LOW TILDE (faked low tilde - code lifted from url.sty)
%  \makeatletter
%  \StrSubstitute{#1}{~}{{\raise.45ex\hbox{\m@th$\scriptstyle\sim$}}}%
%  \makeatother




\safetilde locally sets tildes to normal (letter) category code before converting any tildes in its argument into \~{}'s using \StrSubstitute from the xstring package. Since these characters tend to be set a little high from the baseline, we lower them a small negative \raise amount. Alternatively comment the uncommented \StrSubstitute line and uncommented the following 4 commented lines. This code typesets any tildes using math \sim symbols instead (this code lifted from url.sty).


I'd simply use the mechanism from the url package:

  • I wonder why there isn't a line that says \usepackage{url} or something like that. Is it loaded with hyperref?
    – Kit
    Nov 21, 2010 at 9:52
  • @Kit: yes, hyperref loads url via \RequirePackage{url}. Nov 21, 2010 at 10:30
  • I want the http/https gone. Thanks anyway though. Nov 21, 2010 at 22:58

Slightly more generally than Joseph's solution, you can use \detokenize to (mostly) achieve the effect you're after in other situations. (As Joseph mentions the url package is better for your specific case because it deals with linebreaks sensibly.)



You still need to watch out for % and extra spaces added after control sequences, so it's not a perfect solution. (At least it's short, though.)


Using the url package like Joseph suggested is still the easiest way, even if you want to get rid of the http. The following definition of \myurl provides an optional argument for it using http as default:


  \begingroup% just to keep sffamily local



enter image description here


I used Geoffrey's answer to create my own version, which seems to both maintain the semantics of not including the "http" text, converts tildes correctly, and adds some spaces around "/" characters so they can break lines. You can add two more \! commands if you want the spacing unchanged, but I think it looks nice (for reasonable URLs).

    \expandarg% expand the first argument once
    \StrSubstitute{\detokenize{#1}}{/}{\! /\! }%


The catcode stuff didn't work in XeTeX, so it's noexpandarg now.

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.