考虑这些平方节点:
\begin{tikzpicture}
\node [draw] (A) at (0,0) {demo text};
\node [draw] (B) at (0,-2) {demo text};
\end{tikzpicture}
我想用两条偏移线将它们连接起来。如果 A 和 B 是两个数字坐标,我可以简单地这样做:
\draw [xshift=1em] (0,0) -- (0,-2);
\draw [xshift=-1em] (0,0) -- (0,-2);
不幸的是,此代码根本没有移动线路:
\draw [xshift=1em] (A) --(B);
经过反复尝试,我发现可以这样做:
\begin{tikzpicture}
\node [draw] (A) at (0,0) {demo text};
\node [draw] (B) at (0,-2) {demo text};
\draw ([xshift=1em]A.south) -- ([xshift=1em]B.north);
\draw ([xshift=-1em]A.south) -- ([xshift=-1em]B.north);
\end{tikzpicture}
我必须设置锚点并移动每个点。对于这么简单的任务来说,这似乎太过繁琐了。
您能建议一个更好的方法吗?
干杯。
答案1
问题在于,固定节点或坐标值不受后续变换的影响。例如,scale=3
也不起作用。因此,xshift
如果给定的坐标不固定到节点或坐标,则 有效,即类似(3,4)
有效。
如果您绘制的路径与图形的边界框无关,您可以在低级绘图时强制进行移位,因为以下内容不会影响边界框。
\begin{tikzpicture}
\node [draw] (A) at (0,0) {demo text};
\node [draw] (B) at (0,-2) {demo text};
\draw[transform canvas={xshift=1em}] (A) -- (B);
\end{tikzpicture}
答案2
一种可能性是:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node [draw] (A) at (0,0) {demo text};
\node [draw] (B) at (0,-2) {demo text};
\draw (A.215) -- (B.145);
\draw (A.325) -- (B.35);
\end{tikzpicture}
\end{document}
还有一个,这次使用垂直坐标系,所以只需要两个角度:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node [draw] (A) at (0,0) {demo text};
\node [draw] (B) at (0,-2) {demo text};
\draw (A.215) -- (A.215|-B.north);
\draw (A.325) -- (A.325|-B.north);
\end{tikzpicture}
\end{document}
答案3
这是通过 得到的解决方案double
。我叠加了虚线来比较您的解决方案(黑色)和我的(红色)。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[line width=1pt]
\node [draw] (A) at (0,0) {demo text};
\node [draw] (B) at (0,-2) {demo text};
% method via double
\draw[red,double=white,double distance=2em-\pgflinewidth] (A) -- (B);
% your method
\draw[dashed] ([xshift=1em]A.south) -- ([xshift=1em]B.north);
\draw[dashed] ([xshift=-1em]A.south) -- ([xshift=-1em]B.north);
\end{tikzpicture}
\end{document}
三重变化
\documentclass[tikz]{standalone}
\tikzset{
triple/.style={
double=white,double distance=2em-\pgflinewidth,
postaction={draw},
},
}
\begin{document}
\begin{tikzpicture}[line width=1pt]
\node [draw] (A) at (0,0) {demo text};
\node [draw] (B) at (0,-2) {demo text};
% method via triple
\draw[red,triple] (A) -- (B);
% your method (triple variation)
\draw[dashed] ([xshift=1em]A.south) -- ([xshift=1em]B.north);
\draw[dashed] ([xshift=-1em]A.south) -- ([xshift=-1em]B.north);
\draw[dashed] (A) -- (B);
\end{tikzpicture}
\end{document}