30

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.

Speravir
  • 19,491
mhucka
  • 595
  • 5
  • 14

1 Answers1

28

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}

enter image description here

Werner
  • 603,163
  • Great, I didn't know about the ActualText feature in PDF until now. Thanks – topskip Oct 07 '11 at 07: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 03: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 04:54
  • OK, no problem; I'm happy with having at least this much of a solution. Thanks again! – mhucka Oct 13 '11 at 00: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
  • @Jubobs: See the comments above. I mainly use Adobe Reader. I know that many PDF-related features are mostly supported by Adobe, but not by other PDF viewers. The same goes for security features. – Werner Mar 31 '14 at 14:17
  • @Werner Yes. I only got suspicious when I realised it wasn' working in Preview. I just tested it in Adobe Reader and it works as advertised. That's also good enough for me, because most of my readers use that viewer. – jub0bs Mar 31 '14 at 14:20
  • What's the potential downside of this? If there isn't any downside, shouldn't lineno do this by default? – Ryo Nov 30 '22 at 19:40
  • 1
    @Ryo: It may be viewer-specific. So, if you're not using Adobe-related PDF viewers, selections may include the line numbers even if accessibility options are used. – Werner Nov 30 '22 at 19:59
  • @Werner Understood. But, my question is actually whether there is any potential harm in other PDF viewers when the method is used. If there is no harm, I'll always use this method when using lineno. (Then, if you always write the extra lines in the preamble, you wish it were the default.) – Ryo Dec 01 '22 at 02:54
  • 1
    @Ryo: Harm as in make it not viewable in non-Adobe PDF readers? I doubt that will be the case, but can't say for sure. – Werner Dec 01 '22 at 06:44