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'm trying to use a variable to pass a filename to \includegraphics. I'm having trouble because my filename is built up from other variables. This leaves an empty curly-brace inside the filename. The filename correctly prints in the document, but the braces cause a "file not found" error in \includegraphics.

(I'm working on a genealogy project with many photographs. The names are generated with a script.)

How can I get LaTeX to strip empty curly-braces from a variable before passing it to \includegraphics?

Many thanks, Kevin

Example Code: filevar.tex

% ==============================================================
% Example to show problem with building filenames from variables
% Current directory contains:
%    + This LaTeX source file (filevar.tex)
%    + A directory named "photos"
%    + JPEG photo photos/t-0001_FirstPhotoName.jpg
%    + JPEG photo photos/t-0002_SecondPhotoName.jpg
% This file is processed with the command
%    pdflatex -interaction=batchmode filevar.tex
%    The error messages are:
%    LaTeX Warning: File `./photos/t-00{}01_FirstPhotoName.jpg' not found on input 
%    LaTeX Warning: File `./photos/t-00{}02_SecondPhotoName.jpg' not found on input 



% Here is the macro I want to fix.
%\catcode`_=12 % --does not work inside macro
Here is the example for file \texttt{#1}.

% These variables are generated programmatically...


\title{Problem Example: Variables and Filenames}
\author{Kevin Cundiff}

\section*{Rubber Meets the Road}
Lots of important and earth-shaking text goes here.

% This cryptic command disables the special meaning of '_'.
% This allows underscores to be used in a filename.
\catcode`_=12 % --does work inside environment!

Just below this paragraph, I would like to place the thumbnail for
several photos. The filename is generated using variables. This leaves
a set of empty curly-braces in the middle of the filename which prevent
the actual thumbnail from loading in my document.

How can I tell \LaTeX{} to strip empty curly-braces from the variable?
Many thanks!



share|improve this question
you could strip the braces but why have you put them in? If you went \def\photoSerialNumberFirstPhotoName{\photoSerialNumberBase 01} there would be no {} to remove. – David Carlisle Aug 6 '12 at 22:15
Thanks for your quick response. I thought there was a second problem with another macro (one I hadn't included in the example). But once I removed the {} then it worked like a charm. – Kevin Cundiff Aug 7 '12 at 2:05
OT: Rather than changing the global catcode of _ (which will cause troubles in the math environments), it is better to use the underscore package. – Aditya Aug 7 '12 at 3:03

If you went

\def\photoSerialNumberFirstPhotoName{\photoSerialNumberBase 01} 

there would be no {} to remove.

share|improve this answer

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.