How to obtain verbatim text in a footnote?

The package fancyvrb supports this. Just put \VerbatimFootnotes anywhere after the preamble, and then:

We can put verbatim\footnote{\verb+_Yes!_+} text in footnotes.

Edit: As pointed out in the comment and here, this may conflict with some footnote-specific packages, e.g. footmisc when used with the para option, but there are other alternatives. Quoting two from there:

  • The memoir class defines its \footnote command so that it will accept verbatim in its arguments, without any supporting package.

  • With fancyvrb, you can \SaveVerb something and \UseVerb it in a footnote.

There exist also further packages like examplep.


The bigfoot package allows verbatim material in footnotes:

\documentclass{article}
\usepackage{bigfoot}
% only for this example:
\textheight=.5in
\begin{document}

I want this.\footnote{\verb|But, it does not work!&%$|}

\end{document}

enter image description here


! LaTeX Error: \verb illegal in command argument.? Yes. sir!

Well, then let’s use it in an environment:

The environment lrbox{<savebox>} works similar to \sbox{<savebox>}{<stuff to save>} but instead of saving an argument into a savebox it’s the environment’s body that gets saved into a savebox. That savebox is then used inside the \footnote command.

Code

\documentclass{article}
\newsavebox\myVerb
\newenvironment{verbbox}{\lrbox\myVerb}{\endlrbox}
\newcommand*{\verbBox}{\usebox\myVerb}
\begin{document}
I want this.%
\begin{lrbox}\myVerb%
    \verb|You got this! % \ _ { }|%
\end{lrbox}%
\footnote{\usebox\myVerb}%

I defined a custom environment and a custom macro for you.%
\begin{verbbox}\scriptsize\verb|\begin{verbox} \verb!\begin{verbox} Inception?\end{verbox}! \end{verbbox}|\end{verbbox}%
\footnote{Wow?! \verbBox}%
\end{document}

Output

enter image description here