# How can I use menukeys inside a {forest} environment?

I'm trying to show a menu hierarchy in a diagram with graphical representations of keys using the menukeys and forest packages. I have used the code from the following answer for formatting a forest the way that I would like it: Making a (simple) directory tree

The problem is that the \keys{} command doesn't work inside the forest. how can I make it work?

From this code:

\documentclass[12pt, a4paper]{article}
\usepackage[textwidth=16cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{forest}

\begin{document}

The menu can be navigated with the keyboard, from the main menu you can press \keys{A} to go the menu 2 or \keys{B} to go to menu 3.

\begin{forest}
for tree={
font=\ttfamily,
grow'=0,
child anchor=west,
parent anchor=south,
anchor=west,
calign=first,
edge path={
\noexpand\path [draw, \forestoption{edge}]
(!u.south west) +(7.5pt,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
},
before typesetting nodes={
if n=1
{insert before={[,phantom]}}
{}
},
fit=band,
before computing xy={l=15pt},
}
[ \keys{A} $\rightarrow$ Menu 2
[Foo]
[Bar]
[Baz]
]
[\keys{B} $\rightarrow$ Menu 3
[Quux]
]
]
\end{forest}

\end{document}


I get this output:

Note that the keys are missing from the diagram, just before each \rightarrow.

This is most likely caused by nesting tikzpicture environments. The simplest way to get around it would be to save your keys in \sboxes.

% !TEX TS-program = pdfLaTeX
\documentclass[12pt, a4paper]{article}
\usepackage[textwidth=16cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{forest}
\newsavebox{\keyA}
\sbox{\keyA}{\keys{A}}
\newsavebox{\keyB}
\sbox{\keyB}{\keys{B}}
\begin{document}

The menu can be navigated with the keyboard, from the main menu you can press \keys{A} to go the menu 2 or \keys{B} to go to menu 3.

\begin{forest}
for tree={
font=\ttfamily,
grow'=0,
child anchor=west,
parent anchor=south,
anchor=west,
calign=first,
edge path={
\noexpand\path [draw, \forestoption{edge}]
(!u.south west) +(7.5pt,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
},
before typesetting nodes={
if n=1
{insert before={[,phantom]}}
{}
},
fit=band,
before computing xy={l=15pt},
}
[ \usebox{\keyA} $\rightarrow$ Menu 2
[Foo]
[Bar]
[Baz]
]
[\usebox{\keyB} $\rightarrow$ Menu 3
[Quux]
]
]
\end{forest}

\end{document}


• You don't like the new-fangled edge path' then? – cfr Mar 26 '18 at 23:24
• @cfr Unlike some people I don't have the intricacies of forest embedded in my brain. ;) – Alan Munn Mar 27 '18 at 0:27
• Bits you do, though. Like the fit to stuff I always forget. However, that bit, yes, is embedded in mine now. Mostly, because it is so much less typing :-). – cfr Mar 27 '18 at 0:34

This is a supplement to Alan Munn's answer. It shows how to greatly simplify the code by using the edges library. If you have version 1 of Forest and cannot update, stick to the original code. Otherwise, the edges library offers a more flexible and robust solution.

\documentclass[12pt, a4paper]{article}
\usepackage[textwidth=16cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[edges]{forest}
\newsavebox{\keyA}
\sbox{\keyA}{\keys{A}}
\newsavebox{\keyB}
\sbox{\keyB}{\keys{B}}
\begin{document}

The menu can be navigated with the keyboard, from the main menu you can press \keys{A} to go the menu 2 or \keys{B} to go to menu 3.

\begin{forest}
for tree={
folder,
grow'=0,
edge label={node [midway, inner sep=1.25pt, fill] {}},
font=\ttfamily,
}
[ \usebox{\keyA} $\rightarrow$ Menu 2
[Foo]
[Bar]
[Baz]
]
[\usebox{\keyB} $\rightarrow$ Menu 3
[Quux]
]
]
\end{forest}

\end{document}


• That does look much simpler, I'll give it a go, thanks. – DanJAB Mar 28 '18 at 1:31