0

I was attempting to get part of an image grayed-out with tikz, following some of the proposed code. However, I cannot figure out how to reposition the rectangle...; I'm able to move it around the image, but it seems I don't have control on whether is vertical or horizontal.

In my specific case I need a vertical rectangle which moves from left to right so that in the following slide the leftmost part of the image up to a point will be grayed out. Below a MWE of the code I'm using. Thanks in advance!

\documentclass{beamer}
\usetheme{Boadilla}
\useoutertheme[width=0pt,height=.5in]{sidebar}

\definecolor{bground}{HTML}{005EB8}

\usepackage{tikz}
\usetikzlibrary{calc}
\def\coverfig#1#2{
    \coordinate (zone) at ($(fig.south)!#2!(fig.north)$);
    \fill<#1>[white, opacity=0.8] (fig.south east) rectangle (fig.west|-zone);
}

\begin{document}

\begin{frame}

\begin{tikzpicture}
\node[inner sep=1em] (fig) {\includegraphics[width=0.75\linewidth]{example-image}};
    \coverfig{1}{0}
    \coverfig{2}{.4}
\end{tikzpicture}
\end{frame}

\end{document}

The expected results should be to cover the left part of the image, e.g. rotate the white rectangle 90 degree clockwise, see image below: enter image description here

4
  • Your code does not compile. Please test your example before posting. Commented Aug 5 at 18:49
  • @samcarter_is_at_topanswers.xyz thanks I fixed it!
    – Matteo
    Commented Aug 5 at 18:58
  • People other than you still can't compile the code in your question. Commented Aug 5 at 19:02
  • @samcarter_is_at_topanswers.xyz apologies I left unchanged the image in my example, I just realized it.
    – Matteo
    Commented Aug 5 at 19:07

2 Answers 2

2

I think the easiest approach to adapt to your needs are explicit coordinates. These won't magically adapt to the size of the image, but you can modify them easily to understand what they do:

\documentclass{beamer}
\usetheme{Boadilla}
\useoutertheme[width=0pt,height=.5in]{sidebar}

\usepackage{tikz}

\begin{document}

\begin{frame}
\begin{tikzpicture}
\node[inner sep=1em,anchor=south west] (fig) {\includegraphics[width=0.75\linewidth]{example-image-duck}};
   \fill<2>[white, opacity=0.8,overlay] (0,0) rectangle ++(3,6);
\end{tikzpicture}
\end{frame}

\end{document}
1
  • thank you so much, I was actually quite lucky the image was already divided in part so that made my job easier in guessing the coordinates!
    – Matteo
    Commented Aug 5 at 19:03
1

Here's a coverfig environment which is basically just a wrapper for tikzpicture but it does position the node and install a xyz coordinate system so that the coordinate (0, 0) specifies the picture's bottom left and (1, 1) specifies its upper right corner.

This is actually all you need so that you can done do

\fill<…>[white, opacity=.8] (0,0) rectangle (.3333,1);

to cover the left third of the picture.

But since you probably will use this more often, there's also a \cffill macro (which is just a wrapper for \fill really) but here you can easily use the keys left, right, … so that you only have to specify

\cffill<…>[left=.3333];

to cover the left third of your graphic.

You can still use a normal \fill and make it shorter with a to because the rectangle to path is installed for all paths (as well as the coverfig/style which acts as a default cover style for all paths).

The \EndCover macro resets the defaults for every path and to path in case you want to add other annotations to the diagram but still use the new coordinate system.

Code

\documentclass{beamer}
\usetheme{Boadilla}
\useoutertheme[width=0pt,height=.5in]{sidebar}
\definecolor{bground}{HTML}{005EB8}
\usepackage{tikz}
\NewDocumentEnvironment{coverfig}{O{} m}{
  \begin{tikzpicture}
  \node[shape=rectangle, path only, anchor=south west,
            inner sep=+0pt, outer sep=+0pt, minimum size=+0pt]
        (cf@){\includegraphics[#1]{#2}};
  \def\EndCover{\tikzset{every path/.code=, line to}}% default
  \tikzset{x=([email protected] east), y=([email protected] west),
    every path/.append style=coverfig/style, to path=rectangle(\tikztotarget)}
}{\end{tikzpicture}}
\NewDocumentCommand{\cffill}{D<>{.-} O{}}{%
  \fill<#1>[coverfig={#2}](\pgfkeysvalueof{/tikz/coverfig/start})
                rectangle (\pgfkeysvalueof{/tikz/coverfig/end})}
\newcommand*\coverfigset{\pgfqkeys{/tikz/coverfig}}
\coverfigset{
  .code=\coverfigset{#1},% .search also=/tikz,
  style/.style={color=white, opacity=.5},
  start/.initial={0,0}, end/.initial={1,1},
  left/.style ={start={0,0},        end={{#1},1}},
  right/.style={start={{1-(#1)},0}, end={1,1}},
  top/.style  ={start={0,{1-(#1)}}, end={1,1}},
  bottom/.style={start={0,0}, end={1,{#1}}},
  vertical/.style args={#1 to #2}{start={{#1},0}, end={{#2},1}},
  horizontal/.style args={#1 to #2}{start={0,{#1}}, end={1,{#2}}}}
\begin{document}
\begin{frame}
\begin{coverfig}[width=0.75\linewidth]{example-image}
  \cffill<2>[left=.3333];
  \cffill<3>[left=.6666];
  \cffill<4>[vertical=.35 to .65][blue];
  \fill<5>[rounded corners, green] (.25, .35) to (.75, .65);
%  \EndCover
%  \draw[ultra thick, red, ->]
%     (.1,.15) -- node[above, sloped] {to the middle} (.5, .5);
\end{coverfig}
\end{frame}
\end{document}

Output

enter image description here

2
  • \newenvironment/\newcommand might be better because, I think, it incorporates beamer's overlays better and here's not much to gain from the new versions. Commented Aug 5 at 20:27
  • much appreciated, it's nice to see another way to do things! Also, thanks for the explanation; I will definitely experiment with this one too, as I'm only half-way through my presentation.
    – Matteo
    Commented Aug 5 at 20:42

You must log in to answer this question.

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