1

Im trying to insert in my PDF some long bash commands like this one: Bash command example

I like how it looks but, when trying to copy, it splits the command into 3 lines, which obviously is not how I would like it to work.

I provide the code which generates the image above:

\documentclass[11pt,a4paper]{report}

\RequirePackage{xcolor}

\usepackage{listings}

\lstdefinestyle{bashStyle}{ language=bash, basicstyle=\small\sffamily, frame=tb, columns=fullflexible, backgroundcolor=\color{yellow!20}, linewidth=\linewidth, xleftmargin=0.075\linewidth, breaklines=true, literate = {'}{{\textquotesingle}}1 {-}{{-}}1 }

\begin{document}

\begin{lstlisting}[style=bashStyle] user@machine:~$ apt-get install package1 package2 package3 package4 package5 package6 package7 package8 package9 package10 package11 package12 \end{lstlisting}

\end{document}

EDIT:

For clarification purposes, this is what I get when I paste the command after copying it:

Line 1: apt-get install package1 package2 package3 package4
Line 2: package5 package6 package7 package8 package9 package10
Line 3: package11 package12

And this is what I want:

Line 1: apt-get install package1 package2 package3 package4 package5 package6 package7 package8 package9 package10 package11 package12
  • Still not obvious, what the desirable output is. What exactly do you want to get? – chadoulis Jun 16 '20 at 12:15
  • @chadoulis My reading is that when you copy from the yellow box and paste into a command window, the result contains no newlines. – Teepeemm Jun 16 '20 at 13:41
  • Copying printed code is usually not the right thing to do, because it most depends on the ability of the PDF viewer to respect spaces. You get three printed lines, you copy three lines. – egreg Jun 16 '20 at 17:41
  • Looks like there isn't a good solution. 1 2 3 4 – user202729 Nov 10 '21 at 03:23
  • 1
    In bash file you can escape line breaks with \. The are reproduced verbatim in lstlisting environment, and copy pasted without any flaw to file or to command line. – Jhor Mar 05 '23 at 12:20

1 Answers1

0

You can add a button that will be used by the readers to copy content in the original format.

\documentclass[11pt,a4paper]{report}
\usepackage{listings}

\lstdefinestyle{bashStyle}{ language=bash, basicstyle=\small\sffamily, frame=tb, columns=fullflexible, backgroundcolor=\color{yellow!20}, linewidth=\linewidth, xleftmargin=0.075\linewidth, breaklines=true, literate = {'}{{\textquotesingle}}1 {-}{{-}}1 }

\usepackage{accsupp} \usepackage{verbatim} \usepackage{color} \usepackage[misc]{ifsym}

\definecolor{lstbgcolor}{rgb}{0.9,0.9,0.9}

\makeatletter \lst@RequireAspects{writefile}

% Use accsupp package to add listing content as copyable text. \lstnewenvironment{copyablelisting}{% \lst@BeginAlsoWriteFile{\jobname.lsttmp}% } {% \lst@EndWriteFile \let\verbatim@processline\add@lstline \global\let\lstfile\empty \verbatiminput{\jobname.lsttmp}% \marginpar{(\BeginAccSupp{method=escape,ActualText={\lstfile}}\PaperPortrait\EndAccSupp{})} } \def\add@lstline {\xdef\lstfile{\unexpanded\expandafter{\lstfile}\the\verbatim@line\string^^J}} \makeatother

\begin{document}

\lstset{breakatwhitespace=true,style=bashStyle,breaklines=true,language=[LaTeX]TeX,basicstyle=\small\ttfamily,flexiblecolumns,backgroundcolor=\color{lstbgcolor}}

\begin{copyablelisting} user@machine:~$ apt-get install package1 package2 package3 package4 package5 package6 package7 package8 package9 package10 package11 package12 \end{copyablelisting}

\end{document}

enter image description here

chadoulis
  • 702
  • 1
  • 6
  • 21
  • 1
    Even this is a nice idea, this is not exactly what Im asking. If I write several commands, I wouldn't like to copy all of them at the same time, besides it seems like it would copy also the 'user@machine:~$' (which is undesirable). By last, I cannot make this work: the button appears but if I press it it copies nothing (also its non-clickable). –  Jun 16 '20 at 15:57