
我創建了一個宏\measurement
,可以讓我在繪圖中添加測量值。它運作良好,但標籤有時不穩定。在下面的範例中,即使命令實際上相同,測量標籤也會以不同的方式旋轉。我認為一定有更好的方法來控制標籤。有人有想法嗎?
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{myscale/.code={\edef\myscale{#1}\tikzset{scale=#1}}}
\def\myscale{1}
\newcommand{\measure}[4]{%
\pgfmathsetlengthmacro\mylength{8pt/\myscale}
\draw[very thin] #1 -- ($#1!\mylength!-90:#2$) coordinate (a) -- ($#1!\mylength+0.4*\mylength!-90:#2$);
\draw[very thin] #2 -- ($#2!\mylength!90:#1$) coordinate (b) -- ($#2!\mylength+0.4*\mylength!90:#1$);
\draw[very thin,<->,>=stealth] (a) -- (b) node[auto,sloped,#3] {#4};
}
\begin{document}
\begin{tikzpicture}[myscale=0.12]
\begin{scope}[rotate=20]
\draw[red] (0,0) -- (0,12);
\measure{(0,0)}{(0,12)}{midway,above,rotate=20,font=\small}{red}
\draw[blue] (0,12) -- (0,24);
\measure{(0,12)}{(0,24)}{midway,above,rotate=20,font=\small}{blue}
\end{scope}
\end{tikzpicture}
\end{document}
答案1
你有一條垂直線(除了四捨五入之外),然後將其旋轉 20 度。根據捨去情況,線會稍微向左傾斜或向右傾斜一點,節點會自然翻轉。
我沒有嘗試調查你想要做什麼或如何最好地做到這一點 - 也許是另一個問題。
所以答案是:垂直使其不穩定。
要查看效果,請嘗試以下程式碼:
\documentclass[tikz, border=1cm]{standalone}
\tikzset{
nodetest/.pic={
\coordinate (c) at (1,10); %intermediate coordinate to create rounding
\draw (1,0) -- (c) node[auto, sloped, midway] {test};
}}
\begin{document}
\begin{tikzpicture}
\foreach \pos in {0.01,0.02,...,2}
\pic[scale=\pos] at (10*\pos,0) {nodetest};
\end{tikzpicture}
\end{document}
從 (0,0) 到 (1,10) 的縮放線:
從 (2,0) 到 (1,10) 的縮放線:
從 (1,0) 到 (1,10) 的縮放線:
可以看出,中間節點的暗帶對於垂直線來說是不穩定的。
編輯:
我只能猜測你想要什麼。下圖顯示了從第一個座標讀取到第二個座標的平行線下方的節點。它在所有尺度上都是穩定的。
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\foreach \ang in {0,20,...,340}{
\coordinate (a) at (\ang:1);
\coordinate (b) at (\ang:4);
\draw[Stealth-Stealth] (a) --node[sloped, auto, swap, allow upside down=true]{red} (b);
}
\end{tikzpicture}
\end{document}
答案2
也許這有幫助 - 與tkz-euclide
微量元素
\documentclass{article}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\tkzDefPoints{0/0/A,-4/4/B}
\tkzDefPointWith[linear,K=0.5](A,B)
\tkzGetPoint{C}
\tkzDrawSegment[red, very thick](A,C)
\tkzDrawSegment[blue,very thick](B,C)
\tkzDrawPoints[color=black](A,B,C)
\tkzLabelPoints[above right=3pt](A,B,C)
\tkzDrawSegment[style=red, dashed, dim={$10$,15pt,midway,font=\scriptsize, rotate=45}](A,C)
\tkzDrawSegment[style=blue, dashed, dim={$10$,15pt,midway,font=\scriptsize, rotate=45}](C,B)
\end{tikzpicture}
\end{document}