我\Verb
在脚注中遇到了一个奇怪的错误:
\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
Works inline \Verb!S#Var! but doesn't in footnote.
% \footnote{No really, it doesn't \Verb!S#Var! you see.}
\end{document}
当我取消注释脚注行时,投诉是:
You can't use `macro parameter character #' in restricted horizontal mode.
答案1
有趣的练习。;-)
\documentclass{article}
\usepackage{regexpatch,fancyvrb,xparse}
\makeatletter
\let\do@footnotetext\@footnotetext
\regexpatchcmd{\do@footnotetext}
{\c{insert}\c{footins}\cB.(.*)\cE.}
{\1\c{egroup}}
{}{}
\def\@footnotetext{\insert\footins\bgroup\@makeother\#\do@footnotetext}
\newcommand{\ttvar}{\begingroup\@makeother\#\@ttvar}
\newcommand{\@ttvar}[1]{\ttfamily\detokenize{#1}\endgroup}
\makeatother
\setlength{\textheight}{3cm} % just to shorten the height for the example
\begin{document}
Works inline \ttvar{S#Var} and also in footnote.%
\footnote{Yes, \ttvar{S#Var}, as you see.}
\end{document}
您\detokenize
可以打印所有字符,除了反斜杠和%
(实际上是允许使用反斜杠,但它们可能会产生不需要的空格),只要括号是匹配的。
诀窍是修改\@footnotetext
而不是吸收其参数,而只是执行\insert\footins\bgroup
,更改类别代码#
(您肯定不会在脚注中定义命令)并调用\do@footnotetext
将吸收参数。有人可能会喜欢看看\regexpatch
它在原始副本上是如何工作的\@footnotetext
。
该命令\ttvar
将本地更改的类别代码#
。
替代解决方案。
\documentclass{article}
\usepackage{fancyvrb,cprotect}
\begin{document}
Works inline \Verb!S#Var! and also in footnote.%
\cprotect\footnote{Yes, \Verb!S#Var!, as you see.}
\end{document}
答案2
井号字符似乎可以被转义:
\footnote{No really, it doesn't \Verb!S\#Var!}