我正在尝试使用变量将文件名传递给\includegraphics
。我遇到了麻烦,因为我的文件名是由其他变量构成的。这在文件名中留下了一个空的花括号。文件名在文档中正确打印,但括号导致 中出现“文件未找到”错误\includegraphics
。
(我正在做一个包含很多照片的家谱项目。名字是用脚本生成的。)
在将变量传递给之前,如何让 LaTeX 从变量中去除空花括号\includegraphics
?
非常感谢,凯文
示例代码: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}
答案1
如果你去了
\def\photoSerialNumberFirstPhotoName{\photoSerialNumberBase 01}
就没有什么可{}
删除的了。