在我的示例中,我希望在第二张图像上绘制网格线,但现在它也在第一张图像上绘制:
\documentclass{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[background rectangle/.style={fill=yellow!20}, show background rectangle]
\tikzset{box/.style={anchor=south west,inner sep=0}}
\node[box] (N1) {\includegraphics[width=\textwidth]{example-image-a}};
\node[box,right=0.5cm of N1] (N2) {\includegraphics[width=\textwidth]{example-image-b}};
\begin{scope}[x={(N2.south east)},y={(N2.north west)}]
\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}; }
\end{scope}
\foreach \i in {N2.south east,N2.north west} {
\node[circle,fill=red,minimum size=20pt] at(\i) {};
}
\end{tikzpicture}
\end{document}
答案1
您必须使用它shift={N2.south west}
来更改范围内的原点:
\documentclass{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[background rectangle/.style={fill=yellow!20}, show background rectangle]
\tikzset{box/.style={anchor=south west,inner sep=0}}
\node[box] (N1) {\includegraphics[width=\textwidth]{example-image-a}};
\node[box,right=0.5cm of N1] (N2) {\includegraphics[width=\textwidth]{example-image-b}};
\begin{scope}[shift={(N2.south west)},x={(N2.south east)},y={(N2.north west)}]
\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}; }
\end{scope}
\foreach \i in {N2.south east,N2.north west} {
\node[circle,fill=red,minimum size=20pt] at(\i) {};
}
\end{tikzpicture}
\end{document}
答案2
这更像是一种解决方法,先将第二张图片放在原点,然后将第一张图片相对于它放置。
\documentclass{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[background rectangle/.style={fill=yellow!20}, show background rectangle]
\tikzset{box/.style={anchor=south west,inner sep=0}}
\node[box] (N2) {\includegraphics[width=\textwidth]{example-image-b}};
\node[box,left=0.7cm of N2] (N1) {\includegraphics[width=\textwidth]{example-image-a}};
\begin{scope}[x={(N2.south east)},y={(N2.north west)}]
\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}; }
\end{scope}
\foreach \i in {N2.south east,N2.north west} {
\node[circle,fill=red,minimum size=20pt] at(\i) {};
}
\end{tikzpicture}
\end{document}