주어진 거리를 따라 두 개 이상의 노드를 균등하게 배포해야 합니다. 아래 MWE:
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning, calc}
\begin{document}
\begin{tikzpicture}[
text node/.style={rectangle, draw}
]
\small
\begin{scope}
\node (aa) at (0,0) {};
\node (ab) at (14,0) {};
\node (c) [text node, right=of aa] {This is a rather long node with text};
\node (d) [text node, right=of c] {Short};
\end{scope}
\begin{scope}[shift={(0,-1)}]
\node (ba) at (0,0) {};
\node (bb) at (14,0) {};
\node (c2) [text node] at ($(ba)!0.33!(bb)$) {This is a rather long node with text};
\node (d2) [text node] at ($(ba)!0.66!(bb)$) {Short};
\end{scope}
\foreach \n in {aa, ab, c, d, ba, bb, c2, d2}
\draw[red, shift=(\n.center)] plot[mark=x] coordinates{(0,0)};
\end{tikzpicture}
\end{document}
단순히 노드를 서로 옆에 배치하면 분명히 거리 오른쪽이 어수선해집니다(첫 번째 경우). 그래서 거리에 따른 배치를 계산하려고 했지만 노드의 크기가 다르기 때문에 이 접근 방식은 실패했습니다(두 번째 경우). 노드와 측면 사이의 거리가 동일하도록 노드의 위치를 어떻게 계산할 수 있습니까?
답변1
이 답변은 단계적으로 진행됩니다. 최종 코드만 보려면 맨 아래로 스크롤하세요.
아마도 개념 증명일 겁니다. 노드는 비어 있더라도 항상 차원을 갖습니다. 따라서 다른 노드가 배치되는 노드의 경계를 표시하지 않는다는 점에서 그림이 오해의 소지가 있습니다. 그러나 빨간색 표시가 관련 지점으로 의도된 경우 노드의 경계에 있지 않기 때문에 해당 치수를 고려해야 합니다.
\documentclass[tikz,multi,border=10pt]{standalone}
\usetikzlibrary{positioning, calc}
\begin{document}
\begin{tikzpicture}[
text node/.style={rectangle, draw, inner sep=2.5pt},
spread two/.code n args=6{%
\pgfmathsetmacro\firstwidth{width("#3")}%
\pgfmathsetmacro\secondwidth{width("#5")}%
\path ($(#6.west)-(#1.east)$) ++(-\firstwidth-\secondwidth-10 pt,0) coordinate (a);
\node (#2) [text node, anchor=west] at ($(#1.east)+1/3*(a)$) {#3};
\node (#4) [text node, anchor=east] at ($(#6.west)-1/3*(a)$) {#5};
},
]
\small
\node (aa) at (0,0) {};
\node (ab) at (14,0) {};
\tikzset{spread two={aa}{c}{This is a rather long node with text}{d}{Short}{ab}}
\foreach \n in {aa, ab, c, d}
\draw[red, shift=(\n.center)] plot[mark=x] coordinates{(0,0)};
\end{tikzpicture}
\end{document}
아마도 \let
구문이 더 편리한 접근 방식을 제공할 수 있습니다.
업데이트
이를 어느 정도 자동화하는 것이 가능합니다. 예를 들어, inner xsep
하드 코딩할 필요 없이 가치를 얻을 수 있습니다 . (위에서는 을 사용했습니다 2.5pt
. 기본값은 입니다 .3333em
.)
\begin{tikzpicture}[
text node/.style={rectangle, draw},
spread two/.code n args=6{%
\pgfmathsetmacro\firstwidth{width("#3")}% 145.58443 pt for testing below (default inner xsep is .3333em)
\pgfmathsetmacro\secondwidth{width("#5")}% 22.1179 pt
\path ($(#6.west)-(#1.east)$) ++(-\firstwidth-\secondwidth-4*\pgfkeysvalueof{/pgf/inner xsep},0) coordinate (a);
\node (#2) [text node, anchor=west] at ($(#1.east)+1/3*(a)$) {#3};
\node (#4) [text node, anchor=east] at ($(#6.west)-1/3*(a)$) {#5};
},
]
\small
\node (aa) at (0,0) {};
\node (ab) at (14,0) {};
\tikzset{spread two={aa}{c}{This is a rather long node with text}{d}{Short}{ab}}
\foreach \n in {aa, ab, c, d}
\draw[red, shift=(\n.center)] plot[mark=x] coordinates{(0,0)};
\end{tikzpicture}
이전과 마찬가지로 이는 우리에게
inner xsep
[이번 버전은 별도로 말씀드리지 않아서 기본값을 사용하기 때문에 조금 다르지만 그게 전부입니다.]
다음은 우리가 예상한 대로 작동하는지 확인하는 것입니다.
\begin{tikzpicture}[
text node/.style={rectangle, draw},
spread two/.code n args=6{%
\pgfmathsetmacro\firstwidth{width("#3")}%
\pgfmathsetmacro\secondwidth{width("#5")}%
\path ($(#6.west)-(#1.east)$) ++(-\firstwidth-\secondwidth-4*\pgfkeysvalueof{/pgf/inner xsep},0) coordinate (a);
\node (#2) [text node, anchor=west] at ($(#1.east)+1/3*(a)$) {#3};
\node (#4) [text node, anchor=east] at ($(#6.west)-1/3*(a)$) {#5};
},
]
\small
\node (aa) at (0,0) {};
\node (ab) at (14,0) {};
\tikzset{spread two={aa}{c}{This is a rather long node with text}{d}{Short}{ab}}
\foreach \n in {aa, ab, c, d}
\draw[red, shift=(\n.center)] plot[mark=x] coordinates{(0,0)};
\node [red, inner sep=0pt] at (a) {a};
\path ($(ab.west)-(aa.east)$) ++(-167.7pt,0) ++(-1.3333em,0) node [blue,inner sep=0pt] {a};
\node [blue, inner sep=0pt] at ($(aa.east)+1/3*(a)$) {x};
\node [blue, inner sep=0pt] at ($(ab.west)-1/3*(a)$) {x};
\node [blue, inner sep=0pt, xshift=145.58443pt] at ($(aa.east)+1/3*(a)+(.6667em,0)$) {x};
\node [blue, inner sep=0pt, xshift=145.58443pt] at ($(aa.east)+2/3*(a)+(.6667em,0)$) {x};
\end{tikzpicture}
이는 우리에게
우리가 예상했던 곳에 파란색 표시가 나타납니다.
그러나 구문에는 아쉬운 점이 있습니다. 그러므로 우리는 정의를 spread two
좀 더 의미있게 변경할 수 있습니다. 예를 들어,
\tikzset{%
text node/.style={rectangle, draw},
spread two/.code args={#1 and #2 with contents #3 and #4 between #5 and #6}{%
\pgfmathsetmacro\firstwidth{width("#3")}%
\pgfmathsetmacro\secondwidth{width("#4")}%
\path ($(#6.west)-(#5.east)$) ++(-\firstwidth-\secondwidth-4*\pgfkeysvalueof{/pgf/inner xsep},0) coordinate (a);
\node (#1) [text node, anchor=west] at ($(#5.east)+1/3*(a)$) {#3};
\node (#2) [text node, anchor=east] at ($(#6.west)-1/3*(a)$) {#4};
},
}
이제 우리는 쓸 수 있습니다
\begin{tikzpicture}\small
\node (aa) at (0,0) {};
\node (ab) at (14,0) {};
\tikzset{spread two=c and d with contents {This is a rather long node with text} and {Short} between aa and ab}
\foreach \n in {aa, ab, c, d}
\draw[red, shift=(\n.center)] plot[mark=x] coordinates{(0,0)};
\end{tikzpicture}
생산하는
전체 코드:
\documentclass[tikz,multi,border=10pt]{standalone}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[
text node/.style={rectangle, draw},
spread two/.code n args=6{%
\pgfmathsetmacro\firstwidth{width("#3")}% 145.58443 pt for testing below (default inner xsep is .3333em)
\pgfmathsetmacro\secondwidth{width("#5")}% 22.1179 pt
\path ($(#6.west)-(#1.east)$) ++(-\firstwidth-\secondwidth-4*\pgfkeysvalueof{/pgf/inner xsep},0) coordinate (a);
\node (#2) [text node, anchor=west] at ($(#1.east)+1/3*(a)$) {#3};
\node (#4) [text node, anchor=east] at ($(#6.west)-1/3*(a)$) {#5};
},
]
\small
\node (aa) at (0,0) {};
\node (ab) at (14,0) {};
\tikzset{spread two={aa}{c}{This is a rather long node with text}{d}{Short}{ab}}
\foreach \n in {aa, ab, c, d}
\draw[red, shift=(\n.center)] plot[mark=x] coordinates{(0,0)};
\end{tikzpicture}
\begin{tikzpicture}[
text node/.style={rectangle, draw},
spread two/.code n args=6{%
\pgfmathsetmacro\firstwidth{width("#3")}%
\pgfmathsetmacro\secondwidth{width("#5")}%
\path ($(#6.west)-(#1.east)$) ++(-\firstwidth-\secondwidth-4*\pgfkeysvalueof{/pgf/inner xsep},0) coordinate (a);
\node (#2) [text node, anchor=west] at ($(#1.east)+1/3*(a)$) {#3};
\node (#4) [text node, anchor=east] at ($(#6.west)-1/3*(a)$) {#5};
},
]
\small
\node (aa) at (0,0) {};
\node (ab) at (14,0) {};
\tikzset{spread two={aa}{c}{This is a rather long node with text}{d}{Short}{ab}}
\foreach \n in {aa, ab, c, d}
\draw[red, shift=(\n.center)] plot[mark=x] coordinates{(0,0)};
\node [red, inner sep=0pt] at (a) {a};
\path ($(ab.west)-(aa.east)$) ++(-167.7pt,0) ++(-1.3333em,0) node [blue,inner sep=0pt] {a};
\node [blue, inner sep=0pt] at ($(aa.east)+1/3*(a)$) {x};
\node [blue, inner sep=0pt] at ($(ab.west)-1/3*(a)$) {x};
\node [blue, inner sep=0pt, xshift=145.58443pt] at ($(aa.east)+1/3*(a)+(.6667em,0)$) {x};
\node [blue, inner sep=0pt, xshift=145.58443pt] at ($(aa.east)+2/3*(a)+(.6667em,0)$) {x};
\end{tikzpicture}
\tikzset{%
text node/.style={rectangle, draw},
spread two/.code args={#1 and #2 with contents #3 and #4 between #5 and #6}{%
\pgfmathsetmacro\firstwidth{width("#3")}%
\pgfmathsetmacro\secondwidth{width("#4")}%
\path ($(#6.west)-(#5.east)$) ++(-\firstwidth-\secondwidth-4*\pgfkeysvalueof{/pgf/inner xsep},0) coordinate (a);
\node (#1) [text node, anchor=west] at ($(#5.east)+1/3*(a)$) {#3};
\node (#2) [text node, anchor=east] at ($(#6.west)-1/3*(a)$) {#4};
},
}
\begin{tikzpicture}\small
\node (aa) at (0,0) {};
\node (ab) at (14,0) {};
\tikzset{spread two=c and d with contents {This is a rather long node with text} and {Short} between aa and ab}
\foreach \n in {aa, ab, c, d}
\draw[red, shift=(\n.center)] plot[mark=x] coordinates{(0,0)};
\end{tikzpicture}
\end{document}