# How do I prevent conflicts between accsupp and hyperref?

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:

How do I prevent conflicts between accsupp and hyperref?

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}

• Then I cannot change the color and/or font of the line numbers? – kiss my armpit May 24 '12 at 8:58
• 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. – kiss my armpit 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}

• I am waiting for other possible answers as any hasty decision might not be good. – kiss my armpit May 24 '12 at 8:50
• @Forgiver: Absolutely no problem. – Martin Scharrer May 24 '12 at 8:51
• @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