Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

\hyperlink{my_file.dat}{my_file.dat}\\

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:

\hyperlink{my_file.dat}{\url{my_file.dat}}\\

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
add comment

1 Answer

up vote 3 down vote accepted

Use

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

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
add comment

Your Answer

 
discard

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.