Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

listings: copying code without line numbers

464 views
Skip to first unread message

Jellby

unread,
Mar 4, 2007, 12:37:18 PM3/4/07
to
Hi all,

I include some code or file contents in a pdf through the use of the
listings package. For convenience, sometimes I like to add line numbers the
the listings. The problem is, when I add the line numbers, the code cannot
be easily copy-pasted from the pdf, because the line numbers are copied too
(I mean with acrobat, with xpdf it is possible to select a rectangular
region of text).

Does anyone have a solution or suggestion for this? Maybe attaching the file
(better if it were accessible with a right-click on the code, or something
like that)?

Thanks in advance.

--
Ignacio __ Fernández Galván
/ /\
Linux user / / \ PGP Pub Key
#289967 / / /\ \ 0x01A95F99
/ / /\ \ \
http://djelibeibi.unex.es
/________\ \ \
jellby \___________\/ yahoo.com

Lars Madsen

unread,
Mar 4, 2007, 1:33:34 PM3/4/07
to
Jellby wrote:
> Hi all,
>
> I include some code or file contents in a pdf through the use of the
> listings package. For convenience, sometimes I like to add line numbers the
> the listings. The problem is, when I add the line numbers, the code cannot
> be easily copy-pasted from the pdf, because the line numbers are copied too
> (I mean with acrobat, with xpdf it is possible to select a rectangular
> region of text).
>
> Does anyone have a solution or suggestion for this? Maybe attaching the file
> (better if it were accessible with a right-click on the code, or something
> like that)?
>
> Thanks in advance.
>

AFAIR there is the attachfile package for attaching files to PDFs.

Though it might only be Adobe Reader that actually supports attached files.

/daleif

Ulrich M. Schwarz

unread,
Mar 4, 2007, 2:05:35 PM3/4/07
to
Jellby <m...@privacy.net> writes:

> Hi all,
>
> I include some code or file contents in a pdf through the use of the
> listings package. For convenience, sometimes I like to add line numbers the
> the listings. The problem is, when I add the line numbers, the code cannot
> be easily copy-pasted from the pdf, because the line numbers are copied too
> (I mean with acrobat, with xpdf it is possible to select a rectangular
> region of text).

If the language in question has a construct for "comment until EOL",
say, //, format the line numbers as //\thelstlineno and put them on
the right. (Winged comments /* */ don't work as well because of
nesting behaviour.)

It's prabably not the most beautiful solution, but it works.

HTH
Ulrich
--
http://talcum.sarovar.org/ (Current release: 0.5.138 20050911)

RS

unread,
Mar 4, 2007, 5:48:10 PM3/4/07
to
On 04.03.2007 18:37, Jellby wrote:
> I include some code or file contents in a pdf through the use of the
> listings package. For convenience, sometimes I like to add line numbers the
> the listings. The problem is, when I add the line numbers, the code cannot
> be easily copy-pasted from the pdf, because the line numbers are copied too
> (I mean with acrobat, with xpdf it is possible to select a rectangular
> region of text).

You could add empty replacement text, so that line numbers will be
displayed but not copied:

\documentclass{article}
\usepackage{listings}
\lstset{numbers=left,numberstyle=\tiny}
\makeatletter
\def\lst@PlaceNumber{%
\llap{\normalfont
\pdfliteral direct{/Span<</ActualText()>>BDC}%
\lst@numberstyle{\thelstnumber}%
\pdfliteral direct{EMC}%
\kern\lst@numbersep}}
\makeatother
\begin{document}
\begin{lstlisting}
foo
bar
baz
\end{lstlisting}
\end{document}


Regards,
--
Robert

Ulrich Diez

unread,
Mar 5, 2007, 7:10:39 AM3/5/07
to
Jellby wrote:

> The problem is, when I add the line numbers, the code cannot
> be easily copy-pasted from the pdf, because the line numbers are copied too
> (I mean with acrobat, with xpdf it is possible to select a rectangular
> region of text).

Both with Acrobat 5.05 (full) and with Acrobat Reader 5.05
- I don't know if this works with more recent Acrobat
releases also - you can select a rectangular region of text:

