![在 3d tikz 中按 ex 单位进行移位](https://linux22.com/image/351298/%E5%9C%A8%203d%20tikz%20%E4%B8%AD%E6%8C%89%20ex%20%E5%8D%95%E4%BD%8D%E8%BF%9B%E8%A1%8C%E7%A7%BB%E4%BD%8D.png)
在二维坐标(红色图像)中,我可以使用ex
单位来移动节点:
\path (1,0) ++ (1.0ex,0) node {$x$};
但是,当尝试使用 3D 坐标(蓝色图像)执行同样的事情时,
\path (1,0,0) ++ (1.0ex,0,0) node {$x$};
这有点奇怪。我怀疑这与将 3D 坐标映射到 2D 显示器上的变换矩阵有关。但是,我怎样才能用 3D 坐标移动固定量呢?
下面的每个轴标签都应1.0ex
从尖端移动:
代码:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{xcolor}
\newcommand*{\Offset}{1.0ex}
\begin{document}
\begin{tikzpicture}[ultra thick, red, ->]
\draw (0,0) -- (1,0);
\path (1,0) ++ (\Offset,0) node {$x$};
\draw (0,0) -- (0,1);
\path (0,1) ++ (0,\Offset) node {$y$};
\end{tikzpicture}
\quad
\begin{tikzpicture}[ultra thick, blue, ->]
\draw (0,0,0) -- (1,0,0);
\path (1,0,0) ++ (\Offset,0,0) node {$x$};
\draw (0,0,0) -- (0,1,0);
\path (0,1,0) ++ (0,\Offset,0) node {$y$};
\draw (0,0,0) -- (0,0,1);
\path (0,0,1) ++ (0,0,\Offset) node {$z$};
\end{tikzpicture}
\end{document}
答案1
你不就是想失去单位吗?
\documentclass{article}
\usepackage{pgfplots}
\usepackage{xcolor}
\newcommand{\Offset}{.3}
\begin{document}
\begin{tikzpicture}[ultra thick, red, ->]
\draw (0,0) -- (1,0);
\path (1,0) ++ (\Offset,0) node {$x$};
\draw (0,0) -- (0,1);
\path (0,1) ++ (0,\Offset) node {$y$};
\end{tikzpicture}
\quad
\begin{tikzpicture}[ultra thick, blue, ->]
\draw (0,0,0) -- (1,0,0);
\path (1,0,0) ++ (\Offset,0,0) node {$x$};
\draw (0,0,0) -- (0,1,0);
\path (0,1,0) ++ (0,\Offset,0) node {$y$};
\draw (0,0,0) -- (0,0,1);
\path (0,0,1) ++ (0,0,\Offset) node {$z$};
\end{tikzpicture}
\end{document}
答案2
我定义了树函数(lengthto3dx
,lengthto3dy
和lengthto3dz
)来将长度转换为 x、y 或 z 单位。
\documentclass{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\makeatletter
\tikzset{
declare function={
lengthto3dx(\x) = \x/veclen(\the\pgf@xx,\the\pgf@xy);
lengthto3dy(\y) = \y/veclen(\the\pgf@yx,\the\pgf@yy);
lengthto3dz(\z) = \z/veclen(\the\pgf@zx,\the\pgf@zy);
},
}
\makeatother
\newcommand*{\Offset}{1ex}
\begin{document}
\begin{tikzpicture}[ultra thick, red, ->]
\draw (0,0) -- (1,0);
\path (1,0) ++ (\Offset,0) node {$x$};
\draw (0,0) -- (0,1);
\path (0,1) ++ (0,\Offset) node {$y$};
\end{tikzpicture}
\quad
\begin{tikzpicture}[ultra thick, blue, ->,x=.5cm,y=3cm,z=-2cm]
\draw (0,0,0) -- (1,0,0);
\path (1,0,0) ++ ({lengthto3dx(\Offset)},0,0) node {$x$};
\draw (0,0,0) -- (0,1,0);
\path (0,1,0) ++ (0,{lengthto3dy(\Offset)},0) node {$y$};
\draw (0,0,0) -- (0,0,1);
\path (0,0,1) ++ (0,0,{lengthto3dz(\Offset)}) node {$z$};
\end{tikzpicture}
\begin{tikzpicture}[ultra thick, blue, ->]
\draw (0,0,0) -- (1,0,0);
\path (1,0,0) ++ ({lengthto3dx(\Offset)},0,0) node {$x$};
\draw (0,0,0) -- (0,1,0);
\path (0,1,0) ++ (0,{lengthto3dy(\Offset)},0) node {$y$};
\draw (0,0,0) -- (0,0,1);
\path (0,0,1) ++ (0,0,{lengthto3dz(\Offset)}) node {$z$};
\end{tikzpicture}
\end{document}