我正在使用 lstlisting 创建课程笔记,希望可以从中复制粘贴代码,我已经实施了这些建议 列表中的幻影空间 (pdf)以确保没有虚假空格,但现在的问题是减号 (-) 被复制为非 ASCII,因此当我将代码粘贴到 Matlab 中时(在本例中)我收到错误。我假设这会发生在其他字符上,但此时我只遇到减号问题。我怀疑这很重要,但我一直在使用:
\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{ %
language=Matlab, % the language of the code
basicstyle=\footnotesize, % the size of the fonts that are used for the code
numbers=left, % where to put the line-numbers
numberstyle=\footnotesize, % the size of the fonts that are used for the line-numbers
stepnumber=2, % the step between two line-numbers. If it's 1, each line
% will be numbered
numbersep=5pt, % how far the line-numbers are from the code
backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
frame=single, % adds a frame around the code
tabsize=2, % sets default tabsize to 2 spaces
captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
title=\lstname, % show the filename of files included with \lstinputlisting;
% also try caption instead of title
numberstyle=\tiny\color{gray}, % line number style
keywordstyle=\color{blue}, % keyword style
commentstyle=\color{dkgreen}, % comment style
stringstyle=\color{mauve}, % string literal style
escapeinside={\%*}{*)}, % if you want to add a comment within your code
morekeywords={*,...} % if you want to add more keywords to the set
basicstyle=\ttfamily,
columns=flexible
}
\begin{lstlisting}
% notice how after a % one can write comments which can be used to
% explain the code inline.
% it will actually not bother Matlab if you copy the comments
% together with the code
x=2; % This is our first guess
for k=1:30 % we will iterate 30 times
x=x-(tan(x)-x/3)/(sec(x)-1/3) %notice that we are not using k. That is OK.
end
\end{lstlisting}
我无法将该示例放入 Matlab 中,因为它会抱怨 (-) 符号。
有任何想法吗?
答案1
您的输入有几个错误。您指定了两次basicstyle
,但第二次没有正确终止前面的选项。因此列表不会以脚注大小或打字机类型显示。
我仅重复最后几句:
escapeinside={\%*}{*)}, % if you want to add a comment within your code
% morekeywords={*,...}, % if you want to add more keywords to the set
basicstyle=\footnotesize\ttfamily,
columns=flexible,
}
删除basicstyle=\footnotesize
开头的。我还评论了该morekeywords
选项,因为它似乎没有很好的价值。
当字体不是等宽字体时,listings
将连字符更改为减号,而等宽字体不会发生这种情况。
但是如果你还添加
literate={-}{-}1,
那么无论如何都不会进行替换。
答案2
如果你看看verbatim
包中您可以使用命令的代码\verbatim@font
。这将确保使用减号,而不是某些字体的规则。它还将禁用所有连字。定义您自己的\verbfont
命令
% Ensure that the minus sign is the "-" character in
% listings environments for cut and paste operations from pdf docs
\makeatletter
\newcommand*\verbfont{%
\normalfont\ttfamily\hyphenchar\font\m@ne\@noligs}
\makeatother
然后在列表设置中使用它
\lstset{%
:
basicstyle = \color{black}\raggedright\verbfont\footnotesize\selectfont,
:
}
答案3
我发现了一个extendedchars=false
似乎可行的选择。
答案4
我想提请你注意一个小技巧!
即使您已完美设置所有内容,从 PDF 复制文本时仍然会遇到间距问题。
例如,您使用此乳胶线并导出为 PDF。
\begin{lstlisting}[language=bash]
suwind< $DATA_FOLDER/cmps.azim.su \
key=cdp min=1 max=1 > tmp1.su
suwind< $DATA_FOLDER/cmps.azim.su \
key=cdp min=2 max=2 > tmp2.su
suwind< $DATA_FOLDER/cmps.azim.su \
key=cdp min=3 max=3 > tmp3.su
suwind< $DATA_FOLDER/cmps.azim.su \
key=cdp min=4 max=4 > tmp4.su
suwind< $DATA_FOLDER/cmps.azim.su \
key=cdp min=5 max=5 > tmp5.su
\end{lstlisting}
如果我在 pdf 中选择此功能,则只会选择块而忽略空格。
!!! 如果你使用这个小技巧 !!! 只需在第一个后面添加一个空格即可打破这种对称性suwind
,因此:
\begin{lstlisting}[language=bash]
suwind < $DATA_FOLDER/cmps.azim.su \
key=cdp min=1 max=1 > tmp1.su
suwind< $DATA_FOLDER/cmps.azim.su \
key=cdp min=2 max=2 > tmp2.su
suwind< $DATA_FOLDER/cmps.azim.su \
key=cdp min=3 max=3 > tmp3.su
suwind< $DATA_FOLDER/cmps.azim.su \
key=cdp min=4 max=4 > tmp4.su
suwind< $DATA_FOLDER/cmps.azim.su \
key=cdp min=5 max=5 > tmp5.su
\end{lstlisting}
您可以正确选择全文。不要问我为什么,但它确实有效。
顺便说一下,我使用这个定义:
\lstset{
basicstyle=\ttfamily\scriptsize\color{dark-gray},% dont use footnotesize. it will generate empty spaces when copy paste
showstringspaces=false, % show spaces adding particular underscores
%frame=single, %gives black frame around code
columns=fullflexible, %very important: otherwise when copy paste text, empty spaces are inserted, also DONT use: footnotesize
extendedchars=true,
upquote=true,% ensure that backtick displays correctly
commentstyle=\color{dark-gray}, %blue
keywordstyle=\color{dark-gray} %red
}