DavDav
DavDav

Reputation: 127

LaTeX: code formatted using listings package is a mess if cut'n pasted

I'm documenting an install procedure for multiple components in a Linux based solution using LaTeX. For highlighting commands, I use the listings package. It has to be possible to copy paste the commands from the PDF but the pasted text is messy. Apparentely at random, spaces are inserted between letters.

I have tried to add and remove features via lstset to no avail. This looks as intended in the PDF

\begin{lstlisting}
ldapadd -x -D "cn=Directory Manager" -w DM_PASS -f aci.ldif
\end{lstlisting}

but copy paste yields

ldapadd −x −D " cn=D i r e c t o r y Manager" −w DM_PASS −f a c i . l d i f

Upvotes: 1

Views: 1011

Answers (1)

Lucas Meier
Lucas Meier

Reputation: 449

I found a workaround. On import, define a custom style:

\usepackage{listings}
\lstdefinestyle{mystyle}{
    basicstyle=\ttfamily,
    columns=flexible,
    showstringspaces=false
}
  • basicstyle defines the new font. I would avoid \sffamily and \rmfamily as they do not format - correctly.

  • columns = flexible will solve the spacing problem.

  • showstringspaces will avoid a problem with the spaces within a string (here cn=Directory Manager).

Then, when declaring the listing, do:

\begin{lstlisting}[style=mystyle]
  ldapadd -x -D "cn=Directory Manager" -w DM_PASS -f aci.ldif
\end{lstlisting}

Now, the copy pasting works better.

Upvotes: 1

Related Questions