在相对图像坐标中绘制不同图像之间的 TikZ 箭头

在相对图像坐标中绘制不同图像之间的 TikZ 箭头

beamer我正在尝试使用TikZ和命令在演示文稿中两个导入的 PNG 图像之间画一个箭头\tikzmarknode

一切正常,如下面的示例所示,但我只能在图像上绘制绝对坐标。这样,如果我通过更改来缩放图形\includegraphics[width=...],箭头的位置就会相对于图像发生变化。

我怎样才能在图像的相对坐标中绘制箭头的尖端,以便当我缩放图像时箭头的位置不会改变\includegraphics

我的问题与tikz 相对坐标相对于一个节点的Tikz坐标,但我不确定如何在不同的图像之间应用这个想法。

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}

\begin{frame}{Two Figures}

    \begin{columns}
        
        \column{0.5\textwidth}            
        \tikzmarknode{fig1}{\includegraphics[width=.7\textwidth]{example-image}}

        \column{0.5\textwidth}
        \tikzmarknode{fig2}{\includegraphics[width=.7\textwidth]{example-image}}

    \end{columns}

    \begin{tikzpicture}[remember picture, overlay]
        \draw[->, red, ultra thick] (fig1) -- ([xshift=20, yshift=30]fig2.center);
    \end{tikzpicture}

\end{frame}

\end{document}

在此处输入图片描述

答案1

您可以使用切换到相对于第二幅图像的坐标。左下角为(0,0),右上角为(1,1)

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}

\begin{frame}
  \frametitle{Two Figures}
  \begin{columns}
    \begin{column}{0.5\textwidth}
      \tikzmarknode{fig1}{\includegraphics[width=.7\textwidth]{example-image}}
    \end{column}
    \begin{column}{0.5\textwidth}
      \tikzmarknode{fig2}{\includegraphics[width=.7\textwidth]{example-image}}
    \end{column}
  \end{columns}
  \begin{tikzpicture}[
    remember picture,
    overlay,
    shift={(fig2.south west)},
    x={(fig2.south east)},
    y={(fig2.north west)}
  ]
    \draw[->, green, ultra thick] (fig1) -- (0.7,0.85);        
  \end{tikzpicture}
\end{frame}

\end{document}

在此处输入图片描述

答案2

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}{Two Figures}

\begin{columns}
\column{0.5\textwidth}
\begin{tikzpicture}[remember picture]
\node[anchor=south west, inner sep=0pt] (p1) {\includegraphics[width=.9\textwidth]{example-image}};
\tikzset{x=(p1.south east), y=(p1.north west)}
\coordinate (c1) at (0.29,0.62);
\end{tikzpicture}

\column{0.5\textwidth}
\begin{tikzpicture}[remember picture]
\node[anchor=south west, inner sep=0pt] (p2) {\includegraphics[width=.6\textwidth]{example-image}};
\tikzset{x=(p2.south east), y=(p2.north west)}
\coordinate (c2) at (0.29,0.62);
\end{tikzpicture}
\end{columns}

\begin{tikzpicture}[remember picture, overlay]
\draw[->, red, ultra thick] (c1) -- (c2);
\end{tikzpicture}

\end{frame}
\end{document}

两张图片中间有红色箭头

答案3

有一种有用的方法可以在不需要 的情况下在图像上进行绘制remember picture。我会将该方法用于单个 中的两幅图像tikzpicture。下面的幻灯片演示了该方法的步骤。您不会在最终版本中包含前两个步骤。

基本上,您将目标图像划分为网格,在开发期间将其打印在顶部并用于放置东西。此网格与图片大小相关,因此(0.5,0.5)始终位于中心,(0.1,0.4)始终位于底部上方 10% 处,左侧上方 40% 处。因此,如果您更改图片大小,您的箭头仍将指向图像的同一部分。

该方法基于现有的答案,特别是卡拉姆迪尔的杰克的,但它足够方便,值得打包供您自己使用。当然,这种方法并不特定于包含的图形。它可以用于任何可以放入(矩形)节点的东西。

\begin{filecontents}[overwrite]{tikz-box-helper.sty}
\ProvidesPackage{tikz-box-helper.sty}
% cfr

\RequirePackage{tikz}
\usetikzlibrary{positioning}

% ateb: https://tex.stackexchange.com/a/374029/ addaswyd o gwestiwn Skeleton Bow: https://tex.stackexchange.com/q/374012/
% copi fel tikz-boxhelper.tex yn y lle arferol
% Datrysiad addaswyd o atebion i gwesiwn http://tex.stackexchange.com/questions/9559/drawing-on-an-image-with-tikz gan Caramdir (https://tex.stackexchange.com/a/9561/) a Jake (https://tex.stackexchange.com/a/9562/)

