13

I want to highlight part of an image in beamer by drawing a box around it and setting the rest of the image to a dim setting. I do not know how to decrease the brightness of the rest of the picture. Below is my partial attempt, which almost does the job.

But I am unable to decrease the brightness of the remaining picture outside the small red rectangle at the left bottom. Any help would be greatly appreciated.

\documentclass{beamer}
\usetheme{Copenhagen}
\usecolortheme{whale}
\useinnertheme{rounded}

\usepackage{tikz}

\newcounter{boxes}

\newcommand\ordbox[2]{%
  \stepcounter{boxes}
  \tikz[remember picture,overlay]{
  \node[rectangle,rounded corners,line width=2pt,draw=red,fill=pink,text height=10pt,text depth=3pt,align=left,draw opacity = 0.75, fill opacity=.75,text opacity=1] (box-\theboxes) at #2 {#1};}
}

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay] \node (#1) {};}

\begin{document}
\begin{frame}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] at (0,0) {\includegraphics[scale=0.2]{../images/H1D_level_5.pdf}};
\end{tikzpicture}
\end{frame}
\begin{frame}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] at (0,0) {\includegraphics[scale=0.2]{../images/H1D_level_5.pdf}};
\draw[red,ultra thick] (0,0) rectangle (0.5,0.5);
\end{tikzpicture}
\end{frame}

\end{document}

The code above produces the outputs below. enter image description here enter image description here

19

If you name the node in the second slide you can use a fill=<color> with fill opacity=<num>. By naming the node we can easily access the coordinates of the vertices so that we can traverse the region we want to fill:

\draw[red,ultra thick] (0,0) rectangle (0.5,0.5);
\fill [draw=none, fill=white, fill opacity=0.3] 
    (0,0.5) -- 
    (A.north west) -- (A.north east) -- (A.south east) -- 
    (0.5,0) -- (0.5,0.5) -- cycle;

Using a modified version showing those two images side by side to see the effect:

enter image description here

Notes:

  • To get the images to align I shifted the drawing of the box by 0.5\pgflinewidth. So if you notice that your beamer image shifts from slide to slide, you will need to apply this shift as well.
  • Even with this adjustment, the second image appears to be off slightly and I don't know why. So perhaps another tweak is required.

Code:

\RequirePackage[demo]{graphicx}

\documentclass{beamer}

\usetheme{Copenhagen}
\usecolortheme{whale}
\useinnertheme{rounded}

\usepackage{tikz}

\newcounter{boxes}

\newcommand\ordbox[2]{%
  \stepcounter{boxes}
  \tikz[remember picture,overlay]{
  \node[rectangle,rounded corners,line width=2pt,draw=red,fill=pink,text height=10pt,text depth=3pt,align=left,draw opacity = 0.75, fill opacity=.75,text opacity=1] (box-\theboxes) at #2 {#1};}
}

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay] \node (#1) {};}

\begin{document}
\begin{frame}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] at (0,0) {\includegraphics[scale=0.2]{../images/H1D_level_5.pdf}};
\end{tikzpicture}
\end{frame}
\begin{frame}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (A) at (0,0) {\includegraphics[scale=0.2]{../images/H1D_level_5.pdf}};
\draw[red,ultra thick] (0,0) rectangle (0.5,0.5);
\fill [draw=none, fill=white, fill opacity=0.3] (0,0.5) -- (A.north west) -- (A.north east) -- (A.south east) -- (0.5,0) -- (0.5,0.5) -- cycle;
\end{tikzpicture}
\end{frame}

\end{document}

Code:

Modified to generate the image above.

\documentclass[border=2pt]{standalone}

\usepackage{tikz}

\newcommand\tikzmark[1]{\tikz[remember picture,overlay] \node (#1) {};}

 \newcommand*{\Size}{2.5}%

\begin{document}
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] at (0,0) {\includegraphics[scale=0.1]{./images/EiffelWide.jpg}};
\end{tikzpicture}
%
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (A) at (0,0) {\includegraphics[scale=0.1]{./images/EiffelWide.jpg}};

    \draw[red,ultra thick] (0.5\pgflinewidth,0.5\pgflinewidth) rectangle (\Size,\Size);

    \fill [draw=none, fill=white, fill opacity=0.4] 
        (\pgflinewidth,\Size) -- 
        (A.north west) -- (A.north east) -- (A.south east) -- 
        (\Size,\pgflinewidth) -- (\Size,\Size) -- cycle;
\end{tikzpicture}
\end{document}
  • Hi Peter, can you use the same method if the square is in the middle of the picture? – Til Hund Feb 19 at 15:35
  • @TilHund: Yes. You just need to adjust the location. If that is not clear how to do that, I would recommend you should post a new question about How to position a node as it has nothing to do with drawing on a picture, just with positioning of a node. If you do post a new question, please include a MWE including \documentclass and the appropriate packages that sets up the problem. Feel free to use this example code to get you started and reference this question to give it context if you feel it is related. – Peter Grill Feb 19 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy