8

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}
\usepackage{menukeys}

\begin{document}
\section{Menu structure}

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},
  }
[Main menu
  [ \keys{A} $\rightarrow$ Menu 2
    [Foo]
    [Bar]
    [Baz]
  ]
  [\keys{B} $\rightarrow$ Menu 3
    [Quux]
  ]
]
\end{forest}

\end{document}

I get this output: Output from code above

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

2 Answers 2

6

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}
\usepackage{menukeys}
\newsavebox{\keyA}
\sbox{\keyA}{\keys{A}}
\newsavebox{\keyB}
\sbox{\keyB}{\keys{B}}
\begin{document}
\section{Menu structure}

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},
  }
[Main menu
  [ \usebox{\keyA} $\rightarrow$ Menu 2
    [Foo]
    [Bar]
    [Baz]
  ]
  [\usebox{\keyB} $\rightarrow$ Menu 3
    [Quux]
  ]
]
\end{forest}

\end{document}

output of code

3
  • You don't like the new-fangled edge path' then? Commented Mar 26, 2018 at 23:24
  • @cfr Unlike some people I don't have the intricacies of forest embedded in my brain. ;) Commented Mar 27, 2018 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 :-). Commented Mar 27, 2018 at 0:34
1

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}
\usepackage{menukeys}
\newsavebox{\keyA}
\sbox{\keyA}{\keys{A}}
\newsavebox{\keyB}
\sbox{\keyB}{\keys{B}}
\begin{document}
\section{Menu structure}

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,
  }
  [Main menu
    [ \usebox{\keyA} $\rightarrow$ Menu 2
      [Foo]
      [Bar]
      [Baz]
    ]
    [\usebox{\keyB} $\rightarrow$ Menu 3
      [Quux]
    ]
  ]
\end{forest}

\end{document}

directory structure with <code>edges</code> library

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.