# How to use \verb or something similar inside a \newcommand

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?

-
Hi and welcome to TeX.SX. You can format code inline by enclosing it in back-ticks, or by selecting it and clicking on the {} icon. Larger chunks of code can be similarly selected and will be marked up with a preceding 4 spaces. (BTW, do you mean \includegraphics? – Alan Munn Feb 14 at 23:05
Apart from the macro name and the error in the number of arguments, \verb cannot be used in the argument to another macro, even \newcommand. – egreg Feb 15 at 0:44

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}


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.

-
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 at 0:21
@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 at 0:24

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.

-
Wouldn't it be more helpful to leave \detokenize out? If there's any macro it would expand. – Manuel Feb 15 at 8:34
@Manuel The OP says that underscores are expected. – egreg Feb 15 at 8:42

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?

\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}