1

I'm trying to refer to a tree in the document. The tree has a label and in the text it is referred to as 2.2, but the label 2.2 is not shown next to the tree. The code including the packages I use is as follows:

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary {positioning}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs}
\usegdlibrary{trees}

\usepackage{hyperref}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{geometry}
\usepackage{mathtools}
\usepackage{makeidx}
\usepackage{graphicx}
\begin{document}
       \begin{center}
      \begin{tikzpicture}[>=stealth,sloped]\label{reactiontreeABEPQ}
        \matrix (tree) [%
          matrix of nodes,
          minimum size=1cm,
          column sep=2.5cm,
          row sep=1cm,
        ]
        {
              &  $\EA$ &  \\
            &    $\EB$ & $\EAB$ \\
               $E$ & & \\
               & $\EP$ & $\EPQ$  \\
               &  $\EQ$ & \\
        };
        \draw[<->] (tree-1-2) -- (tree-3-1) node [midway,above] {$\xrightarrow{+A}$};
        \draw[<->] (tree-2-2) -- (tree-3-1) node [midway,below] {$\xrightarrow{+B}$};
        \draw[<->] (tree-4-2) -- (tree-3-1) node [midway,above] {$\xrightarrow{+P}$};
        \draw[<->] (tree-5-2) -- (tree-3-1) node [midway,below] {$\xrightarrow{+Q}$};
        \draw[<->] (tree-1-2) -- (tree-2-3) node [midway,above] {$\xrightarrow{+B}$};
        \draw[<->] (tree-2-2) -- (tree-2-3) node [midway,below] {$\xrightarrow{+A}$};
        \draw[<->] (tree-4-2) -- (tree-4-3) node [midway,above] {$\xrightarrow{+Q}$};
        \draw[<->] (tree-5-2) -- (tree-4-3) node [midway,below] {$\xrightarrow{+P}$};
        \draw[<->] (tree-2-3) -- (tree-4-3) node [midway,above] {};
      \end{tikzpicture}
    \end{center}
    \ref{reactiontreeABEPQ}
\end{document}

This code above is the tree I would like to label in the document. However, the label is not shown next to the tree. Does it have to do with the size of the tree or can tikzpictures maybe not be labeled?

Alan Munn
  • 218,180
Algebear
  • 113
  • 1
    Can't you just put the thing in a figure environment, add a caption and move the label there? Then you can refer to it as figure. –  Jan 21 '19 at 12:10
  • @marmot Putting the tree in a figure environment, like inbetween '\begin{figure}...\end{figure}', doesn't work for me. Also, I have trouble creating a code block in the comments. – Algebear Jan 21 '19 at 12:16

1 Answers1

4

I think you're misunderstanding how labels work. A \label command (roughly) simply picks up the value of the most recent \refstepcounter command, writes its value to the .aux file. Since the tikzpicture environment doesn't contain an instance of \refstepcounter, when you issue a \label command next to a tikzpicture it will pick of the last value of the most recent element in your document that contains an instance of \refstepcounter. For example, in the following document, the \label command will end up with the value 1.1, because that's the output of the value of the the counter for \subsection. If you comment out the \subsection command, and recompile (twice) the label will change to 1, the value of the \section counter.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\section{A section}
\subsection{A subsection}
\begin{tikzpicture}\label{mynode}
\node at (0,0) [draw,red] {A node};
\end{tikzpicture}
This is the label value: \ref{mynode}
\end{document}

So if you want to label and number your trees, you need to put them into some kind of environment that has its own numbering system, such as an equation, or a figure, or some other type of example numbering system. Then the placement of the counter will be specified (in an equation it's on the left or right; in the case of a figure it's in the caption, etc.)

So you need to decide how these trees are supposed to be numbered and then use \label and \ref with respect to those elements. You can't attach a label to a tikzpicture by itself.

For example, here's a version of your diagram using the equation environment to number it. Since all your nodes seem to be math nodes, I've changed the code to be matrix of math nodes which removes the need to wrap each node in $. You might also want to investigate the tikz-cd package which can draw similar types of diagrams with less work, I suspect.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}
\foreach \x in {A,B,P,Q,AB,PQ} {% This code defines all the commands you didn't define in your example
\expandafter\xdef\csname E\x\endcsname{\text{E\x}}} % it should be removed in your actual code
\begin{document}
\section{A section}

\subsection{A subsection}
\begin{tikzpicture}\label{mynode}
\node at (0,0) [draw,red] {A node};
\end{tikzpicture}

Here label value is \ref{mynode} because it gets the number of the most recent subsection.
\begin{equation}
 \begin{tikzpicture}[>=stealth,sloped,baseline]\label{reactiontreeABEPQ}
        \matrix (tree) [%
          matrix of math nodes,
          minimum size=1cm,
          column sep=2.5cm,
          row sep=1cm,
        ]
        {
              &  \EA &  \\
              &  \EB & \EAB \\
            E & & \\
              & \EP & \EPQ  \\
              &  \EQ & \\
        };
        \draw[<->] (tree-1-2) -- (tree-3-1) node [midway,above] {$\xrightarrow{+A}$};
        \draw[<->] (tree-2-2) -- (tree-3-1) node [midway,below] {$\xrightarrow{+B}$};
        \draw[<->] (tree-4-2) -- (tree-3-1) node [midway,above] {$\xrightarrow{+P}$};
        \draw[<->] (tree-5-2) -- (tree-3-1) node [midway,below] {$\xrightarrow{+Q}$};
        \draw[<->] (tree-1-2) -- (tree-2-3) node [midway,above] {$\xrightarrow{+B}$};
        \draw[<->] (tree-2-2) -- (tree-2-3) node [midway,below] {$\xrightarrow{+A}$};
        \draw[<->] (tree-4-2) -- (tree-4-3) node [midway,above] {$\xrightarrow{+Q}$};
        \draw[<->] (tree-5-2) -- (tree-4-3) node [midway,below] {$\xrightarrow{+P}$};
        \draw[<->] (tree-2-3) -- (tree-4-3) node [midway,above] {};
      \end{tikzpicture}
\end{equation}

Here the label value is \ref{reactiontreeABEPQ} because  it references the equation numbering.

\end{document}

output of code

See the following question for some more detail:

As an example of a system for numbering arbitrary objects, you could use one of the linguistics example numbering packages such as gb4e. You can see this in action with trees in the second link below.

Alan Munn
  • 218,180