# How to make text copy in PDF previewers ignore lineno line numbers?

I'm using lineno for a document. I have it configured to put line numbers in the margins of the document. I generate PDF using pdflatex. Here's the problem: when people copy a region of text in a PDF previewer (Acrobat, or Mac OS X's Preview), the line numbers are included in the text that's copied. This leads to confusing text when they paste it into another document or application.

Is there a way to make it so that the line numbers are ignored when someone copies text from within a PDF previewer? I would like just the main text to be copied, not the line numbers.

• This may depend on how you are generating the document, so it is best to compose a MWE that illustrates the problem including the \documentclass so that those trying to help don't have to recreate it. This will also help us to test that the solution actually works in your case. – Peter Grill Oct 7 '11 at 3:37
• – Peter Grill Oct 7 '11 at 3:38

Using a method similar to that proposed in How to make listings code correct copyable from PDF and with hyperlink, the following works (at least in Adobe Reader).

The accsupp package provides accessibility support for PDFs. By using the \BeginAccSupp and \EndAccSupp group, the line numbers can be typeset in the PDF, but actually replaced by an empty space {}. This is done by supplying the option ActualText={}. As such, a simple redefinition of the linenumber counter (the basic/standard counter providing the line numbers for lineno) printing mechanism \thelinenumber does the trick:

\documentclass{article}
\usepackage{lineno}% http://ctan.org/pkg/lineno
\usepackage{accsupp}% http://ctan.org/pkg/accsupp
\usepackage{lipsum}% http://ctan.org/pkg/lipsum

\renewcommand{\thelinenumber}{% Line number printing mechanism
\BeginAccSupp{ActualText={}}\arabic{linenumber}\EndAccSupp{}%
}

\begin{document}
\linenumbers% Turn line numbering on

\lipsum[1-5]% Dummy text
\end{document}


• Great, I didn't know about the ActualText feature in PDF until now. Thanks – topskip Oct 7 '11 at 7:46
• Thank you for this answer (complete with full working example, no less!). I didn't know accsupp existed. I tried it, and indeed it works, but apparently only in Acrobat; on Mac OS X 10.6, using Preview or a 3rd-party application like Skim, the full text (including line numbers) is still copied. Do you (or anyone else) have any thoughts about how to make it work for other PDF readers too? (In any case, thank you again for solving it for Acrobat; that's miles better than not having any solution.) – mhucka Oct 10 '11 at 3:58
• @mhucka: I work with Acrobat Reader 99.9% of the time. Therefore, I don't know about other alternatives. The accsupp documentation also suggests ActualText while referencing (only) Adobe Reader. – Werner Oct 10 '11 at 4:54
• OK, no problem; I'm happy with having at least this much of a solution. Thanks again! – mhucka Oct 13 '11 at 0:05
• @Werner What PDF viewer do you use? Your approach only seems viable for some viewers; it doesn't work in Mac OS X preview, for intance. – jub0bs Mar 31 '14 at 13:02