我在图表中遇到标签问题。我想让它们更靠近线条,但似乎找不到办法。有没有办法让标签更靠近线条?
以下是代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto, minimum width = 3em, minimum height = 3em,
thick, main node/.style={rectangle,fill=white,draw}
]
\node[main node,node distance=3cm] (2) {I(t)};
\node[main node,node distance=3cm] (1) [left of=2] {S(t)};
\node[main node,node distance=3cm] (3) [right of=2] {A(t)};
\node[below of=1,node distance=2cm] (dummy1) {};
\node[below of=2,node distance=2cm] (dummy2) {};
\node[below of=3,node distance=2cm] (dummy3) {};
\node[right of=3,node distance=2cm] (dummy4) {};
\node[left of=1,node distance=2cm] (dummy) {};
\draw[->,draw=black] (1) -- node {$\lambda(t)$} (2);
\draw[->,draw=black] (2) -- node {$\alpha$} (3);
\draw[->,draw=black] (dummy) -- node {$\Lambda$} (1);
\draw[->,draw=black] (1) -- node {$\mu$} (dummy1);
\draw[->,draw=black] (2) -- node {$\mu$} (dummy2);
\draw[->,draw=black] (3) -- node {$\mu$} (dummy3);
\draw[->,draw=black] (3) -- node {$\nu$} (dummy4);
\end{tikzpicture}
\end{document}
答案1
您已设定节点大小3em
在整个环境中,宽度和高度至少为s tikzpicture
,而这应该只与 相关联main node/.style
。我的意思是:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[>=stealth',shorten >=1pt,auto,
thick, main node/.style={rectangle,fill=white,draw, minimum width = 3em, minimum height = 3em}
]
\node[main node,node distance=3cm] (2) {I(t)};
\node[main node,node distance=3cm] (1) [left of=2] {S(t)};
\node[main node,node distance=3cm] (3) [right of=2] {A(t)};
\node[below of=1,node distance=2cm] (dummy1) {};
\node[below of=2,node distance=2cm] (dummy2) {};
\node[below of=3,node distance=2cm] (dummy3) {};
\node[right of=3,node distance=2cm] (dummy4) {};
\node[left of=1,node distance=2cm] (dummy) {};
\draw[->,draw=black] (1) -- node [outer ysep=0pt]{$\lambda(t)$} (2);
\draw[->,draw=black] (2) -- node {$\alpha$} (3);
\draw[->,draw=black] (dummy) -- node {$\Lambda$} (1);
\draw[->,draw=black] (1) -- node {$\mu$} (dummy1);
\draw[->,draw=black] (2) -- node {$\mu$} (dummy2);
\draw[->,draw=black] (3) -- node {$\mu$} (dummy3);
\draw[->,draw=black] (3) -- node {$\nu$} (dummy4);
\end{tikzpicture}
\end{document}
预期输出:
答案2
如果你把这行放在两个代码块之间
\tikzset{every node/.style={draw}}
这就是你将看到的
您可以看到,它们内部的数学文本周围都有额外的间距。这是因为您提供的 tikzpicture 选项minimum width = 3em, minimum height = 3em
适用于所有内容,除非您覆盖它们。
一个选择是像 AboAmmar 展示的那样,把它们带入mainnode
风格或者使用
\tikzset{every node/.style={minimum size=0pt}}
在第一段代码之后重置选项..