3

I am trying to draw a tree in LaTeX via following code. It works; however, it is always aligned to the left. How I can make it aligned to the centre?

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[]{forest}
\forestset{.style={for tree={parent anchor=south, child anchor=north,align=center,inner sep=2pt}}}

\begin{forest}
[TP
    [DP [D\\the] [NP [N\\man ]]]
    [T'
        [T\\will ]
        [VP
            [V\\eat ]
                [DP [D\\a ] [NP [N\\burger ]]]
        ]
    ]
]
\end{forest}

Note: The code is taken from https://msu.edu/~amunn/latex/sharelatex-trees-forest-v1.pdf (Link deprecated; new version here)

  • There's a new version of the document you link to. I've removed the old one and added a new link. – Alan Munn Dec 3 '18 at 17:12
  • Either you have a fairly outdated version of Forest or your settings are not being applied to your trees. – cfr Dec 4 '18 at 0:15
5

align=center only centres text within nodes. It does not centre the forest relative to the page.

A more fundamental problem is that the settings in .style will not be applied with current Forest. To see this, try adding, blue and notice that your tree stays determinedly black.

Current Forest provides default preamble instead.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[]{forest}
\forestset{default preamble={for tree={blue,parent anchor=children, child anchor=parent,align=center,inner sep=2pt}}}
\begin{document}
\begin{center}
  \begin{forest}
    [TP
        [DP [D\\the] [NP [N\\man ]]]
        [T'
            [T\\will ]
            [VP
                [V\\eat ]
                    [DP [D\\a ] [NP [N\\burger ]]]
            ]
        ]
    ]
  \end{forest}
\end{center}

\end{document}

blue tree

Obviously, you should delete the blue in your real document.

You might, however, want to use

\usepackage[linguistics]{forest}

instead which will apply a nice (for linguistics!) set of defaults to your trees without your having to specify them.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[linguistics]{forest}
% \forestset{default preamble={for tree={blue,parent anchor=children, child anchor=parent,align=center,inner sep=2pt}}}
\begin{document}
\begin{center}
  \begin{forest}
    [TP
        [DP [D\\the] [NP [N\\man ]]]
        [T'
            [T\\will ]
            [VP
                [V\\eat ]
                    [DP [D\\a ] [NP [N\\burger ]]]
            ]
        ]
    ]
  \end{forest}
\end{center}

\end{document}

with <code>linguistics</code>

  • This should be the accepted answer. The original code was a hack for forest v1 and was needed because Sharelatex was out of date I forgot that I had that version documented on my web page. – Alan Munn Dec 4 '18 at 0:40
  • @AlanMunn Another answer was accepted before I posted this, so I don't know. Ignasi's answer does answer the actual question. It just repeats the no-longer-working hack. – cfr Dec 4 '18 at 1:11
  • Thanks @cfr, I also think this is the correct answer (I changed the correct answer to this one) – woody Dec 4 '18 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.