Superscripts in verbatim environment

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?

-

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}


-

Update: Don't know why I did not see earlier that you wanted to use numbers instead of just an asterix. Here is a version that allows you to use ^1 to get a superscript added with the given number to yield:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\lstdefinestyle{myVerbatimStyle} {
basicstyle=\ttfamily,
moredelim=[is][\color{blue}\ttfamily\kern-0.1ex\textsuperscript]{^}{\ },
}

\begin{document}
\begin{lstlisting}[style=myVerbatimStyle]
line 1
^1 line 2
line 4
^2 line 5
\end{lstlisting}
\end{document}


Note that this does require you to leave a space as in ^1 line. If you do not like that another option is to use ^ character on both sides and change the moredelim line to read:

moredelim=[is][\color{blue}\ttfamily\kern-0.1ex\textsuperscript]{^}{^},


In this case you would use ^1^line, but I think the one with the space is better.

Here is a revised solution using the listings package, which will automatically superscript the asterix for you, and also add a color to make it stand out:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\lstdefinestyle{myVerbatimStyle} {
basicstyle=\ttfamily,
otherkeywords={*},
morekeywords={*},
keywordstyle={\color{red}\ttfamily\kern-0.1ex\textsuperscript},
}

\begin{document}
\begin{lstlisting}[style=myVerbatimStyle]
line 1
*line 2
line 3
*line 4
line 5
\end{lstlisting}
\end{document}


Old Solution: With the listings package you can use mathescape=true, which means that you can enclose any math within a $...$ and it will get formatted:

\documentclass{article}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[mathescape=true]
line 1
${}^\ast$ line 2
line 3
${}^\ast$ line 4
line 5
\end{lstlisting}
\end{document}

-
Good alternative. Perhaps add the option of making the formatting similar to the verbatim environment by using basicstyle=\ttfamily. Additionally, compare the left margin with that of the verbatim environment. Anything to emulate the verbatim environment exactly? I know, I'm just nitpicking. – Werner Oct 31 '11 at 20:35
@Werner: Good points. See updated solution. – Peter Grill Nov 1 '11 at 0:17