[Tex/LaTex] Superscripts in verbatim environment

superscriptsverbatim

I have some lines in a verbatim environment

...
\begin{verbatim}
    line 1
    * line 2
    line 3
    * line 4
    line 5
\end{verbatim}
...

at the beginning of some lines I want to add a number in superscript. I've indicated that with asterisks now. How can I do this?

Best Answer

You could use the numbers option from the fancyvrb package:

\documentclass{article}
\usepackage{fancyvrb}

\begin{document} 

\begin{Verbatim}[numbers=left]
First verbatim line.
Second verbatim line.
\end{Verbatim}

\end{document}

If the numbers must appear as superscript, add the following to the preamble:

\renewcommand{\theFancyVerbLine}{\textsuperscript{\arabic{FancyVerbLine}}}

Now that the original question has been edited, you can use the commandchars option to introduce escape sequences in verbatim code:

\documentclass{article}
\usepackage{fancyvrb}

\begin{document} 

\begin{Verbatim}[commandchars=\\\{\}]
First verbatim line.
\textsuperscript{1}Second verbatim line.
Third verbatim line.
\textsuperscript{2}Fourth verbatim line.
\end{Verbatim}

\end{document}

enter image description here

Related Question