Using a macro with TikZ style attributes in a draw command

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












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









share|improve this question

















  • 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 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










  • 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














up vote
1
down vote

favorite
1












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









share|improve this question

















  • 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 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










  • 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












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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










  • 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




    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 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










  • 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










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





share|improve this answer




















    Your Answer







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

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

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f456259%2fusing-a-macro-with-tikz-style-attributes-in-a-draw-command%23new-answer', 'question_page');

    );

    Post as a guest






























    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





    share|improve this answer
























      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





      share|improve this answer






















        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 57 mins ago









        RobV

        1565




        1565



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke