1. Stray `space' in KNUTH's `primes` macro 2. Formatting macros for logical clarity











up vote
1
down vote

favorite














  1. The primes macro in Knuth's TeXBook (Version 3.0 1996) in Chapter 20: Definitions (also called Macros) page 218, produces a stray space after the 3, as in 2, 3 , 5, 7, 11, .... I have given up finding where it comes from.



    documentclass{article}
    % RN. 13 Dec 2018
    begin{document}
    newififprime newififunknown % boolean variables
    newcountn newcountp newcountd newcounta % integer variables
    defprimes#1{2,~3% assume that #1 is at least 3
    n=#1 advancen by-2 % n more to go
    p=5% odd primes starting with p
    loopifnumn>0 printifprimeadvancep by2 repeat}
    defprintp{, % we will invoke printp if p is prime
    ifnumn=1 and~fi % ‘and’ precedes the last value
    numberp advancen by -1 }
    defprintifprime{testprimality ifprimeprintpfi}
    deftestprimality{{d=3 globalprimetrue
    looptrialdivision ifunknownadvanced by2 repeat}}
    deftrialdivision{a=p dividea byd
    ifnuma>d unknowntrueelseunknownfalsefi
    multiplya byd
    ifnuma=p globalprimefalseunknownfalsefi}

    defN{10}
    The first N prime numbers are: primes{N~}.

    end{document}



  2. To enhance my ability to read code (including my own) without the help of yellow, green and blue texters and a fat black permanent marker, I am in a habit of formatting it for logical clarity, mainly by using indentation. In the case of Saint Knuth's primes this results in the following, but any hopes to spot the renegade space that way were ill-founded:



    documentclass{article}
    % RN. 13 Dec 2018
    begin{document}
    %%%%%%%%%%%%%%
    % variables:
    %%%%%%%%%%%%%%
    % boolean:
    newififprime
    newififunknown
    % integer:
    newcountn
    newcountp
    newcountd
    newcounta
    %%%%%%%%%%%%%%
    % the macros:
    %%%%%%%%%%%%%%
    defprime#1%
    {
    n=#1 advancen by -2 % n more to go
    p=5% odd primes starting with p
    loop ifnumn>0
    printifprimeadvancep by 2
    repeat
    }

    defprintp
    {%
    , % we will invoke printp if p is prime
    ifnumn=1 and~fi % ‘and’ precedes the last value
    numberp advancen by -1
    }

    defprintifprime
    {%
    testprimality
    ifprime
    printp
    fi
    }

    deftestprimality
    {%
    {%
    d=3 globalprimetrue
    loop
    trialdivision
    ifunknown advanced by 2
    repeat
    }%
    }

    deftrialdivision
    {%
    a=p dividea by d
    ifnuma>d
    unknowntrue
    else
    unknownfalse
    fi
    multiplya by d
    ifnuma=p
    globalprimefalseunknownfalse
    fi
    }

    defN{10}
    The first N prime numbers are: primes{N~}.

    end{document}



NOTE: In formatting the code I introduced % characters as required to achieve the desired expansion purely by trial and error. Any general advise or guidelines for the indentation of plain-TeX code which will avoid making a complete mess of the final printed output are welcome.










share|improve this question




























    up vote
    1
    down vote

    favorite














    1. The primes macro in Knuth's TeXBook (Version 3.0 1996) in Chapter 20: Definitions (also called Macros) page 218, produces a stray space after the 3, as in 2, 3 , 5, 7, 11, .... I have given up finding where it comes from.



      documentclass{article}
      % RN. 13 Dec 2018
      begin{document}
      newififprime newififunknown % boolean variables
      newcountn newcountp newcountd newcounta % integer variables
      defprimes#1{2,~3% assume that #1 is at least 3
      n=#1 advancen by-2 % n more to go
      p=5% odd primes starting with p
      loopifnumn>0 printifprimeadvancep by2 repeat}
      defprintp{, % we will invoke printp if p is prime
      ifnumn=1 and~fi % ‘and’ precedes the last value
      numberp advancen by -1 }
      defprintifprime{testprimality ifprimeprintpfi}
      deftestprimality{{d=3 globalprimetrue
      looptrialdivision ifunknownadvanced by2 repeat}}
      deftrialdivision{a=p dividea byd
      ifnuma>d unknowntrueelseunknownfalsefi
      multiplya byd
      ifnuma=p globalprimefalseunknownfalsefi}

      defN{10}
      The first N prime numbers are: primes{N~}.

      end{document}



    2. To enhance my ability to read code (including my own) without the help of yellow, green and blue texters and a fat black permanent marker, I am in a habit of formatting it for logical clarity, mainly by using indentation. In the case of Saint Knuth's primes this results in the following, but any hopes to spot the renegade space that way were ill-founded:



      documentclass{article}
      % RN. 13 Dec 2018
      begin{document}
      %%%%%%%%%%%%%%
      % variables:
      %%%%%%%%%%%%%%
      % boolean:
      newififprime
      newififunknown
      % integer:
      newcountn
      newcountp
      newcountd
      newcounta
      %%%%%%%%%%%%%%
      % the macros:
      %%%%%%%%%%%%%%
      defprime#1%
      {
      n=#1 advancen by -2 % n more to go
      p=5% odd primes starting with p
      loop ifnumn>0
      printifprimeadvancep by 2
      repeat
      }

      defprintp
      {%
      , % we will invoke printp if p is prime
      ifnumn=1 and~fi % ‘and’ precedes the last value
      numberp advancen by -1
      }

      defprintifprime
      {%
      testprimality
      ifprime
      printp
      fi
      }

      deftestprimality
      {%
      {%
      d=3 globalprimetrue
      loop
      trialdivision
      ifunknown advanced by 2
      repeat
      }%
      }

      deftrialdivision
      {%
      a=p dividea by d
      ifnuma>d
      unknowntrue
      else
      unknownfalse
      fi
      multiplya by d
      ifnuma=p
      globalprimefalseunknownfalse
      fi
      }

      defN{10}
      The first N prime numbers are: primes{N~}.

      end{document}



    NOTE: In formatting the code I introduced % characters as required to achieve the desired expansion purely by trial and error. Any general advise or guidelines for the indentation of plain-TeX code which will avoid making a complete mess of the final printed output are welcome.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite













      1. The primes macro in Knuth's TeXBook (Version 3.0 1996) in Chapter 20: Definitions (also called Macros) page 218, produces a stray space after the 3, as in 2, 3 , 5, 7, 11, .... I have given up finding where it comes from.



        documentclass{article}
        % RN. 13 Dec 2018
        begin{document}
        newififprime newififunknown % boolean variables
        newcountn newcountp newcountd newcounta % integer variables
        defprimes#1{2,~3% assume that #1 is at least 3
        n=#1 advancen by-2 % n more to go
        p=5% odd primes starting with p
        loopifnumn>0 printifprimeadvancep by2 repeat}
        defprintp{, % we will invoke printp if p is prime
        ifnumn=1 and~fi % ‘and’ precedes the last value
        numberp advancen by -1 }
        defprintifprime{testprimality ifprimeprintpfi}
        deftestprimality{{d=3 globalprimetrue
        looptrialdivision ifunknownadvanced by2 repeat}}
        deftrialdivision{a=p dividea byd
        ifnuma>d unknowntrueelseunknownfalsefi
        multiplya byd
        ifnuma=p globalprimefalseunknownfalsefi}

        defN{10}
        The first N prime numbers are: primes{N~}.

        end{document}



      2. To enhance my ability to read code (including my own) without the help of yellow, green and blue texters and a fat black permanent marker, I am in a habit of formatting it for logical clarity, mainly by using indentation. In the case of Saint Knuth's primes this results in the following, but any hopes to spot the renegade space that way were ill-founded:



        documentclass{article}
        % RN. 13 Dec 2018
        begin{document}
        %%%%%%%%%%%%%%
        % variables:
        %%%%%%%%%%%%%%
        % boolean:
        newififprime
        newififunknown
        % integer:
        newcountn
        newcountp
        newcountd
        newcounta
        %%%%%%%%%%%%%%
        % the macros:
        %%%%%%%%%%%%%%
        defprime#1%
        {
        n=#1 advancen by -2 % n more to go
        p=5% odd primes starting with p
        loop ifnumn>0
        printifprimeadvancep by 2
        repeat
        }

        defprintp
        {%
        , % we will invoke printp if p is prime
        ifnumn=1 and~fi % ‘and’ precedes the last value
        numberp advancen by -1
        }

        defprintifprime
        {%
        testprimality
        ifprime
        printp
        fi
        }

        deftestprimality
        {%
        {%
        d=3 globalprimetrue
        loop
        trialdivision
        ifunknown advanced by 2
        repeat
        }%
        }

        deftrialdivision
        {%
        a=p dividea by d
        ifnuma>d
        unknowntrue
        else
        unknownfalse
        fi
        multiplya by d
        ifnuma=p
        globalprimefalseunknownfalse
        fi
        }

        defN{10}
        The first N prime numbers are: primes{N~}.

        end{document}



      NOTE: In formatting the code I introduced % characters as required to achieve the desired expansion purely by trial and error. Any general advise or guidelines for the indentation of plain-TeX code which will avoid making a complete mess of the final printed output are welcome.










      share|improve this question

















      1. The primes macro in Knuth's TeXBook (Version 3.0 1996) in Chapter 20: Definitions (also called Macros) page 218, produces a stray space after the 3, as in 2, 3 , 5, 7, 11, .... I have given up finding where it comes from.



        documentclass{article}
        % RN. 13 Dec 2018
        begin{document}
        newififprime newififunknown % boolean variables
        newcountn newcountp newcountd newcounta % integer variables
        defprimes#1{2,~3% assume that #1 is at least 3
        n=#1 advancen by-2 % n more to go
        p=5% odd primes starting with p
        loopifnumn>0 printifprimeadvancep by2 repeat}
        defprintp{, % we will invoke printp if p is prime
        ifnumn=1 and~fi % ‘and’ precedes the last value
        numberp advancen by -1 }
        defprintifprime{testprimality ifprimeprintpfi}
        deftestprimality{{d=3 globalprimetrue
        looptrialdivision ifunknownadvanced by2 repeat}}
        deftrialdivision{a=p dividea byd
        ifnuma>d unknowntrueelseunknownfalsefi
        multiplya byd
        ifnuma=p globalprimefalseunknownfalsefi}

        defN{10}
        The first N prime numbers are: primes{N~}.

        end{document}



      2. To enhance my ability to read code (including my own) without the help of yellow, green and blue texters and a fat black permanent marker, I am in a habit of formatting it for logical clarity, mainly by using indentation. In the case of Saint Knuth's primes this results in the following, but any hopes to spot the renegade space that way were ill-founded:



        documentclass{article}
        % RN. 13 Dec 2018
        begin{document}
        %%%%%%%%%%%%%%
        % variables:
        %%%%%%%%%%%%%%
        % boolean:
        newififprime
        newififunknown
        % integer:
        newcountn
        newcountp
        newcountd
        newcounta
        %%%%%%%%%%%%%%
        % the macros:
        %%%%%%%%%%%%%%
        defprime#1%
        {
        n=#1 advancen by -2 % n more to go
        p=5% odd primes starting with p
        loop ifnumn>0
        printifprimeadvancep by 2
        repeat
        }

        defprintp
        {%
        , % we will invoke printp if p is prime
        ifnumn=1 and~fi % ‘and’ precedes the last value
        numberp advancen by -1
        }

        defprintifprime
        {%
        testprimality
        ifprime
        printp
        fi
        }

        deftestprimality
        {%
        {%
        d=3 globalprimetrue
        loop
        trialdivision
        ifunknown advanced by 2
        repeat
        }%
        }

        deftrialdivision
        {%
        a=p dividea by d
        ifnuma>d
        unknowntrue
        else
        unknownfalse
        fi
        multiplya by d
        ifnuma=p
        globalprimefalseunknownfalse
        fi
        }

        defN{10}
        The first N prime numbers are: primes{N~}.

        end{document}



      NOTE: In formatting the code I introduced % characters as required to achieve the desired expansion purely by trial and error. Any general advise or guidelines for the indentation of plain-TeX code which will avoid making a complete mess of the final printed output are welcome.







      plain-tex






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 11 mins ago









      Werner

      434k619531641




      434k619531641










      asked 36 mins ago









      Reinhard Neuwirth

      1,52711322




      1,52711322






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          primes{N~} That's your spurious space right there. Remove the ~. This is not expl3, i.e. ~ is not a regular space but def~{penalty@M }.



          When you request primes{N~} this expands to



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


          which further expands to



          2,~3% assume that #1 is at least 3
          n=10penalty@M advancen by-2 % n more to go
          ...


          which is exactly the space you see after the 3.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "85"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f464613%2f1-stray-space-in-knuths-primes-macro-2-formatting-macros-for-logical-cla%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            primes{N~} That's your spurious space right there. Remove the ~. This is not expl3, i.e. ~ is not a regular space but def~{penalty@M }.



            When you request primes{N~} this expands to



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


            which further expands to



            2,~3% assume that #1 is at least 3
            n=10penalty@M advancen by-2 % n more to go
            ...


            which is exactly the space you see after the 3.






            share|improve this answer



























              up vote
              1
              down vote













              primes{N~} That's your spurious space right there. Remove the ~. This is not expl3, i.e. ~ is not a regular space but def~{penalty@M }.



              When you request primes{N~} this expands to



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


              which further expands to



              2,~3% assume that #1 is at least 3
              n=10penalty@M advancen by-2 % n more to go
              ...


              which is exactly the space you see after the 3.






              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                primes{N~} That's your spurious space right there. Remove the ~. This is not expl3, i.e. ~ is not a regular space but def~{penalty@M }.



                When you request primes{N~} this expands to



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


                which further expands to



                2,~3% assume that #1 is at least 3
                n=10penalty@M advancen by-2 % n more to go
                ...


                which is exactly the space you see after the 3.






                share|improve this answer














                primes{N~} That's your spurious space right there. Remove the ~. This is not expl3, i.e. ~ is not a regular space but def~{penalty@M }.



                When you request primes{N~} this expands to



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


                which further expands to



                2,~3% assume that #1 is at least 3
                n=10penalty@M advancen by-2 % n more to go
                ...


                which is exactly the space you see after the 3.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 16 mins ago

























                answered 24 mins ago









                Henri Menke

                68.7k7153257




                68.7k7153257






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f464613%2f1-stray-space-in-knuths-primes-macro-2-formatting-macros-for-logical-cla%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Entries order in /etc/network/interfaces

                    新発田市

                    Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)