# Numbering trees/diagrams in tikzpicture

I want to number my trees/diagrams according to section number and at the center and bottom of them. Like

Diagram 1.1

Here is a simple one that I wrote.

\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}

-

You can use the newfloat package to define a customized floating environment; in this way you can give a caption to your diagrams and you can produce a list of them (similar to the LoT or the 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}


Using the floatrow package you can use \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}


If a floating object is not desired and captions are not required, one can define a new environment using, for example, a minipage, and a user created counter:

\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}


I removed from the examples the packages that were not essential for the solution.

-
thanks but i got this error newfloat.sty' not found. – hamid Sep 25 '12 at 14:29
@hamid I updated my answer providing another option, this time using the floatrow` package. – Gonzalo Medina Sep 25 '12 at 17:02
@hamid new option added: now a floating object is not used and captions are not allowed; only the label "Diagram" and the numbering are produced. – Gonzalo Medina Sep 25 '12 at 18:29