有没有关于在源文件中将行写多长的最佳实践?如果没有特定于 LaTeX 的最佳实践,我也想知道是否有最佳实践。
我最初输入 LaTeX 文件的方式与在 Word 中一样:
- 每段独占一行。
- 然而,我最终发现这相当麻烦,所以我改为每行一个句子(大约在同一时间,我从 Dropbox 转移到 git)。
这对我来说是一个很大的进步。
由于我倾向于写长句子并且喜欢脚注,因此我在一个文件中尝试了一个实验:
- 在逗号后以及句号后换行。
- 到目前为止,似乎这种方法需要大量的手动清理,因为它会产生一些非常短的行,并且文件中仍然有过长的行。
您发现什么方法最适合保持源代码的行长度合理?
答案1
我将使用 TeXBook 源代码中的一个例子
This manual is intended for people who have never used \TeX\ before, as
well as for experienced \TeX\ hackers. In other words, it's supposed to
be a panacea that satisfies everybody, at the risk of satisfying nobody.
Everything you need to know about \TeX\ is explained
here somewhere, and so are a lot of things that most users don't care about.
If you are preparing a simple manuscript, you won't need to
learn much about \TeX\ at all; on the other hand, some
things that go into the printing of technical books are inherently
difficult, and if you wish to achieve more complex effects you
will want to penetrate some of \TeX's darker corners. In order
to make it possible for many types of users to read this manual
effectively, a special sign is used to designate material that is
for wizards only: When the symbol
$$\vbox{\hbox{\dbend}\vskip 11pt}$$
appears at the beginning of a paragraph, it warns of a ``^{dangerous bend}''
in the train of thought; don't read the paragraph unless you need to.
Brave and experienced drivers at the controls of \TeX\ will gradually enter
more and more of these hazardous areas, but for most applications the
details won't matter.
这里的行都少于 80 个字符宽,我可以在这里展示它,每个人都会看到相同的内容。我(或 diff 或类似工具)可以在第 2 行中引用“有经验”,每个人都会看到相同的换行符,整个事情都是可以理解的。
如果都是一行的话看起来就像
This manual is intended for people who have never used \TeX\ before, as well as for experienced \TeX\ hackers. In other words, it's supposed to be a panacea that satisfies everybody, at the risk of satisfying nobody. Everything you need to know about \TeX\ is explained here somewhere, and so are a lot of things that most users don't care about. If you are preparing a simple manuscript, you won't need to learn much about \TeX\ at all; on the other hand, some things that go into the printing of technical books are inherently difficult, and if you wish to achieve more complex effects you will want to penetrate some of \TeX's darker corners. In order to make it possible for many types of users to read this manual effectively, a special sign is used to designate material that is for wizards only: When the symbol $$\vbox{\hbox{\dbend}\vskip 11pt}$$ appears at the beginning of a paragraph, it warns of a ``^{dangerous bend}'' in the train of thought; don't read the paragraph unless you need to. Brave and experienced drivers at the controls of \TeX\ will gradually enter more and more of these hazardous areas, but for most applications the details won't matter.
对于 TeX 来说还可以,但在这里看起来很糟糕
即使你的编辑器“自动换行”到(比如说)70 个字符,它看起来也会像
This manual is intended for people who have never used \TeX\ before,
as well as for experienced \TeX\ hackers. In other words, it's
supposed to be a panacea that satisfies everybody, at the risk of
satisfying nobody. Everything you need to know about \TeX\ is
explained here somewhere, and so are a lot of things that most users
don't care about. If you are preparing a simple manuscript, you won't
need to learn much about \TeX\ at all; on the other hand, some things
that go into the printing of technical books are inherently difficult,
and if you wish to achieve more complex effects you will want to
penetrate some of \TeX's darker corners. In order to make it possible
for many types of users to read this manual effectively, a special
sign is used to designate material that is for wizards only: When the
symbol $$\vbox{\hbox{\dbend}\vskip 11pt}$$ appears at the beginning of
a paragraph, it warns of a ``^{dangerous bend}'' in the train of
thought; don't read the paragraph unless you need to. Brave and
experienced drivers at the controls of \TeX\ will gradually enter more
and more of these hazardous areas, but for most applications the
details won't matter.
但明显的换行符出现在更任意的位置,并且由于它们实际上并不存在,因此不同的工具和不同的人会看到不同的换行符。
您可以在主要标点符号处添加换行符(此处,.;:
和$$
数学显示)
This manual is intended for people who have never used \TeX\ before,
as well as for experienced \TeX\ hackers.
In other words,
it's supposed to be a panacea that satisfies everybody,
at the risk of satisfying nobody.
Everything you need to know about \TeX\ is explained here somewhere,
and so are a lot of things that most users don't care about.
If you are preparing a simple manuscript,
you won't need to learn much about \TeX\ at all; on the other hand,
some things that go into the printing of technical books are inherently difficult,
and if you wish to achieve more complex effects you will want to penetrate some of \TeX's darker corners.
In order to make it possible for many types of users to read this manual effectively,
a special sign is used to designate material that is for wizards only:
When the symbol
$$\vbox{\hbox{\dbend}\vskip 11pt}$$
appears at the beginning of a paragraph,
it warns of a ``^{dangerous bend}'' in the train of thought;
don't read the paragraph unless you need to.
Brave and experienced drivers at the controls of \TeX\ will gradually enter more and more of these hazardous areas,
but for most applications the details won't matter.
这还算不错,但还是有一些过长的行,而且编辑起来更加困难,因为当你编辑标点符号时你必须合并行。
答案2
TeX 引擎基本上可以处理任何(合理)长度的行,因此从这个意义上来说,没有强制限制,并且开始新行很大程度上取决于个人喜好和您对可读性的感知。
但是至少有一个领域,短行(无论是否少于 80 个字符)优于长行(或者在我看来更糟糕的是,某些编辑器默认将整个段落放在一行):
如果您碰巧将文本存储在某个源代码控制系统中,无论是 git 还是 svn 或 CVS 或其他什么,那么通常文本差异会记录为行差异,如果您通过电子邮件、浏览器或 git 客户端获取这些差异,那么如果行数较短,则更改更容易识别。如果工作是与其他作者合作进行的,并且您希望快速掌握他们的更改,那么这尤其有用。
答案3
这里的主要问题是错误消息会给出错误发生的行号。这意味着错误可能出现在该行或之前的几行中。
如果您有很长或很长的行,LaTeX 就无法非常准确地帮助您快速找到所提及行中的错误。
最佳做法是使用短行。在我的例子中,我使用编辑器的技术在第 75 行显示一个换行符(参见我图片中的红色箭头;使用一个数字,使完整的行可以在您的屏幕上显示),如下图所示。
优点是,现在带有错误行号的错误消息会更加清晰,并能让您更快地找到真正的错误。
不使用自动换行符会显示以下带有表格的情况:
\begin{longtable}[c]{lrrrrrrrrrrr}
\caption{A table of the first 10 rows of the mtcars data.}\\
\toprule
& mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
\midrule
car type 1 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0.0 & 1 & 4 & 4\\
car type 1 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0.0 & 1 & 4 & 4\\
car type 2 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1.0 & 1 & 4 & 1\\
\bottomrule
\end{longtable}
在这里,我在一个编辑器行中编写一行表格代码,将&
一行代码写在另一行代码之上。这种在代码中漂亮地打印表格的方式使得查找缺失单元格或错误条目变得容易得多。假设给定的表格有 5 列或更多列... 在这里自动换行会很不愉快...
答案4
在过去几年尝试了硬分线和软分线,并阅读了其他人的评论之后,这里总结了我遇到的优点/缺点。
每行一个段落 + 自动换行:
- 无需维护即可在文本编辑器中产生最佳换行符。
- 对每个人来说看起来都不一样。
diff
对s 和 VCS来说,这是最不优雅的选择。- 由于每行上都有太多错误,因此很难识别错误。
- 不寻常的方法,因此可能会让合作者望而却步。
- 在许多编辑器中,使用上下键(或Vim 中的
j
和k
)会导致每次移动一个段落,而不是在视觉上向上或向下移动(在 Vim 中,gj
和gk
可用于在视觉上向上或向下移动,但这很麻烦)。同样,您可能必须将光标向左/向右移动很远。
固定宽度的硬(实际)换行符:
- 由于屏幕布局是独立的,因此无法充分利用屏幕空间。作者必须选择线宽。
- 必须手动维护换行符(自动硬换行和
gqap
Vim 等命令有帮助,但通常仍需要一些手动维护)。 - 对于 s 和 VCS 来说不是最佳的
diff
,因为线路将被重新排列。 - 搜索 PDF 中的文本片段以找到代码中的相同位置通常很有用。
xt fr
例如,在这篇文章中搜索,会很快显示此项目。如果单词之间有硬换行符,则更难找到。编辑器工具(例如模糊搜索或使用常规空格字符搜索)可能会有所帮助。
每行一个句子(带或不带自动换行):
- 也许对于 s 和 VCS 来说是最优化的
diff
(但在我看来,对人类来说,便利更重要)。 - 行长不均,视觉上不美观。如果使用自动换行,则会出现“每行一个段落 + 自动换行”中提到的相同缺点。如果不使用自动换行,某些行将需要左右滚动。
在作者选择的位置处进行硬(实际)换行:
- 没有充分利用屏幕空间。
- 需要手动维护,此外,还需要花心思考虑在哪里断线。
- 不一致的地方可能会出现换行符。
- 搜索文本片段可能比较困难。
对我来说,让编辑器帮我完成所有格式化(每行一个段落和软换行)的前景非常吸引人。我试过了,它在某种程度上是有效的,但缺点是确实存在的。因此我倾向于第二种选择。您的情况可能会有所不同。我会尝试不断更新此列表。