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.

This question is related to the answer to the question Is there a LaTeX trick to prevent a PDF viewer from copying the line number?

Unless you turn hyperref off, the following MWE

\documentclass{article}

\usepackage{listings}
\usepackage{accsupp}
\renewcommand*{\thelstnumber}{\protect\BeginAccSupp{ActualText={}}\arabic{lstnumber}\protect\EndAccSupp{}}

\lstset
{
    language={[LaTeX]TeX},
    numbers=left,
    columns=flexible,
}

\usepackage{hyperref}

\begin{document}

\begin{lstlisting}
Hello\\
Universe!
\end{lstlisting}

\end{document}

cannot be compiled with the following error message:

enter image description here

How do I prevent conflicts between accsupp and hyperref?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

I would avoid to redefine \thelstnumber in such a way. The command can be used in various places where such fragile content can break easily. In the case of listings I would suggest to use the key numberstyle instead:

\documentclass{article}

\usepackage{listings}
\usepackage{accsupp}

\newcommand\emptyaccsupp[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\lstset
{
    language={[LaTeX]TeX},
    numbers=left,
    columns=flexible,
    numberstyle=\emptyaccsupp
}

\usepackage{hyperref}

\begin{document}

\begin{lstlisting}
Hello\\
Universe!
\end{lstlisting}

\end{document}
share|improve this answer
    
Then I cannot change the color and/or font of the line numbers? –  Who is crazy first May 24 '12 at 8:58
2  
You can add additional commands either to \emptyaccsupp or to the key: numberstyle=\color{red}\tiny\emptyaccsupp. (Be aware that only the last command can take an argument! The other commands must be switches). –  Ulrike Fischer May 24 '12 at 9:06
    
@Forgiver: I would go with this answer. I might code some addon which adds the AccSupp code around the internal numberstyle when I find time (late next week or so). –  Martin Scharrer May 24 '12 at 9:32
    
I also think this answer is simpler than yours. I accepted this answer to finalize this topic. –  Who is crazy first May 24 '12 at 9:37
    
@UlrikeFischer Very good, because this solution also works for line number references. There the copying of line numbers should work, of course. –  Heiko Oberdiek Nov 9 '12 at 9:24

It seems hyperref expands the \thelstnumber in an context which does not set \protect correctly (e.g. a plain \edef) which breaks the fragile code. You can fix this by defining a \theHlstnumber which is used by hyperref. This macro is apparently set at the begin of the document by hyperref and you so you need to redefine it afterwards. Note that the original definition includes also a leading \ifx \lst@@caption \@empty \lst@neglisting \else \thelstlisting \fi, which you might want to add as well.

\documentclass{article}

\usepackage{listings}
\usepackage{accsupp}
\renewcommand*{\thelstnumber}{\protect\BeginAccSupp{ActualText={}}\arabic{lstnumber}\protect\EndAccSupp{}}

\lstset
{
    language={[LaTeX]TeX},
    numbers=left,
    columns=flexible,
}

\usepackage{hyperref}
\AtBeginDocument{\renewcommand*{\theHlstnumber}{\arabic{lstnumber}}}% or manually put it after `\begin{document}`
\begin{document}
\begin{lstlisting}
Hello\\
Universe!
\end{lstlisting}
\end{document}
share|improve this answer
    
I am waiting for other possible answers as any hasty decision might not be good. –  Who is crazy first May 24 '12 at 8:50
    
@Forgiver: Absolutely no problem. –  Martin Scharrer May 24 '12 at 8:51
1  
@MartinScharrer This destroys the uniqueness of listings' \theHlstnumber that adds the listings number in front of the line number. –  Heiko Oberdiek Nov 9 '12 at 9:01

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.