foreach / pgfmath: create a list of specific values
I would need a list, possibly an array (for later use in a foreach loop / TikZ graphic), which contains the following values (a_n) in order (n = 1,2,3, ...):
45,135,225,270,315,
45,45,90,135,135,225,225,270,270,315,315,
45,45,45,90,90,135,135,135,225,225,225,315,315,315,
45,45,45,45,90,90,90,135,135,135,135,225,225,225,225,270,270,270,270,315,315,315,315 .........
The system is the following: 1x45, 0x90, 1x other angles
2x45, 1x90, 2x other angles
3x45, 2x90, 3x other angles
4x45, 3x90, 4x other angles
....
Note: I'm not looking for the formula function or anything like that, just looking for a list for TikZ-use.
documentclass[margin=5pt, varwidth]{standalone}
usepackage{tikz}
begin{document}
pgfmathsetmacro{k}{2}
pgfmathsetmacro{Start}{k}
pgfmathsetmacro{End}{k+5}
foreach[evaluate={N=int(n+1)}] n in {Start,...,End}{
pgfmathsetmacro{A}{int(floor(N/2)-floor(N/3))}
n: A~
}
end{document}
foreach pgfmath
add a comment |
I would need a list, possibly an array (for later use in a foreach loop / TikZ graphic), which contains the following values (a_n) in order (n = 1,2,3, ...):
45,135,225,270,315,
45,45,90,135,135,225,225,270,270,315,315,
45,45,45,90,90,135,135,135,225,225,225,315,315,315,
45,45,45,45,90,90,90,135,135,135,135,225,225,225,225,270,270,270,270,315,315,315,315 .........
The system is the following: 1x45, 0x90, 1x other angles
2x45, 1x90, 2x other angles
3x45, 2x90, 3x other angles
4x45, 3x90, 4x other angles
....
Note: I'm not looking for the formula function or anything like that, just looking for a list for TikZ-use.
documentclass[margin=5pt, varwidth]{standalone}
usepackage{tikz}
begin{document}
pgfmathsetmacro{k}{2}
pgfmathsetmacro{Start}{k}
pgfmathsetmacro{End}{k+5}
foreach[evaluate={N=int(n+1)}] n in {Start,...,End}{
pgfmathsetmacro{A}{int(floor(N/2)-floor(N/3))}
n: A~
}
end{document}
foreach pgfmath
add a comment |
I would need a list, possibly an array (for later use in a foreach loop / TikZ graphic), which contains the following values (a_n) in order (n = 1,2,3, ...):
45,135,225,270,315,
45,45,90,135,135,225,225,270,270,315,315,
45,45,45,90,90,135,135,135,225,225,225,315,315,315,
45,45,45,45,90,90,90,135,135,135,135,225,225,225,225,270,270,270,270,315,315,315,315 .........
The system is the following: 1x45, 0x90, 1x other angles
2x45, 1x90, 2x other angles
3x45, 2x90, 3x other angles
4x45, 3x90, 4x other angles
....
Note: I'm not looking for the formula function or anything like that, just looking for a list for TikZ-use.
documentclass[margin=5pt, varwidth]{standalone}
usepackage{tikz}
begin{document}
pgfmathsetmacro{k}{2}
pgfmathsetmacro{Start}{k}
pgfmathsetmacro{End}{k+5}
foreach[evaluate={N=int(n+1)}] n in {Start,...,End}{
pgfmathsetmacro{A}{int(floor(N/2)-floor(N/3))}
n: A~
}
end{document}
foreach pgfmath
I would need a list, possibly an array (for later use in a foreach loop / TikZ graphic), which contains the following values (a_n) in order (n = 1,2,3, ...):
45,135,225,270,315,
45,45,90,135,135,225,225,270,270,315,315,
45,45,45,90,90,135,135,135,225,225,225,315,315,315,
45,45,45,45,90,90,90,135,135,135,135,225,225,225,225,270,270,270,270,315,315,315,315 .........
The system is the following: 1x45, 0x90, 1x other angles
2x45, 1x90, 2x other angles
3x45, 2x90, 3x other angles
4x45, 3x90, 4x other angles
....
Note: I'm not looking for the formula function or anything like that, just looking for a list for TikZ-use.
documentclass[margin=5pt, varwidth]{standalone}
usepackage{tikz}
begin{document}
pgfmathsetmacro{k}{2}
pgfmathsetmacro{Start}{k}
pgfmathsetmacro{End}{k+5}
foreach[evaluate={N=int(n+1)}] n in {Start,...,End}{
pgfmathsetmacro{A}{int(floor(N/2)-floor(N/3))}
n: A~
}
end{document}
foreach pgfmath
foreach pgfmath
asked Mar 27 at 20:29
ciscis
737516
737516
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
documentclass[margin=5pt, varwidth]{standalone}
usepackage{pgffor}
begin{document}
newcommand{AddOtherAngles}[1]{foreach X in {225,270,315}
{foreach Y in {1,...,#1}
{xdefmyList{myList,X}}}}
foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4}
{foreach a in {1,...,A}
{ifnuma=1
xdefmyList{45}
else
xdefmyList{myList,45}
fi}
ifnumB>0
foreach b in {1,...,B}
{xdefmyList{myList,90}}
fi
AddOtherAngles{A}
myListpar
}
end{document}
Everything in one list.
documentclass[margin=5pt, varwidth]{standalone}
usepackage{tikz}
begin{document}
foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4}
{foreach a in {1,...,A}
{ifnuma=1
ifnumA=1
xdefmyList{45}
else
xdefmyList{myList,45}
fi
else
xdefmyList{myList,45}
fi}
ifnumB>0
foreach b in {1,...,B}
{xdefmyList{myList,90}}
fi
foreach X in {225,270,315}
{foreach Y in {1,...,A}
{xdefmyList{myList,X}}}
}
typeout{myList}
tikz{foreach w in myList { draw (0,0)--(w:1); } }
end{document}
@cis I do not needprotected@xdef
:foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4} {foreach a in {1,...,A} {ifnuma=1 xdefmyList{45} else xdefmyList{myList,45} fi} ifnumB>0 foreach b in {1,...,B} {xdefmyList{myList,90}} fi foreach X in {225,270,315} {foreach Y in {1,...,A} {xdefmyList{myList,X}}} tikz{foreach w in myList { draw (0,0)--(w:1); } } }
– marmot
Mar 28 at 13:56
@marmot
I cannot use this list in this kindtikz foreach w in myList { draw (0,0)--(w:1); }
There has to be a comma after all numbers except after the last number (and maybe no line breaks).
– cis
Mar 28 at 13:58
1
@cis I added such a code.
– marmot
Mar 28 at 14:02
1
Oh yes Sir, that seems to be good. Have a look on the usage: [![enter image description here][1]][1] [1]: i.stack.imgur.com/zPtte.jpg
– cis
Mar 28 at 14:13
@cis Schaut gut aus.
– marmot
Mar 28 at 14:14
add a comment |
With expl3
:
documentclass{article}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{generatelist}{mm}
{% #1 = control sequence, #2 = number of repetitions
genlist_main:nn { #1 } { #2 }
}
cs_new_protected:Nn genlist_main:nn
{
tl_set:Nx l_tmpa_tl
{
45
prg_replicate:nn { #2-1 } { ,45 }
prg_replicate:nn { #2-1 } { ,90 }
prg_replicate:nn { #2 } { ,135 }
prg_replicate:nn { #2 } { ,225 }
prg_replicate:nn { #2 } { ,270 }
prg_replicate:nn { #2 } { ,315 }
}
tl_if_exist:NTF #1
{
msg_error:nnx {genlist}{exist}{token_to_str:N #1}
}
{
tl_new:N #1
tl_set_eq:NN #1 l_tmpa_tl
}
}
msg_new:nnnn {genlist}{exist}
{
Control~sequence~#1~already~defined
}
{
The~control~sequence~#1~is~already~defined,~choose~a~different~name
}
ExplSyntaxOff
generatelist{ONE}{1}
generatelist{TWO}{2}
generatelist{THREE}{3}
generatelist{FOUR}{4}
generatelist{box}{10} % this raises an error
begin{document}
ONE
TWO
THREE
FOUR
end{document}
A classical implementation, which shows why expl3
is preferable.
documentclass{article}
makeatletter
newcommand{genlist@aux}[2]{%
ifnum#1>0
,#2%
expandafter@firstofone
else
expandafter@gobble
fi
{expandaftergenlist@auxexpandafter{thenumexpr#1-1}{#2}}%
}
newcommand{generatelist}[2]{%
@ifdefinable#1{genlist@main{#1}{#2}}%
}
newcommand{genlist@main}[2]{%
edef#1{%
45%
expandaftergenlist@auxexpandafter{thenumexpr#2-1}{45}%
expandaftergenlist@auxexpandafter{thenumexpr#2-1}{90}%
genlist@aux{#2}{135}%
genlist@aux{#2}{225}%
genlist@aux{#2}{270}%
genlist@aux{#2}{315}%
}%
}
makeatother
generatelist{ONE}{1}
generatelist{TWO}{2}
generatelist{THREE}{3}
generatelist{FOUR}{4}
%generatelist{box}{10} % this would raise an error
begin{document}
ONE
TWO
THREE
FOUR
end{document}
Ah, very good. Do you think, there is a possibility with pgfmath, xdef and so on?
– cis
Mar 28 at 0:09
@cis I added a version with no additional package.
– egreg
Mar 28 at 7:44
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f481784%2fforeach-pgfmath-create-a-list-of-specific-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
documentclass[margin=5pt, varwidth]{standalone}
usepackage{pgffor}
begin{document}
newcommand{AddOtherAngles}[1]{foreach X in {225,270,315}
{foreach Y in {1,...,#1}
{xdefmyList{myList,X}}}}
foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4}
{foreach a in {1,...,A}
{ifnuma=1
xdefmyList{45}
else
xdefmyList{myList,45}
fi}
ifnumB>0
foreach b in {1,...,B}
{xdefmyList{myList,90}}
fi
AddOtherAngles{A}
myListpar
}
end{document}
Everything in one list.
documentclass[margin=5pt, varwidth]{standalone}
usepackage{tikz}
begin{document}
foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4}
{foreach a in {1,...,A}
{ifnuma=1
ifnumA=1
xdefmyList{45}
else
xdefmyList{myList,45}
fi
else
xdefmyList{myList,45}
fi}
ifnumB>0
foreach b in {1,...,B}
{xdefmyList{myList,90}}
fi
foreach X in {225,270,315}
{foreach Y in {1,...,A}
{xdefmyList{myList,X}}}
}
typeout{myList}
tikz{foreach w in myList { draw (0,0)--(w:1); } }
end{document}
@cis I do not needprotected@xdef
:foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4} {foreach a in {1,...,A} {ifnuma=1 xdefmyList{45} else xdefmyList{myList,45} fi} ifnumB>0 foreach b in {1,...,B} {xdefmyList{myList,90}} fi foreach X in {225,270,315} {foreach Y in {1,...,A} {xdefmyList{myList,X}}} tikz{foreach w in myList { draw (0,0)--(w:1); } } }
– marmot
Mar 28 at 13:56
@marmot
I cannot use this list in this kindtikz foreach w in myList { draw (0,0)--(w:1); }
There has to be a comma after all numbers except after the last number (and maybe no line breaks).
– cis
Mar 28 at 13:58
1
@cis I added such a code.
– marmot
Mar 28 at 14:02
1
Oh yes Sir, that seems to be good. Have a look on the usage: [![enter image description here][1]][1] [1]: i.stack.imgur.com/zPtte.jpg
– cis
Mar 28 at 14:13
@cis Schaut gut aus.
– marmot
Mar 28 at 14:14
add a comment |
documentclass[margin=5pt, varwidth]{standalone}
usepackage{pgffor}
begin{document}
newcommand{AddOtherAngles}[1]{foreach X in {225,270,315}
{foreach Y in {1,...,#1}
{xdefmyList{myList,X}}}}
foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4}
{foreach a in {1,...,A}
{ifnuma=1
xdefmyList{45}
else
xdefmyList{myList,45}
fi}
ifnumB>0
foreach b in {1,...,B}
{xdefmyList{myList,90}}
fi
AddOtherAngles{A}
myListpar
}
end{document}
Everything in one list.
documentclass[margin=5pt, varwidth]{standalone}
usepackage{tikz}
begin{document}
foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4}
{foreach a in {1,...,A}
{ifnuma=1
ifnumA=1
xdefmyList{45}
else
xdefmyList{myList,45}
fi
else
xdefmyList{myList,45}
fi}
ifnumB>0
foreach b in {1,...,B}
{xdefmyList{myList,90}}
fi
foreach X in {225,270,315}
{foreach Y in {1,...,A}
{xdefmyList{myList,X}}}
}
typeout{myList}
tikz{foreach w in myList { draw (0,0)--(w:1); } }
end{document}
@cis I do not needprotected@xdef
:foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4} {foreach a in {1,...,A} {ifnuma=1 xdefmyList{45} else xdefmyList{myList,45} fi} ifnumB>0 foreach b in {1,...,B} {xdefmyList{myList,90}} fi foreach X in {225,270,315} {foreach Y in {1,...,A} {xdefmyList{myList,X}}} tikz{foreach w in myList { draw (0,0)--(w:1); } } }
– marmot
Mar 28 at 13:56
@marmot
I cannot use this list in this kindtikz foreach w in myList { draw (0,0)--(w:1); }
There has to be a comma after all numbers except after the last number (and maybe no line breaks).
– cis
Mar 28 at 13:58
1
@cis I added such a code.
– marmot
Mar 28 at 14:02
1
Oh yes Sir, that seems to be good. Have a look on the usage: [![enter image description here][1]][1] [1]: i.stack.imgur.com/zPtte.jpg
– cis
Mar 28 at 14:13
@cis Schaut gut aus.
– marmot
Mar 28 at 14:14
add a comment |
documentclass[margin=5pt, varwidth]{standalone}
usepackage{pgffor}
begin{document}
newcommand{AddOtherAngles}[1]{foreach X in {225,270,315}
{foreach Y in {1,...,#1}
{xdefmyList{myList,X}}}}
foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4}
{foreach a in {1,...,A}
{ifnuma=1
xdefmyList{45}
else
xdefmyList{myList,45}
fi}
ifnumB>0
foreach b in {1,...,B}
{xdefmyList{myList,90}}
fi
AddOtherAngles{A}
myListpar
}
end{document}
Everything in one list.
documentclass[margin=5pt, varwidth]{standalone}
usepackage{tikz}
begin{document}
foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4}
{foreach a in {1,...,A}
{ifnuma=1
ifnumA=1
xdefmyList{45}
else
xdefmyList{myList,45}
fi
else
xdefmyList{myList,45}
fi}
ifnumB>0
foreach b in {1,...,B}
{xdefmyList{myList,90}}
fi
foreach X in {225,270,315}
{foreach Y in {1,...,A}
{xdefmyList{myList,X}}}
}
typeout{myList}
tikz{foreach w in myList { draw (0,0)--(w:1); } }
end{document}
documentclass[margin=5pt, varwidth]{standalone}
usepackage{pgffor}
begin{document}
newcommand{AddOtherAngles}[1]{foreach X in {225,270,315}
{foreach Y in {1,...,#1}
{xdefmyList{myList,X}}}}
foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4}
{foreach a in {1,...,A}
{ifnuma=1
xdefmyList{45}
else
xdefmyList{myList,45}
fi}
ifnumB>0
foreach b in {1,...,B}
{xdefmyList{myList,90}}
fi
AddOtherAngles{A}
myListpar
}
end{document}
Everything in one list.
documentclass[margin=5pt, varwidth]{standalone}
usepackage{tikz}
begin{document}
foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4}
{foreach a in {1,...,A}
{ifnuma=1
ifnumA=1
xdefmyList{45}
else
xdefmyList{myList,45}
fi
else
xdefmyList{myList,45}
fi}
ifnumB>0
foreach b in {1,...,B}
{xdefmyList{myList,90}}
fi
foreach X in {225,270,315}
{foreach Y in {1,...,A}
{xdefmyList{myList,X}}}
}
typeout{myList}
tikz{foreach w in myList { draw (0,0)--(w:1); } }
end{document}
edited Mar 28 at 14:02
answered Mar 27 at 21:17
marmotmarmot
115k5146277
115k5146277
@cis I do not needprotected@xdef
:foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4} {foreach a in {1,...,A} {ifnuma=1 xdefmyList{45} else xdefmyList{myList,45} fi} ifnumB>0 foreach b in {1,...,B} {xdefmyList{myList,90}} fi foreach X in {225,270,315} {foreach Y in {1,...,A} {xdefmyList{myList,X}}} tikz{foreach w in myList { draw (0,0)--(w:1); } } }
– marmot
Mar 28 at 13:56
@marmot
I cannot use this list in this kindtikz foreach w in myList { draw (0,0)--(w:1); }
There has to be a comma after all numbers except after the last number (and maybe no line breaks).
– cis
Mar 28 at 13:58
1
@cis I added such a code.
– marmot
Mar 28 at 14:02
1
Oh yes Sir, that seems to be good. Have a look on the usage: [![enter image description here][1]][1] [1]: i.stack.imgur.com/zPtte.jpg
– cis
Mar 28 at 14:13
@cis Schaut gut aus.
– marmot
Mar 28 at 14:14
add a comment |
@cis I do not needprotected@xdef
:foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4} {foreach a in {1,...,A} {ifnuma=1 xdefmyList{45} else xdefmyList{myList,45} fi} ifnumB>0 foreach b in {1,...,B} {xdefmyList{myList,90}} fi foreach X in {225,270,315} {foreach Y in {1,...,A} {xdefmyList{myList,X}}} tikz{foreach w in myList { draw (0,0)--(w:1); } } }
– marmot
Mar 28 at 13:56
@marmot
I cannot use this list in this kindtikz foreach w in myList { draw (0,0)--(w:1); }
There has to be a comma after all numbers except after the last number (and maybe no line breaks).
– cis
Mar 28 at 13:58
1
@cis I added such a code.
– marmot
Mar 28 at 14:02
1
Oh yes Sir, that seems to be good. Have a look on the usage: [![enter image description here][1]][1] [1]: i.stack.imgur.com/zPtte.jpg
– cis
Mar 28 at 14:13
@cis Schaut gut aus.
– marmot
Mar 28 at 14:14
@cis I do not need
protected@xdef
: foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4} {foreach a in {1,...,A} {ifnuma=1 xdefmyList{45} else xdefmyList{myList,45} fi} ifnumB>0 foreach b in {1,...,B} {xdefmyList{myList,90}} fi foreach X in {225,270,315} {foreach Y in {1,...,A} {xdefmyList{myList,X}}} tikz{foreach w in myList { draw (0,0)--(w:1); } } }
– marmot
Mar 28 at 13:56
@cis I do not need
protected@xdef
: foreach A [evaluate=A as B using {int(A-1)}] in {1,...,4} {foreach a in {1,...,A} {ifnuma=1 xdefmyList{45} else xdefmyList{myList,45} fi} ifnumB>0 foreach b in {1,...,B} {xdefmyList{myList,90}} fi foreach X in {225,270,315} {foreach Y in {1,...,A} {xdefmyList{myList,X}}} tikz{foreach w in myList { draw (0,0)--(w:1); } } }
– marmot
Mar 28 at 13:56
@marmot
I cannot use this list in this kind tikz foreach w in myList { draw (0,0)--(w:1); }
There has to be a comma after all numbers except after the last number (and maybe no line breaks).– cis
Mar 28 at 13:58
@marmot
I cannot use this list in this kind tikz foreach w in myList { draw (0,0)--(w:1); }
There has to be a comma after all numbers except after the last number (and maybe no line breaks).– cis
Mar 28 at 13:58
1
1
@cis I added such a code.
– marmot
Mar 28 at 14:02
@cis I added such a code.
– marmot
Mar 28 at 14:02
1
1
Oh yes Sir, that seems to be good. Have a look on the usage: [![enter image description here][1]][1] [1]: i.stack.imgur.com/zPtte.jpg
– cis
Mar 28 at 14:13
Oh yes Sir, that seems to be good. Have a look on the usage: [![enter image description here][1]][1] [1]: i.stack.imgur.com/zPtte.jpg
– cis
Mar 28 at 14:13
@cis Schaut gut aus.
– marmot
Mar 28 at 14:14
@cis Schaut gut aus.
– marmot
Mar 28 at 14:14
add a comment |
With expl3
:
documentclass{article}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{generatelist}{mm}
{% #1 = control sequence, #2 = number of repetitions
genlist_main:nn { #1 } { #2 }
}
cs_new_protected:Nn genlist_main:nn
{
tl_set:Nx l_tmpa_tl
{
45
prg_replicate:nn { #2-1 } { ,45 }
prg_replicate:nn { #2-1 } { ,90 }
prg_replicate:nn { #2 } { ,135 }
prg_replicate:nn { #2 } { ,225 }
prg_replicate:nn { #2 } { ,270 }
prg_replicate:nn { #2 } { ,315 }
}
tl_if_exist:NTF #1
{
msg_error:nnx {genlist}{exist}{token_to_str:N #1}
}
{
tl_new:N #1
tl_set_eq:NN #1 l_tmpa_tl
}
}
msg_new:nnnn {genlist}{exist}
{
Control~sequence~#1~already~defined
}
{
The~control~sequence~#1~is~already~defined,~choose~a~different~name
}
ExplSyntaxOff
generatelist{ONE}{1}
generatelist{TWO}{2}
generatelist{THREE}{3}
generatelist{FOUR}{4}
generatelist{box}{10} % this raises an error
begin{document}
ONE
TWO
THREE
FOUR
end{document}
A classical implementation, which shows why expl3
is preferable.
documentclass{article}
makeatletter
newcommand{genlist@aux}[2]{%
ifnum#1>0
,#2%
expandafter@firstofone
else
expandafter@gobble
fi
{expandaftergenlist@auxexpandafter{thenumexpr#1-1}{#2}}%
}
newcommand{generatelist}[2]{%
@ifdefinable#1{genlist@main{#1}{#2}}%
}
newcommand{genlist@main}[2]{%
edef#1{%
45%
expandaftergenlist@auxexpandafter{thenumexpr#2-1}{45}%
expandaftergenlist@auxexpandafter{thenumexpr#2-1}{90}%
genlist@aux{#2}{135}%
genlist@aux{#2}{225}%
genlist@aux{#2}{270}%
genlist@aux{#2}{315}%
}%
}
makeatother
generatelist{ONE}{1}
generatelist{TWO}{2}
generatelist{THREE}{3}
generatelist{FOUR}{4}
%generatelist{box}{10} % this would raise an error
begin{document}
ONE
TWO
THREE
FOUR
end{document}
Ah, very good. Do you think, there is a possibility with pgfmath, xdef and so on?
– cis
Mar 28 at 0:09
@cis I added a version with no additional package.
– egreg
Mar 28 at 7:44
add a comment |
With expl3
:
documentclass{article}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{generatelist}{mm}
{% #1 = control sequence, #2 = number of repetitions
genlist_main:nn { #1 } { #2 }
}
cs_new_protected:Nn genlist_main:nn
{
tl_set:Nx l_tmpa_tl
{
45
prg_replicate:nn { #2-1 } { ,45 }
prg_replicate:nn { #2-1 } { ,90 }
prg_replicate:nn { #2 } { ,135 }
prg_replicate:nn { #2 } { ,225 }
prg_replicate:nn { #2 } { ,270 }
prg_replicate:nn { #2 } { ,315 }
}
tl_if_exist:NTF #1
{
msg_error:nnx {genlist}{exist}{token_to_str:N #1}
}
{
tl_new:N #1
tl_set_eq:NN #1 l_tmpa_tl
}
}
msg_new:nnnn {genlist}{exist}
{
Control~sequence~#1~already~defined
}
{
The~control~sequence~#1~is~already~defined,~choose~a~different~name
}
ExplSyntaxOff
generatelist{ONE}{1}
generatelist{TWO}{2}
generatelist{THREE}{3}
generatelist{FOUR}{4}
generatelist{box}{10} % this raises an error
begin{document}
ONE
TWO
THREE
FOUR
end{document}
A classical implementation, which shows why expl3
is preferable.
documentclass{article}
makeatletter
newcommand{genlist@aux}[2]{%
ifnum#1>0
,#2%
expandafter@firstofone
else
expandafter@gobble
fi
{expandaftergenlist@auxexpandafter{thenumexpr#1-1}{#2}}%
}
newcommand{generatelist}[2]{%
@ifdefinable#1{genlist@main{#1}{#2}}%
}
newcommand{genlist@main}[2]{%
edef#1{%
45%
expandaftergenlist@auxexpandafter{thenumexpr#2-1}{45}%
expandaftergenlist@auxexpandafter{thenumexpr#2-1}{90}%
genlist@aux{#2}{135}%
genlist@aux{#2}{225}%
genlist@aux{#2}{270}%
genlist@aux{#2}{315}%
}%
}
makeatother
generatelist{ONE}{1}
generatelist{TWO}{2}
generatelist{THREE}{3}
generatelist{FOUR}{4}
%generatelist{box}{10} % this would raise an error
begin{document}
ONE
TWO
THREE
FOUR
end{document}
Ah, very good. Do you think, there is a possibility with pgfmath, xdef and so on?
– cis
Mar 28 at 0:09
@cis I added a version with no additional package.
– egreg
Mar 28 at 7:44
add a comment |
With expl3
:
documentclass{article}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{generatelist}{mm}
{% #1 = control sequence, #2 = number of repetitions
genlist_main:nn { #1 } { #2 }
}
cs_new_protected:Nn genlist_main:nn
{
tl_set:Nx l_tmpa_tl
{
45
prg_replicate:nn { #2-1 } { ,45 }
prg_replicate:nn { #2-1 } { ,90 }
prg_replicate:nn { #2 } { ,135 }
prg_replicate:nn { #2 } { ,225 }
prg_replicate:nn { #2 } { ,270 }
prg_replicate:nn { #2 } { ,315 }
}
tl_if_exist:NTF #1
{
msg_error:nnx {genlist}{exist}{token_to_str:N #1}
}
{
tl_new:N #1
tl_set_eq:NN #1 l_tmpa_tl
}
}
msg_new:nnnn {genlist}{exist}
{
Control~sequence~#1~already~defined
}
{
The~control~sequence~#1~is~already~defined,~choose~a~different~name
}
ExplSyntaxOff
generatelist{ONE}{1}
generatelist{TWO}{2}
generatelist{THREE}{3}
generatelist{FOUR}{4}
generatelist{box}{10} % this raises an error
begin{document}
ONE
TWO
THREE
FOUR
end{document}
A classical implementation, which shows why expl3
is preferable.
documentclass{article}
makeatletter
newcommand{genlist@aux}[2]{%
ifnum#1>0
,#2%
expandafter@firstofone
else
expandafter@gobble
fi
{expandaftergenlist@auxexpandafter{thenumexpr#1-1}{#2}}%
}
newcommand{generatelist}[2]{%
@ifdefinable#1{genlist@main{#1}{#2}}%
}
newcommand{genlist@main}[2]{%
edef#1{%
45%
expandaftergenlist@auxexpandafter{thenumexpr#2-1}{45}%
expandaftergenlist@auxexpandafter{thenumexpr#2-1}{90}%
genlist@aux{#2}{135}%
genlist@aux{#2}{225}%
genlist@aux{#2}{270}%
genlist@aux{#2}{315}%
}%
}
makeatother
generatelist{ONE}{1}
generatelist{TWO}{2}
generatelist{THREE}{3}
generatelist{FOUR}{4}
%generatelist{box}{10} % this would raise an error
begin{document}
ONE
TWO
THREE
FOUR
end{document}
With expl3
:
documentclass{article}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{generatelist}{mm}
{% #1 = control sequence, #2 = number of repetitions
genlist_main:nn { #1 } { #2 }
}
cs_new_protected:Nn genlist_main:nn
{
tl_set:Nx l_tmpa_tl
{
45
prg_replicate:nn { #2-1 } { ,45 }
prg_replicate:nn { #2-1 } { ,90 }
prg_replicate:nn { #2 } { ,135 }
prg_replicate:nn { #2 } { ,225 }
prg_replicate:nn { #2 } { ,270 }
prg_replicate:nn { #2 } { ,315 }
}
tl_if_exist:NTF #1
{
msg_error:nnx {genlist}{exist}{token_to_str:N #1}
}
{
tl_new:N #1
tl_set_eq:NN #1 l_tmpa_tl
}
}
msg_new:nnnn {genlist}{exist}
{
Control~sequence~#1~already~defined
}
{
The~control~sequence~#1~is~already~defined,~choose~a~different~name
}
ExplSyntaxOff
generatelist{ONE}{1}
generatelist{TWO}{2}
generatelist{THREE}{3}
generatelist{FOUR}{4}
generatelist{box}{10} % this raises an error
begin{document}
ONE
TWO
THREE
FOUR
end{document}
A classical implementation, which shows why expl3
is preferable.
documentclass{article}
makeatletter
newcommand{genlist@aux}[2]{%
ifnum#1>0
,#2%
expandafter@firstofone
else
expandafter@gobble
fi
{expandaftergenlist@auxexpandafter{thenumexpr#1-1}{#2}}%
}
newcommand{generatelist}[2]{%
@ifdefinable#1{genlist@main{#1}{#2}}%
}
newcommand{genlist@main}[2]{%
edef#1{%
45%
expandaftergenlist@auxexpandafter{thenumexpr#2-1}{45}%
expandaftergenlist@auxexpandafter{thenumexpr#2-1}{90}%
genlist@aux{#2}{135}%
genlist@aux{#2}{225}%
genlist@aux{#2}{270}%
genlist@aux{#2}{315}%
}%
}
makeatother
generatelist{ONE}{1}
generatelist{TWO}{2}
generatelist{THREE}{3}
generatelist{FOUR}{4}
%generatelist{box}{10} % this would raise an error
begin{document}
ONE
TWO
THREE
FOUR
end{document}
edited Mar 28 at 7:43
answered Mar 27 at 22:37
egregegreg
732k8919303253
732k8919303253
Ah, very good. Do you think, there is a possibility with pgfmath, xdef and so on?
– cis
Mar 28 at 0:09
@cis I added a version with no additional package.
– egreg
Mar 28 at 7:44
add a comment |
Ah, very good. Do you think, there is a possibility with pgfmath, xdef and so on?
– cis
Mar 28 at 0:09
@cis I added a version with no additional package.
– egreg
Mar 28 at 7:44
Ah, very good. Do you think, there is a possibility with pgfmath, xdef and so on?
– cis
Mar 28 at 0:09
Ah, very good. Do you think, there is a possibility with pgfmath, xdef and so on?
– cis
Mar 28 at 0:09
@cis I added a version with no additional package.
– egreg
Mar 28 at 7:44
@cis I added a version with no additional package.
– egreg
Mar 28 at 7:44
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.
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%2f481784%2fforeach-pgfmath-create-a-list-of-specific-values%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