How to obtain verbatim text in a footnote?

I want to employ a \verb command in a footnote. But, when I run pdflatex on

\documentclass{article}

\begin{document}

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

\end{document}


I get this:

! LaTeX Error: \verb illegal in command argument.


How can I do what I am after? (I know that I could just use \footnote{\texttt{But, I lose the semantic mark-up.}})

-
You want to do something that LaTeX doesn't allow you to do. You either (a) go in and mess with the code under the hood to allow you to do it or (b) work around it. For (b), it'd be nice to know what "semantic mark-up" you are after. –  Willie Wong Jul 26 '10 at 22:53
1) I know that LaTeX doesn't allow it. (a) That is in the question. (b) I'm trying to help seed in the beta. 2) I'm puzzled by your (b). As I understand it \texttt{} is formatting mark-up, and \verb|| is semantic mark-up. (You mark something as \verb and leave the formatting of it up to LaTeX and your documentclass. –  vanden Jul 26 '10 at 23:04
\verb is absolutely the opposite of semantic markup it is just a low level instruction to the TeX parser to change the lexical analysis of its input (which is why it doesn't work in macro arguments where the input has already been tokenised). It doesn't say anything about what the content means, just that \  and {} should be parsed in a different way. –  David Carlisle Dec 18 '12 at 12:22

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.

-
Note, though, that \VerbatimFootnotes doesn't play well with some other packages. See the UK TeX FAQ entry for discussion and alternatives. –  vanden Jul 26 '10 at 23:08

! 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

-
Related: lrbox in \newenvironment –  Qrrbrbirlbel Dec 18 '12 at 12:59

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}


-

Another plug for the memoir class: its \verbfootnote allows for verbatim material.

\documentclass{memoir}
\usepackage[T1]{fontenc}
\pagestyle{empty}
\begin{document}
% from the memmoir manual (§ 12.1.1):
Below, footnote~\ref{fn1} is a \verb?\footnote? while
footnote~\ref{fn2} is a \verb?\verbfootnote?.

The \verb?\verbfootnote? command should appear%
\footnote{There may be some problems if color is used.\label{fn1}} %
to give identical results as the normal \verb?\footnote?, but it can
include some verbatim text%
\verbfootnote{The \verb?\footnote? macro, like all other macros except
for \verb?\verbfootnote?, can not contain verbatim text in its
argument.\label{fn2}} %
in the \meta{text} argument.

\end{document}

-

In this approach, the verbatim material is placed in a box prior to entering the footnote, and the box is recalled within the footnote.

\documentclass{article}
\usepackage{verbatimbox}
\textheight=1in
\begin{document}
I am ready for a footnote.%
\begin{verbbox}[\footnotesize]
\verbatim material
\end{verbbox}
\footnote{See if \theverbbox\ can appear in footnotes.} And we continue\ldots

\end{document}


-