剧情

剧情

剧情

使用 tikz,我有一个包含几行(三行)文本的主节点,以及三个指向它们的“图例”节点。

如果我使用rectangle节点,那么我就无法访问各个线条的位置,因此我只能将图例附加到主节点本身,而不能将它们与各自的线条垂直对齐:

包含三条线的矩形节点,整个节点具有独特的图例。

如果我使用一个matrix节点,每行都在一个单独的单元格中,那么行间距就是错误的。我可以添加row sep=42pt,但我不知道用哪个值来代替 42:

矩阵节点的每一行都有一个单元格,每一行都有一个图例,这些行粘在一起而没有任何垂直间距。

作为最后的手段,我\baselineskip为除第一条线之外的每一条线(单元格)添加了一个高度为的隐形规则,这似乎给出了与矩形节点相同的结果,只是我可以访问各个线的位置:

矩阵节点每行一个单元格,每行一个图例,垂直间距与矩形节点相同。

然而,如果该线包含方程式或任何使其高于常规线的东西,这种方法就不能很好地发挥作用。这个 TeX.sx 答案表明该情况下的行间空间由\lineskip和给出\lineskiplimit,但我真的很想使用 tikz 重新实现这个想法——我必须承认,我甚至不知道从哪里开始。

问题

有没有更简单/更强大的方法来访问 tikz 中的每一条单独的线,node就好像该线是一个矩形节点一样?

代码

前言

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, matrix}
\def\legendlines#1#2{
  \draw (#1) -- (#2);
  \draw (#2.north west) -- (#2.south west);
}
\begin{document}
\tikzset{
  mynode/.style={draw,fill=blue!30,align=center},
  mylegend/.style={align=left, font=\scriptsize, inner sep=1pt}
}

矩形节点

% I can't access the position of each line using a rectangle node
\begin{tikzpicture}
  \node[mynode] (N123) {Node line one\\Node line two\\Node line three};
  \node[mylegend, xshift=1cm, at=(N123.east), anchor=west] (L123) {Legend 1\\Legend 2\\Legend 3};
  \legendlines{N123}{L123}
\end{tikzpicture}

矩阵节点

% Using a matrix, the inter-line space is wrong. I can add row sep=42pt, but I don't know what is the correct value.
\begin{tikzpicture}
  \node[mynode, matrix, matrix of nodes, nodes={inner sep=0pt}] (N123) {Node line one\\Node line two\\Node line three\\};
  % Coordinates along the east side of N123
  \coordinate (N1east) at (N123-1-1.east -| N123.east);
  \coordinate (N2east) at (N123-2-1.east -| N123.east);
  \coordinate (N3east) at (N123-3-1.east -| N123.east);
  % Legend texts
  \node[mylegend, right=1.0cm of N1east] (L1) {Legend 1};
  \node[mylegend, right=1.2cm of N2east] (L2) {Legend 2};
  \node[mylegend, right=1.4cm of N3east] (L3) {Legend 3};
  % Legend lines
  \legendlines{N1east}{L1}
  \legendlines{N2east}{L2}
  \legendlines{N3east}{L3}
\end{tikzpicture}

隐形规则

% Using an invisible rule
\begin{tikzpicture}
  \node[mynode, matrix, matrix of nodes, nodes={inner sep=0pt}] (N123) {Node line one\\\rule{0cm}{\baselineskip}Node line two\\\rule{0cm}{\baselineskip}Node line three\\};
  % Coordinates along the east side of N123
  \coordinate (N1east) at (N123-1-1.east -| N123.east);
  \coordinate (N2east) at (N123-2-1.east -| N123.east);
  \coordinate (N3east) at (N123-3-1.east -| N123.east);
  % Legend texts
  \node[mylegend, right=1.0cm of N1east] (L1) {Legend 1};
  \node[mylegend, right=1.2cm of N2east] (L2) {Legend 2};
  \node[mylegend, right=1.4cm of N3east] (L3) {Legend 3};
  % Legend lines
  \legendlines{N1east}{L1}
  \legendlines{N2east}{L2}
  \legendlines{N3east}{L3}
\end{tikzpicture}

结语

\end{document}

完整代码

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, matrix}
\def\legendlines#1#2{
  \draw (#1) -- (#2);
  \draw (#2.north west) -- (#2.south west);
}
\begin{document}
\tikzset{
  mynode/.style={draw,fill=blue!30,align=center},
  mylegend/.style={align=left, font=\scriptsize, inner sep=1pt}
}

% I can't access the position of each line using a rectangle node
\begin{tikzpicture}
  \node[mynode] (N123) {Node line one\\Node line two\\Node line three};
  \node[mylegend, xshift=1cm, at=(N123.east), anchor=west] (L123) {Legend 1\\Legend 2\\Legend 3};
  \legendlines{N123}{L123}
\end{tikzpicture}

% Using a matrix, the inter-line space is wrong. I can add row sep=42pt, but I don't know what is the correct value.
\begin{tikzpicture}
  \node[mynode, matrix, matrix of nodes, nodes={inner sep=0pt}] (N123) {Node line one\\Node line two\\Node line three\\};
  % Coordinates along the east side of N123
  \coordinate (N1east) at (N123-1-1.east -| N123.east);
  \coordinate (N2east) at (N123-2-1.east -| N123.east);
  \coordinate (N3east) at (N123-3-1.east -| N123.east);
  % Legend texts
  \node[mylegend, right=1.0cm of N1east] (L1) {Legend 1};
  \node[mylegend, right=1.2cm of N2east] (L2) {Legend 2};
  \node[mylegend, right=1.4cm of N3east] (L3) {Legend 3};
  % Legend lines
  \legendlines{N1east}{L1}
  \legendlines{N2east}{L2}
  \legendlines{N3east}{L3}
\end{tikzpicture}

% Using an invisible rule
\begin{tikzpicture}
  \node[mynode, matrix, matrix of nodes, nodes={inner sep=0pt}] (N123) {Node line one\\\rule{0cm}{\baselineskip}Node line two\\\rule{0cm}{\baselineskip}Node line three\\};
  % Coordinates along the east side of N123
  \coordinate (N1east) at (N123-1-1.east -| N123.east);
  \coordinate (N2east) at (N123-2-1.east -| N123.east);
  \coordinate (N3east) at (N123-3-1.east -| N123.east);
  % Legend texts
  \node[mylegend, right=1.0cm of N1east] (L1) {Legend 1};
  \node[mylegend, right=1.2cm of N2east] (L2) {Legend 2};
  \node[mylegend, right=1.4cm of N3east] (L3) {Legend 3};
  % Legend lines
  \legendlines{N1east}{L1}
  \legendlines{N2east}{L2}
  \legendlines{N3east}{L3}
\end{tikzpicture}
\end{document}

答案1

这是使用\subnode实验tikzmark包的解决方案。在这种情况下,“实验”表示“尚未在 CTAN 上”:您需要从TeX-SX 启动板站点。下载文件tikzmark.dtx并运行tex tikzmark.dtx(如果运行latex或者pdflatex它会提示缺少文件 - 忽略它)。将生成的文件放在tex可以找到它们的地方。

这是您的最后代码\subnode

\documentclass{article}
%\url{http://tex.stackexchange.com/q/86456/86}
\usepackage{tikz}
\usetikzlibrary{positioning, matrix,tikzmark}
\def\legendlines#1#2{
  \draw (#1) -- (#2);
  \draw (#2.north west) -- (#2.south west);
}
\begin{document}
\tikzset{
  mynode/.style={draw,fill=blue!30,align=center},
  mylegend/.style={align=left, font=\scriptsize, inner sep=1pt}
}
% I can't access the position of each line using a rectangle node
\begin{tikzpicture}
  \node[mynode] (N123) {Node line one\\Node line two\\Node line three};
  \node[mylegend, xshift=1cm, at=(N123.east), anchor=west] (L123) {Legend 1\\Legend 2\\Legend 3};
  \legendlines{N123}{L123}
\end{tikzpicture}

% Using a matrix, the inter-line space is wrong. I can add row sep=42pt, but I don't know what is the correct value.
\begin{tikzpicture}
  \node[mynode, matrix, matrix of nodes, nodes={inner sep=0pt}] (N123) {Node line one\\Node line two\\Node line three\\};
  % Coordinates along the east side of N123
  \coordinate (N1east) at (N123-1-1.east -| N123.east);
  \coordinate (N2east) at (N123-2-1.east -| N123.east);
  \coordinate (N3east) at (N123-3-1.east -| N123.east);
  % Legend texts
  \node[mylegend, right=1.0cm of N1east] (L1) {Legend 1};
  \node[mylegend, right=1.2cm of N2east] (L2) {Legend 2};
  \node[mylegend, right=1.4cm of N3east] (L3) {Legend 3};
  % Legend lines
  \legendlines{N1east}{L1}
  \legendlines{N2east}{L2}
  \legendlines{N3east}{L3}
\end{tikzpicture}

% Using an invisible rule
\begin{tikzpicture}
  \node[mynode, matrix, matrix of nodes, nodes={inner sep=0pt}] (N123) {Node line one\\\rule{0cm}{\baselineskip}Node line two\\\rule{0cm}{\baselineskip}Node line three\\};
  % Coordinates along the east side of N123
  \coordinate (N1east) at (N123-1-1.east -| N123.east);
  \coordinate (N2east) at (N123-2-1.east -| N123.east);
  \coordinate (N3east) at (N123-3-1.east -| N123.east);
  % Legend texts
  \node[mylegend, right=1.0cm of N1east] (L1) {Legend 1};
  \node[mylegend, right=1.2cm of N2east] (L2) {Legend 2};
  \node[mylegend, right=1.4cm of N3east] (L3) {Legend 3};
  % Legend lines
  \legendlines{N1east}{L1}
  \legendlines{N2east}{L2}
  \legendlines{N3east}{L3}
\end{tikzpicture}

\begin{tikzpicture}[remember picture]
  \node[mynode] (N123) {\subnode{mynode-1}{Node line one}\\\subnode{mynode-2}{Node line two}\\\subnode{mynode-3}{Node line three}};
  \coordinate (L123) at=(N123.east);
  \foreach \k in {1,2,3} {
  \node[mylegend,xshift=\k cm,anchor=base west] (L\k) at (mynode-\k.base -| L123) {Legend \k};
  \legendlines{mynode-\k.mid west -| N123.mid east}{L\k}
}
\end{tikzpicture}

\end{document}

得出的结果为:

节点的子节点划分

答案2

矩阵是恕我直言最好的解决方案。如果你不想让所有行都乱七八糟,为什么要放node={inner sep=0pt}?删除此选项会导致:

结果

正如 OP 在评论中指出的那样,上述解决方案比标准矩形解决方案产生更多的填充。可以通过将inner sep=0pt外部矩阵节点和inner sep=3.333pt(这是默认的内部分隔符)提供给每个内部节点(单元)来避免这种情况。有一个 MWE 包含矩形以供比较:

\usetikzlibrary{positioning, matrix}
\def\legendlines#1#2{
  \draw (#1) -- (#2);
  \draw (#2.north west) -- (#2.south west);
}
\tikzset{
  mynode/.style={draw,fill=blue!30,align=center},
  mylegend/.style={align=left, font=\scriptsize, inner sep=1pt}
}

% Using a matrix, the inter-line space is wrong. I can add row sep=42pt, but I don't know what is the correct value.
\begin{tikzpicture}
  \node[mynode, inner sep=0pt, matrix of nodes,nodes={inner sep=3.33pt}] (N123) {Node line one\\Node line two\\Node line three\\};
  % Coordinates along the east side of N123
  \coordinate (N1east) at (N123-1-1.east -| N123.east);
  \coordinate (N2east) at (N123-2-1.east -| N123.east);
  \coordinate (N3east) at (N123-3-1.east -| N123.east);
  % Legend texts
  \node[mylegend, right=1.0cm of N1east] (L1) {Legend 1};
  \node[mylegend, right=1.2cm of N2east] (L2) {Legend 2};
  \node[mylegend, right=1.4cm of N3east] (L3) {Legend 3};
  % Legend lines
  \legendlines{N1east}{L1}
  \legendlines{N2east}{L2}
  \legendlines{N3east}{L3}
\end{tikzpicture}

\begin{tikzpicture}
 \node[mynode] (N123) {Node line one\\Node line two\\Node line three};
  \node[mylegend, xshift=1cm, at=(N123.east), anchor=west] (L123) {Legend 1\\Legend 2\\Legend 3};
  \legendlines{N123}{L123}
\end{tikzpicture}

结果

答案3

下面带有[记住图片]的解决方案看起来更简单,更灵活,但需要两次编译。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning, matrix}

\begin{document}


\begin{tikzpicture}[remember picture]

\node[text width=5cm,draw](tab){ ligne 1\tikz \coordinate(l1); \\
ligne 2 plus longue\tikz \coordinate(l2); \\
~\\
ligne 4\tikz \coordinate(l4); };

\draw[-|] (l1-|tab.east) -- ++(2,0) node[right]{legende 1};
\draw [-|](l2-|tab.east) -- ++(2.5,0) node[right]{legende 2};
\draw [-|](l4-|tab.east) -- ++(3,0) node[right]{legende 3};
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容