我遇到了以下问题:我向路径添加了一个节点(使用 \draw 绘制),我希望该节点位于“中间”或“pos=.5”,但在以下示例中,该节点位于图形的第一个节点(rrhp)上,而不是圆弧上。我使用相同的加载库进行了其他绘图,定位成功,所以我想知道下面的代码有什么问题。
包含许多其他软件包,但它们似乎与 tikz 没有冲突,我包含了 pgfplots,尽管消除它并不能解决问题。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows,arrows,calc,backgrounds,fit,shapes,snakes,shapes.multipart,decorations.pathreplacing,shapes.misc,patterns,positioning}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[node distance=4mm]
\node[align=center] (rrhp) { Robust Relational \\ Hyperproperty Preservation};
\node[align=center, below = of rrhp.south east] (r2rsp) { Robust 2-Relational \\ Safety Preservation};
\node[align=center, below = of r2rsp.south, yshift=-3em] (fac2) { Fully Abstract\\ Compilation };
\draw[-] (rrhp.south) to (r2rsp.north west);
\draw[-, dotted] (r2rsp.south) to (fac2.north) node [right,align=left,pos=.5] { + determinacy } ;
\end{tikzpicture}
\end{document}
答案1
使用 Line-To operation
(< 坐标 > -- < 坐标 >) 代替To-Path operation
(< 坐标 > 到 < 坐标 >) 就足够了:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows,arrows,calc,backgrounds,fit,shapes,snakes,shapes.multipart,decorations.pathreplacing,shapes.misc,patterns,positioning}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[node distance=4mm]
\node[align=center] (rrhp) { Robust Relational \\ Hyperproperty Preservation};
\node[align=center, below = of rrhp.south east] (r2rsp) { Robust 2-Relational \\ Safety Preservation};
\node[align=center, below = of r2rsp.south, yshift=-3em] (fac2) { Fully Abstract\\ Compilation };
\draw[-] (rrhp.south) to (r2rsp.north west);
\draw[-, dotted] (r2rsp.south) -- (fac2.north) node [right,align=left,pos=.5] { + determinacy } ;
\end{tikzpicture}
\end{document}
第二种解决方案:
您使用的语法 To-Path operation
与手册不匹配(3.0.1.a 手册第 157 页)
\path ... to[options] < nodes > < coordinate or cycle > ...;
代码如下
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows,arrows,calc,backgrounds,fit,shapes,snakes,shapes.multipart,decorations.pathreplacing,shapes.misc,patterns,positioning}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[node distance=4mm]
\node[align=center] (rrhp) { Robust Relational \\ Hyperproperty Preservation};
\node[align=center, below = of rrhp.south east] (r2rsp) { Robust 2-Relational \\ Safety Preservation};
\node[align=center, below = of r2rsp.south, yshift=-3em] (fac2) { Fully Abstract\\ Compilation };
\draw[-] (rrhp.south) to (r2rsp.north west);
\draw[-, dotted] (r2rsp.south) to node [right,align=left,pos=.5] { + determinacy } (fac2.north);
\end{tikzpicture}
\end{document}
答案2
我将使用quotes
库并用它稍微简化图片代码:
\documentclass{article}
\usepackage{pgfplots} % <--- it load `tikz`
\usetikzlibrary{arrows,
backgrounds,
decorations.pathreplacing,
calc,
fit,
patterns, positioning,
quotes, % <--- added
shadows, shapes, shapes.multipart, shapes.misc, snakes
}
\begin{document}
\begin{tikzpicture}[node distance=4mm, align=center]
\node (rrhp) { Robust Relational \\ Hyperproperty Preservation};
\node [below=of rrhp.south east] (r2rsp) { Robust 2-Relational \\ Safety Preservation};
\node[below=12mm of r2rsp] (fac2) { Fully Abstract\\ Compilation };
%
\draw (rrhp.south) -- (r2rsp.north west); % <--- changed
\draw[dotted] (r2rsp) to ["+ determinacy"] (fac2); % <--- changed
\end{tikzpicture}
\end{document}