防止子程序扩展

防止子程序扩展

我希望我的例程能够独立于主程序比例绘制长度。例如,在下面的示例中,我定义了一个例程,\test它在两点之间绘制一条线,但将一侧的线延长 30pt,并绘制与该线垂直的 30pt 线。

奇怪的是,shorten例程不会随 tikzpicture 缩放,但垂直线例程会。为什么会这样,如何防止垂直线例程缩放?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\test}[2]{%
\draw[shorten <=-30pt] #1 -- #2 node[pos=-0.1] {30pt};
\draw #1 -- ($#1!30pt!-90:#2$) node[midway] {30pt};
\draw[red] #1 -- #2;}

\begin{document}

\begin{tikzpicture}[auto,scale=10]
\draw[white] (-0.8,-0.8) rectangle (1,1);
\test{(0,0)}{(1,1)}
\end{tikzpicture}

\begin{tikzpicture}[auto,scale=3]
\draw[white] (-1,-1) rectangle (1,1);
\test{(0,0)}{(1,1)}
\end{tikzpicture}

\begin{tikzpicture}[auto,scale=1]
\draw[white] (-1.8,-1.8) rectangle (1,1);
\test{(0,0)}{(1,1)}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

缩放是针对坐标进行的,但(通常)不针对节点内容(可以更改)和长度值等进行缩放。这给出了和坐标计算line width之间的差异。shorten

您可以使用以下方法来防止扩展:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\test}[2]{%
\pgfmathsetlengthmacro\mylength{30pt/\myscale}%
\draw ($#1!-\mylength!#2$) -- #1 node[midway] {30pt};
\draw #1 -- ($#1!\mylength!-90:#2$) node[midway] {30pt};
\draw[red] #1 -- #2;}

\tikzset{myscale/.code={\edef\myscale{#1}\tikzset{scale=#1}}}
\def\myscale{1}

\begin{document}

\begin{tikzpicture}[auto,myscale=10]
\draw[white] (-0.8,-0.8) rectangle (1,1);
\test{(0,0)}{(1,1)}
\end{tikzpicture}

\begin{tikzpicture}[auto,myscale=3]
\draw[white] (-1,-1) rectangle (1,1);
\test{(0,0)}{(1,1)}
\end{tikzpicture}

\begin{tikzpicture}[auto,myscale=1]
\draw[white] (-1.8,-1.8) rectangle (1,1);
\test{(0,0)}{(1,1)}
\end{tikzpicture}

\end{document}

在此处输入图片描述

另一种方法是允许两条线缩放(这更容易):

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\test}[2]{%
\draw ($#1!-30pt!#2$) -- #1 node[midway] {30pt};
\draw #1 -- ($#1!30pt!-90:#2$) node[midway] {30pt};
\draw[red] #1 -- #2;}


\begin{document}

\begin{tikzpicture}[auto,scale=10]
\draw[white] (-0.8,-0.8) rectangle (1,1);
\test{(0,0)}{(1,1)}
\end{tikzpicture}

\begin{tikzpicture}[auto,scale=3]
\draw[white] (-1,-1) rectangle (1,1);
\test{(0,0)}{(1,1)}
\end{tikzpicture}

\begin{tikzpicture}[auto,scale=1]
\draw[white] (-1.8,-1.8) rectangle (1,1);
\test{(0,0)}{(1,1)}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容