Using a macro with TikZ style attributes in a draw command
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
For use in a foreach
loop, I would like to have the style attributes for a rectangle stored in a macro. The style attributes would include the fill color, opacity, etcetera (but the attributes that occur each time are not the same ones each time). Then I would like to use that command, to be expanded in a TikZ draw
command.
I have tried several things to make it work (wrapping the draw
command in a macro, using edef
and/or expandafter
), and tried to google a solution for this problem, but so far unsuccessfully. I boiled down the issue to the following minimal working example. Can anyone help me access the style attributes inside a macro and transfer them to a TikZ drawing command?
documentclassminimal
usepackagetikz
begindocument
begintikzpicture
% this works
draw[fill=blue,opacity=0.8] (0,0) rectangle (1,1);
endtikzpicture
begintikzpicture
% this doesn't work, but I want it to
defstyleattributesfill=blue,opacity=0.8
draw[styleattributes] (0,0) rectangle (1,1);
endtikzpicture
enddocument
tikz-pgf macros
 |Â
show 1 more comment
up vote
1
down vote
favorite
For use in a foreach
loop, I would like to have the style attributes for a rectangle stored in a macro. The style attributes would include the fill color, opacity, etcetera (but the attributes that occur each time are not the same ones each time). Then I would like to use that command, to be expanded in a TikZ draw
command.
I have tried several things to make it work (wrapping the draw
command in a macro, using edef
and/or expandafter
), and tried to google a solution for this problem, but so far unsuccessfully. I boiled down the issue to the following minimal working example. Can anyone help me access the style attributes inside a macro and transfer them to a TikZ drawing command?
documentclassminimal
usepackagetikz
begindocument
begintikzpicture
% this works
draw[fill=blue,opacity=0.8] (0,0) rectangle (1,1);
endtikzpicture
begintikzpicture
% this doesn't work, but I want it to
defstyleattributesfill=blue,opacity=0.8
draw[styleattributes] (0,0) rectangle (1,1);
endtikzpicture
enddocument
tikz-pgf macros
2
Use a TikZ style, see here, for example: tex.stackexchange.com/questions/21395/â¦
â CarLaTeX
1 hour ago
The number of arguments isn't fixed. The list of attributes is of variable length and the attributes themselves can differ.
â RobV
1 hour ago
1
Styles can have arguments which can be parametrically set in aforeach
. You can also define "n" styles and use them as parameter of theforeach
. See here, for example: tex.stackexchange.com/a/64237/101651
â CarLaTeX
1 hour ago
Yes, thank you! I had come across usingexpand once
but I didn't know about the trick withapply style
definition. I will answer my question with code that makes the MWE work.
â RobV
59 mins ago
Or you can add the (same) answer as well and I can accept that one as the answer, if you want to get the credit.
â RobV
56 mins ago
 |Â
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
For use in a foreach
loop, I would like to have the style attributes for a rectangle stored in a macro. The style attributes would include the fill color, opacity, etcetera (but the attributes that occur each time are not the same ones each time). Then I would like to use that command, to be expanded in a TikZ draw
command.
I have tried several things to make it work (wrapping the draw
command in a macro, using edef
and/or expandafter
), and tried to google a solution for this problem, but so far unsuccessfully. I boiled down the issue to the following minimal working example. Can anyone help me access the style attributes inside a macro and transfer them to a TikZ drawing command?
documentclassminimal
usepackagetikz
begindocument
begintikzpicture
% this works
draw[fill=blue,opacity=0.8] (0,0) rectangle (1,1);
endtikzpicture
begintikzpicture
% this doesn't work, but I want it to
defstyleattributesfill=blue,opacity=0.8
draw[styleattributes] (0,0) rectangle (1,1);
endtikzpicture
enddocument
tikz-pgf macros
For use in a foreach
loop, I would like to have the style attributes for a rectangle stored in a macro. The style attributes would include the fill color, opacity, etcetera (but the attributes that occur each time are not the same ones each time). Then I would like to use that command, to be expanded in a TikZ draw
command.
I have tried several things to make it work (wrapping the draw
command in a macro, using edef
and/or expandafter
), and tried to google a solution for this problem, but so far unsuccessfully. I boiled down the issue to the following minimal working example. Can anyone help me access the style attributes inside a macro and transfer them to a TikZ drawing command?
documentclassminimal
usepackagetikz
begindocument
begintikzpicture
% this works
draw[fill=blue,opacity=0.8] (0,0) rectangle (1,1);
endtikzpicture
begintikzpicture
% this doesn't work, but I want it to
defstyleattributesfill=blue,opacity=0.8
draw[styleattributes] (0,0) rectangle (1,1);
endtikzpicture
enddocument
tikz-pgf macros
tikz-pgf macros
asked 1 hour ago
RobV
1565
1565
2
Use a TikZ style, see here, for example: tex.stackexchange.com/questions/21395/â¦
â CarLaTeX
1 hour ago
The number of arguments isn't fixed. The list of attributes is of variable length and the attributes themselves can differ.
â RobV
1 hour ago
1
Styles can have arguments which can be parametrically set in aforeach
. You can also define "n" styles and use them as parameter of theforeach
. See here, for example: tex.stackexchange.com/a/64237/101651
â CarLaTeX
1 hour ago
Yes, thank you! I had come across usingexpand once
but I didn't know about the trick withapply style
definition. I will answer my question with code that makes the MWE work.
â RobV
59 mins ago
Or you can add the (same) answer as well and I can accept that one as the answer, if you want to get the credit.
â RobV
56 mins ago
 |Â
