2

Is there a simple way to set the color for a (blank) example-image?

The naive way doesn't work:

\begin{figure}
\color{red}
\includegraphics[width=\textwidth]{example-image}
\color{black}
\end{figure}

I certainly don't want to have to create and store lots of colored example images and insert each individually. It also makes it awkward being forced to store multiple copies for different document environments or futz with image file paths.

As background: I will use this for categorizing and prioritizing figures to be inserted later (e.g., red = landscape photo, green = portrait painting, blue = computer diagram, orange = figure to be computed, yellow = figure to be purchased....).

  • If the image is grayscale… No, you just insert the PDF. – egreg Dec 30 '20 at 23:39
  • Hah! As Henry Ford said to potential customers of his first Model-T: "You can get it in any color you like... so long as it is black." – David G. Stork Dec 30 '20 at 23:40
  • Duplicate? https://tex.stackexchange.com/q/29227/4427 – egreg Dec 30 '20 at 23:42
  • I adopted Ford's quote to warn our young basketball referees about their whistle: it can be any color you like, so long as it's black. And I point to the fact that most football (soccer) referees use a bright yellow one. – egreg Dec 30 '20 at 23:45
  • Your (potential) duplicate was useful, but not quite a true duplicate, I believe. First, I'm not asking to change the color of an arbitrary image (as the linked question addresses). Frankly, I was hoping there was a parameter on example-image (analogous to example-image-a, example-image-b, ...) that would support color changes. I suppose there's no way. But thanks for trying. – David G. Stork Dec 30 '20 at 23:49
  • Wouldn't a rectangle of the appropriate color do? – egreg Dec 30 '20 at 23:57
  • @egreg: Sort of... but I want to keep the colored placeholder within a traditional \includegraphics[]{} so I can easily replace example-image with (say) myPhoto.jpg. (I do all my figure drawing in Mathematica, never tikz, but what would you suggest I include in your proposal?) – David G. Stork Dec 31 '20 at 00:07
  • @DavidG.Stork: How about this as an option - thick border? – Werner Dec 31 '20 at 00:19
  • @Werner: Sure... that would be an acceptable compromise. But how do I implement that within \includegraphics[]{example-image} so I can then easily swap in or out a desired image, e.g., myPhoto.jpg? – David G. Stork Dec 31 '20 at 00:22
  • @DavidG.Stork: I used \fcolorbox{red}{red}{\includegraphics{<image>}}. The amount of red is set by the length \fboxsep + \fboxrule. – Werner Dec 31 '20 at 00:32
  • @Werner: Thanks, but frankly that may be just too awkward for broad use. After all, I have tabular environments for arraying multiple images, of different dimensions and such and surrounding an image will disrupt the layout, or require more work than it is worth. – David G. Stork Dec 31 '20 at 00:38

2 Answers2

3

This makes colored rectangles if the argument to \includegraphics is a declared color. In the part

\prop_gset_from_keyval:Nn \g_stork_ig_ratios_prop
 {
  red=3/4, green=16/9, blue=3/4, orange=3/4, yellow=3/4
 }

you set both the known color and the aspect ratio for the corresponding type of image (height/width).

Example of usage. The complicated code could well stay in a separate file that you \input and when all images are filled in you can remove the \input line.

\documentclass{article}
\usepackage{graphicx,xcolor}

