在源代码行的两端排版 M 破折号,两侧不加空格

在源代码行的两端排版 M 破折号,两侧不加空格

使用版本控制,我的源代码或多或少可以用单个语法单位(句子、短语等)占据单个源代码行来完成,这很好。但这往往会导致(至少对我来说)源代码中有很多行以 M 破折号(---在源代码中)开头或结尾。

我发现,当发生这种情况时,换行符在文档中排版为空格。也就是说,M 破折号和它相邻的一个单词(但只有一个)之间有一个空格。也就是说,具体来说,下面的代码中数字 1 和 2 对我来说是错误的。如果您喜欢的话,第 4、5 和 6 行看起来不错,但只有第 3 行看起来符合我的要求。

\documentclass{article}
\begin{document}
1. A line in the source with an M-dash at the end---
like that one; with no space before it in the source.

2. A line in the source with an M-dash at the beginning
---like this one; also with no spaces around it.

3. A line---this one, I mean---with M-dashes in the middle,
and no spaces around them.

4. This line has an M-dash at the end of the line ---
like that; but with a space before it.

5. This one has the M-dash at the beginning of a line
--- like it? But there is a space after it.

6. This line --- the last line --- has M-dashes in the
middle, with spaces around them.
\end{document}

我非常希望:A) 不必在每次使用破折号时都求助于宏之类的笨拙的校对工具(“\sensiblemdash”或类似的东西);B) 能够用破折号结束或开始行;C) 能够在源代码中使用看起来或多或少像破折号的东西;D) 不必在文档的排版中在破折号的两端都留有空格。(关于最后这一点,我知道这是某些人的偏好,但不是我的偏好,尽管我愿意接受争论。)我不介意在源代码的两侧输入空格,但我不希望它以这种方式排版。

编辑:忘了说:

$ latex --version
pdfTeX 3.14159265-2.6-1.40.17 (TeX Live 2016/Cygwin)
kpathsea version 6.2.2

答案1

源代码中的行尾与空格完全相同(它生成一个字符代码为 32 的空格标记,与空格字符生成的完全相同)

但是如果你输入破折号为 — 那么很容易定义忽略前后的空格,

在此处输入图片描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{2014}{\leavevmode\unskip---\ignorespaces}


\begin{document}
1. A line in the source with an M-dash at the end—
like that one; with no space before it in the source.

2. A line in the source with an M-dash at the beginning
—like this one; also with no spaces around it.

3. A line—this one, I mean—with M-dashes in the middle,
and no spaces around them.

4. This line has an M-dash at the end of the line —
like that; but with a space before it.

5. This one has the M-dash at the beginning of a line
— like it? But there is a space after it.

6. This line — the last line — has M-dashes in the
middle, with spaces around them.
\end{document}

相关内容