
tikzで画像内のテキストを長方形でハイライトし、ページの残りの部分をグレー(または不透明な黒)の色調で塗りつぶすにはどうすればいいでしょうか。以下の例を参照してください(ここ)?
GIMP から取得したスクリーンショットでは、長方形セレクター ツールを使用してテキストを強調表示しています。xetex で tikz を使用して同様の効果を実現するにはどうすればよいでしょうか (境界の黄色の破線なし)?
これまでの私のコードは次のとおりです:
\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}
このコードで遭遇する問題は、ページ全体を不透明な黒で塗りつぶし、四角形を不透明な白で塗りつぶすと、不透明な灰色になってしまうことです。
答え1
レシピと応用例をご紹介します。まずはレシピから。この答え。
- 上にグリッドを追加します。
- 強調したい輪郭を描きます。
- 塗りつぶしに輪郭を使用します
even odd rule
(グリッドは削除します)。
意図的に、水平ボックスを使用したくないドキュメントを使用しました。
\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}
もちろん、最終的には補助的なものをすべて取り除いて
\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}
私が強調表示したテキストを選んだことがメキシコ国境の壁と関係があるかどうかについては、詳しく述べたくありません。
答え2
画像の形式によっては、次のものはtikz
必要ありません。
\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}
カラーパレットによっては、decodearray
調整が必要になる場合があります。
\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
カラースキームには機能せずcmyk
、限られた数の画像形式(jpeg など)でのみ機能します。