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 a macro that I define as

\newcommand{\versionPath}{5\textunderscore0\textunderscore7} 

to denote the word *5_0_7*, which I use in lstlisting's like this:

\begin{lstlisting}
(*\versionPath*)
\end{lstlisting}

The configuration of my lstset is:

% Configuration for the listing package.
\lstset{%
language=,
escapeinside={(*}{*)},%
frame=single,
breaklines=true,
basicstyle=\color{blue}\ttfamily\scriptsize,
rulecolor=\color{light-gray},
keywordstyle=\ttfamily\color{red},
upquote=true,
columns=fullflexible,
literate={*}{{\char42}}1
         {-}{{\char45}}1
}

The problem is that once compiled, the underscores in the macro look slightly different to those produced otherwise in the lstlisting, and when copied from the PDF they turn up as blank spaces. How do I get the underscores in the macro to format properly?

share|improve this question
3  
perhaps you might make a minimal (but compilable) example; without an example, those of us who don't regularly use listings are stuck trying to replicate you problem. (fwiw, \_, in the default case, doesn't produce a character, so there's nothing to copy out of the pdf file.) –  wasteofspace Nov 16 '12 at 14:14
    
Does tex.stackexchange.com/q/34129/5763 help? –  Martin Schröder Nov 16 '12 at 14:15
1  
If you need that \_ or \textunderscore produces a copyable character, load \usepackage[T1]{fontenc}. –  egreg Nov 16 '12 at 16:56
    
@egreg: You solution works great, so that should be the answer. –  Peter Grill Nov 17 '12 at 5:34
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Jan 16 at 13:04
add comment

1 Answer

When the font encoding is OT1 (the default), the command \_ is implemented using a rule, that can't be copied from a PDF. By contrast, a literal _ in a lstlisting environment uses the real glyph present in the typewriter type font.

If you need to be able to copy the underscore, then the only solution is to make sure it is always using a real glyph:

\usepackage[T1]{fontenc}

in the preamble ensures this.

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