Drawing concentric circles with alternating colors by means of foreach in TikZ
Clash Royale CLAN TAG#URR8PPP
I would like to draw concentric circles like these:
by means of TikZ and foreach
command. The way I achieved it is very elementary (and burdensome):
documentclass[standalone]
usepackagetikz
begindocument
begintikzpicture[scale=0.2]
coordinate (O) at (0,0);
fill[red!70] (O) circle (20);
fill[white] (O) circle (19);
fill[yellow!70] (O) circle (18);
fill[white] (O) circle (17);
fill[red!70] (O) circle (16);
fill[white] (O) circle (15);
fill[yellow!70] (O) circle (14);
fill[white] (O) circle (13);
fill[red!70] (O) circle (12);
fill[white] (O) circle (11);
fill[yellow!70] (O) circle (10);
fill[white] (O) circle (9);
fill[red!70] (O) circle (8);
fill[white] (O) circle (7);
fill[yellow!70] (O) circle (6);
fill[white] (O) circle (5);
fill[red!70] (O) circle (4);
fill[white] (O) circle (3);
fill[yellow!70] (O) circle (2);
fill[white] (O) circle (1);
endtikzpicture
enddocument
To no avail did I try applying foreach
, but this is definitely due to my lack of fluency with TikZ (I always ended up with one color, or at most two nested circle with different color). Could you be so kind and help me with these? A suggestion how to do this would be even better than the full answer.
tikz-pgf foreach loops
add a comment |
I would like to draw concentric circles like these:
by means of TikZ and foreach
command. The way I achieved it is very elementary (and burdensome):
documentclass[standalone]
usepackagetikz
begindocument
begintikzpicture[scale=0.2]
coordinate (O) at (0,0);
fill[red!70] (O) circle (20);
fill[white] (O) circle (19);
fill[yellow!70] (O) circle (18);
fill[white] (O) circle (17);
fill[red!70] (O) circle (16);
fill[white] (O) circle (15);
fill[yellow!70] (O) circle (14);
fill[white] (O) circle (13);
fill[red!70] (O) circle (12);
fill[white] (O) circle (11);
fill[yellow!70] (O) circle (10);
fill[white] (O) circle (9);
fill[red!70] (O) circle (8);
fill[white] (O) circle (7);
fill[yellow!70] (O) circle (6);
fill[white] (O) circle (5);
fill[red!70] (O) circle (4);
fill[white] (O) circle (3);
fill[yellow!70] (O) circle (2);
fill[white] (O) circle (1);
endtikzpicture
enddocument
To no avail did I try applying foreach
, but this is definitely due to my lack of fluency with TikZ (I always ended up with one color, or at most two nested circle with different color). Could you be so kind and help me with these? A suggestion how to do this would be even better than the full answer.
tikz-pgf foreach loops
add a comment |
I would like to draw concentric circles like these:
by means of TikZ and foreach
command. The way I achieved it is very elementary (and burdensome):
documentclass[standalone]
usepackagetikz
begindocument
begintikzpicture[scale=0.2]
coordinate (O) at (0,0);
fill[red!70] (O) circle (20);
fill[white] (O) circle (19);
fill[yellow!70] (O) circle (18);
fill[white] (O) circle (17);
fill[red!70] (O) circle (16);
fill[white] (O) circle (15);
fill[yellow!70] (O) circle (14);
fill[white] (O) circle (13);
fill[red!70] (O) circle (12);
fill[white] (O) circle (11);
fill[yellow!70] (O) circle (10);
fill[white] (O) circle (9);
fill[red!70] (O) circle (8);
fill[white] (O) circle (7);
fill[yellow!70] (O) circle (6);
fill[white] (O) circle (5);
fill[red!70] (O) circle (4);
fill[white] (O) circle (3);
fill[yellow!70] (O) circle (2);
fill[white] (O) circle (1);
endtikzpicture
enddocument
To no avail did I try applying foreach
, but this is definitely due to my lack of fluency with TikZ (I always ended up with one color, or at most two nested circle with different color). Could you be so kind and help me with these? A suggestion how to do this would be even better than the full answer.
tikz-pgf foreach loops
I would like to draw concentric circles like these:
by means of TikZ and foreach
command. The way I achieved it is very elementary (and burdensome):
documentclass[standalone]
usepackagetikz
begindocument
begintikzpicture[scale=0.2]
coordinate (O) at (0,0);
fill[red!70] (O) circle (20);
fill[white] (O) circle (19);
fill[yellow!70] (O) circle (18);
fill[white] (O) circle (17);
fill[red!70] (O) circle (16);
fill[white] (O) circle (15);
fill[yellow!70] (O) circle (14);
fill[white] (O) circle (13);
fill[red!70] (O) circle (12);
fill[white] (O) circle (11);
fill[yellow!70] (O) circle (10);
fill[white] (O) circle (9);
fill[red!70] (O) circle (8);
fill[white] (O) circle (7);
fill[yellow!70] (O) circle (6);
fill[white] (O) circle (5);
fill[red!70] (O) circle (4);
fill[white] (O) circle (3);
fill[yellow!70] (O) circle (2);
fill[white] (O) circle (1);
endtikzpicture
enddocument
To no avail did I try applying foreach
, but this is definitely due to my lack of fluency with TikZ (I always ended up with one color, or at most two nested circle with different color). Could you be so kind and help me with these? A suggestion how to do this would be even better than the full answer.
tikz-pgf foreach loops
tikz-pgf foreach loops
edited Jan 23 at 14:21
Mad Hatter
asked Jan 23 at 12:02
Mad HatterMad Hatter
659520
659520
add a comment |
add a comment |
7 Answers
7
active
oldest
votes
You mean something like this:
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,3,5,7,9,11,13,15,17,19
node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) ;
foreach x in 2,4,6,8,10,12,14,16,18, 20
node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) ;
endtikzpicture
enddocument
which would give you:
I just made the yellow a bit lighter because, it was bleeding yellow too much :D
1
Perfect. Thanks a lot!
– Mad Hatter
Jan 23 at 12:20
1
@MadHatter Also, this happens to be the first time I am usingfor
loop inTeX
. So, thanks to you ;)
– Raaja
Jan 23 at 12:36
1
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
Jan 23 at 12:38
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
Jan 23 at 12:39
1
@Raaja You're welcome :)
– Mad Hatter
Jan 23 at 14:22
|
show 1 more comment
A slight generalization of Raaja's and TeXncian's answers in that I allow arbitrary color cycle lists, which is illustrated by an additional list of length 3.
documentclass[tikz,border=3.14mm]standalone
begindocument
begintikzpicture
deflstColors"red!70","yellow!50"
foreach X [evaluate=X as Col using lstColors[int(mod(X,2))]] in 1,...,20
draw[line width = 8pt,Col] (0,0) circle (X);
endtikzpicture
begintikzpicture
deflstColors"red!70","yellow!50","blue!70"
foreach X [evaluate=X as Col using lstColors[int(mod(X,3))]] in 1,...,20
draw[line width = 8pt,Col] (0,0) circle (X);
endtikzpicture
enddocument
1
Neat. I'd wondered how to do that... I'm certainly missing something basic, but forX=1
(the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors inlstColors
, but just wondered...
– sgmoye
Jan 23 at 15:10
@sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)
– marmot
Jan 23 at 22:04
I saw that, and to counteract it I changedlstColors[int(mod(X,2))]
tolstColors[int(mod(X-1,2))]
in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)
– sgmoye
Jan 23 at 22:58
add a comment |
A PSTricks solution.
documentclass[pstricks]standalone
pssetrunit=4pt,unit=psrunit
begindocument
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in 1,2,...,10%
ifoddidefcyellowelsedefcredfi
pscircle[linecolor=c]i
endpspicture
enddocument
Note: There is an unnecessary white spot after converting to PNG.
Animated version
documentclass[pstricks]standalone
pssetrunit=4pt,unit=psrunit
begindocument
foreach j in 1,...,10%
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in 1,...,j%
ifoddidefcyellowelsedefcredfi
pscircle[linecolor=c]i%
endpspicture
enddocument
1
You did not animate this.
– AlexG
Jan 23 at 13:13
add a comment |
This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width
to make it thicker).
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,...,20
pgfmathparseisodd(x)ifnumpgfmathresult=1defcurrcolred!70elsedefcurrcolyellow!50fi
draw[line width=8pt,currcol] (0,0) circle (x cm);
endtikzpicture
enddocument
Next time, I will know how to apply thisifodd
;) [IMO, it is useful, if the loop counts grows].
– Raaja
Jan 23 at 12:46
1
@Raaja Yes, it is :)
– TeXnician
Jan 23 at 12:46
Thank you very much. P.S. I think you mean thinner not thicker lines :).
– Mad Hatter
Jan 23 at 14:18
@MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)
– TeXnician
Jan 23 at 14:31
@TeXnician Sorry, I misread your comment.
– Mad Hatter
Jan 23 at 15:01
add a comment |
A SkiaSharp solution only for comparison purposes.
using SkiaSharp; // needs skiasharp nuget
using System.Diagnostics;
class ConcentricCircle
static readonly SKPaint yellowStroke = new SKPaint
Style = SKPaintStyle.Stroke,
Color = SKColors.Yellow,
IsAntialias = true
;
static readonly SKPaint redStroke = new SKPaint
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
IsAntialias = true
;
static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
static readonly float width = 6 * scale; // 6 cm
static readonly float height = 6 * scale; // 6 cm
static float PtToCm(float pt) => pt / scale;
public static void Generate(string filename)
yellowStroke.StrokeWidth = PtToCm(4); // 4pt
redStroke.StrokeWidth = PtToCm(4); // 4pt
using (var stream = new SKFileWStream($"filename.pdf"))
using (var document = SKDocument.CreatePdf(stream))
using (var canvas = document.BeginPage(width, height))
// translate first and then scale, don't reverse!
canvas.Translate(width / 2, height / 2);
canvas.Scale(scale);
// draw a red circle
for (int i = 0; i < 5; i++)
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
document.EndPage();
private static void Main()
string filename = nameof(ConcentricCircle);
Generate(filename);
// convert to PNG with ImageMagick
using (Process p = new Process())
p.StartInfo.FileName = "magick";
p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove filename.pdf filename.png";
p.Start();
2
Please don't radiate too much :) (+1)
– Raaja
Jan 23 at 12:46
1
I am a SkiaSharp evangelist.
– Artificial Stupidity
Jan 23 at 12:47
2
Nice alternative indeed.
– Raaja
Jan 23 at 12:49
1
I really don't know the answer for that :/
– Raaja
Jan 23 at 14:32
add a comment |
Just for fun. Another solution which draws a couple of red and yellow circles in each iteration:
documentclass[tikz,border=3mm]standalone
usetikzlibrarypositioning
begindocument
begintikzpicture[
mycircle/.style=circle, draw, fill=none, line width=8pt,
twocircle/.style=
mycircle,
minimum size=#1cm,
color=yellow!70,
append after command=%
pgfextra
node[mycircle, minimum size=thenumexpr#1-1relax cm, color=red!70] at (tikzlastnode.center) ;
endpgfextra,
]
foreach i in 2,4,...,12
node[twocircle=i] ;
endtikzpicture
enddocument
add a comment |
A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,2,...,19
pgfmathsetmacromycolorisodd x?"red!70":"yellow!50"%
node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) ;
%
endtikzpicture
enddocument
Why do you refer to the post aboutxifthen
? Neither I do use it nor do you. And you do not even need thenumexpr
becausex
already is a number (the number you want to check).
– TeXnician
Jan 23 at 14:05
@TeXnician Had you checked, you would have seen thatxifthen
is in the title of the question from which I used some code, and which is substituted automatically for the URL.numexpr
is superfluous. Will remove.
– sgmoye
Jan 23 at 14:39
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%2f471465%2fdrawing-concentric-circles-with-alternating-colors-by-means-of-foreach-in-tikz%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
You mean something like this:
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,3,5,7,9,11,13,15,17,19
node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) ;
foreach x in 2,4,6,8,10,12,14,16,18, 20
node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) ;
endtikzpicture
enddocument
which would give you:
I just made the yellow a bit lighter because, it was bleeding yellow too much :D
1
Perfect. Thanks a lot!
– Mad Hatter
Jan 23 at 12:20
1
@MadHatter Also, this happens to be the first time I am usingfor
loop inTeX
. So, thanks to you ;)
– Raaja
Jan 23 at 12:36
1
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
Jan 23 at 12:38
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
Jan 23 at 12:39
1
@Raaja You're welcome :)
– Mad Hatter
Jan 23 at 14:22
|
show 1 more comment
You mean something like this:
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,3,5,7,9,11,13,15,17,19
node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) ;
foreach x in 2,4,6,8,10,12,14,16,18, 20
node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) ;
endtikzpicture
enddocument
which would give you:
I just made the yellow a bit lighter because, it was bleeding yellow too much :D
1
Perfect. Thanks a lot!
– Mad Hatter
Jan 23 at 12:20
1
@MadHatter Also, this happens to be the first time I am usingfor
loop inTeX
. So, thanks to you ;)
– Raaja
Jan 23 at 12:36
1
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
Jan 23 at 12:38
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
Jan 23 at 12:39
1
@Raaja You're welcome :)
– Mad Hatter
Jan 23 at 14:22
|
show 1 more comment
You mean something like this:
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,3,5,7,9,11,13,15,17,19
node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) ;
foreach x in 2,4,6,8,10,12,14,16,18, 20
node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) ;
endtikzpicture
enddocument
which would give you:
I just made the yellow a bit lighter because, it was bleeding yellow too much :D
You mean something like this:
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,3,5,7,9,11,13,15,17,19
node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) ;
foreach x in 2,4,6,8,10,12,14,16,18, 20
node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) ;
endtikzpicture
enddocument
which would give you:
I just made the yellow a bit lighter because, it was bleeding yellow too much :D
answered Jan 23 at 12:14
RaajaRaaja
3,64521037
3,64521037
1
Perfect. Thanks a lot!
– Mad Hatter
Jan 23 at 12:20
1
@MadHatter Also, this happens to be the first time I am usingfor
loop inTeX
. So, thanks to you ;)
– Raaja
Jan 23 at 12:36
1
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
Jan 23 at 12:38
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
Jan 23 at 12:39
1
@Raaja You're welcome :)
– Mad Hatter
Jan 23 at 14:22
|
show 1 more comment
1
Perfect. Thanks a lot!
– Mad Hatter
Jan 23 at 12:20
1
@MadHatter Also, this happens to be the first time I am usingfor
loop inTeX
. So, thanks to you ;)
– Raaja
Jan 23 at 12:36
1
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
Jan 23 at 12:38
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
Jan 23 at 12:39
1
@Raaja You're welcome :)
– Mad Hatter
Jan 23 at 14:22
1
1
Perfect. Thanks a lot!
– Mad Hatter
Jan 23 at 12:20
Perfect. Thanks a lot!
– Mad Hatter
Jan 23 at 12:20
1
1
@MadHatter Also, this happens to be the first time I am using
for
loop in TeX
. So, thanks to you ;)– Raaja
Jan 23 at 12:36
@MadHatter Also, this happens to be the first time I am using
for
loop in TeX
. So, thanks to you ;)– Raaja
Jan 23 at 12:36
1
1
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
Jan 23 at 12:38
+1, but iirc TikZ has an ifodd test which could make this a bit shorter…
– TeXnician
Jan 23 at 12:38
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
Jan 23 at 12:39
@TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?
– Raaja
Jan 23 at 12:39
1
1
@Raaja You're welcome :)
– Mad Hatter
Jan 23 at 14:22
@Raaja You're welcome :)
– Mad Hatter
Jan 23 at 14:22
|
show 1 more comment
A slight generalization of Raaja's and TeXncian's answers in that I allow arbitrary color cycle lists, which is illustrated by an additional list of length 3.
documentclass[tikz,border=3.14mm]standalone
begindocument
begintikzpicture
deflstColors"red!70","yellow!50"
foreach X [evaluate=X as Col using lstColors[int(mod(X,2))]] in 1,...,20
draw[line width = 8pt,Col] (0,0) circle (X);
endtikzpicture
begintikzpicture
deflstColors"red!70","yellow!50","blue!70"
foreach X [evaluate=X as Col using lstColors[int(mod(X,3))]] in 1,...,20
draw[line width = 8pt,Col] (0,0) circle (X);
endtikzpicture
enddocument
1
Neat. I'd wondered how to do that... I'm certainly missing something basic, but forX=1
(the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors inlstColors
, but just wondered...
– sgmoye
Jan 23 at 15:10
@sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)
– marmot
Jan 23 at 22:04
I saw that, and to counteract it I changedlstColors[int(mod(X,2))]
tolstColors[int(mod(X-1,2))]
in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)
– sgmoye
Jan 23 at 22:58
add a comment |
A slight generalization of Raaja's and TeXncian's answers in that I allow arbitrary color cycle lists, which is illustrated by an additional list of length 3.
documentclass[tikz,border=3.14mm]standalone
begindocument
begintikzpicture
deflstColors"red!70","yellow!50"
foreach X [evaluate=X as Col using lstColors[int(mod(X,2))]] in 1,...,20
draw[line width = 8pt,Col] (0,0) circle (X);
endtikzpicture
begintikzpicture
deflstColors"red!70","yellow!50","blue!70"
foreach X [evaluate=X as Col using lstColors[int(mod(X,3))]] in 1,...,20
draw[line width = 8pt,Col] (0,0) circle (X);
endtikzpicture
enddocument
1
Neat. I'd wondered how to do that... I'm certainly missing something basic, but forX=1
(the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors inlstColors
, but just wondered...
– sgmoye
Jan 23 at 15:10
@sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)
– marmot
Jan 23 at 22:04
I saw that, and to counteract it I changedlstColors[int(mod(X,2))]
tolstColors[int(mod(X-1,2))]
in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)
– sgmoye
Jan 23 at 22:58
add a comment |
A slight generalization of Raaja's and TeXncian's answers in that I allow arbitrary color cycle lists, which is illustrated by an additional list of length 3.
documentclass[tikz,border=3.14mm]standalone
begindocument
begintikzpicture
deflstColors"red!70","yellow!50"
foreach X [evaluate=X as Col using lstColors[int(mod(X,2))]] in 1,...,20
draw[line width = 8pt,Col] (0,0) circle (X);
endtikzpicture
begintikzpicture
deflstColors"red!70","yellow!50","blue!70"
foreach X [evaluate=X as Col using lstColors[int(mod(X,3))]] in 1,...,20
draw[line width = 8pt,Col] (0,0) circle (X);
endtikzpicture
enddocument
A slight generalization of Raaja's and TeXncian's answers in that I allow arbitrary color cycle lists, which is illustrated by an additional list of length 3.
documentclass[tikz,border=3.14mm]standalone
begindocument
begintikzpicture
deflstColors"red!70","yellow!50"
foreach X [evaluate=X as Col using lstColors[int(mod(X,2))]] in 1,...,20
draw[line width = 8pt,Col] (0,0) circle (X);
endtikzpicture
begintikzpicture
deflstColors"red!70","yellow!50","blue!70"
foreach X [evaluate=X as Col using lstColors[int(mod(X,3))]] in 1,...,20
draw[line width = 8pt,Col] (0,0) circle (X);
endtikzpicture
enddocument
answered Jan 23 at 14:00
marmotmarmot
98.9k4113219
98.9k4113219
1
Neat. I'd wondered how to do that... I'm certainly missing something basic, but forX=1
(the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors inlstColors
, but just wondered...
– sgmoye
Jan 23 at 15:10
@sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)
– marmot
Jan 23 at 22:04
I saw that, and to counteract it I changedlstColors[int(mod(X,2))]
tolstColors[int(mod(X-1,2))]
in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)
– sgmoye
Jan 23 at 22:58
add a comment |
1
Neat. I'd wondered how to do that... I'm certainly missing something basic, but forX=1
(the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors inlstColors
, but just wondered...
– sgmoye
Jan 23 at 15:10
@sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)
– marmot
Jan 23 at 22:04
I saw that, and to counteract it I changedlstColors[int(mod(X,2))]
tolstColors[int(mod(X-1,2))]
in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)
– sgmoye
Jan 23 at 22:58
1
1
Neat. I'd wondered how to do that... I'm certainly missing something basic, but for
X=1
(the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors in lstColors
, but just wondered...– sgmoye
Jan 23 at 15:10
Neat. I'd wondered how to do that... I'm certainly missing something basic, but for
X=1
(the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors in lstColors
, but just wondered...– sgmoye
Jan 23 at 15:10
@sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)
– marmot
Jan 23 at 22:04
@sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)
– marmot
Jan 23 at 22:04
I saw that, and to counteract it I changed
lstColors[int(mod(X,2))]
to lstColors[int(mod(X-1,2))]
in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)– sgmoye
Jan 23 at 22:58
I saw that, and to counteract it I changed
lstColors[int(mod(X,2))]
to lstColors[int(mod(X-1,2))]
in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)– sgmoye
Jan 23 at 22:58
add a comment |
A PSTricks solution.
documentclass[pstricks]standalone
pssetrunit=4pt,unit=psrunit
begindocument
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in 1,2,...,10%
ifoddidefcyellowelsedefcredfi
pscircle[linecolor=c]i
endpspicture
enddocument
Note: There is an unnecessary white spot after converting to PNG.
Animated version
documentclass[pstricks]standalone
pssetrunit=4pt,unit=psrunit
begindocument
foreach j in 1,...,10%
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in 1,...,j%
ifoddidefcyellowelsedefcredfi
pscircle[linecolor=c]i%
endpspicture
enddocument
1
You did not animate this.
– AlexG
Jan 23 at 13:13
add a comment |
A PSTricks solution.
documentclass[pstricks]standalone
pssetrunit=4pt,unit=psrunit
begindocument
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in 1,2,...,10%
ifoddidefcyellowelsedefcredfi
pscircle[linecolor=c]i
endpspicture
enddocument
Note: There is an unnecessary white spot after converting to PNG.
Animated version
documentclass[pstricks]standalone
pssetrunit=4pt,unit=psrunit
begindocument
foreach j in 1,...,10%
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in 1,...,j%
ifoddidefcyellowelsedefcredfi
pscircle[linecolor=c]i%
endpspicture
enddocument
1
You did not animate this.
– AlexG
Jan 23 at 13:13
add a comment |
A PSTricks solution.
documentclass[pstricks]standalone
pssetrunit=4pt,unit=psrunit
begindocument
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in 1,2,...,10%
ifoddidefcyellowelsedefcredfi
pscircle[linecolor=c]i
endpspicture
enddocument
Note: There is an unnecessary white spot after converting to PNG.
Animated version
documentclass[pstricks]standalone
pssetrunit=4pt,unit=psrunit
begindocument
foreach j in 1,...,10%
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in 1,...,j%
ifoddidefcyellowelsedefcredfi
pscircle[linecolor=c]i%
endpspicture
enddocument
A PSTricks solution.
documentclass[pstricks]standalone
pssetrunit=4pt,unit=psrunit
begindocument
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in 1,2,...,10%
ifoddidefcyellowelsedefcredfi
pscircle[linecolor=c]i
endpspicture
enddocument
Note: There is an unnecessary white spot after converting to PNG.
Animated version
documentclass[pstricks]standalone
pssetrunit=4pt,unit=psrunit
begindocument
foreach j in 1,...,10%
pspicture[linewidth=.5](-11,-11)(11,11)
foreach i in 1,...,j%
ifoddidefcyellowelsedefcredfi
pscircle[linecolor=c]i%
endpspicture
enddocument
edited Jan 23 at 13:19
answered Jan 23 at 13:07
Artificial StupidityArtificial Stupidity
5,05511040
5,05511040
1
You did not animate this.
– AlexG
Jan 23 at 13:13
add a comment |
1
You did not animate this.
– AlexG
Jan 23 at 13:13
1
1
You did not animate this.
– AlexG
Jan 23 at 13:13
You did not animate this.
– AlexG
Jan 23 at 13:13
add a comment |
This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width
to make it thicker).
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,...,20
pgfmathparseisodd(x)ifnumpgfmathresult=1defcurrcolred!70elsedefcurrcolyellow!50fi
draw[line width=8pt,currcol] (0,0) circle (x cm);
endtikzpicture
enddocument
Next time, I will know how to apply thisifodd
;) [IMO, it is useful, if the loop counts grows].
– Raaja
Jan 23 at 12:46
1
@Raaja Yes, it is :)
– TeXnician
Jan 23 at 12:46
Thank you very much. P.S. I think you mean thinner not thicker lines :).
– Mad Hatter
Jan 23 at 14:18
@MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)
– TeXnician
Jan 23 at 14:31
@TeXnician Sorry, I misread your comment.
– Mad Hatter
Jan 23 at 15:01
add a comment |
This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width
to make it thicker).
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,...,20
pgfmathparseisodd(x)ifnumpgfmathresult=1defcurrcolred!70elsedefcurrcolyellow!50fi
draw[line width=8pt,currcol] (0,0) circle (x cm);
endtikzpicture
enddocument
Next time, I will know how to apply thisifodd
;) [IMO, it is useful, if the loop counts grows].
– Raaja
Jan 23 at 12:46
1
@Raaja Yes, it is :)
– TeXnician
Jan 23 at 12:46
Thank you very much. P.S. I think you mean thinner not thicker lines :).
– Mad Hatter
Jan 23 at 14:18
@MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)
– TeXnician
Jan 23 at 14:31
@TeXnician Sorry, I misread your comment.
– Mad Hatter
Jan 23 at 15:01
add a comment |
This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width
to make it thicker).
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,...,20
pgfmathparseisodd(x)ifnumpgfmathresult=1defcurrcolred!70elsedefcurrcolyellow!50fi
draw[line width=8pt,currcol] (0,0) circle (x cm);
endtikzpicture
enddocument
This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width
to make it thicker).
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,...,20
pgfmathparseisodd(x)ifnumpgfmathresult=1defcurrcolred!70elsedefcurrcolyellow!50fi
draw[line width=8pt,currcol] (0,0) circle (x cm);
endtikzpicture
enddocument
edited Jan 23 at 12:50
answered Jan 23 at 12:43
TeXnicianTeXnician
24.9k63288
24.9k63288
Next time, I will know how to apply thisifodd
;) [IMO, it is useful, if the loop counts grows].
– Raaja
Jan 23 at 12:46
1
@Raaja Yes, it is :)
– TeXnician
Jan 23 at 12:46
Thank you very much. P.S. I think you mean thinner not thicker lines :).
– Mad Hatter
Jan 23 at 14:18
@MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)
– TeXnician
Jan 23 at 14:31
@TeXnician Sorry, I misread your comment.
– Mad Hatter
Jan 23 at 15:01
add a comment |
Next time, I will know how to apply thisifodd
;) [IMO, it is useful, if the loop counts grows].
– Raaja
Jan 23 at 12:46
1
@Raaja Yes, it is :)
– TeXnician
Jan 23 at 12:46
Thank you very much. P.S. I think you mean thinner not thicker lines :).
– Mad Hatter
Jan 23 at 14:18
@MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)
– TeXnician
Jan 23 at 14:31
@TeXnician Sorry, I misread your comment.
– Mad Hatter
Jan 23 at 15:01
Next time, I will know how to apply this
ifodd
;) [IMO, it is useful, if the loop counts grows].– Raaja
Jan 23 at 12:46
Next time, I will know how to apply this
ifodd
;) [IMO, it is useful, if the loop counts grows].– Raaja
Jan 23 at 12:46
1
1
@Raaja Yes, it is :)
– TeXnician
Jan 23 at 12:46
@Raaja Yes, it is :)
– TeXnician
Jan 23 at 12:46
Thank you very much. P.S. I think you mean thinner not thicker lines :).
– Mad Hatter
Jan 23 at 14:18
Thank you very much. P.S. I think you mean thinner not thicker lines :).
– Mad Hatter
Jan 23 at 14:18
@MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)
– TeXnician
Jan 23 at 14:31
@MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)
– TeXnician
Jan 23 at 14:31
@TeXnician Sorry, I misread your comment.
– Mad Hatter
Jan 23 at 15:01
@TeXnician Sorry, I misread your comment.
– Mad Hatter
Jan 23 at 15:01
add a comment |
A SkiaSharp solution only for comparison purposes.
using SkiaSharp; // needs skiasharp nuget
using System.Diagnostics;
class ConcentricCircle
static readonly SKPaint yellowStroke = new SKPaint
Style = SKPaintStyle.Stroke,
Color = SKColors.Yellow,
IsAntialias = true
;
static readonly SKPaint redStroke = new SKPaint
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
IsAntialias = true
;
static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
static readonly float width = 6 * scale; // 6 cm
static readonly float height = 6 * scale; // 6 cm
static float PtToCm(float pt) => pt / scale;
public static void Generate(string filename)
yellowStroke.StrokeWidth = PtToCm(4); // 4pt
redStroke.StrokeWidth = PtToCm(4); // 4pt
using (var stream = new SKFileWStream($"filename.pdf"))
using (var document = SKDocument.CreatePdf(stream))
using (var canvas = document.BeginPage(width, height))
// translate first and then scale, don't reverse!
canvas.Translate(width / 2, height / 2);
canvas.Scale(scale);
// draw a red circle
for (int i = 0; i < 5; i++)
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
document.EndPage();
private static void Main()
string filename = nameof(ConcentricCircle);
Generate(filename);
// convert to PNG with ImageMagick
using (Process p = new Process())
p.StartInfo.FileName = "magick";
p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove filename.pdf filename.png";
p.Start();
2
Please don't radiate too much :) (+1)
– Raaja
Jan 23 at 12:46
1
I am a SkiaSharp evangelist.
– Artificial Stupidity
Jan 23 at 12:47
2
Nice alternative indeed.
– Raaja
Jan 23 at 12:49
1
I really don't know the answer for that :/
– Raaja
Jan 23 at 14:32
add a comment |
A SkiaSharp solution only for comparison purposes.
using SkiaSharp; // needs skiasharp nuget
using System.Diagnostics;
class ConcentricCircle
static readonly SKPaint yellowStroke = new SKPaint
Style = SKPaintStyle.Stroke,
Color = SKColors.Yellow,
IsAntialias = true
;
static readonly SKPaint redStroke = new SKPaint
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
IsAntialias = true
;
static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
static readonly float width = 6 * scale; // 6 cm
static readonly float height = 6 * scale; // 6 cm
static float PtToCm(float pt) => pt / scale;
public static void Generate(string filename)
yellowStroke.StrokeWidth = PtToCm(4); // 4pt
redStroke.StrokeWidth = PtToCm(4); // 4pt
using (var stream = new SKFileWStream($"filename.pdf"))
using (var document = SKDocument.CreatePdf(stream))
using (var canvas = document.BeginPage(width, height))
// translate first and then scale, don't reverse!
canvas.Translate(width / 2, height / 2);
canvas.Scale(scale);
// draw a red circle
for (int i = 0; i < 5; i++)
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
document.EndPage();
private static void Main()
string filename = nameof(ConcentricCircle);
Generate(filename);
// convert to PNG with ImageMagick
using (Process p = new Process())
p.StartInfo.FileName = "magick";
p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove filename.pdf filename.png";
p.Start();
2
Please don't radiate too much :) (+1)
– Raaja
Jan 23 at 12:46
1
I am a SkiaSharp evangelist.
– Artificial Stupidity
Jan 23 at 12:47
2
Nice alternative indeed.
– Raaja
Jan 23 at 12:49
1
I really don't know the answer for that :/
– Raaja
Jan 23 at 14:32
add a comment |
A SkiaSharp solution only for comparison purposes.
using SkiaSharp; // needs skiasharp nuget
using System.Diagnostics;
class ConcentricCircle
static readonly SKPaint yellowStroke = new SKPaint
Style = SKPaintStyle.Stroke,
Color = SKColors.Yellow,
IsAntialias = true
;
static readonly SKPaint redStroke = new SKPaint
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
IsAntialias = true
;
static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
static readonly float width = 6 * scale; // 6 cm
static readonly float height = 6 * scale; // 6 cm
static float PtToCm(float pt) => pt / scale;
public static void Generate(string filename)
yellowStroke.StrokeWidth = PtToCm(4); // 4pt
redStroke.StrokeWidth = PtToCm(4); // 4pt
using (var stream = new SKFileWStream($"filename.pdf"))
using (var document = SKDocument.CreatePdf(stream))
using (var canvas = document.BeginPage(width, height))
// translate first and then scale, don't reverse!
canvas.Translate(width / 2, height / 2);
canvas.Scale(scale);
// draw a red circle
for (int i = 0; i < 5; i++)
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
document.EndPage();
private static void Main()
string filename = nameof(ConcentricCircle);
Generate(filename);
// convert to PNG with ImageMagick
using (Process p = new Process())
p.StartInfo.FileName = "magick";
p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove filename.pdf filename.png";
p.Start();
A SkiaSharp solution only for comparison purposes.
using SkiaSharp; // needs skiasharp nuget
using System.Diagnostics;
class ConcentricCircle
static readonly SKPaint yellowStroke = new SKPaint
Style = SKPaintStyle.Stroke,
Color = SKColors.Yellow,
IsAntialias = true
;
static readonly SKPaint redStroke = new SKPaint
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
IsAntialias = true
;
static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
static readonly float width = 6 * scale; // 6 cm
static readonly float height = 6 * scale; // 6 cm
static float PtToCm(float pt) => pt / scale;
public static void Generate(string filename)
yellowStroke.StrokeWidth = PtToCm(4); // 4pt
redStroke.StrokeWidth = PtToCm(4); // 4pt
using (var stream = new SKFileWStream($"filename.pdf"))
using (var document = SKDocument.CreatePdf(stream))
using (var canvas = document.BeginPage(width, height))
// translate first and then scale, don't reverse!
canvas.Translate(width / 2, height / 2);
canvas.Scale(scale);
// draw a red circle
for (int i = 0; i < 5; i++)
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
document.EndPage();
private static void Main()
string filename = nameof(ConcentricCircle);
Generate(filename);
// convert to PNG with ImageMagick
using (Process p = new Process())
p.StartInfo.FileName = "magick";
p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove filename.pdf filename.png";
p.Start();
edited Jan 23 at 14:39
answered Jan 23 at 12:43
Artificial StupidityArtificial Stupidity
5,05511040
5,05511040
2
Please don't radiate too much :) (+1)
– Raaja
Jan 23 at 12:46
1
I am a SkiaSharp evangelist.
– Artificial Stupidity
Jan 23 at 12:47
2
Nice alternative indeed.
– Raaja
Jan 23 at 12:49
1
I really don't know the answer for that :/
– Raaja
Jan 23 at 14:32
add a comment |
2
Please don't radiate too much :) (+1)
– Raaja
Jan 23 at 12:46
1
I am a SkiaSharp evangelist.
– Artificial Stupidity
Jan 23 at 12:47
2
Nice alternative indeed.
– Raaja
Jan 23 at 12:49
1
I really don't know the answer for that :/
– Raaja
Jan 23 at 14:32
2
2
Please don't radiate too much :) (+1)
– Raaja
Jan 23 at 12:46
Please don't radiate too much :) (+1)
– Raaja
Jan 23 at 12:46
1
1
I am a SkiaSharp evangelist.
– Artificial Stupidity
Jan 23 at 12:47
I am a SkiaSharp evangelist.
– Artificial Stupidity
Jan 23 at 12:47
2
2
Nice alternative indeed.
– Raaja
Jan 23 at 12:49
Nice alternative indeed.
– Raaja
Jan 23 at 12:49
1
1
I really don't know the answer for that :/
– Raaja
Jan 23 at 14:32
I really don't know the answer for that :/
– Raaja
Jan 23 at 14:32
add a comment |
Just for fun. Another solution which draws a couple of red and yellow circles in each iteration:
documentclass[tikz,border=3mm]standalone
usetikzlibrarypositioning
begindocument
begintikzpicture[
mycircle/.style=circle, draw, fill=none, line width=8pt,
twocircle/.style=
mycircle,
minimum size=#1cm,
color=yellow!70,
append after command=%
pgfextra
node[mycircle, minimum size=thenumexpr#1-1relax cm, color=red!70] at (tikzlastnode.center) ;
endpgfextra,
]
foreach i in 2,4,...,12
node[twocircle=i] ;
endtikzpicture
enddocument
add a comment |
Just for fun. Another solution which draws a couple of red and yellow circles in each iteration:
documentclass[tikz,border=3mm]standalone
usetikzlibrarypositioning
begindocument
begintikzpicture[
mycircle/.style=circle, draw, fill=none, line width=8pt,
twocircle/.style=
mycircle,
minimum size=#1cm,
color=yellow!70,
append after command=%
pgfextra
node[mycircle, minimum size=thenumexpr#1-1relax cm, color=red!70] at (tikzlastnode.center) ;
endpgfextra,
]
foreach i in 2,4,...,12
node[twocircle=i] ;
endtikzpicture
enddocument
add a comment |
Just for fun. Another solution which draws a couple of red and yellow circles in each iteration:
documentclass[tikz,border=3mm]standalone
usetikzlibrarypositioning
begindocument
begintikzpicture[
mycircle/.style=circle, draw, fill=none, line width=8pt,
twocircle/.style=
mycircle,
minimum size=#1cm,
color=yellow!70,
append after command=%
pgfextra
node[mycircle, minimum size=thenumexpr#1-1relax cm, color=red!70] at (tikzlastnode.center) ;
endpgfextra,
]
foreach i in 2,4,...,12
node[twocircle=i] ;
endtikzpicture
enddocument
Just for fun. Another solution which draws a couple of red and yellow circles in each iteration:
documentclass[tikz,border=3mm]standalone
usetikzlibrarypositioning
begindocument
begintikzpicture[
mycircle/.style=circle, draw, fill=none, line width=8pt,
twocircle/.style=
mycircle,
minimum size=#1cm,
color=yellow!70,
append after command=%
pgfextra
node[mycircle, minimum size=thenumexpr#1-1relax cm, color=red!70] at (tikzlastnode.center) ;
endpgfextra,
]
foreach i in 2,4,...,12
node[twocircle=i] ;
endtikzpicture
enddocument
answered Jan 24 at 10:25
IgnasiIgnasi
93k4167310
93k4167310
add a comment |
add a comment |
A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,2,...,19
pgfmathsetmacromycolorisodd x?"red!70":"yellow!50"%
node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) ;
%
endtikzpicture
enddocument
Why do you refer to the post aboutxifthen
? Neither I do use it nor do you. And you do not even need thenumexpr
becausex
already is a number (the number you want to check).
– TeXnician
Jan 23 at 14:05
@TeXnician Had you checked, you would have seen thatxifthen
is in the title of the question from which I used some code, and which is substituted automatically for the URL.numexpr
is superfluous. Will remove.
– sgmoye
Jan 23 at 14:39
add a comment |
A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,2,...,19
pgfmathsetmacromycolorisodd x?"red!70":"yellow!50"%
node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) ;
%
endtikzpicture
enddocument
Why do you refer to the post aboutxifthen
? Neither I do use it nor do you. And you do not even need thenumexpr
becausex
already is a number (the number you want to check).
– TeXnician
Jan 23 at 14:05
@TeXnician Had you checked, you would have seen thatxifthen
is in the title of the question from which I used some code, and which is substituted automatically for the URL.numexpr
is superfluous. Will remove.
– sgmoye
Jan 23 at 14:39
add a comment |
A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,2,...,19
pgfmathsetmacromycolorisodd x?"red!70":"yellow!50"%
node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) ;
%
endtikzpicture
enddocument
A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):
documentclassstandalone
usepackagetikz
begindocument
begintikzpicture[mystyle/.style=circle,draw,fill=none,minimum size=20, line width = 8pt]
foreach x in 1,2,...,19
pgfmathsetmacromycolorisodd x?"red!70":"yellow!50"%
node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) ;
%
endtikzpicture
enddocument
edited Jan 23 at 14:42
answered Jan 23 at 13:02
sgmoyesgmoye
3,85311326
3,85311326
Why do you refer to the post aboutxifthen
? Neither I do use it nor do you. And you do not even need thenumexpr
becausex
already is a number (the number you want to check).
– TeXnician
Jan 23 at 14:05
@TeXnician Had you checked, you would have seen thatxifthen
is in the title of the question from which I used some code, and which is substituted automatically for the URL.numexpr
is superfluous. Will remove.
– sgmoye
Jan 23 at 14:39
add a comment |
Why do you refer to the post aboutxifthen
? Neither I do use it nor do you. And you do not even need thenumexpr
becausex
already is a number (the number you want to check).
– TeXnician
Jan 23 at 14:05
@TeXnician Had you checked, you would have seen thatxifthen
is in the title of the question from which I used some code, and which is substituted automatically for the URL.numexpr
is superfluous. Will remove.
– sgmoye
Jan 23 at 14:39
Why do you refer to the post about
xifthen
? Neither I do use it nor do you. And you do not even need the numexpr
because x
already is a number (the number you want to check).– TeXnician
Jan 23 at 14:05
Why do you refer to the post about
xifthen
? Neither I do use it nor do you. And you do not even need the numexpr
because x
already is a number (the number you want to check).– TeXnician
Jan 23 at 14:05
@TeXnician Had you checked, you would have seen that
xifthen
is in the title of the question from which I used some code, and which is substituted automatically for the URL. numexpr
is superfluous. Will remove.– sgmoye
Jan 23 at 14:39
@TeXnician Had you checked, you would have seen that
xifthen
is in the title of the question from which I used some code, and which is substituted automatically for the URL. numexpr
is superfluous. Will remove.– sgmoye
Jan 23 at 14:39
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%2f471465%2fdrawing-concentric-circles-with-alternating-colors-by-means-of-foreach-in-tikz%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