帮助 tikz 包

帮助 tikz 包

你好我需要在 latex 中编写一个多状态模型,我的代码如下

\begin{tikzpicture}
        % Draw the states
        \node[state]             (aa) {Husband Alive- Wife Alive (0)};
        \node[state, right=of aa] (ad) {Husband Alive- Wife Dead(1)};
 \node[state, below=of ad] (dd){Husband Dead- Wife Dead(3)};
  \node[state, below=of aa] (da){Husband Dead- Wife Alive(4)};
        % Connect the states with arrows
        \draw[every loop,
              ]
            (aa) edge[right ] node {$m_{x+t:y+t}^{01}$} (ad)
            (ad) edge[ right] node {} (dd)
            (da) edge[right] node {} (dd)
            (aa) edge[ right] node {} (da);
\end{tikzpicture}

我怎样才能使线条长度更大以适合文本以及如何使节点变成正方形?

谢谢

答案1

这是一种可能性。我定义了一种名为 的新样式,square用来代替state。使用text width键将自动换行,尽管我在示例中添加了手动换行符。

node distance用于设置使用时节点之间的默认距离right=of othernode,因此我将其设置为3cm。

对于 上的节点edge,我将 放置为 ,above而不是right。另一种可能性是将 完全删除[right],而是将auto其添加到\draw,即\draw [auto] ...

您可能还对该库感兴趣。这定义了创建边缘节点的快捷方式。您可以使用quotes而不是。(只有一个选项,不需要括号。)我在下面的代码中添加了第二个选项来演示这一点。(a) edge node[<options>] {foo} (b)(a) edge["foo"{<options>}] (b)<options>tikzpicture

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{
   positioning,
   quotes 
}
\begin{document}

% first version, with explicit placement for edge nodes
\begin{tikzpicture}[
  square/.style={
    text width=3cm, % makes the node a minipage-like box of width3cm
    align=center, % center align text in node
    minimum size=3.3cm, % sets minimum width and height
    draw % draw outline
  },
  node distance=3cm % default distance between nodes
]
    % Draw the states
    \node[square]              (aa) {Husband Alive--\\Wife Alive(0)};
    \node[square,right=of aa]  (ad) {Husband Alive--\\Wife Dead(1)};
    \node[square, below=of ad] (dd) {Husband Dead--\\Wife Dead(3)};
    \node[square, below=of aa] (da) {Husband Dead--\\Wife Alive(4)};

    % Connect the states with arrows
    \draw
            (aa) edge node[above] {$m_{x+t:y+t}^{01}$} (ad)
            (ad) edge node[right] {foo} (dd)
            (da) edge node[below] {bar} (dd)
            (aa) edge node[left]  {baz} (da);
\end{tikzpicture}

% second version with automatic placement of edge nodes,
% and the quotes syntax
\begin{tikzpicture}[
  square/.style={
    text width=3cm, % makes the node a minipage-like box of width3cm
    align=center, % center align text in node
    minimum size=3.3cm, % sets minimum width and height
    draw % draw outline
  },
  node distance=3cm, % default distance between nodes
]
    % Draw the states
    \node[square]              (aa) {Husband Alive--\\Wife Alive(0)};
    \node[square, right=of aa] (ad) {Husband Alive--\\Wife Dead(1)};
    \node[square, below=of ad] (dd) {Husband Dead--\\Wife Dead(3)};
    \node[square, below=of aa] (da) {Husband Dead--\\Wife Alive(4)};

    % Connect the states with arrows
    \draw [auto] % automatically places nodes next to path, instead of on top of path
            (aa) edge["$m_{x+t:y+t}^{01}$"] (ad)
            (ad) edge["foo"] (dd)
            (da) edge["bar"swap] (dd)
            (aa) edge["baz"swap] (da);
\end{tikzpicture}
\end{document}

相关内容