show 1 more comment
2
Use a TikZ style, see here, for example: tex.stackexchange.com/questions/21395/â¦
â CarLaTeX
1 hour ago
The number of arguments isn't fixed. The list of attributes is of variable length and the attributes themselves can differ.
â RobV
1 hour ago
1
Styles can have arguments which can be parametrically set in aforeach
. You can also define "n" styles and use them as parameter of theforeach
. See here, for example: tex.stackexchange.com/a/64237/101651
â CarLaTeX
1 hour ago
Yes, thank you! I had come across usingexpand once
but I didn't know about the trick withapply style
definition. I will answer my question with code that makes the MWE work.
â RobV
59 mins ago
Or you can add the (same) answer as well and I can accept that one as the answer, if you want to get the credit.
â RobV
56 mins ago
2
2
Use a TikZ style, see here, for example: tex.stackexchange.com/questions/21395/â¦
â CarLaTeX
1 hour ago
Use a TikZ style, see here, for example: tex.stackexchange.com/questions/21395/â¦
â CarLaTeX
1 hour ago
The number of arguments isn't fixed. The list of attributes is of variable length and the attributes themselves can differ.
â RobV
1 hour ago
The number of arguments isn't fixed. The list of attributes is of variable length and the attributes themselves can differ.
â RobV
1 hour ago
1
1
Styles can have arguments which can be parametrically set in a
foreach
. You can also define "n" styles and use them as parameter of the foreach
. See here, for example: tex.stackexchange.com/a/64237/101651â CarLaTeX
1 hour ago
Styles can have arguments which can be parametrically set in a
foreach
. You can also define "n" styles and use them as parameter of the foreach
. See here, for example: tex.stackexchange.com/a/64237/101651â CarLaTeX
1 hour ago
Yes, thank you! I had come across using
expand once
but I didn't know about the trick with apply style
definition. I will answer my question with code that makes the MWE work.â RobV
59 mins ago
Yes, thank you! I had come across using
expand once
but I didn't know about the trick with apply style
definition. I will answer my question with code that makes the MWE work.â RobV
59 mins ago
Or you can add the (same) answer as well and I can accept that one as the answer, if you want to get the credit.
â RobV
56 mins ago
Or you can add the (same) answer as well and I can accept that one as the answer, if you want to get the credit.
â RobV
56 mins ago
 |Â
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
3
down vote
Thanks to CarLaTeX's comment that pointed to this question, I found the following solution:
documentclassminimal
usepackagetikz
tikzsetapply style/.code=tikzset#1
begindocument
begintikzpicture
defstyleattributesfill=blue,opacity=0.8
draw[apply style/.expand once=styleattributes] (0,0) rectangle (1,1);
endtikzpicture
enddocument
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Thanks to CarLaTeX's comment that pointed to this question, I found the following solution:
documentclassminimal
usepackagetikz
tikzsetapply style/.code=tikzset#1
begindocument
begintikzpicture
defstyleattributesfill=blue,opacity=0.8
draw[apply style/.expand once=styleattributes] (0,0) rectangle (1,1);
endtikzpicture
enddocument
add a comment |Â
up vote
3
down vote
Thanks to CarLaTeX's comment that pointed to this question, I found the following solution:
documentclassminimal
usepackagetikz
tikzsetapply style/.code=tikzset#1
begindocument
begintikzpicture
defstyleattributesfill=blue,opacity=0.8
draw[apply style/.expand once=styleattributes] (0,0) rectangle (1,1);
endtikzpicture
enddocument
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Thanks to CarLaTeX's comment that pointed to this question, I found the following solution:
documentclassminimal
usepackagetikz
tikzsetapply style/.code=tikzset#1
begindocument
begintikzpicture
defstyleattributesfill=blue,opacity=0.8
draw[apply style/.expand once=styleattributes] (0,0) rectangle (1,1);
endtikzpicture
enddocument
Thanks to CarLaTeX's comment that pointed to this question, I found the following solution:
documentclassminimal
usepackagetikz
tikzsetapply style/.code=tikzset#1
begindocument
begintikzpicture
defstyleattributesfill=blue,opacity=0.8
draw[apply style/.expand once=styleattributes] (0,0) rectangle (1,1);
endtikzpicture
enddocument
answered 57 mins ago
RobV
1565
1565
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f456259%2fusing-a-macro-with-tikz-style-attributes-in-a-draw-command%23new-answer', 'question_page');
);
Post as a guest
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
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
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
2
Use a TikZ style, see here, for example: tex.stackexchange.com/questions/21395/â¦
â CarLaTeX
1 hour ago
The number of arguments isn't fixed. The list of attributes is of variable length and the attributes themselves can differ.
â RobV
1 hour ago
1
Styles can have arguments which can be parametrically set in a
foreach
. You can also define "n" styles and use them as parameter of theforeach
. See here, for example: tex.stackexchange.com/a/64237/101651â CarLaTeX
1 hour ago
Yes, thank you! I had come across using
expand once
but I didn't know about the trick withapply style
definition. I will answer my question with code that makes the MWE work.â RobV
59 mins ago
Or you can add the (same) answer as well and I can accept that one as the answer, if you want to get the credit.
â RobV
56 mins ago