TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
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
1  
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}

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

share|improve this answer
    
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
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 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.

share|improve this answer
    
Wouldn't it be more helpful to leave \detokenize out? If there's any macro it would expand. – Manuel Feb 15 at 8:34
1  
@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?

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}
share|improve this answer
    
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 at 0: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 at 4:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.