考虑这个应用程序:
\documentclass[border=5pt]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node(a) at (0.0, 0.0)[anchor=south west]{\includegraphics[width=0.3\paperwidth]{example-image-a}};
\draw[blue, fill=red] (0.15\paperwidth, 0.0\paperwidth) rectangle (0.3\paperwidth, 0.15\paperwidth);
\end{tikzpicture}
\end{document}
我的目标是在示例图像的右半部分绘制一个矩形。我猜测矩形坐标,知道宽度width=0.3\paperwidth
和大约纵横比。
我希望非常精确。如何才能在示例图像 a 的一半上精确绘制矩形?例如知道节点的高度和宽度a
。
答案1
outer sep
矩形更精确地覆盖图像与图像节点的校正:
\documentclass[border=5pt]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node (a) [inner sep=0pt,
outer sep=-0.5\pgflinewidth] % <---
{\includegraphics[width=0.3\paperwidth]{example-image-a}};
\draw[blue, fill=red, semitransparent] (a.south) rectangle (a.north east);
\end{tikzpicture}
\end{document}
或者通过使用fit
库:
\documentclass[border=5pt]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}
\node (a) [inner sep=0pt,
outer sep=-0.5\pgflinewidth] % <---
{\includegraphics[width=0.3\paperwidth]{example-image-a}};
\node [draw=blue, fill=red, semitransparent, inner sep=0pt,
fit=(a.south)(a.north east)] {};
\end{tikzpicture}
\end{document}
结果和以前一样。
答案2
您可以使用节点位置将红色方块精确定位在图片上方。
\documentclass[border=5pt]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node(a) at (0,0)[inner sep=0pt, outer sep=0pt,anchor=south west] {\includegraphics[width=0.3\paperwidth,keepaspectratio]{example-image-a}};
\draw[blue, fill=red, opacity=0.5] (a.south) rectangle (a.north east);
\end{tikzpicture}
\end{document}