1. KNUTH 的 `\primes` 宏中的杂散 `空格' 2. 格式化宏以提高逻辑清晰度

1. KNUTH 的 `\primes` 宏中的杂散 `空格' 2. 格式化宏以提高逻辑清晰度
  1. \primes第 218 页中的宏Knuth's TeXBook (Version 3.0 1996)在后产生Chapter 20: Definitions (also called Macros)了一个杂散,如 。我已经放弃寻找它的来源了。space32, 3 , 5, 7, 11, ...

    \documentclass{article}
    % RN. 13 Dec 2018
    \begin{document}
    \newif\ifprime \newif\ifunknown % boolean variables 
    \newcount\n \newcount\p \newcount\d \newcount\a % integer variables
    \def\primes#1{2,~3% assume that #1 is at least 3 
    \n=#1 \advance\n by-2 % n more to go 
    \p=5% odd primes starting with p 
    \loop\ifnum\n>0 \printifprime\advance\p by2 \repeat} 
    \def\printp{, % we will invoke \printp if p is prime 
    \ifnum\n=1 and~\fi % ‘and’ precedes the last value 
    \number\p \advance\n by -1 } 
    \def\printifprime{\testprimality \ifprime\printp\fi} 
    \def\testprimality{{\d=3 \global\primetrue 
    \loop\trialdivision \ifunknown\advance\d by2 \repeat}} 
    \def\trialdivision{\a=\p \divide\a by\d 
    \ifnum\a>\d \unknowntrue\else\unknownfalse\fi 
    \multiply\a by\d 
    \ifnum\a=\p \global\primefalse\unknownfalse\fi}
    
    \def\N{10}
    The first \N\ prime numbers are: \primes{\N~}.
    
    \end{document}
    
  2. 为了提高我阅读代码(包括我自己的代码)的能力,而无需借助黄色、绿色和蓝色文本以及粗黑色永久性记号笔,我习惯于对其进行格式化,以提高逻辑清晰度,主要是通过使用缩进。在这种情况下,Saint Knuth's \primes结果如下,但任何希望以space这种方式发现叛徒的想法都是没有根据的:

    \documentclass{article}
    % RN. 13 Dec 2018
    \begin{document}
    %%%%%%%%%%%%%%
    %   variables:
    %%%%%%%%%%%%%% 
    % boolean:
    \newif\ifprime
    \newif\ifunknown 
    % integer: 
    \newcount\n
    \newcount\p
    \newcount\d
    \newcount\a
    %%%%%%%%%%%%%%
    %   the macros:
    %%%%%%%%%%%%%% 
    \def\prime#1%
    {
        \n=#1 \advance\n by -2 % n more to go 
        \p=5% odd primes starting with p 
        \loop \ifnum\n>0 
            \printifprime\advance\p by 2 
        \repeat
    } 
    
    \def\printp
    {%
        , % we will invoke \printp if p is prime 
        \ifnum\n=1 and~\fi  % ‘and’ precedes the last value 
        \number\p \advance\n by -1 
    } 
    
    \def\printifprime
    {%
        \testprimality 
        \ifprime 
            \printp 
        \fi
    } 
    
    \def\testprimality
    {%
        {%
            \d=3 \global\primetrue 
            \loop
                \trialdivision 
                \ifunknown \advance\d by 2 
            \repeat
        }%
    } 
    
    \def\trialdivision
    {%
        \a=\p \divide\a by \d 
        \ifnum\a>\d 
            \unknowntrue
        \else
            \unknownfalse
        \fi 
        \multiply\a by \d 
        \ifnum\a=\p 
            \global\primefalse\unknownfalse
        \fi
    }
    
    \def\N{10}
    The first \N\ prime numbers are: \primes{\N~}.
    
    \end{document}
    

注意:在格式化代码时,我引入了% characters仅通过反复试验才能实现所需扩展的功能。plain-TeX欢迎提供任何有关代码缩进的一般建议或指导,以避免最终打印输出完全混乱。

答案1

\primes{\N~}那就是你的虚假空格。删除~。这不是expl3,也就是说~不是常规空格,而是\def~{\penalty\@M \ }

当你请求时,\primes{\N~}它会扩展为

2,~3% assume that #1 is at least 3
\n=\N~ \advance\n by-2 % n more to go
%    ^

进一步扩展为

2,~3% assume that #1 is at least 3
\n=10\penalty\@M \  \advance\n by-2 % n more to go
%    ^^^^^^^^^^^^^^    will not be collapsed

这正是您在 3 之后看到的空格。

相关内容