% to be removed when the document has all the pictures \NewCommandCopy{\originalincludegraphics}{\includegraphics} \ExplSyntaxOn \RenewDocumentCommand{\includegraphics}{O{}m} { \stork_ig:nn { #1 } { #2 } }

\prg_generate_conditional_variant:Nnn \clist_if_in:Nn { Nx } { T,F,TF } \prop_new:N \g_stork_ig_ratios_prop \clist_new:N \g_stork_ig_colors_clist

\keys_define:nn { stork/ig } { width .dim_set:N = \l_stork_ig_wd_dim, width .initial:n = 8cm, unknown .code:n = {}, }

\cs_new_protected:Nn \stork_ig:nn { \group_begin: \clist_if_in:NxTF \g_stork_ig_colors_clist { \tl_to_str:n { #2 } } { \keys_set:nn { stork/ig } { #1 } \stork_ig_fake:n { #2 } } { \originalincludegraphics[#1]{#2} } \group_end: }

\cs_new_protected:Nn \stork_ig_fake:n { \textcolor{#1} { \rule {% width \l_stork_ig_wd_dim } {% height \dim_eval:n { \l_stork_ig_wd_dim * \prop_item:Nn \g_stork_ig_ratios_prop { #1 } } } } }

% set the known colors \prop_gset_from_keyval:Nn \g_stork_ig_ratios_prop { red=3/4, green=16/9, blue=3/4, orange=3/4, yellow=3/4 } \prop_map_inline:Nn \g_stork_ig_ratios_prop { \clist_gput_right:Nn \g_stork_ig_colors_clist { #1 } } \ExplSyntaxOff % end of temporary code

\begin{document}

\includegraphics[width=0.4\textwidth]{red} \includegraphics[width=0.2\textwidth]{green} \includegraphics[width=0.2\textwidth]{orange}

\includegraphics[width=0.4\textwidth]{example-image}

\end{document}

enter image description here

If you don't want fixed aspect ratios for the various colors, you can modify the code to calls such as

\includegraphics[width=0.3\textwidth]{red:3/4}

where the aspect ratio is always height/width, but a single integer is allowed (with /1 implied).

\documentclass{article}
\usepackage{graphicx,xcolor}

% to be removed when the document has all the pictures \NewCommandCopy{\originalincludegraphics}{\includegraphics} \ExplSyntaxOn \RenewDocumentCommand{\includegraphics}{O{}m} { \stork_ig:nn { #1 } { #2 } }

\prg_generate_conditional_variant:Nnn \clist_if_in:Nn { Nx } { T,F,TF } \cs_generate_variant:Nn \seq_set_split:Nnn { NV }

\seq_new:N \l_stork_ig_name_seq \clist_new:N \g_stork_ig_colors_clist

\keys_define:nn { stork/ig } { width .dim_set:N = \l_stork_ig_wd_dim, width .initial:n = 8cm, unknown .code:n = {}, }

\cs_new_protected:Nn \stork_ig:nn { \group_begin: \seq_set_split:NVn \l_stork_ig_name_seq \c_colon_str { #2 } \clist_if_in:NxTF \g_stork_ig_colors_clist { \seq_item:Nn \l_stork_ig_name_seq { 1 } } { \keys_set:nn { stork/ig } { #1 } \stork_ig_fake:xx { \seq_item:Nn \l_stork_ig_name_seq { 1 } } { \seq_item:Nn \l_stork_ig_name_seq { 2 } } } { \originalincludegraphics[#1]{#2} } \group_end: }

\cs_new_protected:Nn \stork_ig_fake:nn { \textcolor{ #1 } { \rule {% width \l_stork_ig_wd_dim } {% height __stork_ig_height:n { #2 } } } } \cs_generate_variant:Nn \stork_ig_fake:nn { xx }

\cs_new:Nn __stork_ig_height:n { \dim_eval:n { \l_stork_ig_wd_dim * \tl_if_blank:nTF { #1 } { 3/4 } { #1 } } }

% set the known colors \clist_gset:Nn \g_stork_ig_colors_clist { red, green, blue, orange, yellow } \ExplSyntaxOff % end of temporary code

\begin{document}

\includegraphics[width=0.4\textwidth]{red:3/4} \includegraphics[width=0.2\textwidth]{green:16/11} \includegraphics[width=0.2\textwidth]{orange:1}

\includegraphics[width=0.4\textwidth]{example-image}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Thanks for all the effort! But by sizes and ratios will be arbitrary (not some clean ratio like 3/4). And now suppose I have myPhoto.jpg and want to replace it in the green location. What do I do? (Currently, I might have an \includegraphics[]{example-image} with special aspect ratio, or such, and merely replace example-image with myPhoto.jpg.). Oh... wait... let me study your solution more closely.... Regardless (+1) for steps in the right direction. – David G. Stork Dec 31 '20 at 00:42
  • @DavidG.Stork I'm not sure what your expectations are: if you don't know what image you're going to use, you don't know its aspect ratio either. Anyway, would something like green:16/9 be an acceptable syntax? – egreg Dec 31 '20 at 10:10
  • Perhaps. Most of my figures are set in tabular arrays, where I set the width of each figure element by some proportion of the \textwidth. Example: \begin{tabular}{ccc} \includegraphics[width=.3\textwidth]{image1} & \includegraphics[width=.3\textwidth]{image2} & \includegraphics[width=.3\textwidth]{image3} \end{tabular}. I'd like to make (say) one of them a red {example-image} and later when I get the correct image simply replace example-image with image3. – David G. Stork Dec 31 '20 at 17:43
  • @DavidG.Stork Replacing green:16/9 with image3 would be as easy, wouldn't it? – egreg Dec 31 '20 at 20:28
  • I suppose so. And seeing no better (smaller code) solution, I hereby accept yours. Thanks for all your diligence. – David G. Stork Dec 31 '20 at 21:17
1

The following approach

  • Adds the option color to the list of keys for \includegraphics (default is red);

  • Assumes that your "substitute" image is given by example-image.

If you supply example-image, a box of colour color that matches the dimensions set with \includegraphics is placed. Anything other than example-image is set as needed (even with a color specification).

enter image description here

\documentclass{article}

\usepackage{xcolor,graphicx}

\makeatletter \define@key{Gin}{color}{\def\Gin@color{#1}}

\setlength{\fboxsep}{0pt}% Remove \fbox separation \let\oldincludegraphics\includegraphics \renewcommand{\includegraphics}[2][]{% \ifnum\pdfstrcmp{#2}{example-image}=0% \begingroup \setkeys{Gin}{color=red,#1}% Default color is red \colorbox{\Gin@color}{\phantom{\oldincludegraphics[#1]{#2}}}% \endgroup \else \oldincludegraphics[#1]{#2}% \fi } \makeatother

\begin{document}

\includegraphics[width=3em,height=4em,color=red]{example-image}

\includegraphics[width=3em,height=4em,color=red]{example-image-a}

\end{document}

Werner
  • 603,163