我试图构造一棵树,但 Latex 没有绘制它,也没有显示任何具体错误或警告。代码有什么问题?
\documentclass[a4paper,12pt]{article}
\usepackage{cmap}
\usepackage{mathtext}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{amsmath,amsfonts,amssymb,amsthm,mathtools}
\usepackage{icomma}
\newcommand*{\hm}[1]{#1\nobreak\discretionary{}
{\hbox{$\mathsurround=0pt #1$}}{}}
\usepackage{graphicx}
\setlength\fboxsep{3pt}
\setlength\fboxrule{1pt}
\usepackage{wrapfig}
\usepackage{array,tabularx,tabulary,booktabs}
\usepackage{longtable}
\usepackage{multirow}
\theoremstyle{remark}
\newtheorem*{nonum}{Solution}
\usepackage{etoolbox}
\usepackage{lastpage}
\usepackage{soulutf8}
\usepackage{hyperref}
\usepackage[usenames,dvipsnames,svgnames,table,rgb]{xcolor}
\usepackage{multicol}
\usepackage{tikz}
\usetikzlibrary{trees}
\usetikzlibrary{matrix}
\begin{document}
\section{drawing a tree}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [matrix of nodes, column sep=2cm]
{
& & & & & & $\a$ \\
& & & & \ & &\\
& & & & & & $\b$ \\
& & \ & & & &\\
& & & & & & $\c$ \\
\ & & & & \ & &\\
& & & & & & $\d$ \\
& & & & & & \\
& & \ & & & & $\e$ \\
& & \ & & \ & &\\
& & & & & & $\f$ \\
};
\draw[->] (tree-6-1)--(tree-4-3) node [midway,above] {$\alpha$};
\draw[->] (tree-6-1)--(tree-10-5) node [midway,below] {$(1-\alpha)$};
\draw[->] (tree-4-3)--(tree-2-5) node [midway,above] {$\delta$};
\draw[->] (tree-4-3)--(tree-6-5) node [midway,below] {$(1-\delta)$};
\draw[->] (tree-2-5)--(tree-1-7);
\draw[->] (tree-2-5)--(tree-3-7);
\draw[->] (tree-6-5)--(tree-5-7);
\draw[->] (tree-6-5)--(tree-7-7);
\draw[->] (tree-10-5)--(tree-9-7);
\draw[->] (tree-10-5)--(tree-11-7);
\end{tikzpicture}
\end{document}
答案1
Gregory,您在用值定义矩阵后跳过了该行。显然,“};”之前的行被遗漏了,导致语法错误。
答案2
作为对 @user1146332 的回答 (+1) 的补充。添加了边缘标签的自动定位和一些小改进:
\documentclass[margin=3mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\tikzset{
ELS/.style = {% Edge Label Style
pos=0.5, sloped, node font=\footnotesize, text=blue,
inner sep=2pt, anchor=#1},
}
\begin{document}
\begin{forest}
for tree={
% nodes
inner sep=2pt,
math content,
% tree
grow'=east,
anchor=west,
l sep=12mm,
s sep=2mm,
edge={semithick,-Stealth}%
},
EL/.style={if n=1{edge label={node[ELS=south]{$#1$}}}
{edge label={node[ELS=north]{$#1$}}}}
[
[, EL=\alpha
[, tier=L2, EL=\delta,
[\epsilon]
[\epsilon+\mu]
]
[, tier=L2, EL=(1-\delta)
[\epsilon+\mu]
[\epsilon]
]
]
[, tier=L2, EL=(1-\delta)
[\epsilon]
[\epsilon]
]
]
\end{forest}
\end{document}
答案3
我看到你已经修复了代码。但是,如果你想用类似的 tikz 代码映射几棵树,你可能会精神崩溃。软件开发人员会说你的代码的可维护性很差。而且因为编码也应该很有趣,所以你应该考虑尝试其他方法。
我想向您展示利用该forest
包的树的另一种实现方式。
您forest
可以采用更加通用的方法,同时又具有高度的可定制性。
\documentclass[a4paper,10pt]{article}
\usepackage{forest}
\forestset{
e-label/.style 2 args = {
edge label={
node[midway, #2, sloped]{#1}
}
}
}
\begin{document}
\begin{forest}
for tree={
grow'=east,
l sep=1.5cm,
edge={->, >=stealth},
anchor=west,
child anchor=west
},
[
[, e-label={$\alpha$}{above}
[, e-label={$\delta$}{above}, tier=second
[$\epsilon$]
[$\epsilon +\mu$]
]
[, e-label={$(1-\delta)$}{below}
[$\epsilon +\mu$]
[$\epsilon$]
]
]
[, e-label={$(1-\alpha)$}{below}, tier=second
[$\epsilon$]
[$\epsilon$]
]
]
\end{forest}
\end{document}