我试图在我的 PDF 中插入一些像这样的长 bash 命令:
我喜欢它的外观,但是当尝试复制时,它会将命令分成 3 行,这显然不是我希望的工作方式。
我提供了生成上述图像的代码:
\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}
编辑:
为了澄清起见,这是我复制命令后粘贴时得到的结果:
Line 1: apt-get install package1 package2 package3 package4
Line 2: package5 package6 package7 package8 package9 package10
Line 3: package11 package12
这就是我想要的:
Line 1: apt-get install package1 package2 package3 package4 package5 package6 package7 package8 package9 package10 package11 package12
答案1
您可以添加一个按钮,供读者复制原始格式的内容。
\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}