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}
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}