Mouse-click on the small triangle near the "Text-Select"-symbol.
[The symbol looks like a large T with a small rectangular box
on the right (formed by dashed lines)].
Then you get a menu with two or three items from which you
can choose the "Column-Select-Tool". [The associated symbol
looks like the "Text-Select"-symbol but this time within a
rectangular "dashed box" instead of the box on the right.]
If you are interested, I can post a small screen-shot to your
e-mail address.

Ulrich


Dominik Waßenhoven

unread,
Mar 5, 2007, 7:37:46 AM3/5/07
to
Ulrich Diez schrieb:

> Both with Acrobat 5.05 (full) and with Acrobat Reader 5.05
> - I don't know if this works with more recent Acrobat
> releases also - you can select a rectangular region of text:

It is also possible with Adobe Reader 7.0.9 and Adobe Acrobat Standard 7.0

Regards,
Dominik.-

Jellby

unread,
Mar 5, 2007, 3:24:36 PM3/5/07
to
Among other things, Ulrich Diez saw fit to write:

> Both with Acrobat 5.05 (full) and with Acrobat Reader 5.05
> - I don't know if this works with more recent Acrobat
> releases also - you can select a rectangular region of text:
>
> Mouse-click on the small triangle near the "Text-Select"-symbol.

I think I know what you mean. Unfortunately, it doesn't seem to be possible
with Adobe Reader 7.0.8 under linux. There's just no "small triangle" on
the select tool, and I don't see any menu item that could help.

Jellby

unread,
Mar 5, 2007, 3:51:43 PM3/5/07
to
Among other things, RS saw fit to write:

>> I include some code or file contents in a pdf through the use of the
>> listings package. For convenience, sometimes I like to add line numbers
>> the the listings. The problem is, when I add the line numbers, the code
>> cannot be easily copy-pasted from the pdf, because the line numbers are
>> copied too (I mean with acrobat, with xpdf it is possible to select a
>> rectangular region of text).
>
> You could add empty replacement text, so that line numbers will be
> displayed but not copied:

Great! That's exactly the kind of thing I was looking for.

For the record, I included this code (overriding the definition in
lstmisc.sty) in my file, so I could selectively set the behaviour (it adds
the options "phantomleft" and "phantomright" to the "numbers" keyword).

\lst@Key{numbers}{none}{%
\let\lst@PlaceNumber\@empty
\lstKV@SwitchCases{#1}%
{none&\\%
left&\def\lst@PlaceNumber{\llap{\normalfont
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}}\\%
right&\def\lst@PlaceNumber{\rlap{\normalfont
\kern\linewidth \kern\lst@numbersep
\lst@numberstyle{\thelstnumber}}}\\%
phantomleft&\def\lst@PlaceNumber{\llap{\normalfont


\pdfliteral direct{/Span<</ActualText()>>BDC}%
\lst@numberstyle{\thelstnumber}%
\pdfliteral direct{EMC}%

\kern\lst@numbersep}}\\%
phantomright&\def\lst@PlaceNumber{\rlap{\normalfont


\pdfliteral direct{/Span<</ActualText()>>BDC}%

\kern\linewidth \kern\lst@numbersep


\lst@numberstyle{\thelstnumber}%
\pdfliteral direct{EMC}}}%

}{\PackageError{Listings}{Numbers #1 unknown}\@ehc}}


I don't know what others will think, but I believe this would be a very
useful feature for the listings package.

Thanks for the code!

Michele Dondi

unread,
Mar 7, 2007, 4:20:22 PM3/7/07
to
On Mon, 05 Mar 2007 21:51:43 +0100, Jellby <m...@privacy.net> wrote:

>I don't know what others will think, but I believe this would be a very
>useful feature for the listings package.

Indeed! But it would be nice if it were device driver independent,
where available.


Michele
--
>It's because the universe was programmed in C++.
No, no, it was programmed in Forth. See Genesis 1:12:
"And the earth brought Forth ..."
- Robert Israel in sci.math, thread "Why numbers?"

0 new messages