3

I would like to center the graphics included inside a tikzpicture independent from the additional drawings (like text on the left side of a picture). Currently I am using

% 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}

And I am obtaining a shifted picture:

Shifted Pic

Unfortunately, I am unable to find the answer in the forums. How is this possible?

1 Answers1

4

Use 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}

Just to "prove" that it works:

\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}

enter image description here

The question is, of course, whether you want the annotations go over the boundaries of the page.

  • overlay is the TikZ key for pgfinterruptboundingbox – percusse Apr 28 '18 at 11:18
  • @percusse I see. I didn't know that. Thanks! (But luckily I don't say anything that disagrees with that statement, do I? ;-) –  Apr 28 '18 at 11:20