我怎样在 tikz 中绘制这棵简单的树?

我怎样在 tikz 中绘制这棵简单的树?

在此处输入图片描述

我怎样才能在 tikz 中绘制这样的内容?谢谢

\documentclass{article} 
\usepackage{tikz} 
\usetikzlibrary{arrows} 
\newcommand{\midarrow}{\tikz \draw[-triangle 90] (0,0) -- +(.1,0);}

\begin{document} 
\begin{tikzpicture} 
\begin{scope}[very thick, every node/.style={sloped,allow upside down}] 
  \draw (0,0)-- node {\midarrow} (2,0); 
  \draw (2,0)-- node {\midarrow} (2,2); 
  \draw (2,0)-- node {\midarrow} (4,0); 
  \draw (4,2)-- node {\midarrow} (4,0); 
  \draw (4,0)-- node {\midarrow} (6,0); 
  \draw (4,0)-- node {\midarrow} (4,-2); 
  \draw (4,-2)-- node {\midarrow} (6,-2); 
  \draw (4,-2)-- node {\midarrow} (4,-4); 
\end{scope}
\end{tikzpicture} 
\end{document}

但是我如何添加$\alpha$$\beta$和箭头标签?

答案1

一种方法是使用库matrix of nodes中的matrix。可以进行许多调整。请参阅代码中的注释以获取完整解释。

\documentclass[tikz]{standalone}
\usetikzlibrary{
  decorations.markings, % needed for arrows midway along paths
  matrix                % needed for "matrix of nodes"
}
\tikzset{
  mat/.style={                 % style for the matrix layout of the graph
    matrix of nodes,           % each cell contains the text of a node
    row sep=2cm,               % distance between rows
    column sep=2cm,            % distance between columns
    nodes=dot,                 % apply the `dot' style to each node in the matrix
    nodes in empty cells=false % do not draw empty cells in the matrix
  },
  dot/.style={    % style for the dots in the matrix
    fill,         % fill the node shape
    circle,       % circle node shape
    inner sep=0pt % tightly fitted to node text
  },
  con/.style={                              % style for the connecting lines
    postaction={decorate},                  % decorate path after drawing
    decoration={                            % specify the decoration
      markings,                             % mark the path
      mark=at position 0.5 with {\arrow{>}} % mark halfway along with arrow
    }
  },
  lab/.style={ % style for the path labels
    midway,    % label midway along path
    auto       % auto position, use `swap' to override
  }
}

\begin{document}
\begin{tikzpicture}
\matrix [mat] (m) { % enter the matrix :-)
  . & . & . &   \\
  . & . & . & . \\
    &   & . & . \\
    &   & . & . \\
};
\draw[con] (m-1-2) -- (m-1-1) node[lab,swap] {$b$};
\draw[con] (m-2-1) -- (m-2-2) node[lab,swap] {$a$};
\draw[con] (m-2-2) -- (m-1-2) node[lab,swap] {$b$};
\draw[con] (m-2-2) -- (m-2-3) node[lab,swap] {$a$};
\draw[con] (m-1-3) -- (m-2-3) node[lab] {$b$};
\draw[con] (m-2-3) -- (m-2-4) node[lab] {$a$};
\draw[con] (m-2-3) -- (m-3-3) node[lab] {$c$};
\draw[con] (m-3-3) -- (m-3-4) node[lab] {$c$};
\draw[con] (m-3-3) -- (m-4-3) node[lab] {$a$};
\draw[con] (m-4-3) -- (m-4-4) node[lab] {$b$};
\node[below] at (m-2-2) {$\alpha$};
\node[right] at (m-1-2) {$\beta$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\newcommand{\midarrow}{\tikz \draw[-triangle 90] (0,0) -- +(.1,0);}

\begin{document}
\begin{tikzpicture}
\begin{scope}[very thick, every node/.style={sloped,allow upside down}]
  \draw (0,0)-- node {\midarrow} (2,0);
  \draw (2,0)-- node {\midarrow} (2,2);
  \draw (2,0)-- node {\midarrow} (4,0);
  \draw (4,2)-- node {\midarrow} (4,0);
  \draw (4,0)-- node {\midarrow} (6,0);
  \draw (4,0)-- node {\midarrow} (4,-2);
  \draw (4,-2)-- node {\midarrow} (6,-2);
  \draw (4,-2)-- node {\midarrow} (4,-4);
\end{scope}
\end{tikzpicture}
\end{document}

\end{document}

但是我如何添加 $\alpha$ 和 $\beta$ 和箭头标签?

相关内容