![使用坐标系轴 cs 将使用范围的 tikz-draw 定位在轴环境内](https://linux22.com/image/353735/%E4%BD%BF%E7%94%A8%E5%9D%90%E6%A0%87%E7%B3%BB%E8%BD%B4%20cs%20%E5%B0%86%E4%BD%BF%E7%94%A8%E8%8C%83%E5%9B%B4%E7%9A%84%20tikz-draw%20%E5%AE%9A%E4%BD%8D%E5%9C%A8%E8%BD%B4%E7%8E%AF%E5%A2%83%E5%86%85.png)
问题:使用坐标系轴 cs 将使用范围的 tikz-draw 定位在轴环境内。
在下面的代码之前更好地解释了这个问题,这是目前尚未实现的好的解决方案。
\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}
从代码中可以看出,提到的 tikz-draw 由第一个代码块指定,其位置由坐标 (car1) 设置。依次是 axis 环境,在实际实现中,它有一些图形。我需要通过坐标 (car1) 将 tikz-draw 放置在与 axis 环境中的图形相关的相同位置。目前,通过尝试和错误方法,我花了很长时间才将 tikz-draw 放置在所需的位置。
我认为可以将与 tikz-draw 相关的代码块放在 axis 环境中,然后使用轴坐标系坐标系设置 tikz-draw 位置如下图所示
\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} %
但令我惊讶的是,它看起来是轴和范围环境之间的一些不兼容,因为这个新的解决方案检索到错误:
包 pgf 错误:没有已知名为 car1 的形状。\begin{scope} [rotate around={-25:(car1)}] 包 pgf 错误:没有已知名为 car1 的形状。\begin{scope} [rotate around={-25:(car1)}]
因此,我的问题是如何解决这个问题。
谢谢。
答案1
把 放在\coordinate
里面axis
,然后scope
放在 后面\end{axis}
。
\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}