我正在尝试在 TikZ 中制作这张图片:
我写
\documentclass{standalone}
\usepackage{tikz}
\newcommand*{\rinn}{2}
\newcommand*{\rout}{3.8}
\newcommand*{\tangle}{37}
\newcommand*{\sangle}{-30}
\begin{document}
\begin{tikzpicture}
\coordinate [label=above:$T_1$] (T1) at (\tangle:\rinn);
\coordinate [label=above right:$T_2$] (T2) at (\tangle:\rout);
\coordinate [label=below right:$S_1$] (S1) at (\sangle:\rinn);
\coordinate [label=below right:$S_2$] (S2) at (\sangle:\rout);
\draw (0,0) circle [radius=\rinn];
\draw (0,0) circle [radius=\rout];
\draw (0,0) -- (T2);
\draw (0,0) -- (S2);
\end{tikzpicture}
\end{document}
看起来像
图像是自动的,但每次发生变化时标签位置都需要调整,即使这样,它们的位置也不太好。
\newcommand
为 TikZ 定义变量的最佳方法是什么?- 有没有办法调整标签位置,以使其始终与其他线条保持距离?
答案1
这是一个可能的解决方案:
\documentclass[tikz]{standalone}
\begin{document}
\foreach {\tangle} in {80,79,...,10}{
\pgfmathsetmacro{\sangle}{-\tangle}
\begin{tikzpicture}[auto]
% constants
\def\rinn{2}
\def\rout{3.8}
% just to fix the global bounding box
\path circle[radius=\rout cm+5mm+2em];
\draw (0,0) circle [radius=\rinn];
\draw (0,0) circle [radius=\rout];
\coordinate (T1) at (\tangle:\rinn);
\coordinate (T2) at (\tangle:\rout);
\coordinate (S1) at (\sangle:\rinn);
\coordinate (S2) at (\sangle:\rout);
\draw (0,0) -- (T1) -- (T2);
\draw (0,0) -- (S1) -- (S2);
\path (T1) ++(\tangle+45:5mm) node{T1};
\path (T2) ++(\tangle+45:5mm) node{T2};
\path (S1) ++(\sangle-45:5mm) node{S1};
\path (S2) ++(\sangle-45:5mm) node{S2};
\end{tikzpicture}
}
\end{document}
答案2
不是完美的解决方案...
\documentclass[tikz,border=3mm]{standalone}
\usepackage{}
\newcommand*{\rinn}{2}
\newcommand*{\rout}{3.8}
\newcommand*{\tangle}{37}
\newcommand*{\sangle}{-30}
\begin{document}
\begin{tikzpicture}[auto]
\coordinate (T1) at (\tangle:\rinn);
\coordinate [label=\tangle:$T_2$] (T2) at (\tangle:\rout);
\coordinate (S1) at (\sangle:\rinn);
\coordinate [label=\sangle:$S_2$] (S2) at (\sangle:\rout);
\draw (0,0) circle [radius=\rinn];
\draw (0,0) circle [radius=\rout];
\draw (0,0) -- (T1) -- node[near start] {T1} (T2);
\draw (0,0) -- (S1) -- node[near start] {S1} (S2);
\end{tikzpicture}
\end{document}