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.

A PDF output is obtained by compiling the following code.

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\lstset
{
    language={[LaTeX]TeX},
        numbers=left,
        numbersep=1em,
        numberstyle=\tiny,
    frame=single,
    framesep=\fboxsep,
    framerule=\fboxrule,
    rulecolor=\color{red},
    xleftmargin=\dimexpr\fboxsep+\fboxrule\relax,
    xrightmargin=\dimexpr\fboxsep+\fboxrule\relax,
    breaklines=true,
    basicstyle=\small\tt,
    keywordstyle=\color{blue},
    commentstyle=\color[rgb]{0.13,0.54,0.13},
    backgroundcolor=\color{yellow!10},
    tabsize=2,
    columns=flexible,
    morekeywords={maketitle},
}

\begin{document}

\begin{lstlisting}
\documentclass{article}
\usepackage{listings}
\title{Sample Document}
\author{John Smith}
\date{\today}
\begin{document}
\maketitle
Hello World!
% This is a comment.
\end{document}
\end{lstlisting}

\end{document}

I attempted to copy the code only inside Acrobat Reader. Unfortunately, the line numbers also got copied as shown on the following screen shot.

enter image description here

The line numbers are useful, but readers want not to copy them.

Is there a LaTeX trick to prevent a PDF viewer from copying the line number?

share|improve this question
3  
Possible duplicate: How to make text copy in PDF previewers ignore lineno line numbers? –  Marco May 24 '12 at 6:00
1  
@Marco: It is relevant but I cannot find the listings counterpart for \thelinenumber. –  Please don't touch May 24 '12 at 6:06
    
You should be able to use the accsupp package as mentioned in the question @Marco linked to, and use \renewcommand*\thelstnumber{} to change how the line numbers are formatted. –  Peter Grill May 24 '12 at 6:07
7  
Something like this might be a nice listing addon package. I could add it to my lstaddons bundle. –  Martin Scharrer May 24 '12 at 8:26
    
@MartinScharrer, what is the "gob­ble" effect that you mention when you describe lstaddons, BTW is the feature mentioned in the question implemented in your lstaddons. (suggestion: Another extension can be to have to have a filename in the margin, e.g. next to the line numbers) –  alfC Sep 13 '13 at 7:53

3 Answers 3

up vote 49 down vote accepted
+200

This solution is very similar to that contained in How to make text copy in PDF previewers ignore lineno line numbers? \protecting the accsupp is the only requirement, perhaps due to the nature in which listings treats everything:

enter image description here

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{accsupp}% http://ctan.org/pkg/accsupp
\renewcommand{\thelstnumber}{% Line number printing mechanism
  \protect\BeginAccSupp{ActualText={}}\arabic{lstnumber}\protect\EndAccSupp{}%
}
\lstset
{
    language={[LaTeX]TeX},
        numbers=left,
        numbersep=1em,
        numberstyle=\tiny,
    frame=single,
    framesep=\fboxsep,
    framerule=\fboxrule,
    rulecolor=\color{red},
    xleftmargin=\dimexpr\fboxsep+\fboxrule\relax,
    xrightmargin=\dimexpr\fboxsep+\fboxrule\relax,
    breaklines=true,
    basicstyle=\small\tt,
    keywordstyle=\color{blue},
    commentstyle=\color[rgb]{0.13,0.54,0.13},
    backgroundcolor=\color{yellow!10},
    tabsize=2,
    columns=flexible,
    morekeywords={maketitle},
}

\begin{document}

\begin{lstlisting}
\documentclass{article}
\usepackage{listings}
\title{Sample Document}
\author{John Smith}
\date{\today}
\begin{document}
\maketitle
Hello World!
% This is a comment.
\end{document}
\end{lstlisting}

\end{document}
share|improve this answer
6  
For those who want to load hyperref together with accsupp, please read How do I prevent conflicts between accsupp and hyperref?. –  Please don't touch May 24 '12 at 8:09
    
