![如何在 {forest} 环境中使用菜单键?](https://linux22.com/image/372960/%E5%A6%82%E4%BD%95%E5%9C%A8%20%7Bforest%7D%20%E7%8E%AF%E5%A2%83%E4%B8%AD%E4%BD%BF%E7%94%A8%E8%8F%9C%E5%8D%95%E9%94%AE%EF%BC%9F.png)
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
环境引起的。解决此问题的最简单方法是将密钥保存在\sbox
es 中。
% !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}