在 3d tikz 中按 ex 单位进行移位

在 3d tikz 中按 ex 单位进行移位

在二维坐标(红色图像)中,我可以使用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

我定义了树函数(lengthto3dxlengthto3dylengthto3dz)来将长度转换为 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}

相关内容