\newif\ifhelpermarks

\newenvironment{boxhelper}[2][]{%
  \begin{scope}[shift=(#2.south west), x=(#2.south east), y=(#2.north west), #1]%
    \ifhelpermarks
      \begin{pgfinterruptboundingbox}%
        \begin{scope}[every helper line, every node/.append style={every helper mark}]
          \foreach \i [evaluate=\i as \j using {\i/10}, evaluate=\i as \k using { \i>0 ? ( \i<10 ? 0.\i : 1 ) : 0 } ] in {0,1,...,10}%
          {%
            \node [below right=2.5pt and \j of #2.south west, anchor=north] {\k};
            \node [above left=\j and 2.5pt of #2.south west, anchor=east] {\k};
            \node [above right=2.5pt and \j of #2.north west, anchor=south] {\k};
            \node [above right=\j and 2.5pt of #2.south east, anchor=west] {\k};
            \draw (\j,0) -- (\j,1) (0,\j) -- (1,\j);
          }%
        \end{scope}%
      \fi
    }{%
      \ifhelpermarks
        \end{pgfinterruptboundingbox}%
      \fi
  \end{scope}%
}

\tikzset{%
  helper marks/.is if=helpermarks,
  helper marks/.initial=false,
  helper marks/.default=true,
  every helper mark/.style={help lines, font=\sffamily\scriptsize},
  every helper line/.style={help lines},
}
\endinput
\end{filecontents}

\documentclass{beamer}
\usepackage{tikz}
\usepackage{tikz-box-helper}
\begin{document}
\begin{frame}{Set Up}
  \begin{tikzpicture}[every node/.append style={inner sep=0pt}]
    \node (img1) {\includegraphics[width=.35\textwidth]{example-image-a}};
    \node [xshift=.5\textwidth] (img2) {\includegraphics[width=.35\textwidth]{example-image-b}};
    \begin{boxhelper}[helper marks]{img2}
      
    \end{boxhelper}
  \end{tikzpicture}
\end{frame}
\begin{frame}{Configuration}
  \begin{tikzpicture}[every node/.append style={inner sep=0pt}]
    \node (img1) {\includegraphics[width=.35\textwidth]{example-image-a}};
    \node [xshift=.5\textwidth] (img2) {\includegraphics[width=.35\textwidth]{example-image-b}};
    \begin{boxhelper}[helper marks]{img2}
      \draw [->,red,ultra thick] (img1) -- (0.6,0.8);
    \end{boxhelper}
  \end{tikzpicture}
\end{frame}
\begin{frame}{Two Figures}
  \begin{tikzpicture}[every node/.append style={inner sep=0pt}]
    \node (img1) {\includegraphics[width=.35\textwidth]{example-image-a}};
    \node [xshift=.5\textwidth] (img2) {\includegraphics[width=.35\textwidth]{example-image-b}};
    \begin{boxhelper}{img2}
      \draw [->,red,ultra thick] (img1) -- (0.6,0.8);
    \end{boxhelper}
  \end{tikzpicture}
\end{frame}
\begin{frame}{Down-scaling}
  \begin{tikzpicture}[every node/.append style={inner sep=0pt}]
    \node (img1) {\includegraphics[width=.25\textwidth]{example-image-a}};
    \node [xshift=.5\textwidth] (img2) {\includegraphics[width=.25\textwidth]{example-image-b}};
    \begin{boxhelper}{img2}
      \draw [->,red,ultra thick] (img1) -- (0.6,0.8);
    \end{boxhelper}
  \end{tikzpicture}
\end{frame}
\begin{frame}{Up-scaling}
  \begin{tikzpicture}[every node/.append style={inner sep=0pt}]
    \node (img1) {\includegraphics[width=.45\textwidth]{example-image-a}};
    \node [xshift=.5\textwidth] (img2) {\includegraphics[width=.45\textwidth]{example-image-b}};
    \begin{boxhelper}{img2}
      \draw [->,red,ultra thick] (img1) -- (0.6,0.8);
    \end{boxhelper}
  \end{tikzpicture}
\end{frame}
\end{document}

由于 KDE6/当前 Okular 存在错误,下面的图片质量很差。PDF 没问题。

发展:

发展

结果:

结果

缩放:

扩展

相关内容