5

How can I have images replaced with a placeholder, without the filepath appearing, but with a custom text (such as "Image removed for copyright reasons")? I have seen this answer (placeholder without path) but would like to apply it to the whole document without the key, and have been struggling to modify it to my needs. I want to keep the pagination the same in the very large document I'm applying these changes to. Equally, this document is broken up into many different subfiles so ideally the solution will not involve altering every single instance of an image being used.

\documentclass{article}
\usepackage[draft]{graphicx}
\begin{document}
\includegraphics[width=\textwidth]{mypicture}
\end{document}

2 Answers 2

4

You can patch the command to replace the file name by some other text

\documentclass{article}
\usepackage[draft]{graphicx}
\usepackage{xpatch}
\makeatletter
\xpatchcmd\Gin@setfile{\expandafter\strip@prefix\meaning\@tempa}{Image removed}{}{}
\makeatother
\begin{document}
\includegraphics[width=\textwidth]{example-image}
\end{document}

enter image description here

Edit

To center the text you can use

\makeatletter
\xpatchcmd\Gin@setfile{\expandafter\strip@prefix\meaning\@tempa}{\makebox[\Gin@req@width]{Image removed}}{}{}
\makeatother
8
  • This works perfectly; many thanks. How would I center such text?
    – alice19
    Commented May 22, 2016 at 18:10
  • @AndrewCashner like this: \xpatchcmd\Gin@setfile{\expandafter\strip@prefix\meaning\@tempa}{\hfil Image removed}{}{} If so, I don't seem to be able to get that to work
    – alice19
    Commented May 22, 2016 at 20:26
  • @marcellinus12: I added the code to center the text. Commented May 22, 2016 at 21:02
  • @UlrikeFischer Many thanks! This works beautifully.
    – alice19
    Commented May 22, 2016 at 21:15
  • sorry but I wonder if you could let me know how I could break the text onto a new line? The who line overfills some of my images but if I try \\ or \newline etc it doesn't seem to work
    – alice19
    Commented May 23, 2016 at 7:59
3

You can wrap the include graphics command in your own command, which includes a conditional to test whether to display graphics or an alternate text.

It would be better to use the same conditional that graphicx uses in draft mode, but just to demonstrate I've used a simple \newif command.

Xparse allows you to define a command with two optional arguments with default values using O{}. So in the command below the first optional argument specifies the size parameters passed to \includegraphics, otherwise a default value is used. The mandatory argument is the filename. The last optional argument is the alternate text, which again uses a default if you don't specify anything.

\documentclass{article}
\usepackage[draft]{graphicx}

\usepackage{xparse}

\newif\ifimages
\imagesfalse

\NewDocumentCommand{\maybeincludegraphics}
  { O{width=\textwidth} m O{Image removed for copyright reasons} }{%
  \ifimages
     \includegraphics[#1]{#2}%
  \else
      \fbox{\emph{#3}}%
  \fi
}

\begin{document}

\maybeincludegraphics{image1}
\maybeincludegraphics[width=3in]{image2}

\maybeincludegraphics{image3}[You can't handle this image]

\end{document}

(Warning: I haven't been able to test this where I am.)

4
  • This looks good, but i'd have to go through every image and replace the \includegraphics with the new command; is there any way to avoid this?
    – alice19
    Commented May 22, 2016 at 14:51
  • @marcellinus12 Doesn't your editor have find and replace?
    – cfr
    Commented May 22, 2016 at 15:00
  • This will change the page breaking etc., though, won't it? That is, there's no attempt to match the dimensions, even if they are specified in the options. Of course, this may not matter for the OP - the question doesn't make this clear.
    – cfr
    Commented May 22, 2016 at 15:02
  • @cfr Apologies, I should have made that clear. Please see my edits for clarity.
    – alice19
    Commented May 22, 2016 at 15:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .