1. Stray `space' in KNUTH's `primes` macro 2. Formatting macros for logical clarity
up vote
1
down vote
favorite
The
primesmacro inKnuth's TeXBook (Version 3.0 1996)inChapter 20: Definitions (also called Macros)page 218, produces a strayspaceafter the3, as in2, 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}
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'sprimesthis results in the following, but any hopes to spot the renegadespacethat 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
add a comment |
up vote
1
down vote
favorite
The
primesmacro inKnuth's TeXBook (Version 3.0 1996)inChapter 20: Definitions (also called Macros)page 218, produces a strayspaceafter the3, as in2, 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}
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'sprimesthis results in the following, but any hopes to spot the renegadespacethat 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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
The
primesmacro inKnuth's TeXBook (Version 3.0 1996)inChapter 20: Definitions (also called Macros)page 218, produces a strayspaceafter the3, as in2, 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}
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'sprimesthis results in the following, but any hopes to spot the renegadespacethat 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
The
primesmacro inKnuth's TeXBook (Version 3.0 1996)inChapter 20: Definitions (also called Macros)page 218, produces a strayspaceafter the3, as in2, 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}
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'sprimesthis results in the following, but any hopes to spot the renegadespacethat 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
plain-tex
edited 11 mins ago
Werner
434k619531641
434k619531641
asked 36 mins ago
Reinhard Neuwirth
1,52711322
1,52711322
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited 16 mins ago
answered 24 mins ago
Henri Menke
68.7k7153257
68.7k7153257
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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