我想打印图形文件的真实文件名,例如 example.jpg,接近图形在 pdf 文件中实际出现的位置?
答案1
您可以为此定义一个命令,例如
\newcommand\Includegraphics[2][scale=1]{\includegraphics[#1]{#2}\par\nobreak\texttt{#2}}
在图片下方排版文件名:
\documentclass{article}
\usepackage{graphicx}
\newcommand\Includegraphics[2][scale=1]{\includegraphics[#1]{#2}\par\nobreak\texttt{#2}}
\begin{document}
\noindent
\Includegraphics[width=4cm]{ctanlion.png}
\noindent
\Includegraphics[width=4cm]{mushrooms.jpg}
\end{document}
或者,使用\marginnote
将名称排版为边注:
\documentclass{article}
\usepackage{graphicx}
\usepackage{marginnote}
\usepackage{adjustbox}
\newcommand\Includegraphics[2][]{%
\marginnote{\fbox{\texttt{#2}}}[-\baselineskip]\adjustbox{valign=c}{\includegraphics[#1]{#2}}}
\begin{document}
\noindent
\Includegraphics[width=4cm]{ctanlion.png}
\noindent
\Includegraphics[width=4cm]{mushrooms.jpg}
\end{document}
CTAN 狮子绘画由 Duane Bibby 绘制。
答案2
我的一个朋友想出了另一个类似的解决方案
%%for the preamble
\newcommand{\saveinclude}{}
\let\saveinclude\includegraphics
\newcommand{\filename}[1]{\tiny\ttfamily{\detokenize{#1}}}
\renewcommand{\includegraphics}[2][]{\saveinclude[#1]{#2}\marginnote{\filename{#2}}}
%%in the text just use \includegraphics[]{}
答案3
我们重新定义\includegraphics
输出所包含的图形文件的名称。
请注意,代码也适用于给定的文件名没有扩展名。将使用与包所使用的相同的文件扩展名列表graphicx
来搜索匹配的文件:
\documentclass{article}
\usepackage{graphicx}
\makeatletter
\def\getfilename#1{%
\begingroup%
\IfFileExists{#1}{\texttt{#1}}{%
\let\firstmatch\undefined%
\filename@parse{#1}\edef\curbase{\filename@area\filename@base}%
\@for\tempa:=\Gin@extensions\do{%
\edef\curfile{{\curbase\tempa}}%
\ifdefined\firstmatch\else%
\expandafter\IfFileExists\curfile{\expandafter\texttt\curfile\def\firstmatch{}}{}%
\fi%
}%
}%
\endgroup%
}
\makeatletter
\let\igraphicsorig\includegraphics
\renewcommand\includegraphics[2][]{\vtop{%
\hbox{\igraphicsorig[#1]{#2}}%
\hbox{\getfilename{#2}}}%
}
\begin{document}
\noindent%
%includes ctanlion.png, if found first
\verb+\includegraphics[width=4cm]{ctanlion}+:\\
\includegraphics[width=4cm]{ctanlion}
\vspace{2cm}
\noindent%
\verb+\includegraphics[width=4cm]{ctanlion.eps}+:\\
\includegraphics[width=4cm]{ctanlion.eps}
\end{document}