我使用 listings 包,在换行时会插入一个空格,代码会转到该新行。为什么这个空格会插入到新行中?我该如何删除它?
\documentclass[12pt]{article}
\usepackage[top=0.3in, bottom=1.2in, left=0.8in, right=0.8in]{geometry}
\setlength{\parindent}{0cm}
\usepackage{listings}
\begin{document}
\lstset{basicstyle=\ttfamily}
\begin{lstlisting}[breaklines=true]
//Comments are // and /* */
//Namespaces Classes Methods
namespace General //Can be ommited
{
class Specific
{
static void Main
{
int[] array=new int[n] {1, 2,..., n};
int [,] array;
string[] names = new string[] {"Matt", "Joanne", "Robert"};
//Also the new can be omitted
int[] array={1, 2, ..., n};
string[] names = string {"Matt", "Joanne", "Robert"};
}
}
}
\end{lstlisting}
\end{document}
编辑一:
我使用了一个长注释,最终得到了一个奇怪的结果:
//When declaring an abstract property (such as Area in this example), you simply indicate what property accessors are available, but do not implement them. In this example, only a Get accessor is available, so the property is read-only.
为什么“available”一词前面有这么多空格,为什么同一个词的开头比其他词稍微靠左一点?另外,如果是缩进,难道不应该只有第一行以更多空格开头,而其他行则不然,或者至少所有行都应该有相同的空格吗?
编辑二:
另外,我认为问题出在连字符上。我用过breakindent=0pt, breakatwhitespace=true
,效果有所改善,但有些行中也有很多空格,所以我认为问题出在连字符上。如果是,我该如何修复?如果不行,那原因是什么?
答案1
此外
breakindent=0pt,
breakatwhitespace,
你可能还想
columns=fullflexible,
以下是一个比较。
使用columns=fixed
(默认)
和columns=fullflexible
listings
有关密钥的更多详细信息,请参阅文档中的 2.10 小节columns
。
\documentclass[12pt]{article}
\usepackage
[
top = 0.3in,
bottom = 1.2in,
left = 0.8in,
right = 0.8in,
]{geometry}
\usepackage{listings}
\lstset{
basicstyle = \ttfamily,
breakatwhitespace = true,
breakindent = 0pt,
columns = fullflexible,
}
\begin{document}
\begin{lstlisting}[breaklines=true]
//Comments are // and /* */
//Namespaces Classes Methods
//When declaring an abstract property (such as Area in this example), you simply indicate what property accessors are available, but do not implement them. In this example, only a Get accessor is available, so the property is read-only.
namespace General //Can be ommited
{
class Specific
{
static void Main
{
int[] array=new int[n] {1, 2,..., n};
int [,] array;
string[] names = new string[] {"Matt", "Joanne", "Robert"};
//Also the new can be omitted
int[] array={1, 2, ..., n};
string[] names = string {"Matt", "Joanne", "Robert"};
}
}
}
\end{lstlisting}
\end{document}
答案2
可以关闭缩进:
\lstset{breakindent=0pt}
续行甚至可以从最左边开始:
\lsetset{
breakindent=0pt,
autobreakindent=0pt,
}
请参阅“4.10 边距和线条形状”文档的包裹listings
。