如何将列表中的评论自动转换为脚注?

如何将列表中的评论自动转换为脚注?

是否可以自动检测列表中的评论并将其转换为脚注?

答案1

尝试这个

\documentclass{article}
\usepackage{listings}
\usepackage[paperheight=13cm]{geometry}% for display purposes only

\begingroup\catcode`\^^M\active%
\gdef\gatherfootnote #1^^M{\footnote{#1}^^M}\endgroup

\lstset{language=python,escapeinside={\%}{\^^M},
        escapebegin={\gatherfootnote},
        escapeend={},numbers=left,numberstyle=\tiny}


\begin{document}
  \begin{lstlisting}
    for i in range(4): % a simple python for-loop
       print(i)
       print(2*i)
       print(3*i)
       print(4*i)% this is a code comment
       print(5*i)
       print(6*i)
       print(7*i)% and one more code comment
       print(8*i)
  \end{lstlisting}
Another code listing:
  \begin{lstlisting}
    for j in range(4): % fourth footnote 
       print(j)
       print(2*j)
       print(3*j)
       print(4*j)% fifth footnote
  \end{lstlisting}
\end{document}

在此处输入图片描述

相关内容