我创建了一个宏,用于绘制不同类型的 3D 坐标(此处为:sistemaTd 和 sistemaT2)。当我使用 tikz 中包含的系统时,Y 轴的图例处于错误的位置。
我在各种文件中使用了\eixo
数百次,所以我想找到一种不改变其参数数量的方法
我认为我需要类似这样的定义\eixo
:
IF #2 contains the chain "sistema" DO pos="below" ELSE pos="right" ENDIF
这是示例:
\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}
\tikzset{sistemaTd/.style={y={(1 cm,0 cm)},z={(0 cm, 1 cm)},x={(-0.5 cm, -0.5 cm)}}}
\tikzset{sistemaTe/.style={x={(0 cm,1 cm)},y={(1 cm, 0 cm)},z={(-0.5 cm, -0.5 cm)}}}
%eixo{dim}{color, etc} Ejemplo: \texttt{eixo}{black!80,sistemaTd,-latex...}
\newcommand{\eixo}[2]{
\begin{scope}[->,#2]
\draw (0,0,0) -- (#1,0,0) node[right]{$X$};
\draw (0,0,0) -- (0,#1,0) node[below]{$Y$}; %node[below]{$Y$}
\draw (0,0,0) -- (0,0,#1) node[right]{$Z$};
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\eixo{3}{gray,sistemaTd}
\begin{scope}[xshift=5.5cm]
\eixo{3}{gray,sistemaTe}
\end{scope}
\begin{scope}[xshift=11cm]
\eixo{3}{gray} %note the legend of Y axis
\end{scope}
\end{tikzpicture}
\end{document}
答案1
这里我使用listofitems
来搜索#2
字符串分隔符sistema
。如果找到它(列表长度 > 1 即可证明),则使用below
说明符,否则使用right
。
\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen,listofitems}
\tikzset{sistemaTd/.style={y={(1 cm,0 cm)},z={(0 cm, 1 cm)},x={(-0.5 cm, -0.5 cm)}}}
\tikzset{sistemaTe/.style={x={(0 cm,1 cm)},y={(1 cm, 0 cm)},z={(-0.5 cm, -0.5 cm)}}}
%eixo{dim}{color, etc} Ejemplo: \texttt{eixo}{black!80,sistemaTd,-latex...}
\newcommand{\eixo}[2]{
\begin{scope}[->,#2]
\draw (0,0,0) -- (#1,0,0) node[right]{$X$};
\setsepchar{sistema}%
\readlist\argtwo{#2}%
\ifnum\listlen\argtwo[]>1\relax
\draw (0,0,0) -- (0,#1,0) node[below]{$Y$};
\else
\draw (0,0,0) -- (0,#1,0) node[right]{$Y$};
\fi
\draw (0,0,0) -- (0,0,#1) node[right]{$Z$};
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\eixo{3}{gray,sistemaTd}
\begin{scope}[xshift=5.5cm]
\eixo{3}{gray,sistemaTe}
\end{scope}
\begin{scope}[xshift=11cm]
\eixo{3}{gray} %note the legend of Y axis
\end{scope}
\end{tikzpicture}
\end{document}
如果我正确理解了这个问题的话,这里有一个版本,它也能调整 X 标签位置:
\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen,listofitems}
\tikzset{sistemaTd/.style={y={(1 cm,0 cm)},z={(0 cm, 1 cm)},x={(-0.5 cm, -0.5 cm)}}}
\tikzset{sistemaTe/.style={x={(0 cm,1 cm)},y={(1 cm, 0 cm)},z={(-0.5 cm, -0.5 cm)}}}
%eixo{dim}{color, etc} Ejemplo: \texttt{eixo}{black!80,sistemaTd,-latex...}
\newcommand{\eixo}[2]{
\begin{scope}[->,#2]
\setsepchar{sistema}%
\readlist\argtwo{#2}%
\ifnum\listlen\argtwo[]>1\relax
\draw (0,0,0) -- (#1,0,0) node[right]{$X$};
\draw (0,0,0) -- (0,#1,0) node[below]{$Y$};
\else
\draw (0,0,0) -- (#1,0,0) node[below]{$X$};
\draw (0,0,0) -- (0,#1,0) node[right]{$Y$};
\fi
\draw (0,0,0) -- (0,0,#1) node[right]{$Z$};
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\eixo{3}{gray,sistemaTd}
\begin{scope}[xshift=5.5cm]
\eixo{3}{gray,sistemaTe}
\end{scope}
\begin{scope}[xshift=11cm]
\eixo{3}{gray} %note the legend of Y axis
\end{scope}
\end{tikzpicture}
\end{document}
答案2
您可以calc
现场检查(使用)轴是水平还是垂直,并根据该信息附加标签。这样,如果您添加更多坐标系和/或更改其定义,这将继续起作用。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc}
\tikzset{sistemaTd/.style={y={(1 cm,0 cm)},z={(0 cm, 1 cm)},x={(-0.5 cm, -0.5 cm)}}}
\tikzset{sistemaTe/.style={x={(0 cm,1 cm)},y={(1 cm, 0 cm)},z={(-0.5 cm, -0.5 cm)}}}
%eixo{dim}{color, etc} Ejemplo: \texttt{eixo}{black!80,sistemaTd,-latex...}
\newcommand{\eixo}[2]{
\begin{scope}[->,#2]
\draw (0,0,0) -- (#1,0,0)
let \p1=($(#1,0,0)-(0,0,0)$),\n1={abs(sin(atan2(\y1,\x1))))} in
\ifdim\n1<0.4pt [below] \else [right] \fi node {$X$};
\draw (0,0,0) -- (0,#1,0) let \p1=($(0,#1,0)-(0,0,0)$),\n1={abs(sin(atan2(\y1,\x1))))} in
\ifdim\n1<0.4pt [below] \else [right] \fi node {$Y$};
\draw (0,0,0) -- (0,0,#1) let \p1=($(0,0,#1)-(0,0,0)$),\n1={abs(sin(atan2(\y1,\x1))))} in
\ifdim\n1<0.4pt [below] \else [right] \fi node{$Z$};
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\eixo{3}{gray,sistemaTd}
\begin{scope}[xshift=5.5cm]
\eixo{3}{gray,sistemaTe}
\end{scope}
\begin{scope}[xshift=11cm]
\eixo{3}{gray} %note the legend of Y axis
\end{scope}
\end{tikzpicture}
\end{document}