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


%            
\documentclass[letterpaper,twoside]{article}

\usepackage{graphicx}
\graphicspath{{./}{./photos}}

%
% Here is the macro I want to fix.
\newcommand{\pfile}[1]{%
%\catcode`_=12 % --does not work inside macro
Here is the example for file \texttt{#1}.
\newline
\includegraphics[height=0.40in,width=0.55in,keepaspectratio=true]{./photos/t-#1}
\newline
}%

% These variables are generated programmatically...
\def\photoSerialNumberBase{00}
\def\photoSerialNumberFirstPhotoName{\photoSerialNumberBase{}01}
\def\photoSerialNumberSecondPhotoName{\photoSerialNumberBase{}02}

\begin{document}

\title{Problem Example: Variables and Filenames}
\author{Kevin Cundiff}
\date{\today}
\maketitle
\pagenumbering{arabic}

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

\pfile{\photoSerialNumberFirstPhotoName_FirstPhotoName.jpg}


\pfile{\photoSerialNumberSecondPhotoName_SecondPhotoName.jpg}


\end{document}
share|improve this question
3  
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

1 Answer 1

If you went

\def\photoSerialNumberFirstPhotoName{\photoSerialNumberBase 01} 

there would be no {} to remove.

share|improve this answer

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.