TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am generating LaTeX code in an automated way and have run into a problem I'm not quite sure how to solve. The problem occurs when I am generating a clickable contents page using hyperlinks. File names must be given as clickable text in the contents, in the following manner:


The difficulty arises in coping with the special characters that can arise in filenames (and I do not have some defined filename format). I know that I could manually escape the underscore, but this isn't an option here because this code is to be generated automatically for many files.

My first attempt involved using verbatim in conjunction with cprotect, however I couldn't get this to work. My current attempt involves the use of url, in the following manner:


However, this is inelegant. Would anyone have some inventive solutions to this or some ideas I might try?

Many preemptive thanks for any assistance

share|improve this question
up vote 3 down vote accepted


\usepackage[T1]{fontenc}% http://ctan.org/pkg/fontenc
\usepackage{hyperref}% http://ctan.org/pkg/hyperref

The T1 font encoding allows for proper printing of the underscore _, while \detokenize removes the meaning of _ which denotes/switches to subscript notation.

The solution is similar to Escaping from the escaping hell.

share|improve this answer
Ah, this seems to work well. I thank you sincerely for your assistance! – d3pd Aug 2 '12 at 12:02

Your Answer


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.