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 want to underline some code in my source code included using listings. I did the following but the underline is including the white space before the statement. How could I do it so that it only underlines "return 0"?

\lstset{
language=C, 
basicstyle=\ttfamily,
moredelim=[is][\underbar]{_}{_},
}

\begin{lstlisting}
int main() {
  _return 0_;
}
\end{lstlisting}
share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

Use keepspaces=true as part of your listing settings.

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\begin{document}

\lstset{
  language=C, 
  basicstyle=\ttfamily,
  moredelim=[is][\underbar]{_}{_}
}

\begin{lstlisting}
int main() {
  _return 0_;
}
\end{lstlisting}

\begin{lstlisting}[keepspaces=true]
int main() {
  _return 0_;
}
\end{lstlisting}

\end{document}
share|improve this answer
    
thanks! that works. –  umberto Jan 3 '12 at 1:02
add comment

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.