Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 8 down vote accepted

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

share|improve this answer

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:

enter image description here

\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:

enter image description here

\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}
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.