首页>LaTeX>source
情节

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

如果我使用 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}
最新回答
  • 2019-12-5
    1 #

    这是使用 \subnode的解决方案   来自实验 tikzmark   包.在这种情况下,"实验"意味着"还没有CTAN":您需要从TeX-SX Launchpad网站下载它.下载文件 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}
    

    这会产生:

  • 2019-12-5
    2 #

    矩阵是imho最好的解决方案.如果你不想把所有的线都弄皱,你为什么要放 node={inner sep=0pt} ? 删除此选项会显示:

    正如OP在评论中指出的那样,上述解决方案比标准矩形解决方案产生更多填充.这可以通过给 inner sep=0pt来避免   到外部矩阵节点,和 inner sep=3.333pt   (每个内部节点(单元格)的默认内部sep)。 有一个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}
    

  • 2019-12-5
    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}
    

  • LaTeX tex core:什么设置结转到发货页面?
  • LaTeX sectioning:如何使用乳胶或xetex中的孟加拉数字获取部分编号和页码?