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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a macro that I define as


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


The configuration of my lstset is:

% Configuration for the listing package.

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
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
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 '14 at 13:04

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:


in the preamble ensures this.

share|improve this answer

Your Answer


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.