3

The problem: Position a tikz-draw that uses scope inside an axis environment using the coordinate system axis cs.

The problem is better explained before the following code, which is the currently not good solution implemented.

\documentclass[12pt]{report}
\usepackage[portuges, brazilian]{babel} % Hiphenação em portugues
\usepackage{graphics} % Suporte padrão para gráficos
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{grid=major,compat=1.11}

\begin{document}
\begin{figure}[!htb]
\centering
\begin{tikzpicture}

\pgfmathsetmacro{\Ffactor}{0.6}
\coordinate (car1) at (0.50,1.95);
\begin{scope} [rotate around={-25:(car1)}]
\draw[fill=yellow]     (car1) +  ($\Ffactor*(-0.250, 0.100)$) rectangle + ($\Ffactor*(0.250,-0.100)$);
\draw[fill=black]      (car1) ++ ($\Ffactor*(-0.250, 0.100)$) rectangle + ($\Ffactor*( 0.175, 0.075)$); % rear left wheel
\draw[fill=black]      (car1) ++ ($\Ffactor*( 0.250, 0.100)$) rectangle + ($\Ffactor*(-0.175, 0.075)$); % front left wheel
\draw[fill=black]      (car1) ++ ($\Ffactor*( 0.250,-0.100)$) rectangle + ($\Ffactor*(-0.175,-0.075)$); % front right wheel
\draw[fill=black]      (car1) ++ ($\Ffactor*(-0.250,-0.100)$) rectangle + ($\Ffactor*( 0.175,-0.075)$); % rear right wheel
\end{scope}

\begin{axis}[ ]
%
% SOME CODES
%
\end{axis}

\end{tikzpicture} %
\end{figure}
\end{document}

From the code, it is possible to see that the mentioned tikz-draw is specified by the first block of code and its position set by the coordinate (car1). In sequence, there is the axis environment, which has, in the real implementation, some graphs. I need to place the tikz-draw via the coordinate (car1) in same place that is, some how, related with the graphs from the axis environment. Currently, with an attempt and error approach, I am taking quite a while to place the tikz-draw in the desired place.

I thought that it would be possible, to put that block of code related with the tikz-draw inside the axis environment and use the axis cs coordinate frame to set the tikz-draw position as show bellow

\begin{tikzpicture}
\begin{axis}[ ]
%
% SOME CODES
%
\pgfmathsetmacro{\Ffactor}{0.6}
\coordinate (car1) at (axis cs: 0.50,1.95);  <----------
\begin{scope} [rotate around={-25:(car1)}]
\draw[fill=yellow]     (car1) +  ($\Ffactor*(-0.250, 0.100)$) rectangle + ($\Ffactor*(0.250,-0.100)$);
\draw[fill=black]      (car1) ++ ($\Ffactor*(-0.250, 0.100)$) rectangle + ($\Ffactor*( 0.175, 0.075)$); % rear left wheel
\draw[fill=black]      (car1) ++ ($\Ffactor*( 0.250, 0.100)$) rectangle + ($\Ffactor*(-0.175, 0.075)$); % front left wheel
\draw[fill=black]      (car1) ++ ($\Ffactor*( 0.250,-0.100)$) rectangle + ($\Ffactor*(-0.175,-0.075)$); % front right wheel
\draw[fill=black]      (car1) ++ ($\Ffactor*(-0.250,-0.100)$) rectangle + ($\Ffactor*( 0.175,-0.075)$); % rear right wheel
\end{scope}
%
% SOME CODE
%
\end{axis}

\end{tikzpicture} %

but, for my surprise, its seens to be some incompatibility between the axis and the scope environment, because this new solution retrieves the errors:

Package pgf Error: No shape named car1 is known. \begin{scope} [rotate around={-25:(car1)}] Package pgf Error: No shape named car1 is known. \begin{scope} [rotate around={-25:(car1)}]

Therefore, my question is how to work around this issue.

Thanks.

  • The problems cited are not related with mine. – Randerson Apr 25 '17 at 17:04
6

Put the \coordinate inside the axis, and the scope after \end{axis}.

enter image description here

\documentclass[12pt]{report}
\usepackage[portuges, brazilian]{babel} % Hiphenação em portugues
\usepackage{graphicx} % Suporte padrão para gráficos
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{grid=major,compat=1.11}

\begin{document}
\begin{figure}[!htb]
\centering
\begin{tikzpicture}
\pgfmathsetmacro{\Ffactor}{0.6}
\begin{axis}[ ]
%
\coordinate (car1) at (0.5,0.5); % axis cs is default since you have compat=1.11
\addplot coordinates {(0,0)}; % need to have something, or it doesn't work
\end{axis}

\begin{scope} [rotate around={-25:(car1)}]
\draw[fill=yellow]     (car1) +  ($\Ffactor*(-0.250, 0.100)$) rectangle + ($\Ffactor*(0.250,-0.100)$);
\draw[fill=black]      (car1) ++ ($\Ffactor*(-0.250, 0.100)$) rectangle + ($\Ffactor*( 0.175, 0.075)$); % rear left wheel
\draw[fill=black]      (car1) ++ ($\Ffactor*( 0.250, 0.100)$) rectangle + ($\Ffactor*(-0.175, 0.075)$); % front left wheel
\draw[fill=black]      (car1) ++ ($\Ffactor*( 0.250,-0.100)$) rectangle + ($\Ffactor*(-0.175,-0.075)$); % front right wheel
\draw[fill=black]      (car1) ++ ($\Ffactor*(-0.250,-0.100)$) rectangle + ($\Ffactor*( 0.175,-0.075)$); % rear right wheel
\end{scope}
%
\end{tikzpicture} %
\end{figure}
\end{document}
  • Just a small issue: I would like to place the tikz-draw in the lowest layout. In other words, I what It to be bellow everything. Is it possible? – Randerson Apr 25 '17 at 17:03
  • 2
    @Randerson Add \usetikzlibrary{backgrounds}, and use \begin{scope}[on background layer,rotate around={-25:(car)}] – Torbjørn T. Apr 25 '17 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.