1

this may look like a duplicate question to this one and many, but really I had a hardtime in finding the desired solution.

The breaklines=true didn't work as it breaks the long command. Hence simple copy and paste doesn't work, because when the code is pasted in terminal it gets split over many lines with preceding angle brackets > .

  • 1
    Your main question here is about the fact that the visual output should have a line-break in it, but when you copy-and-paste it should not? – Werner Aug 19 '15 at 20:52
  • This is a copy/paste problem, probably the solution is to use accsup – alfC Aug 19 '15 at 20:55
  • 1
    ...and a copy-and-paste problem fixed by accsupp might be reader-dependent. – Werner Aug 19 '15 at 21:04
  • @Werner, Yeah that's what I want. – user22180 Aug 19 '15 at 21:25
  • @Werner, any copy-and-paste will be reader-dependent. For example even a plain listings usage will give different copy behavior. In my Evince the copied code includes random spaces (I need to use accsup to make it behave the same in Acrobat and Evince, even if I don't mind the line breaks). – alfC Aug 19 '15 at 22:57
2

Use accsupp package. This way you can copy regarless of visual line breaks.

\documentclass{article}
\usepackage{accsupp}
\usepackage{listings}
\begin{document}
\BeginAccSupp{method=plain,ActualText=long long long command long long long command long long long command long long long command}
\begin{lstlisting}[breaklines=true]
long long long command long long long command long long long command long long long command
\end{lstlisting}
\EndAccSupp{}
\end{document}

If the code includes special characters you need something more fancy (method=pdfstringdef and hyperref):

\documentclass{article}
\usepackage[pdfencoding=auto]{hyperref}
\usepackage{accsupp}
\usepackage{listings}
\begin{document}
\BeginAccSupp{method=pdfstringdef,ActualText=
grep -rw --include \unichar{"005C}*.h --include \unichar{"005C}*e.c* "Write()" . |sed -e 's/Write/Read/g'}
\begin{lstlisting}[breaklines=true]
grep -rw --include \*.h --include \*e.c* "Write()" . |sed -e 's/Write/Read/g'
\end{lstlisting}
\EndAccSupp{}
\end{document}
  • This is not working for my case as I have a command like grep -rw --include \*.h --include \*e.c* "Write()" . |sed -e 's/Write/Read/g' which is not a plain text. – user22180 Aug 19 '15 at 21:27
  • 1
    That is why you have to show a MWE to have better answers later. The problem in your case is the "\" character. There are workarounds within accsup for this problem, see my edit. – alfC Aug 19 '15 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.