I get a strange error with a \Verb escape in a footnote:

Works inline \Verb!S#Var! but doesn't in footnote.
% \footnote{No really, it doesn't \Verb!S#Var! you see.}

When I uncomment the footnote line, the complaint is:

You can't use `macro parameter character #' in restricted horizontal mode.

Fun exercise. ;-)


\setlength{\textheight}{3cm} % just to shorten the height for the example

Works inline \ttvar{S#Var} and also in footnote.%
\footnote{Yes, \ttvar{S#Var}, as you see.}

enter image description here

With \detokenize you can print all characters, except backslashes and % (actually backslashes are allowed, but they might produce unwanted spaces), so long as braces are balanced.

The trick is to modify \@footnotetext not to absorb its argument, but just doing \insert\footins\bgroup, changing the category code of # (you quite certainly won't be defining commands in a footnote) and calling \do@footnotetext that will absorb the argument. Somebody might enjoy looking how \regexpatch does its work on a copy of the original \@footnotetext.

The command \ttvar will locally change the category code of #.

Alternative solution.

Works inline \Verb!S#Var! and also in footnote.%
\cprotect\footnote{Yes, \Verb!S#Var!, as you see.}
| improve this answer | |

The hash character can be escaped it seems:

\footnote{No really, it doesn't \Verb!S\#Var!}
| improve this answer | |
  • 1
    You can not use verb in a macro argument that is just \texttt(S\#Var} – David Carlisle Oct 19 '13 at 20:18
  • 1
    @DavidCarlisle Actually \Verb doesn't throw an error, but I believe the result will be unpredictable, in general. – egreg Oct 19 '13 at 20:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.