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}
alice19
  • 746

2 Answers2

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
Ulrike Fischer
  • 327,261
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.)

musarithmia
  • 12,463
  • 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 May 22 '16 at 14:51
  • @marcellinus12 Doesn't your editor have find and replace? – cfr May 22 '16 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 May 22 '16 at 15:02
  • @cfr Apologies, I should have made that clear. Please see my edits for clarity. – alice19 May 22 '16 at 15:11