从渲染的 PDF 复制代码时如何保持格式?

从渲染的 PDF 复制代码时如何保持格式?

我正在寻找一种在文档中编写代码的方法,以便读者可以轻松地从文档中复制代码并将其粘贴到编辑器中,而不会破坏格式(缩进、换行符和空格)。我该如何实现这一点?

以前,我曾使用如下所示的清单编写过:

\documentclass{article}
\usepackage{verbatim}       % code
\usepackage{listings}       % code
\usepackage{xcolor}         % color
\usepackage{color}          % bgrnd

\definecolor{light-gray}{gray}{0.95} 
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
    basicstyle={\footnotesize\ttfamily},
    morekeywords={matlab2tikz},
    morekeywords=[2]{1},   keywordstyle=[2]{\color{black}},
    emph=[1]{for,end,break},emphstyle=[1]\color{blue},
    language=Matlab,
    aboveskip=3mm,
    belowskip=3mm,
    showstringspaces=false,
    keepspaces=true,  
    columns=flexible,
    identifierstyle=\color{black},
    basicstyle={\small\ttfamily},
    numbers=left,
    numbersep=9pt,
    numberstyle={\tiny\color{gray}},
    keywordstyle=\color{blue},
    commentstyle=\color{dkgreen},
    stringstyle=\color{mauve},
    breaklines=true,
    breakatwhitespace=true,
    tabsize=3,
    morestring=*[d]{"},
}




\begin{document}

\begin{lstlisting}[backgroundcolor=\color{light-gray},deletekeywords={and,using,new}]
disp('Starting')

variable = 5;
x = linspace(40000,140000,variable); % motor
    
for k = 1:x 
    sim("file")
    a = b(end); % comment 
end

%% comment
disp('Finished!')
\end{lstlisting}

\end{document}

相关内容