锚点位置不当

锚点位置不当

我正在尝试使用 Beamer 框架内的 TikZ 在图片上叠加一些透明框。我使用的代码如下:

\documentclass{beamer}
\usepackage[spanish]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}

\begin{document}
\begin{frame}
\begin{tikzpicture}[remember picture, overlay]
    \onslide<1->{   
        \draw []
            (-0.95,-2.75) rectangle (1.5,-4.00)
            node (rNode) {};

        \tikzset
            {pt/.style={circle,fill=red,inner sep=0mm,minimum size=4pt}}
        \node[pt] at (rNode.center){};
        \node[pt] at (rNode.west){};
        \node[pt] at (rNode.south){};
        \node[pt] at (rNode.east){};
        \node[pt] at (rNode.north){};
    };
\end{tikzpicture}
\end{frame}
\end{document}

问题是我创建的 TikZ 矩形的锚点不正确:它们似乎是我没有创建的另一个矩形的锚点。我想知道我做错了什么以及如何解决这个问题。

在此处输入图片描述

答案1

免责声明

其他答案已经解释了这个问题,但是我在晚餐前开始写同样的动机,所以我还是把它留下了。


实际上,你所面临的行为是正确的,因为当你使用

\draw []
     (-0.95,-2.75) rectangle (1.5,-4.00)
     node (rNode) {};

你放置节点只是在右下角,但节点没有矩形的尺寸。您已经用示例证明了这一点,但为了强调一下,这里还有另一个证明。以下示例将节点cross out精确地放置在 上rNode

\documentclass[tikz,png,border=2pt]{standalone}
\usetikzlibrary{backgrounds,shapes.misc,positioning}

\begin{document}
\begin{tikzpicture}
    \draw []
        (-0.95,-2.75) rectangle (1.5,-4.00)
        node (rNode) {};
    \node[draw,cross out,blue,scale=1.1] at (rNode){};    
    
    \begin{scope}[on background layer]
    \tikzset
        {pt/.style={circle,fill=red,inner sep=0mm,minimum size=4pt}}
    \node[pt] at (rNode.center){};
    \node[pt] at (rNode.west){};
    \node[pt] at (rNode.south){};
    \node[pt] at (rNode.east){};
    \node[pt] at (rNode.north){};
    \end{scope}
\end{tikzpicture}
\end{document}

结果:

在此处输入图片描述

因此正确的方法是使用rectangle里面的标准形状\node

\documentclass{beamer}
\usepackage[spanish]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}

\begin{document}
\begin{frame}
\begin{tikzpicture}[remember picture, overlay]
    \onslide<1->{   
        \node[rectangle, draw, minimum height=1.25cm, minimum width=2.45cm] 
           (rNode) at (0.275,-3.375){};

        \tikzset
            {pt/.style={circle,fill=red,inner sep=0mm,minimum size=4pt}}
        \node[pt] at (rNode.center){};
        \node[pt] at (rNode.west){};
        \node[pt] at (rNode.south){};
        \node[pt] at (rNode.east){};
        \node[pt] at (rNode.north){};
    };
\end{tikzpicture}
\end{frame}
\end{document}

结果:

在此处输入图片描述

回到最初的问题,最好的办法是将\node图像包含在内,并将inner sep选项设置为0pt(使锚点精确地位于边框上);例如:

\documentclass{beamer}
\usepackage[spanish]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mwe} % for dummy images
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}
\tikzset{pt/.style={
  circle,
  fill=blue,
  inner sep=0mm,
  minimum size=1pt,
  }
}
\tikzset{legend/.style={
  rectangle,
  rounded corners,
  draw,
  top color=white,
  bottom color=#1,
  align=center,
  font=\footnotesize,
  }
}

\begin{document}
\begin{frame}
\begin{tikzpicture}[remember picture, overlay]
    \onslide<1->{   
        \node[inner sep=0pt](rNode) at (2,0){\includegraphics[scale=0.3]{example-image}};

        \node[pt] at (rNode.center){};
        \node[pt] at (rNode.west){};
        \node[pt] at (rNode.south){};
        \node[pt] at (rNode.east){};
        \node[pt] at (rNode.north){};
        
        \node[legend={red},anchor=south west,yshift=1ex] at (rNode.north) {Some description};
        \node[legend={red},anchor=north east,yshift=-1ex,text width=1.75cm] at (rNode.south) {Some other description};
    };
    \onslide<2->{   
        \node[inner sep=0pt](xNode) at (8,2){\includegraphics[scale=0.3]{example-image-a}};

        \node[pt] at (xNode.center){};
        \node[pt] at (xNode.west){};
        \node[pt] at (xNode.south){};
        \node[pt] at (xNode.east){};
        \node[pt] at (xNode.north){};
        
        \node[legend={cyan!60!blue!60},anchor=west,xshift=1em,text width=1cm] at (xNode.east) {Some other text};
    };
    \onslide<3->{   
        \node[inner sep=0pt](yNode) at (8,-2){\includegraphics[scale=0.3]{example-image-b}};

        \node[pt] at (yNode.center){};
        \node[pt] at (yNode.west){};
        \node[pt] at (yNode.south){};
        \node[pt] at (yNode.east){};
        \node[pt] at (yNode.north){};
        
        \node[legend={green!60!lime!60},anchor=west,xshift=1em,text width=1cm] at (yNode.east) {Some other text};
        \node[legend={green!60!lime!60},anchor=north,yshift=-1ex] at (yNode.south) {Yet another description};
        
    };  

    \draw<4->[-stealth,red](rNode.east)--(xNode.west);
    \draw<5->[-stealth,red](xNode.south)--(yNode.north);
    \path<6->[-stealth,red](yNode.west)edge[bend left](rNode.south);

\end{tikzpicture}
\end{frame}
\end{document}

结果:

在此处输入图片描述

请注意,通过这种方式,您可以访问您创建的节点的标准锚点来放置箭头或描述性文本。

为了完整起见,如果你必须进行操作里面单张图片最好的方法是参考在投影仪幻灯片中使用矩形覆盖突出显示图像的某个区域。该方法使用坐标,但您始终可以使用锚点(也许借助库calc)。

一个例子:

\documentclass{beamer}
\usepackage[spanish]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mwe} % for dummy images
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{pt/.style={
  circle,
  draw=blue,
  fill=green!50!lime,
  inner sep=0mm,
  minimum size=3pt,
  }
}
\tikzset{legend/.style={
  rectangle,
  rounded corners,
  draw,
  top color=white,
  bottom color=#1,
  align=center,
  font=\footnotesize,
  }
}

\begin{document}
\begin{frame}
\begin{tikzpicture}[remember picture, overlay]
    \onslide<1->{   
        \node[inner sep=0pt](rNode) at (5.5,0){\includegraphics[scale=0.75]{example-image}};

        \foreach \anchor in 
        {north,south,east,west,center,north west,north east,
        south west, south east,center}{
        \node[pt] at (rNode.\anchor){};
        }
      
        
    };
    \node<2->[legend={red},anchor=south west,yshift=1ex] (x) at ($(rNode.north)!0.5!(rNode.center)$) {Some description};
    \node<3->[legend={red},anchor=north east,yshift=-1ex,text width=1.75cm] 
     (y) at ($(rNode.south)!0.65!(rNode.center)$) {Some other description};
    
    \path<4->[-stealth,red, thick](y.west) edge[bend left, looseness=2](x.west);

\end{tikzpicture}
\end{frame}
\end{document}

结果:

在此处输入图片描述

答案2

您的问题在于您的节点的定义,即您没有定义矩形节点。

此代码:

\draw []
    (-0.95,-2.75) rectangle (1.5,-4.00)
    node (rNode) {};

方法:

  1. 在 (-0.95,-2.75) 和 (1.5,-4.00) 之间画一个矩形
  2. 然后在 (1.5,-4.00) 处绘制一个空的 ( {}) 文本节点,名为 rNode

您的空文本节点默认为矩形,宽度和高度等于 2 x 内部分隔符。因此,当您写入 (rNode.south) 时,Tikz 会在正确的位置绘制 :-)

你的第一个矩形最好像这样写:

\node[draw,minimum width=2.45cm,minimum height=1.25cm] (rNode) at (.275,-3.375) {};

相关内容