有没有办法将 tikz 范围缩放到另一个 tikz 范围的宽度?我有一种方法,将每个范围放在一个单独的 tikzpicture 中,该图片包含在一个调整大小的框中。只要在这些各自的范围内的坐标之间没有要绘制的线,这种方法就可以正常工作。如果我在重新缩放后在两个坐标(每个范围内一个坐标)之间画一条线,坐标就不再位于其原始位置,并且线处于错误的位置。
如果我能够根据另一个范围(或线宽)缩放一个范围,我可以将所有内容放在一个 tikzpicture 中,并且不会出现错误坐标的问题。
我有一个代码示例(未缩放和缩放):
代码:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[remember picture]
\begin{scope}
\node[circle, thick, minimum width = 3cm, color=blue, draw] (n1) {};
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}[remember picture]
\begin{scope}
\node[circle, thick, minimum width = 2cm, color=green, draw] (n2) {};
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}[overlay, remember picture]
\path[thick, red, draw] (n1.east) edge (n2.east);
\end{tikzpicture}
\resizebox{0.5\textwidth}{!}{
\begin{tikzpicture}[remember picture]
\begin{scope}
\node[circle, thick, minimum width = 3cm, color=blue, draw] (s1) {};
\end{scope}
\end{tikzpicture}
}
\resizebox{0.5\textwidth}{!}{
\begin{tikzpicture}[remember picture]
\begin{scope}
\node[circle, thick, minimum width = 2cm, color=green, draw] (s2) {};
\end{scope}
\end{tikzpicture}
}
\begin{tikzpicture}[overlay, remember picture]
\path[thick, red, draw] (s1.east) edge (s2.east);
\end{tikzpicture}
\end{document}
答案1
我似乎找到了一种解决方案,需要使用假节点来获取稍后要缩放的范围或节点的原始宽度。假节点的宽度用于计算两个范围的比例,然后将其中一个范围缩放为另一个范围的宽度。
代码:
\documentclass{article}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgf}
\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}%
\newlength{\firstscope}
\newlength{\secondscope}
\begin{document}
\resizebox{0.5\linewidth}{!}{
\begin{tikzpicture}
\begin{scope}
\node[circle, thick, minimum width = 3cm, color=blue, draw] (t1) {};
\end{scope}
\coordinate (width1) at ($(t1.east)-(t1.west)$);
\ExtractCoordinate{width1}
\setlength{\firstscope}{\XCoord}
% get fake node to determine width of scope
\node[circle, thick, minimum width = 2cm, color=black!0, draw, opacity=0] (t2) {};
\coordinate (width2) at ($(t2.east)-(t2.west)$);
\ExtractCoordinate{width2}
\setlength{\secondscope}{\XCoord}
\pgfmathsetmacro{\ratio}{\firstscope/\secondscope}
\begin{scope}[scale=\ratio, every node/.append style={scale=\ratio}, yshift=-70]
\node[circle, thick, minimum width = 2cm, color=green, draw] (t2) {};
\end{scope}
\node at ($(t1)!0.5!(t2)$) {ratio=\ratio};
\path[ultra thick, red, draw] (t1.east) edge (t2.east);
\end{tikzpicture}
}
\end{document}
结果如下所示(指定原始比例):