当文件没有扩展名时,如何避免指定所包含图形的大小?

当文件没有扩展名时,如何避免指定所包含图形的大小?

我正在使用 xelatex 渲染以下代码:

\documentclass{article}
\usepackage[debugshow]{graphicx}
\begin{document}
%% This gives ! LaTeX Error: Cannot determine size of graphic in lowres (no size specifed).
\includegraphics[type=png]{lowres}
%% This works fine without specified size.
\includegraphics{highres.png}
\end{document}

我当前目录中有文件“lowres”和“highres.png”:

$ file lowres 
lowres: PNG image data, 348 x 350, 8-bit colormap, non-interlaced
$ file highres.png 
highres.png: PNG image data, 348 x 350, 8-bit colormap, non-interlaced

这些文件之间的明显区别是缺少“lowres”的扩展名,所以这就是我在“includegraphics”中指定类型的原因。

问题是,当文件没有任何扩展名时,似乎必须指定大小(尽管我们将类型作为参数)。

这种行为对我来说毫无意义,有什么办法可以解决它吗?

答案1

如果您未指定扩展名作为文件名的一部分,\includegraphics则将使用一组预定义的扩展名来搜索合适的文件。如果您仅使用,lowres则它会查找文件lowres.pnglowres.jpglowres.pdf(不必按此顺序),并使用找到的第一个文件。它不会查找lowres没有任何扩展名的文件。据我type所知,该选项已被弃用,可能无法达到您的预期。

最好的办法是直接重命名相关文件,使其具有正确的文件扩展名。您还可以定义一个空扩展名作为默认扩展名列表的一部分,然后删除该type选项。

以下对我有用。显然 XeTeX 足够聪明,无需扩展即可找出真实类型,这实际上并不难。

\documentclass{article}
\usepackage[debugshow]{graphicx}
\DeclareGraphicsExtensions{.pdf,.png,.jpg,}% Note the trailing ',' which defines an empty extension
\begin{document}
\includegraphics{lowres}
\includegraphics{highres.png}
\end{document}

相关内容