Tikz - highlight text in an image
Here is a recipe and an application. First the recipe, using this answer.
- Add a grid on top.
- Draw the contour you want to highlight.
- 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}
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}
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.
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}
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).