7

The command \displaygraphics[width-1in]{figure=some_fig.pdf} will display a graphic. Sometimes I want to typeset when I do not have the graphics files available, so I want to do something like:

\renewcommand{\includegraphics}[1][]{\verb|#2|}

The intention is that it will simply display the name of the file to be used, rather than try to display the graphic. Note graphics names have underscores, which is the reason I would want something like verbatim. So the command \renewcommand{\includegraphics}[1][]{#2} does not work.

Of course the solution above does not work either, since \verb cannot be used like this inside a \renewcommand. How do I do this?

Alan Munn
  • 218,180

3 Answers3

6

The reason your redefinition doesn't work is that you are specifying the wrong number of arguments in the \renewcommand. If I understand your question correctly you just want filename placeholders for your images. Here's an example:

\documentclass{article}
\usepackage[]{graphicx}
\renewcommand\includegraphics[2][]{\begin{center}\texttt{\detokenize{#2}\end{center}}
\begin{document}
\begin{figure}
\includegraphics[width=\linewidth]{Path/to/your/image/file}
\caption{When I finally get the file this will be a cool image}
\end{figure}
\end{document}

output of code

Alternatively, you can use the [demo] option of graphicx which will replace any \includegraphics command with a black box as below. In this case, of course, you don't get the filenames printed out, but you also don't need to redefine \includegraphics.

output of demo

Alan Munn
  • 218,180
  • I've seen a draft document that doesn't add the black box but the name of the file; I don't remember how that was done, though. – Manuel Feb 15 '16 at 00:21
  • 1
    @Manuel draft mode only prints the name of the file if it exists, otherwise it throws an error. demo mode ignores files altogether. – Alan Munn Feb 15 '16 at 00:24
4

There are a number of errors in your code:

\renewcommand{displaygraphics}[1][]{\verb|#2|}
  1. The first argument to \renewcommand must be a macro name (with the backslash (fixed in the question).

  2. The command to redefine is \includegraphics (fixed in the question)

  3. The number of arguments should be 2, not 1: when another pair of brackets follows, the contents is the default value of the optional argument, which is denoted by #1 in the replacement text; you also want a mandatory argument, which makes two.

  4. \verb cannot be used in the argument to another macro.

Solution:

\renewcommand{\includegraphics}[2][]{\texttt{\detokenize{#2}}}

This will also allow underscores.

egreg
  • 1,121,712
3

What about first check \IfFileExists and if already there, simply \includegraphics or otherwise, for underscores and long paths as well, in teletype style, better use an \url?

mwe

\documentclass[twocolumn]{article}
\usepackage{url}
\usepackage{graphicx}
\parskip1em

\newcommand\extimg[1]{
\IfFileExists{#1}
{\includegraphics[width=.5\linewidth]{#1}} 
{\fboxsep1em\fbox{\parbox{\dimexpr.5\linewidth-2em}{\url{#1}}}}}

\begin{document}

Some already made image:

\extimg{/usr/local/texlive/2015/texmf-dist/tex/latex/mwe/example-image-a.jpg}

Some image to do:

\extimg{/home/Richard/my_funny/proyect/more_carpets/and_more/images/todo_tomorrow/my_image.jpg}

\end{document}
Fran
  • 80,769
  • Thanks for all the suggestions. detokenize was the thing that I was missing. All the other suggestions are very useful as well. – Richard Hartley Feb 16 '16 at 00:57
  • @RichardHartley You are welcome. Then, please accept the best detokenizing answer in your opinion (i.e., click in the gray checkmark under the votes of that answer). But note that these only fix your original approach that do not allow check what images are missing, and you need re-edit the source to remove your macro after providing the images. Then, if you do not already have all the images, you will get a "nice" error message in the log instead of a preliminary draft. – Fran Feb 16 '16 at 04:56