# File name with underscore as figure caption [duplicate]

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:

\documentclass[a4paper,10pt]{article}
\usepackage{setspace}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{eps2pdf}
\usepackage{subfigure}
\usepackage{float}
\usepackage[super,numbers,sort&compress]{natbib}
\usepackage{color}
\usepackage{grrfile}
\newcommand{\hilight}[1]{\colorbox{yellow}{#1}}
\renewcommand{\thefootnote}{\alph{footnote}}

% \usepackage[dvips]{hyperref}

\date{07/31/12}

\begin{document}

\clearpage

\begin{figure}[h]
\begin{center}
\centering
\includegraphics[scale=0.75]{/home/dnaneet/Research/Dissertation/APS2012/G=0.0/L_2lambda_max_1wl_zg_2m.pdf}
\caption{L_2lambda_max_1wl_zg_2m.pdf}
\label{fig:fig1}
\end{center}
\end{figure}
\end{document}


Edit:

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.

-

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

@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

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:

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

\begin{figure}[h]
\centering
\includegraphics[scale=0.75]{/home/dnaneet/Research/Dissertation/APS2012/G=0.0/L_2lambda_max_1wl_zg_2m.pdf}
\caption{\detokenize{L_2lambda_max_1wl_zg_2m.pdf} }
\label{fig:fig1}
\end{figure}
\end{document}


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

-
\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