当我想包含没有扩展名的图像时,如何使用 includegraphics?
当我尝试type=jpg
手动设置时出现以下错误:
LaTeX Error: Cannot determine size of graphic in images/posters/sample (no size specified)
如果我将其更改sample
为sample.jpg
全部有效,但我有一堆这些图像,我无法更改其后缀(另一个应用程序依赖于它)。
有什么建议吗?
答案1
我想不出一个简单的解决方案,但这里有一个可能的解决方案,可以适应所使用的内部命令\includegraphics
。
\documentclass{article}
\usepackage{graphicx}
\makeatletter
\newcommand{\jpg@Ginclude@graphics}[1]{%
\begingroup
\let\input@path\Ginput@path
\filename@parse{#1}%
\def\Gin@ext{.jpg}%
\def\Gin@base{\filename@area\filename@base}%
\Gread@jpg{\Gin@base}%
\Gin@viewport@code\Gin@nat@height\Gin@ury bp%
\advance\Gin@nat@height-\Gin@lly bp%
\Gin@nat@width \Gin@urx bp%
\advance\Gin@nat@width-\Gin@llx bp%
\Gin@req@sizes
\expandafter\ifx\csname Ginclude@jpg\endcsname\relax
\Gin@drafttrue
\expandafter\ifx\csname Gread@jpg\endcsname\relax
\@latex@error {Can not include graphics of type: jpg}\@ehc
\global\let\Gread@jpg\@empty
\fi
\fi
\leavevmode
\ifGin@draft
\hb@xt@
\Gin@req@width{\vrule \hss \vbox to \Gin@req@height{\hrule \@width
\Gin@req@width \vss
\edef\@tempa{\Gin@base}%
\rlap{\ttfamily\expandafter\strip@prefix\meaning\@tempa}%
\vss\hrule}\hss\vrule
}%
\else
\@addtofilelist{\Gin@base}%
\ProvidesFile{\Gin@base}[Graphic file (type jpg)]%
\setbox\z@\hbox{\Ginclude@jpg{\Gin@base}}%
\dp\z@\z@\ht\z@\Gin@req@height\wd\z@\Gin@req@width\box\z@
\fi
\endgroup
}%
\newcommand{\includejpgnoext}[2][]{%
\begingroup
\let\Ginclude@graphics\jpg@Ginclude@graphics
\includegraphics[#1]{#2}%
\endgroup
}
\makeatother
\begin{document}
\includejpgnoext{sample} % jpg image file called 'sample' with no extension
\end{document}
它仅适用于 PDFLaTeX。