5

I have below code available in LaTeX, I want to add caption in \begin{forest} please suggest how to do the same.

\documentclass[review]{elsarticle}
\usepackage[edges]{forest}

\begin{document}
\begin{figure}
    \begin{forest}
        for tree={
            align=center,
            parent anchor=south,
            child anchor=north,
            font=\sffamily,
            edge={thick, -{Stealth[]}},
            l sep+=10pt,
            edge path={
                \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-5pt) -| (.child anchor)\forestoption{edge label};
            },
            if level=0{
                inner xsep=0pt,
                tikz={\draw [thick] (.south east) -- (.south west);}
            }{}
        }
        % Some test here

    \end{forest}
\caption{Tree}
\end{figure}
\end{document}

This code gives error as below:

Extra }, or forgotten \endgroup. \end{forest}
2
  • 1
    Please edit your question to post complete code for a minimal document. The above code will not compile if completed in the obvious way, so remember to check it works before posting!
    – cfr
    Jan 25, 2018 at 2:09
  • 1
    Forest abhors a vacuum: you can't leave blank lines. Is that semi-colon intentional?
    – cfr
    Jan 25, 2018 at 2:11

1 Answer 1

6

Here's a working version of your example, updated for current Forest and sans the (presumably spurious) semi-colon.

Note, especially, the removal of the blank line. You cannot include a blank line in a forest. It doesn't matter whether you want a caption or not. The blank line will always give you an error.

\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{forest}
  forked edges,
  for tree={
    align=center,
    font=\sffamily,
    edge={thick, -{Stealth[]}},
    l sep+=10pt,
    if level=0{
      inner xsep=0pt,
      tikz={\draw [thick] (.south east) -- (.south west);}
    }{}
  }
  [Sensor Based \\ Methods
    [4W1H \\ \cite{178}]
    [Wearable sensor \\ \cite{179}]
    [Depth sensor \\ \cite{190}]
    [Smartphone  \\ sensor \\ \cite{191}]
    [Time of Flight sensor \\ \cite{192}]
    [Inertial sensor \\ \cite{156}]
  ]
\end{forest}  
\end{document}

If you want a caption, switch to article and put the tree in a figure.

\documentclass{article}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{figure} 
  \centering   
  \begin{forest}
  ...
  \end{forest}
  \caption{Tree}\label{fig:tree}
\end{figure}
\end{document}

Please read one of the many excellent introductions to LaTeX so you get a sense of the basics.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .