当我scale
在 tikzpictures 中使用(不带transform shape
)时,节点中的文本大小不会改变(这很好)。现在假设我想使用与节点字体成比例的距离将某个节点放在缩放图中;我天真地以为我可以使用ex
坐标,但请参见下文...
我还打印了坐标值,并且\pgf@yy
这应该给出单位向量,但这里有一些东西我错过了……
有没有一种方法可以表达与(默认)字体大小成比例的距离,而与比例无关?
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newdimen\mydimA\newdimen\mydimB
\makeatletter
\newcommand{\showat}[1]{%
\pgfextracty\mydimA{\pgfpointanchor{A}{center}}
\pgfextracty\mydimB{\pgfpointanchor{B}{center}}
\node[red, font=\tiny, align=left] at(#1) {Before \the\mydimA \\ After \the\mydimB \\
Scale y \the\pgf@yy};
}
\makeatother
\begin{document}
\begin{tikzpicture}[baseline]
\draw (0,0) --(1,0) coordinate(A) -- (2,0);
\path (A) ++(0,1ex) coordinate(B);
\node [anchor=base] at (B) {$R_g$};
\showat{1,-1}
\end{tikzpicture}
\begin{tikzpicture}[baseline, scale=2]
\draw (0,0) --(0.5,0) coordinate(A) -- (1,0);
\path (A) ++(0,1ex) coordinate(B);
\node [anchor=base] at (B) {$R_g$};
\showat{0.5,-0.5}
\end{tikzpicture}
\begin{tikzpicture}[baseline, scale=0.01]
\draw (0,0) --(100,0) coordinate(A) -- (200,0);
\path (A) ++(0,1ex) coordinate(B);
\node [anchor=base] at (B) {$R_g$};
\showat{100,-100}
\end{tikzpicture}
\end{document}
答案1
您的1em
坐标系在读取时被转换为坐标系,然后受到图片比例的影响。您可以使用reset cm
局部重置坐标变换矩阵,从而局部取消图片比例效果。
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[baseline]
\draw (0,0) --(1,0) coordinate(A) -- (2,0);
\path (A) ++(0,1em) coordinate(B);
\node [anchor=base] at (B) {$R_g$};
\end{tikzpicture}
\begin{tikzpicture}[baseline, scale=2]
\draw (0,0) --(0.5,0) coordinate(A) -- (1,0);
\path[reset cm] (A) ++(0,1em) coordinate(B);
\node [anchor=base] at (B) {$R_g$};
\end{tikzpicture}
\begin{tikzpicture}[baseline, scale=0.01]
\draw (0,0) --(100,0) coordinate(A) -- (200,0);
\path[reset cm] (A) ++(0,1em) coordinate(B);
\node [anchor=base] at (B) {$R_g$};
\end{tikzpicture}
\end{document}
答案2
在本地重置规模是一种选择(参见 Marsden 的回答),但还有更好的方法可以解决问题。
和scale=factor
,全部坐标按 缩放factor
,无论其维度如何(而单位向量保持不变,正如您所发现的)。相反,根据所需的缩放比例重新定义单位向量的长度。例如,当y={(0cm,2cm)}
时,虽然(0,0) -- (1,0)
将是通常距离的两倍,但具有诸如 等维度的坐标(0,1ex)
将保持不变:
\begin{document}
\begin{tikzpicture}[baseline]
\draw (0,0) --(1,0) coordinate(A) -- (2,0);
\path (A) ++(0,1ex) coordinate(B);
\node [anchor=base] at (B) {$R_g$};
\showat{1,-1cm}
\end{tikzpicture}
\begin{tikzpicture}[baseline, x={(2cm,0cm)},y={(0cm,2cm)}]
% Identical code to previous picture:
\draw (0,0) --(1,0) coordinate(A) -- (2,0);
\path[scale=1] (A) ++(0,1ex) coordinate(B);
\node [anchor=base] at (B) {$R_g$};
\showat{1,-1cm}
\end{tikzpicture}
\end{document}
输出:
A
请注意和之间的距离B
没有变化,而却\the\pgf@yy
增加了一倍,正如预期的那样。
答案3
为什么不把 B 本身作为一个节点(相对位置)?
那将解决你所有的问题。
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\newdimen\mydimA\newdimen\mydimB
\makeatletter
\newcommand{\showat}[1]{%
\pgfextracty\mydimA{\pgfpointanchor{A}{center}}
\pgfextracty\mydimB{\pgfpointanchor{B}{center}}
\node[red, font=\tiny, align=left] at(#1) {Before \the\mydimA \\ After \the\mydimB \\
Scale y \the\pgf@yy};
}
\makeatother
\begin{document}
\tikzset{node distance=1ex}
\begin{tikzpicture}[baseline]
\draw (0,0) --(1,0) coordinate(A) -- (2,0);
\node[above=of A,anchor=base](B){$R_g$};
\showat{1,-1}
\end{tikzpicture}
\begin{tikzpicture}[baseline, scale=2]
\draw (0,0) --(0.5,0) coordinate(A) -- (1,0);
%\path (A) ++(0,1ex) coordinate(B)node{y};
\node[above=of A,anchor=base] (B) {$R_g$};
\showat{0.5,-0.5}
\end{tikzpicture}
\begin{tikzpicture}[baseline, scale=0.01]
\draw (0,0) --(100,0) coordinate(A) -- (200,0);
%\path (A) ++(0,1ex) coordinate(B)node{y};
\node[above=of A,anchor=base] (B) {$R_g$};
\showat{100,-100}
\end{tikzpicture}
\end{document}
答案4
Eric Marsden 的回答很好地解释了发生了什么。但是,我只需读出变换矩阵的相关条目(它是 22 个条目)并“反转”它。这也是我用来缩放线宽和形状的技巧(当然没有反转),因为默认情况下它不会缩放。
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newdimen\mydimA\newdimen\mydimB
\makeatletter
\newcommand{\showat}[1]{%
\pgfextracty\mydimA{\pgfpointanchor{A}{center}}
\pgfextracty\mydimB{\pgfpointanchor{B}{center}}
\node[red, font=\tiny, align=left] at(#1) {Before \the\mydimA \\ After \the\mydimB \\
Scale y \the\pgf@yy};
}
\makeatother
\begin{document}
\begin{tikzpicture}[baseline]
\draw (0,0) --(1,0) coordinate(A) -- (2,0);
\pgfgettransformentries{\tmp}{\tmp}{\tmp}{\myscale}{\tmp}{\tmp}
\path (A) ++(0,1ex/\myscale) coordinate(B);
\node [anchor=base] at (B) {$R_g$};
\showat{1,-1}
\end{tikzpicture}
\begin{tikzpicture}[baseline, scale=2]
\draw (0,0) --(0.5,0) coordinate(A) -- (1,0);
\pgfgettransformentries{\tmp}{\tmp}{\tmp}{\myscale}{\tmp}{\tmp}
\path (A) ++(0,1ex/\myscale) coordinate(B);
\node [anchor=base] at (B) {$R_g$};
\showat{0.5,-0.5}
\end{tikzpicture}
\begin{tikzpicture}[baseline, scale=0.01]
\draw (0,0) --(100,0) coordinate(A) -- (200,0);
\pgfgettransformentries{\tmp}{\tmp}{\tmp}{\myscale}{\tmp}{\tmp}
\path (A) ++(0,1ex/\myscale) coordinate(B);
\node [anchor=base] at (B) {$R_g$};
\showat{100,-100}
\end{tikzpicture}
\end{document}