Another note: If the rendered listing spans more than one page, copying the code might include the header and footer. It is not a requirement, but if you can avoid this, it will be better. :-) –  Please don't touch May 24 '12 at 9:15
1  
Don't forget to compile it with xelatex or pdflatex. latex-dvips-ps2pdf cannot produce the expected result!!! –  Please don't touch Jun 20 '12 at 15:32
1  
@Werner This solution has the problem that line number references cannot be copied. See Ulrike Fischer's answer or the answer of Garbage Collector. –  Heiko Oberdiek Nov 9 '12 at 9:28
1  
@Werner The questions is about line numbers in listings. But in a reference "see line number \ref{foobar}" it would be odd, if the line number is omitted when copying. –  Heiko Oberdiek Nov 9 '12 at 14:47

Due to problems that came to light in the first answer when loading also the hyperref package, a related question was asked (How do I prevent conflicts between accsupp and hyperref?). The following solution is based on Ulrike Fischer's answer to that question, and illustrates one other advantage of this solution (as pointed out by Heiko Oberdiek) even if hyperref package is not involved: line numbers in references can be copied.

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor

\usepackage{accsupp}% http://ctan.org/pkg/accsupp
\newcommand{\emptyaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\usepackage{listings}% http://ctan.org/pkg/listings
\lstset
{
    language={[LaTeX]TeX},
    numbers=left,
    numbersep=1em,
    numberstyle=\tiny\emptyaccsupp,
    frame=single,
    framesep=\fboxsep,
    framerule=\fboxrule,
    rulecolor=\color{red},
    xleftmargin=\dimexpr\fboxsep+\fboxrule\relax,
    xrightmargin=\dimexpr\fboxsep+\fboxrule\relax,
    breaklines=true,
    basicstyle=\small\tt,
    keywordstyle=\color{blue},
    commentstyle=\color[rgb]{0.13,0.54,0.13},
    backgroundcolor=\color{yellow!10},
    tabsize=2,
    columns=flexible,
    morekeywords={maketitle},
    escapeinside={\%LISTING:}{\^^M},
}

\usepackage{hyperref}

\begin{document}

\begin{lstlisting}
\documentclass{article}
\usepackage{listings}
\title{Sample Document}
\author{John Smith}
\date{\today}
\begin{document}
\maketitle %LISTING:\label{title}
Hello World!
% This is a comment.
\end{document}
\end{lstlisting}

Line \ref{title} shows the title.

\end{document}

Output of the listing showing selection

I deliberately set this answer to community wiki to prevent me from getting reputation points.

share|improve this answer
    
In the production, I use this answer because hyperref can be loaded with no problem. –  Please don't touch May 24 '12 at 9:47
    
I took the liberty of editing your CW answer to make the advantages of this solution more obvious without having to click through to the follow up question. Hope you don't mind. –  cyberSingularity Dec 15 '12 at 11:33

This answer only deals with latex/dvips/ps2pdf.

Package accsupp works well with dvips. The generated PostScript file contains the right pdfmark operators. However ghostscript (version 9.05) has not implemented these tags, from the source file gdevpdfm.c:

/* [ tag propdict /BDC pdfmark */
static int
pdfmark_BDC(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
            const gs_matrix *pctm, const gs_param_string *objname)
{
    return 0;                   /****** NOT IMPLEMENTED YET ******/
}

/* [ /EMC pdfmark */
static int
pdfmark_EMC(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
            const gs_matrix *pctm, const gs_param_string *objname)
{
    return 0;                   /****** NOT IMPLEMENTED YET ******/
}

Solution sketch:

If the line number is not text, then it cannot be copied. Thus a solution would be to use bitmap images. For each digit and variant (color, size, …) a bitmap needs to be generated. As image format BMP can be used, that is supported by dvips. Then \thelstnumber is redefined to use these images for its digits.

share|improve this answer

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.