在 TikZ 图表中的节点中获取适当的行间距

在 TikZ 图表中的节点中获取适当的行间距

我有两个节点中的文本排版在多行上。包含“未来顶点”的节点上的行间距不美观。我不得不添加以\\{}在两行之间留出更多空间。如果没有它,行间距会使节点中的文本显得拥挤。

\documentclass{amsart}
\usepackage{amssymb}
\usepackage{mathtools,array}

\usepackage{tikz}
\usetikzlibrary{calc,intersections}

\begin{document}


\begin{tikzpicture}[nodes={inner sep=0, font=\scriptsize}, shorten/.style={shorten >=#1,shorten <=#1}]


%Here is the blow-up of the vertex labeled b-n.
\draw (-4,-9) -- (-2,-9) -- (0,-9) (2,-9) -- (5,-9);
\draw[fill] (-4,-9) circle (1.5pt);
\draw[fill] (-2,-9) circle (1.5pt);
\draw[fill] (0,-9) circle (1.5pt);
\node at (1,-9){$\ldots$};
\draw[fill] (2,-9) circle (1.5pt);
\draw[fill] (5,-9) circle (1.5pt);
%
\node[anchor=north] at ($(-4,-9) +(0,-0.25)$){\textit{b}};
\node[anchor=south] at ($(-4,-9) +(0,0.25)$){$\dfrac{0}{1}$};
%
\node[anchor=north] at ($(-2,-9) +(0,-0.25)$){$b-1$};
\node[anchor=south] at ($(-2,-9) +(0,0.25)$){$\dfrac{1}{1}$};
%
\node[anchor=north] at ($(0,-9) +(0,-0.25)$){$b-2$};
\node[anchor=south] at ($(0,-9) +(0,0.25)$){$\dfrac{2}{1}$};
%
\node[anchor=north] at ($(2,-9) +(0,-0.25)$){\textit{b-n}};
\node[anchor=south] at ($(2,-9) +(0,0.25)$){$\dfrac{n}{1}$};
%
\node[anchor=north] at ($(5,-9) +(0,-0.25)$){$b-(n+1)$};
\node[anchor=south] at ($(5,-9) +(0,0.25)$){$\dfrac{n+1}{1}$};
%
%
%A "pin" is drawn between the midpoint of last two vertices and the label of the mediants of these vertices.
\draw[-latex, dashed, line width=0.8pt, shorten <=3mm, shorten >=1mm] ($(3.5,-9) +(60:2)$) -- (3.5,-9);
\path node[anchor=south, align=center] at ($(3.5,-9) +(60:2)$){future vertex\\of\\{}\\$\dfrac{2n+1}{2}$};
%
%A "pin" is drawn between the midpoint of the edge between the last two vertices and its label.
\coordinate (label_for_Edge) at ($(3.5,-9.5) +(0,-0.75)$);
\draw[draw=gray, line width=0.8pt, shorten <=1mm, shorten >=1mm] (3.5,-9) -- (label_for_Edge);
\node[anchor=north, align=center, inner sep=0, font=\scriptsize] at (label_for_Edge)
{Present edge label of\\$2\bigl[(b-n)+(b-(n+1))\bigr]$\\$=2^{2}b-(2n+1)2$};

\end{tikzpicture}

\end{document}

答案1

(La)TeX 之所以如此吸引人,是因为它有办法为您提供适当的间距。然而,许多方程式环境只有在“知道”文本宽度时才有效,也就是说,您需要将它们放在类似小页面的结构中。您可以通过添加键在节点中获取这些结构。这允许text width您使用\[...\]

\path node[anchor=south, align=center,text width={width("future vertex")}] 
    at ($(3.5,-9) +(60:2)$){future vertex\\of\[\dfrac{2n+1}{2}\]};

然后使用 LaTeX 间距来表示方程式。当然,您不限于此\[...\],这只是一个例子。对于我使用的第二个方程式,multline但最好的选择取决于您自己的偏好。您可以通过重新定义来进一步调整间距\belowdisplayskip,例如 \setlength\belowdisplayskip{0.75ex}。我现在在节点本地执行此操作,但您tikzpicture也可以在环境内部执行此操作`

\documentclass{amsart}
\usepackage{amssymb}
\usepackage{mathtools,array}

\usepackage{tikz}
\usetikzlibrary{calc,intersections}

\begin{document}


\begin{tikzpicture}[nodes={inner sep=0, font=\scriptsize,
execute at begin node={\setlength\abovedisplayskip{0.75ex}%
\setlength\belowdisplayskip{0.75ex}%
\setlength\abovedisplayshortskip{0.75ex}%
\setlength\belowdisplayshortskip{0.75ex}}}, 
shorten/.style={shorten >=#1,shorten <=#1}]


%Here is the blow-up of the vertex labeled b-n.
\draw (-4,-9) -- (-2,-9) -- (0,-9) (2,-9) -- (5,-9);
\draw[fill] (-4,-9) circle (1.5pt);
\draw[fill] (-2,-9) circle (1.5pt);
\draw[fill] (0,-9) circle (1.5pt);
\node at (1,-9){$\ldots$};
\draw[fill] (2,-9) circle (1.5pt);
\draw[fill] (5,-9) circle (1.5pt);
%
\node[anchor=north] at ($(-4,-9) +(0,-0.25)$){\textit{b}};
\node[anchor=south] at ($(-4,-9) +(0,0.25)$){$\dfrac{0}{1}$};
%
\node[anchor=north] at ($(-2,-9) +(0,-0.25)$){$b-1$};
\node[anchor=south] at ($(-2,-9) +(0,0.25)$){$\dfrac{1}{1}$};
%
\node[anchor=north] at ($(0,-9) +(0,-0.25)$){$b-2$};
\node[anchor=south] at ($(0,-9) +(0,0.25)$){$\dfrac{2}{1}$};
%
\node[anchor=north] at ($(2,-9) +(0,-0.25)$){\textit{b-n}};
\node[anchor=south] at ($(2,-9) +(0,0.25)$){$\dfrac{n}{1}$};
%
\node[anchor=north] at ($(5,-9) +(0,-0.25)$){$b-(n+1)$};
\node[anchor=south] at ($(5,-9) +(0,0.25)$){$\dfrac{n+1}{1}$};
%
%
%A "pin" is drawn between the midpoint of last two vertices and the label of the mediants of these vertices.
\draw[-latex, dashed, line width=0.8pt, shorten <=3mm, shorten >=1mm] ($(3.5,-9) +(60:2)$) -- (3.5,-9);
\path node[anchor=south, align=center,text width={width("future vertex")}] 
at ($(3.5,-9) +(60:2)$){future vertex\\of\[\dfrac{2n+1}{2}\]};
%
%A "pin" is drawn between the midpoint of the edge between the last two vertices and its label.
\coordinate (label_for_Edge) at ($(3.5,-9.5) +(0,-0.75)$);
\draw[draw=gray, line width=0.8pt, shorten <=1mm, shorten >=1mm] (3.5,-9) -- (label_for_Edge);
\node[anchor=north, align=center, inner sep=0, font=\scriptsize,
,text width={width("Present edge label of")}] at (label_for_Edge)
{Present edge label of
\begin{multline*}2\bigl[(b-n)+(b-(n+1))\bigr]\\=2^{2}b-(2n+1)2
\end{multline*}};

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

通过使用multipart节点形状并付出一些努力可以使您的代码更短:

\documentclass{amsart}
\usepackage{mathtools}

\usepackage{tikz}
\usetikzlibrary{chains,           % new
                positioning,      % new
                shapes.multipart} % new

\begin{document}
    \begin{tikzpicture}[
node distance = 7mm and 21mm,  % new
  start chain = going right,   % new
dot/.style args = {#1/#2}{circle, draw, fill, minimum size=3pt, inner sep=0pt,
                          label=$#1$,
                          label=below:$#2$,
                          node contents={},
                          on chain}, % new
lbl/.style args = {#1/#2}{rectangle split, rectangle split parts=2,
                          font=\footnotesize, inner sep=2pt,   
                          node contents={#1\nodepart{two}#2},
                          }, % new
                    ]
\node (n1) [dot=\frac{0}{1}/b];
\node (n2) [dot=\frac{1}{1}/b-1];
\node (n3) [dot=\frac{2}{1}/b-2];
\node (n4) [dot=\frac{n}{1}/b-n];
\node (n5) [dot=\frac{n+1}{1}/b-(n+1)];
%
\draw[thick]    (n1) -- (n3)   (n4) -- node (aux) {} (n5);
\path           (n3) -- node {$\dotsm$} (n4);
% "pin" above
\draw[latex- , dashed, semithick]
        (aux) -- ++ (6mm,9mm) node[above, lbl=Future vertex of/
                                              $\dfrac{2n+1}{2}$];
% "pin" below
\draw[semithick]
        (aux) -- ++ (0,-9mm) node[below, lbl=Present edge label of/
                                             {$\begin{gathered}
                                                  2\bigl[(b-n)+(b-(n+1))\bigr]\\
                                                   = 2^{2}b-(2n+1)2 
                                               \end{gathered}$}];
    \end{tikzpicture}
\end{document}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容