我正在查看/usr/local/texlive/2015/texmf-dist/source/latex/graphics/graphicx.dtx
并试图弄清楚哪个宏实际上排版了图像文件。
例如,在用户级别,我们输入
\usepackage{graphicx} % Load the graphicx.dtx file within preamble
\includegraphics{file} % Load the image file within body
但\includegraphics
确实有一些作用。我认为
- 将文件名与文件扩展名分开
- 迭代所有可接受的文件扩展名,这样您就不必明确输入它
- 输入错误时产生错误
- ETC。?
在的宏定义中\Ginclude@graphics
,我发现\Gin@setfile
。这种类型放文件?什么机制决定了草稿模式与非草稿模式?
原因是我想重新定义或修补它以包含位于图像下方中央的文件名。
\Gin@setfile
截至 2015 年 5 月 4 日
从/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/graphics.sty
\def\Gin@setfile#1#2#3{%
\ifx\\#2\\\Gread@false\fi
\ifGin@bbox\else
\ifGread@
\csname Gread@%
\expandafter\ifx\csname Gread@#1\endcsname\relax
eps%
\else
#1%
\fi
\endcsname{\Gin@base#2}%
\else
\Gin@nosize{#3}%
\fi
\fi
\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@#1\endcsname\relax
\Gin@drafttrue
\expandafter\ifx\csname Gread@#1\endcsname\relax
\@latex@error{Can not include graphics of type: #1}\@ehc
\global\expandafter\let\csname Gread@#1\endcsname\@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{#3}%
\rlap{ \ttfamily\expandafter\strip@prefix\meaning\@tempa}%
\vss
\hrule}%
\hss\vrule}%
\else
\@addtofilelist{#3}%
\ProvidesFile{#3}[Graphic file (type #1)]%
\setbox\z@\hbox{\csname Ginclude@#1\endcsname{#3}}%
\dp\z@\z@
\ht\z@\Gin@req@height
\wd\z@\Gin@req@width
\box\z@
\fi}
答案1
您引用的代码的主要部分是
\setbox\z@\hbox{\csname Ginclude@#1\endcsname{#3}}%
调用文件类型(根据扩展名确定或作为键传递给)的\Ginclude@xxx
地方,因此如果是,则将调用,如果指定的驱动程序(在选项或设置中)是,则将被加载,它通过以下方式定义 EPS 包含xxx
\includegraphics
xxx
eps
\Ginclude@eps
graphics.cfg
dvips
dvips.def
\def\Ginclude@eps#1{%
\message{<#1>}%
\bgroup
\def\@tempa{!}%
\dimen@\Gin@req@width
\[email protected]%
\divide\dimen@\dimen@ii
\@tempdima\Gin@req@height
\divide\@tempdima\dimen@ii
\special{PSfile="#1"\space
llx=\Gin@llx\space
lly=\Gin@lly\space
urx=\Gin@urx\space
ury=\Gin@ury\space
\ifx\Gin@scalex\@tempa\else rwi=\number\dimen@\space\fi
\ifx\Gin@scaley\@tempa\else rhi=\number\@tempdima\space\fi
\ifGin@clip clip\fi}%
\egroup}
答案2
以下是使用修补和重新定义的 的实现\includegraphics
。目前,文件名的宽度没有考虑在内,但可以通过在参数列表中将T
参数更改为来实现。文件名的下划线间隙目前为 3pt。请注意,无论在调用参数中是否指定了扩展名,文件名都会正确生成。F
\stackengine
已编辑以处理文件名中的特殊字符,例如下划线。
\documentclass{article}
%\usepackage[T1]{fontenc}% TO PROPERLY FORMAT UNDERSCORES IN FILENAMES
\usepackage{stackengine,graphicx,xpatch}
\makeatletter
\apptocmd{\Gin@setfile}{\xdef\file@root{#3}}{}{}
\let\svincludegraphics\includegraphics
\renewcommand\includegraphics[2][]{%
\savestack\tmp{\svincludegraphics[#1]{#2}}%
\stackengine{3pt}{\tmp}{\detokenize\expandafter{\file@root}}%
{U}{c}{F}{T}{S}% <- CHANGE T TO F TO ACCOUNT FOR FILENAME WIDTH
}
\makeatother
\begin{document}
Does it work? \includegraphics[width=.7in]{example-image-a}
Here is another test \includegraphics[width=2.7in]{example-image-c.jpg}
\end{document}