TikZ:使用绘图将图形置于 tikzpicture 的中心

TikZ:使用绘图将图形置于 tikzpicture 的中心

我想将 tikzpicture 中的图形置于中心位置,独立于其他图形(如图片左侧的文本)。目前我正在使用

% Color RecPos_leftbottom RecPos_righttop Text TextPos
\newcommand{\picelement}[5]{
\node [anchor=east] (cam) at (#5) {\Large #4};
\draw[#1,ultra thick,rounded corners] (#2) rectangle (#3);
\draw [-latex, ultra thick, #1] (cam) to[out=0, in=-120] (#2);
}

\begin{figure}
\centering
    \begin{tikzpicture}

    \node[anchor=north west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.7\textwidth]{duckiebot.jpg}};
    \begin{scope}[x={(image.north east)},y={(image.south west)}]

    % Camera
    \picelement{green}{0.63,0.5}{0.8,0.37}{Sensing}{-0.1,0.4};
    % LEDS
    \picelement{yellow}{0.395,0.64}{0.48,0.57}{Communication}{-0.1,0.6};
    % Raspi
    \picelement{blue}{0.35,0.5}{0.61,0.33}{Computation}{-0.1,0.5};

    \end{scope}

    \end{tikzpicture}

\caption{A Duckiebot.}
\label{fig:duckiebot}
\end{figure}

我得到了一张移位的图片:

移位图片

遗憾的是,我无法在论坛中找到答案。这是怎么回事?

答案1

使用pgfinterruptboundingbox

\documentclass{article}
\usepackage{tikz}
\begin{document}
% Color RecPos_leftbottom RecPos_righttop Text TextPos
\newcommand{\picelement}[5]{
\node [anchor=east] (cam) at (#5) {\Large #4};
\draw[#1,ultra thick,rounded corners] (#2) rectangle (#3);
\draw [-latex, ultra thick, #1] (cam) to[out=0, in=-120] (#2);
}

\begin{figure}
\centering
    \begin{tikzpicture}

    \node[anchor=north west,inner sep=0] (image) at (0,0)
    {\includegraphics[width=0.7\textwidth]{duckiebot.jpg}};
    \begin{pgfinterruptboundingbox}
    \begin{scope}[x={(image.north east)},y={(image.south west)}]

    % Camera
    \picelement{green}{0.63,0.5}{0.8,0.37}{Sensing}{-0.1,0.4};
    % LEDS
    \picelement{yellow}{0.395,0.64}{0.48,0.57}{Communication}{-0.1,0.6};
    % Raspi
    \picelement{blue}{0.35,0.5}{0.61,0.33}{Computation}{-0.1,0.5};

    \end{scope}
    \end{pgfinterruptboundingbox}
    \end{tikzpicture}

\caption{A Duckiebot.}
\label{fig:duckiebot}
\end{figure}
\end{document}

只是为了“证明”它有效:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\begin{document}
% Color RecPos_leftbottom RecPos_righttop Text TextPos
\newcommand{\picelement}[5]{
\node [anchor=east] (cam) at (#5) {\Large #4};
\draw[#1,ultra thick,rounded corners] (#2) rectangle (#3);
\draw [-latex, ultra thick, #1] (cam) to[out=0, in=-120] (#2);
}

\begin{figure}
\centering
    \begin{tikzpicture}

    \node[anchor=north west,inner sep=0] (image) at (0,0)
    {\includegraphics[width=0.7\textwidth]{example-image-a}};
    \begin{pgfinterruptboundingbox}
    \begin{scope}[x={(image.north east)},y={(image.south west)}]

    % Camera
    \picelement{green}{0.63,0.5}{0.8,0.37}{Sensing}{-0.1,0.4};
    % LEDS
    \picelement{yellow}{0.395,0.64}{0.48,0.57}{Communication}{-0.1,0.6};
    % Raspi
    \picelement{blue}{0.35,0.5}{0.61,0.33}{Computation}{-0.1,0.5};

    \end{scope}
    \end{pgfinterruptboundingbox}
    \end{tikzpicture}

\caption{A Duckiebot.}
\label{fig:duckiebot}
\end{figure}
\begin{tikzpicture}[remember picture,overlay]
\draw[red] (current page text area.south west) rectangle (current page text
area.north east);
\end{tikzpicture}
\end{document}

在此处输入图片描述

当然,问题在于您是否希望注释超越页面的界限。

相关内容