11

I have seen how to add verbatim text to a footnote: How to obtain verbatim text in a footnote?

What I would like to do is the opposite, include a footnote in a verbatim section. I am using the memoir class, listings for verbatim sections, and perpage footnotes. The footnote should appear at the bottom of the page just as a regular footnote would. I don't mind if the footnote symbol/superscript (not sure of technical term) is texttt vs the normal style.

3 Answers 3

16

listings provides the possibility to escape to LaTeX inside a listing, see section 5.12 Escaping to LaTeX of the manual for details. The basic idea is to set an escapechar which switches to LateX and back:

\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[basicstyle=\ttfamily,columns=fullflexible,gobble=2,escapechar=ß]
  helloß\footnote{test}ß world
\end{lstlisting}
\end{document}

enter image description here

enter image description here

Instead of escapechar=<tokens> which sets one or more characters <tokens> for starting and ending the escape you can also use escapeinside={<tokens1>}{<tokens2>} or escapebegin=<tokens1>,escapeend=<tokens2> if you want two different characters/strings <tokens1> and <tokens2> for begin and end. <tokens1> and <tokens2> also can be the same tokenlist. (Tests suggest that if <tokens> is longer than one token it cannot only contain the same token – I haven't determined a definite pattern behind this behaviour, though, and the manual doesn't seem to say anything about it.)

1
  • Use the parameter texcl of the package listing, for example \begin{lstlisting}[texcl] ... Then you can use footnotes after comment sign, e.g. "SELECT ?city ?cityLabel WHERE { # \footnote{test}" for SPARQL script. The symbol # is the SPARQL comment sign. Nov 2, 2020 at 18:44
6

Here is a version without the use of packages (i.e., works in plain verbatim environment). I make ? (or any char of your choosing) active and employ ! (or any char of your choosing) as a delimiter.

With this syntax, ?!My footnote! is the equivalent of \footnote{My footnote}.

\documentclass{article}
\textheight=1.5in\relax% FOR THIS MWE ONLY
\begin{document}
{\catcode`?=\active
\def?!#1!{\footnote{#1}}
\begin{verbatim}
This is a?!My footnote! test, and another?!another footnote!;
And now for some more?!yet another footnote! here.
Finally, the last one?!final footnote! here.
\end{verbatim}
}
\end{document}

enter image description here

2

You can use the verbatim package to define a new environment that defines a character to enclose the footnote text.

\documentclass{article}
\usepackage{verbatim}

\textheight=3cm % just not to waste space

\newcommand{\vfchar}[1]{%
  % the usual trick for using a "variable" active character
  \begingroup\lccode`~=`#1 \lowercase{\endgroup\def~##1~}{%
    % separate the footnote mark from the footnote text
    % so the footnote mark will occupy the same space as
    % any other character
    \makebox[0.5em][l]{\footnotemark}%
    \footnotetext{##1}%
  }%
  \catcode`#1=\active
}
\newenvironment{fverbatim}[1]
 {\verbatim\vfchar{#1}}
 {\endverbatim}

\begin{document}

\begin{fverbatim}{?}
Some verbatim and a footnote?Text of the footnote? here
Another?Text? and let's see the alignment
is good enough
\end{fverbatim}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .