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.

2

1 Answer 1

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

11
  • Great, I didn't know about the ActualText feature in PDF until now. Thanks
    – topskip
    Commented Oct 7, 2011 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
    Commented Oct 10, 2011 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
    Commented Oct 10, 2011 at 4:54
  • 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
    Commented Nov 30, 2022 at 19:59
  • 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
    Commented Dec 1, 2022 at 6:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .