my wrappers around “includegraphics”

To put an image to a document, LaTeX provides the command “\includegraphics“. As it often happens, due to LaTeX was designed for manual typesetting, this command is a nightmare for automatic generation. For a long time, I use a wrapper to solve 99% of the problems.

I.

\RequirePackage{grffile}

Without this package, code

\includegraphics{ANSIboat.v1.pdf}
raises the dummy error:
! LaTeX Error: Unknown graphics extension: .boat.v1.pdf.

II.

\def\includegraphicsII[#1]#2{%
\IfFileExists{#2}{\includegraphics[#1]{#2}}{%
\typeout{! LaTeX warning: File `#2' not found.}%
\includegraphics[#1]{dummy.pdf}}}

If there is no image file, LaTeX

* annonces this problem to the console and the log file,
* uses the image “dummy.pdf” instead

III.

\catcode`\_=11
\edef\ImageUnderscore{_}
\catcode`\_=8
\def\Image[#1]#2{{%
\let\_=\ImageUnderscore
\includegraphicsII[#1]{#2}}}

This allows to specify a file name with the underscore symbol using the standard LaTeX escaping rules:

\Image{ANSI\_boat.pdf}

instead of

\includegraphicsII{ANSI_boat.pdf}

Benefit: no need for special escaping rules in an automatic XML to LaTeX converter.

Actually, the code must be extended, to include “\{”, “\textless” and other special symbols, but it’s TODO.

Leave a Reply