5

How can I highlight text in an image with tikz by using a rectangle and have the rest of the page filled with a grey (or opaque black) tone, see example below (taken from here)?

enter image description here

You can see a screenshot taken from GIMP where I just highlight text using the rectangle selector tool. How can I achieve a similar effect using tikz in xetex (without the yellow dashed lines at the borders)?

My code I have so far is:

\begin{tikzpicture}[remember picture, overlay]
        \node [anchor=south west,inner sep=0] (A) at (0,0) {\includegraphics[width=\linewidth, height=\paperheight]{image}}{};
        \fill [draw=none, fill=black, fill opacity=0.3]
        (0,0) -- 
        (A.north west) -- (A.north east) -- (A.south east) -- 
        (0,0) -- (0,0) -- cycle;
        \filldraw [fill=white, draw=white, fill opacity=.3] (3.2,3.2) rectangle (14.6,10.5);
    \end{tikzpicture}

The issue I run into with this code is that filling the whole page with an opaque black and filling the rectangle with an opaque white leads to an opaque grey.

1

2 Answers 2

6

Here is a recipe and an application. First the recipe, using this answer.

  1. Add a grid on top.
  2. Draw the contour you want to highlight.
  3. Use the contour in an even odd rule fill (and drop the grid).

On purpose I used a document in which you may not want to use a horizontal box.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\foreach \X in {0,1,2}
{\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) 
    {\includegraphics[width=0.9\textwidth]{Dunlap_broadside_copy_of_the_United_States_Declaration_of_Independence,_LOC.jpg}};
    % ^^^ https://upload.wikimedia.org/wikipedia/commons/4/4e/Dunlap_broadside_copy_of_the_United_States_Declaration_of_Independence%2C_LOC.jpg
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
      \path[use as bounding box] (-0.1,-0.1) rectangle (1,1);
       % ^^^ only for animation
      \ifnum\X<2
        \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
        \foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
        \foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y}; }
      \fi   
      \ifnum\X=1    
        \draw (0.11,0.4) -- (0.37,0.404) --  (0.37,0.414) -- (0.11,0.41) -- cycle;
      \fi
      \ifnum\X=2
        \fill[even odd rule,opacity=0.4]
        (0.11,0.4) -- (0.37,0.404) --  (0.37,0.414) -- (0.11,0.41) -- cycle
        (0,0) rectangle (1,1);
      \fi       
    \end{scope} 
\end{tikzpicture}}
\end{document}

enter image description here

Of course, in the end you drop all the auxiliary stuff to arrive at

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) 
    {\includegraphics[width=0.9\textwidth]{Dunlap_broadside_copy_of_the_United_States_Declaration_of_Independence,_LOC.jpg}};
    % ^^^ https://upload.wikimedia.org/wikipedia/commons/4/4e/Dunlap_broadside_copy_of_the_United_States_Declaration_of_Independence%2C_LOC.jpg
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        \fill[even odd rule,opacity=0.4]
        (0.11,0.4) -- (0.37,0.404) --  (0.37,0.414) -- (0.11,0.41) -- cycle
        (0,0) rectangle (1,1);
    \end{scope} 
\end{tikzpicture}
\end{document}

enter image description here

enter image description here

Whether or not my choice of the highlighted text has anything to do with a wall at the border to Mexico, I do not want to elaborate on.

2
  • Thank you, marmot, this solves my issue and let's see how it goes with the wall...
    – Til Hund
    Feb 20, 2019 at 12:34
  • +1 for demonstrating on such a document as the Declaration of Independence. Feb 20, 2019 at 13:41
4

Depending on what format the images are, no tikz required:

\documentclass{article}
\usepackage{graphicx,stackengine,trimclip}
\begin{document}
\stackinset{l}{150pt}{b}{130pt}{%
  \clipbox{150pt 130pt 100pt 50pt}{\includegraphics{example-image.jpg}}%
}{%
  \includegraphics[decodearray={.3 .5 .3 .5 .3 .5}]{example-image.jpg}%
}
\end{document}

enter image description here

Depending on the color palette, the decodearray may need tweaking:

\documentclass{article}
\usepackage{graphicx,stackengine,trimclip}
\begin{document}
\stackinset{l}{20pt}{b}{40pt}{%
  \clipbox{20pt 40pt 15pt 15pt}{\includegraphics{example-grid-100x100bp.png}}%
}{%
  \includegraphics[decodearray={.3 .6 .3 .6 .3 .6}]{example-grid-100x100bp.jpg}%
}
\end{document}

decodearray will not work for cmyk color schemes, and only a limited number of image formats (jpeg being one).

enter image description here

2
  • I like this solution, Steven, however I wanted the solution in tikz for a project. Thanks for your answer!
    – Til Hund
    Feb 20, 2019 at 12:33
  • 1
    @TilHund Thanks. I understand your request and requirement completely. Feb 20, 2019 at 13:40

You must log in to answer this question.

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