在 tikzpicture 中对树/图表进行编号

在 tikzpicture 中对树/图表进行编号

我想根据章节编号对树/图表进行编号,并在其中心和底部进行编号。例如

图 1.1

这是我写的一个简单的。

\usepackage{tikz}
\usepackage{amsfonts}
\usepackage{amsthm,amsmath}
\newcounter{dummy} 
\numberwithin{dummy}{section}
\numberwithin{figure}{section}
\newtheorem{thm}[dummy]{Theorem}
\newtheorem{defn}[dummy]{Definition}
\usepackage{etoolbox}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\numberwithin{equation}{section}
\begin{document}\begin{center}
\tikzstyle{bag} = [text width=2em, text centered]
\tikzstyle{end} = []
\begin{tikzpicture}[sloped]
  \node (a) at ( 0,0) [bag] {$S$};
  \node (b) at ( 4,-1.5) [bag] {$ dS $};
  \node (c) at ( 4,1.5) [bag] {$ uS $};

  \draw [-] (a) to node [below] {$(1-q)$} (b);
  \draw [-] (a) to node [above] {$q$} (c);

\end{tikzpicture}
\end{center}
\end{document}

答案1

您可以使用newfloat包来定义定制的浮动环境;通过这种方式,您可以为图表添加标题,并生成图表列表(类似于 LoT 或 LoF):

\documentclass{article}
\usepackage{tikz}
\usepackage{newfloat}

\DeclareFloatingEnvironment[fileext=dia,within=section]{diagram}

\begin{document}
\section{Test Section}
\begin{diagram}[!ht]
\centering
\tikzstyle{bag} = [text width=2em, text centered]
\tikzstyle{end} = []
\begin{tikzpicture}[sloped]
  \node (a) at ( 0,0) [bag] {$S$};
  \node (b) at ( 4,-1.5) [bag] {$ dS $};
  \node (c) at ( 4,1.5) [bag] {$ uS $};

  \draw [-] (a) to node [below] {$(1-q)$} (b);
  \draw [-] (a) to node [above] {$q$} (c);
\end{tikzpicture}
\caption{ a test caption for the diagram}
\label{dia:test}
\end{diagram}

\end{document}

在此处输入图片描述

使用floatrow您可以使用的包\DeclareNewFloatType

\documentclass{article}
\usepackage{tikz}
\usepackage{floatrow}

\DeclareNewFloatType{diagram}{fileext=dia ,within=section,name=Diagram}

\begin{document}
\listof{diagram}{List of Diagrams}
\section{Test Section}
\begin{diagram}[!ht]
\centering
\tikzstyle{bag} = [text width=2em, text centered]
\tikzstyle{end} = []
\begin{tikzpicture}[sloped]
  \node (a) at ( 0,0) [bag] {$S$};
  \node (b) at ( 4,-1.5) [bag] {$ dS $};
  \node (c) at ( 4,1.5) [bag] {$ uS $};

  \draw [-] (a) to node [below] {$(1-q)$} (b);
  \draw [-] (a) to node [above] {$q$} (c);
\end{tikzpicture}
\caption{ a test caption for the diagram}
\label{dia:test}
\end{diagram}

\end{document}

\end{document}

如果不需要浮动对象并且不需要标题,则可以使用例如minipage和用户创建的计数器来定义新环境:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}

\newcounter{diagram}
\numberwithin{diagram}{section}

\newenvironment{diagram}
  {\stepcounter{diagram}\par\smallskip\noindent\begin{minipage}{\linewidth}\centering}
  {\par Diagram~\thediagram\end{minipage}\par\smallskip}

\begin{document}
\section{Test Section}
\begin{diagram}
\centering
\tikzstyle{bag} = [text width=2em, text centered]
\tikzstyle{end} = []
\begin{tikzpicture}[sloped]
  \node (a) at ( 0,0) [bag] {$S$};
  \node (b) at ( 4,-1.5) [bag] {$ dS $};
  \node (c) at ( 4,1.5) [bag] {$ uS $};

  \draw [-] (a) to node [below] {$(1-q)$} (b);
  \draw [-] (a) to node [above] {$q$} (c);
\end{tikzpicture}
\end{diagram}

\end{document}

在此处输入图片描述

我从示例中删除了对于解决方案不必要的包。

相关内容