如何在 {forest} 环境中使用菜单键?

如何在 {forest} 环境中使用菜单键?

menukeys我正在尝试使用和包在图表中显示带有按键图形表示的菜单层次结构forest。我已使用以下答案中的代码按我想要的方式格式化森林:制作(简单)目录树

问题是该\keys{}命令在森林内不起作用。我该如何让它起作用?

从此代码中:

\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}

我得到这个输出: 上述代码的输出

请注意,图表中每个键之前的键都缺失了\rightarrow

答案1

这很可能是由嵌套tikzpicture环境引起的。解决此问题的最简单方法是将密钥保存在\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}

代码输出

答案2

这是对Alan Munn 的回答。它展示了如何使用edges库大大简化代码。如果您拥有 Forest 的 1 版且无法更新,请坚持使用原始代码。否则,该edges库提供了更灵活、更强大的解决方案。

\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}

包含 <code>edges</code> 库的目录结构

相关内容