Tikz 中多个作用域的困难

Tikz 中多个作用域的困难

我有几张图片,我想用 tikz 在上面写一些文字。请参阅下面的示例代码-

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (a) at (0,0,0) {\includegraphics{example-image-a}};
    \begin{scope}[x={(a.south east)},y={(a.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) {.\x}; }
        \foreach \y in {0,1,...,9} {\node[anchor=east] at (0,\y/10) {.\y};}
        \begin{scope}[x={(a.south east)},y={(a.north west)}]
            % draw something on image a
            \draw (.5,.5) node {Center of A};
        \end{scope}
    \end{scope}

    \node (b) [below=of a] {\includegraphics{example-image-b}};
    \begin{scope}[x={(b.south east)},y={(b.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) {.\x}; }
        \foreach \y in {0,1,...,9} {\node[anchor=east] at (0,\y/10) {.\y};}
        \begin{scope}[x={(b.south east)},y={(b.north west)}]            
            % draw something on image b
            \draw (.5,.5) node {Center of B};
        \end{scope}
    \end{scope}
\end{tikzpicture}
\end{document}

请查看生成的 PDF 的屏幕截图-

在此处输入图片描述

第一个范围的原点位于第一张图像的左下方。但第二张图像的原点却不是这样。

基本上,存在以下问题-

  1. 请注意,第二幅图像上的网格水平线没有正确呈现
  2. 第二张图片上的网格垂直线太长
  3. 与网格对应的文本未正确放置

答案1

我采纳了@percusse@Paul Gaborit基本上,第二个作用域的原点设置不正确。为了保持一致性,我设置了所有作用域的原点。请参阅下面的代码片段-

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=0}]
    \node[anchor=south west] (a) {\includegraphics{example-image-a}};
    \begin{scope}[shift={(a.south west)},x={(a.south east)},y={(a.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) {.\x}; }
        \foreach \y in {0,1,...,9} {\node[anchor=east] at (0,\y/10) {.\y};}
        \begin{scope}[x={(a.south east)},y={(a.north west)}]
            % draw something on image a
            \draw (.5,.5) node[text=red, font=\Huge] {Center of A};
        \end{scope}
    \end{scope}

    \node (b) [below=of a] {\includegraphics{example-image-b}};
    \begin{scope}[shift={(b.south west)},x={(b.south east)},y={(b.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) {.\x}; }
        \foreach \y in {0,1,...,9} {\node[anchor=east] at (0,\y/10) {.\y};}
        \begin{scope}[x={(b.south east)},y={(b.north west)}]            
            % draw something on image b
            \draw (.5,.5) node[text=red, font=\Huge] {Center of B};
        \end{scope}
    \end{scope}
\end{tikzpicture}
\end{document}

谢谢@percusse@Paul Gaborit

相关内容