How to Center Titles in Forest Trees?
That's because you're setting the caption as a node, which is also not the standard way of doing this. Captions are added to figures in a Latex document externally to the picture, i.e. they are not part of it.
Also, you're manually assigning a number to your figure and this makes the use of Latex a bit pointless, because one of the great advantages of using Latex is that captions are automatically numbered.
You can customize them, but they are automatic, so if you add another figure, they are all automatically fixed, while in your case, you'd have to fix each one manually. And that is not good in a long document.
You can add \renewcommand{\thefigure}{\Roman{figure}}
to your preamble to make figure numbering with uppercase Roman numerals, and \usepackage[labelfont=bf]{caption}
to make it bold.
Output
Code
\documentclass{article}
\usepackage{forest}
\usepackage[labelfont=bf]{caption}
\renewcommand{\thefigure}{\Roman{figure}}
\begin{document}
\begin{figure}
\centering
\caption{Extensive Form of a Centipede Game Variant}
\medskip
\begin{forest} for tree={l sep=4em, s sep=8em, anchor=center}
[$P_1$, circle, draw,
[{2, 2}, edge label={node[midway,left]{$D$}}]
[$P_2$, edge label={node[midway,right]{$A$}}, circle, draw,
[{1, 1}, edge label={node[midway,left]{$d$}}]
[$P_1$, edge label={node[midway,right]{$a$}}, circle, draw,
[{0, 0}, edge label={node[midway,left]{$D$}}]
[{3, 3}, edge label={node[midway,right]{$A$}}]]]]
\end{forest}
\end{figure}
\end{document}
Alenanno is entirely correct about how to handle figure
captions. However, it might nonetheless be useful to know how to centre a node relative to the tree.
This can be done by centring the node relative to the current bounding box of the picture, after the entire tree is drawn, rather than centring it relative to some particular node.
\node at ([yshift=10pt]current bounding box.north) {Centred within Tree: Extensive Form of a Centipede Game Variant};
Zarko is quite correct, as well, but I'm lazier and prefer to handle things automatically. So I'd use something which made specifying the tree, including the labels, a bit simpler by modifying the tree's preamble a bit.
\begin{forest}
First, let's create a style with the options common to all edge label
s. This will be some positive inner sep
and midway
.
/tikz/my edge label/.style={inner sep=5pt, midway},
Now for the tree configuration.
for tree={
l sep=4em,
s sep=8em,
anchor=center,
We'll delay our changes a bit to make sure the options are set.
before typesetting nodes={
If the node is the first child going from right to left, then any edge labels should be set on the right, as well as using the style for all edge labels. Also, the edge label
specification is essentially the same in all cases, so we might as well do the whole node ...
thing here as well.
if n'=1{
edge label/.wrap value={\noexpand node [my edge label,right] {$#1$} }
If not, the node must be on the left, because all parent nodes have exactly zero or two children. (Either n=1
or n'=1
except for the root node which couldn't have an edge label
being nobody's child at all.)
}{
edge label/.wrap value={\noexpand node [my edge label,left] {$#1$} }
},
},
All non-terminal nodes get the options circle
and draw
, so we don't need to specify the options manually each time.
if n children=0{}{
circle,
draw,
}
}
With this configuration in place, we can specify the tree in the following way.
[$P_1$
[{2, 2}, edge label=D]
[$P_2$, edge label=A
[{1, 1}, edge label=d]
[$P_1$, edge label=a
[{0, 0}, edge label=D]
[{3, 3}, edge label=A]
]
]
]
to produce
Complete code:
\documentclass{article}
\usepackage{forest}
\usepackage[labelfont=bf]{caption}
\renewcommand{\thefigure}{\Roman{figure}}
\begin{document}
\begin{figure}
\centering
\caption{Extensive Form of a Centipede Game Variant}
\medskip
\begin{forest}
/tikz/my edge label/.style={inner sep=5pt, midway},
for tree={
l sep=4em,
s sep=8em,
anchor=center,
before typesetting nodes={
if n'=1{
edge label/.wrap value={\noexpand node [my edge label,right] {$#1$} }
}{
edge label/.wrap value={\noexpand node [my edge label,left] {$#1$} }
},
},
if n children=0{}{
circle,
draw,
}
}
[$P_1$
[{2, 2}, edge label=D]
[$P_2$, edge label=A
[{1, 1}, edge label=d]
[$P_1$, edge label=a
[{0, 0}, edge label=D]
[{3, 3}, edge label=A]
]
]
]
\node at ([yshift=10pt]current bounding box.north) {Centred within Tree: Extensive Form of a Centipede Game Variant};
\end{forest}
\end{figure}
\end{document}
Regarding distance between edge labels and edges, see if the following addition to Alenanno's code gives what you looking for:
\tikzset{el/.style = {% edge label
midway, outer sep=1.5mm, #1} % <--- #1: for position (left, right)
}
Put this before \begin{forest}
and than instead of edge label={node[midway,left]{...}
use
edge label={node[el=left]{...}
or
edge label={node[el=right]{...}
depending on which side of the edge the label should go. With it the graph becomes: