TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.}})

share|improve this question
    
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
4  
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
4  
\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
up vote 30 down vote accepted

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.

share|improve this answer
2  
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

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

share|improve this answer
    
This worked for me. It's the simplest solution to the problem. I tried fancyvrb package first, but I might not have understood exactly how to use \VerbatimFootnotes. – CyberFonic Jan 6 at 23:08
    
Sadly bigfoot.sty also conflicts with footmisc.sty, which allows you to have double spaced text and single spaced footnotes. Here's the example that's used to illustrate how footmisc works, but with the addition of bigfoot. Is there a way to use both and still get singlespaced footnotes with \doublespacing? \documentclass[11pt]{article} \usepackage{setspace} \usepackage{bigfoot} \usepackage{footmisc} \usepackage{lipsum} \renewcommand\footnotelayout{\fontsize{10}{12}\selectfont} \doublespacing \begin{document} \lipsum*[1]\footnote{\lipsum[1]}\footnote{\lipsum[1]} \end{document} – Leo Simon Feb 16 at 19:11
    
@LeoSimon I don't get double spaced footnotes with bigfoot alone nor with both footmisc and bigfoot, neither with or without changing \footnotelayout. (I have an up to date TeX Live, updated yesterday…) – clemens Feb 17 at 11:02
    
@clemens, thanks I'll try to figure out how to update. – Leo Simon Feb 17 at 18:21

! 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

share|improve this answer
    
Related: lrbox in \newenvironment – Qrrbrbirlbel Dec 18 '12 at 12:59

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

\documentclass{memoir}
\usepackage[T1]{fontenc}
\usepackage[nohead, paperwidth=15cm, paperheight=7cm]{geometry}
\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}
share|improve this answer

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}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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