# Shift children and subtrees horizontally in forest

I am trying to draw a tree diagram with forest package. In order to have the diagram fitting within the page margins, I moved down the subtree of NastranBulkData object, as you can see from the image. However, there is still quite a big white space left between NastranCaseControl object and NastranBulkData object that prevents the diagram from fitting into the page margins.

How can I remove such white space so that both NastranBulkData object and NastranSubcaseResult object are shifted left? Ideally they should be shifted in such a way that the space between Objects related to other Nastran case control entries and the line departing from NastranBulkData object is equal to the separation between children in the tree (but I do not care too much about this).

MWE:

% Class
\documentclass[10pt]{article}

% Set paper geometry
\usepackage[letterpaper,margin=1in]{geometry}

% Forest package
\usepackage[edges]{forest}

% Document
\begin{document}
\begin{figure}[hbt!]
\centering
\begin{forest}
for tree={              % style of tree nodes
draw, semithick, rounded corners,
align = center,
inner sep = 2mm,
anchor=north,
%                     styles of tree
forked edge,
l sep = 6mm,
fork sep = 3mm,
}
[\texttt{NastranAnalysis}\\object
[\texttt{NastranExecutiveControl}\\object
[Solution\\sequence \#]
]
[\texttt{NastranCaseControl}\\object
[\texttt{NastranSubcase}\\object array]
[Objects related to\\other \textsc{Nastran}\\case control entries]
]
[\texttt{NastranBulkData}\\object
[\texttt{Grid}\\object array, fork sep=3cm, l*=5, name=grid]
[Objects related to other \\\textsc{Nastran} bulk data cards, fork sep=3cm, l*=5]
[\texttt{NastranPart}\\object array, fork sep=3cm, l*=5
[\texttt{NastranPart}\\object 1]
[\texttt{NastranPart}\\object 2]
[\dots]
]
]
[\texttt{NastranSubcaseResult}\\object array]
]
\end{forest}
\end{figure}
\end{document}


Try the following:

\documentclass[10pt]{article}
% Set paper geometry
\usepackage[letterpaper,margin=1in]{geometry}
% Forest package
\usepackage[edges]{forest}

% Document
\begin{document}
\begin{figure}[hbt!]
\centering
\begin{forest}
for tree={              % style of tree nodes
draw, semithick, rounded corners,
font=\small,        % <---
align = center,
inner sep = 2mm,
anchor=north,
%                     styles of tree
forked edge,
s sep = 2mm,
l sep = 6mm,
fork sep = 3mm,
}
[\texttt{NastranAnalysis}\\object
[\texttt{NastranExecutiveControl}\\object
[Solution\\sequence \#]
]
[\texttt{NastranCaseControl}\\object
[\texttt{NastranSubcase}\\object array]
[Objects related to\\other \textsc{Nastran}\\case control entries]
]
[\texttt{NastranBulkData}\\object
[, l*=4,coordinate
[\texttt{Grid}\\object array]
[Objects related to other \\\textsc{Nastran} bulk data cards]
[\texttt{NastranPart}\\object array,
[\texttt{NastranPart}\\object 1]
[\texttt{NastranPart}\\object 2]
[\dots]
]
]
]
[\texttt{NastranSubcaseResult}\\object array]
]
\end{forest}
\end{figure}
\end{document}


In code is added additional level with node as coordinate. Also is reduced font size to\small. One option is also to reduceinner sep to 1mm and use normal font size.

(red lines indicate text area borders)

• Grid object array is at the same level of Objects related to other Nastran bulk data cards. Apart from this, your solution works perfectly adding l*=4 after coordinate. – fma Sep 13 '19 at 18:52
• huh, I insert coordinate on the wrong level :-(. Corrected now. – Zarko Sep 13 '19 at 19:22
• This does not seem to reproduce the OP's tree. For instance, in your tree, the Grid node has two children while in the OP's version it has none. – Schrödinger's cat Sep 13 '19 at 21:16
• @Schrödinger'scat, you are right. Corrected. – Zarko Sep 14 '19 at 5:15

A reliable way of shifting nodes in a forest is to use the before computing xy key. (I also added calign child=2 to avoid the kink.)

\documentclass[10pt]{article}

% Set paper geometry
\usepackage[letterpaper,margin=1in,showframe]{geometry}

% Forest package
\usepackage[edges]{forest}

% Document
\begin{document}
\begin{figure}[hbt!]
\centering
\begin{forest}
for tree={              % style of tree nodes
draw, semithick, rounded corners,
align = center,
inner sep = 2mm,
anchor=north,
%                     styles of tree
forked edge,
l sep = 6mm,
fork sep = 3mm,s sep=0.35em
}
[\texttt{NastranAnalysis}\\object
[\texttt{NastranExecutiveControl}\\object
[Solution\\sequence \#]
]
[\texttt{NastranCaseControl}\\object
[\texttt{NastranSubcase}\\object array]
[Objects related to\\other \textsc{Nastran}\\case control entries]
]
[\texttt{NastranBulkData}\\object,before computing
xy={s+=-9em},calign child=2
[\texttt{Grid}\\object array, fork sep=3cm, l*=5, name=grid]
[Objects related to other \\\textsc{Nastran} bulk data cards, fork sep=3cm, l*=5]
[\texttt{NastranPart}\\object array, fork sep=3cm, l*=5
[\texttt{NastranPart}\\object 1]
[\texttt{NastranPart}\\object 2]
[\dots]
]
]
[\texttt{NastranSubcaseResult}\\object array,
before computing xy={s+=-9em}]
]
\end{forest}
\end{figure}
\end{document}


The showframe option is used to show that the resulting tree really fits on the page, of course you want to drop it in your document.