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


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:

  helloß\footnote{test}ß world

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

  • 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

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

\textheight=1.5in\relax% FOR THIS MWE ONLY
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.

enter image description here


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


\textheight=3cm % just not to waste space

  % 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


Some verbatim and a footnote?Text of the footnote? here
Another?Text? and let's see the alignment
is good enough


enter image description here

You must log in to answer this question.

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