1

I need to draw a tree-like diagram for a classification purpose. Getting a cue from this question, I came up with the following MWE

\documentclass{book}
\usepackage[tmargin=2in, bmargin=2in, lmargin=1.5in, rmargin=1.5in]{geometry}
\usepackage{showframe}
\usepackage[edges]{forest}
\usepackage{mathtools}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{varioref}
\usepackage{cleveref}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
    \begin{enumerate}
        \item \label{usual metric on R}
        \item \label{usual metric on C}
        \item \label{usual metric on R2}
        \item \label{5}
        \item \label{6}
        \item \label{16}
        \item \label{17}
        \item \label{C_int}
        \item \label{C_sup}
        \item \label{norm}
        \item \label{extended R}
        \item \label{extended C}
        \item \label{discrete}
        \item \label{d/(1+d)}
        \item \label{product metric}
    \end{enumerate}
    \begin{figure}[h!]
        \centering
        \begin{forest}
            for tree={draw,
                where level=1{minimum height=8ex,anchor=north}{},
                align=center,
                %           font=\sffamily,
                edge+={thick, -{Stealth[]}},
                l sep'+=10pt,
                fork sep'=10pt,
            },
            forked edges,
            if level=0{
                inner xsep=0pt,
                tikz={\draw [thick] (.children first) -- (.children last);}
            }{},
            [
            \qquad Examples of metric spaces in this subsection \qquad
            [
            \Cref{usual metric on R,usual metric on C,usual metric on R2,5,6,16,17,C_int,C_sup,norm}:\\
            Normed Linear Spaces
            [
            \Cref{usual metric on R,usual metric on C,usual metric on R2,5,6}:\\
            Finite dimensional\\ vector spaces
            [\Cref{5,6}:\\ $ p $ metric and\\ \emph{sup} metric on $ \mathbb{R}^{n} $]
            [\Cref{usual metric on R,usual metric on C,usual metric on R2}:\\ usual metrics on \\ $ \mathbb{R} $\text{,} $ \mathbb{C} $ and $ \mathbb{R}^{2} $]
            ]
            [
            \Cref{16,17}:\\ Sequence spaces
            ]
            [
            \Cref{C_int,C_sup}:\\ Spaces of continuous \\ functions
            ]
            ]
            [
            \Cref{extended R,extended C}:\\ Extended set of real\\ and complex numbers
            ]
            [
            \Cref{discrete}:\\ Discrete metric
            ]
            [
            \Cref{d/(1+d),product metric}:\\ Derived metrics
            ]
            ]
        \end{forest}
        \caption{Examples of metric spaces in this subsection}
    \end{figure}
\end{document}

What I am looking for is to fit the diagram within the margin. For that, I want to left-align the boxes in the third and fourth level. Something like this:

enter image description here

How do I achieve this?

EDIT

To add more alternatives to achieve my goal, please consider the following options:

  1. the solution need not be using forest environment only. Any other way of giving the desired output is also welcome.

  2. In case it is easier, the following output also works for me as well:

enter image description here

Please help.

  • 1
    Normally you should be able to fix the problems by adding some suitable text width. However, this does not seem to work with the lengthy \Crefs you are using. (I am also wondering why you load varioref.) – user194703 Jun 8 at 19:18
  • I added varioref because I am using that package in my original document and I thought it would have some influence so I added in the MWE. I came across the forest package today itself to do this and Do not have much idea about how it works. Any help in this regard will be appreciated. – Subhajit Paul Jun 8 at 19:22
  • @Schrödinger'scat I definitely like the way it is showing the cross-references (I also have hyperref package installed in the original document) now, but an answer removing those packages would also work for me. I will insert the \Cref strings manually in that case. – Subhajit Paul Jun 8 at 19:26
  • 1
    I am definitely not suggesting to drop forest or cleveref, I just said what I tried and didn't work. – user194703 Jun 8 at 19:27
  • @Schrödinger'scat, please have a look at the edit. – Subhajit Paul Jun 9 at 14:13
1
+50

This is more an extended comment than a real answer. I tried to control the nodes via text width, and failed. Almost certainly I did something stupid. Just in case there is really a problem with text width, here is something that installs an equivalent style, and works.

\documentclass{book}
\usepackage[tmargin=2in, bmargin=2in, lmargin=1.5in, rmargin=1.5in]{geometry}
\usepackage{showframe}
\usepackage[edges]{forest}
\usepackage{mathtools}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
%\usepackage{varioref}
\usepackage{cleveref}
\usetikzlibrary{arrows.meta}
\begin{document}
    \begin{enumerate}
        \item \label{usual metric on R}
        \item \label{usual metric on C}
        \item \label{usual metric on R2}
        \item \label{5}
        \item \label{6}
        \item \label{16}
        \item \label{17}
        \item \label{C_int}
        \item \label{C_sup}
        \item \label{norm}
        \item \label{extended R}
        \item \label{extended C}
        \item \label{discrete}
        \item \label{d/(1+d)}
        \item \label{product metric}
    \end{enumerate}
\fbox{\begin{minipage}{5em}\centering
\Cref{usual metric on R,usual metric on C,usual metric on R2,5,6,16,17,C_int,C_sup,norm}:\\
Normed Linear Spaces
\end{minipage}}    
    
\begin{figure}[h!]
        \centering
        \begin{forest}
            forked edges,
            for tree={draw,l sep'+=10pt,minimum height=8ex,anchor=north,
                where level=1{anchor=north,l sep+=1.5em}{},
                where level=2{fork sep=4.2em,calign=first}{fork sep=10pt},    
                where level=0{}{execute at begin node={\begin{minipage}{6em}\centering},
                execute at end node={\end{minipage}}},
                %           font=\sffamily,
                edge+={thick, -{Stealth[]}},                                
            },
            [Examples of metric spaces in this subsection,text width=21em
            [%
            \Cref{usual metric on R,usual metric on C,usual metric on R2,5,6,16,17,C_int,C_sup,norm}:\\
            Normed Linear Spaces
            [%
            \Cref{usual metric on R,usual metric on C,usual metric on R2,5,6}:\\
            Finite dimensional\\ vector spaces
            [\Cref{5,6}:\\ $ p $ metric and\\ \emph{sup} metric on $ \mathbb{R}^{n} $]
            [\Cref{usual metric on R,usual metric on C,usual metric on R2}:\\ usual metrics on \\ $ \mathbb{R} $\text{,} $ \mathbb{C} $ and $ \mathbb{R}^{2} $]
            ]
            [%
            \Cref{16,17}:\\ Sequence spaces
            ]
            [%
            \Cref{C_int,C_sup}:\\ Spaces of continuous \\ functions
            ]
            ]
            [%
            \Cref{extended R,extended C}:\\ Extended set of real\\ and complex numbers
            ]
            [%
            \Cref{discrete}:\\ Discrete metric
            ]
            [%
            \Cref{d/(1+d),product metric}:\\ Derived metrics
            ]
            ]
        \end{forest}
        \caption{Examples of metric spaces in this subsection.}
    \end{figure}
\end{document}

enter image description here

| improve this answer | |
  • 1
    Thanks for your effort. Taking a cue from your answer, I added calign=first to my code. This is the final code for the figure and this is how it looks now. – Subhajit Paul Jun 11 at 6:33

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.