首頁>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}
最新回復
  • 12月前
    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}
    

    這会产生:

  • 12月前
    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}
    

  • 12月前
    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中的孟加拉數字获取部分編號和頁碼?