Sign up ×
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.

Possible Duplicate:
Escaping from the escaping hell

I did scour the internet for solution but I found no satisfactory answer for this problem.

I have a bash script that searches my folder for .pdf images and creates a latex file with several includegraphics commands with the file names as the figure caption.

However, my file names have underscores and this is unavoidable. When I run pdflatex on the below latex file, I do get a pdf file with all my figures as required but the captions are all italicized or subscripted.

Any ideas on how I might avoid this (how should I allow underscores in figure captions?):

Latex file:


% \usepackage[dvips]{hyperref}






Using the underscore package helped only partially:

  • The caption is fine now and the file name is displayed.

  • Since the file name has underscore, that messed up the path and hence the figures aren't displayed anymore.

share|improve this question

marked as duplicate by David Carlisle, Marco Daniel, percusse, Claudio Fiandrino, Paul Gaborit Sep 15 '12 at 21:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

@GonzaloMedina Right, but the underscore package only "_ in text mode (i.e., \textunderscore) prints an underscore"... – drN Jul 31 '12 at 18:31
@GonzaloMedina I loaded the underscore package. No change in the situaiton. In fact it is worse as the figures are no longer displayed in the eventual pdf file. – drN Jul 31 '12 at 18:33
@DavidCarlisle Not really. Not a duplicate. – drN Jul 31 '12 at 18:34
@DNA Ah, I didn't saw the path also contained underscores; I'll delete my comments. – Gonzalo Medina Jul 31 '12 at 18:35
@GonzaloMedina Your comment was useful! :) however, like you noticed , the path is screwed up now! :P – drN Jul 31 '12 at 18:37

1 Answer 1

up vote 6 down vote accepted

Using \detokenize{L_2lambda_max_1wl_zg_2m.pdf} removes the math interpretation of _ and adding \usepackage[T1]{fontenc} allows for the correct printing of _ inside the caption. For completeness, the following setup should work:


  \caption{\detokenize{L_2lambda_max_1wl_zg_2m.pdf} }

For preserving the underscores within the LoF, use \caption{\protect\detokenize{..}}.

share|improve this answer
\detokenize{} works quite well, but it stops if I want to generate the listoffigures. How could I get around it? Right now, the work around is to escape each _ (\_). – Albert Netymk Jun 9 '13 at 21:36
@AlbertNetymk: Use \protect\detokenize{..}. It prohibits expansion of \detokenize which happens typically within moving arguments as they are written to (auxiliary) files. – Werner Jun 12 '13 at 14:15
Thanks for the tip. \protect does the trick and it compiles fine. Unfortunately, in listoffigues, the underscore is missing. In other words, \protect\detokinize{first_name} becomes first name (Not exactly one ` `, but you could try and see what it is really. – Albert Netymk Jun 20 '13 at 14:55
@AlbertNetymk: I am not getting that output at all (see the output using a very minimal document). Underscores are preserved in the LoF. Perhaps, if this is a pressing concern, ask a follow-up question. – Werner Jun 20 '13 at 16:25

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