Draw a shape with operation on parameters in Tikz

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











up vote
3
down vote

favorite












I want to draw a shape that I want to reuse. This shape is defined by geometrical input parameters (distances, radii) in a newcommand. Thus I want to perform some operations to define for example the center of my shape for example.



Maybe I am not treating thos problem correctly as I also want this shpae to work with the node proprties tu use the anchors and not only the origin of the shape for placement.



Here my small code. Thank you in advance.



documentclass[border=2mm]standalone %<--- standalone
usepackagetikz


newcommandhinge[3] % #1 = name , #2 = rotation angle

draw[black, line width=1mm] (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;



begindocument


begintikzpicture
hinge(0,0,1);
draw[red] (0,0) circle (0.1);
endtikzpicture
enddocument









share|improve this question

























    up vote
    3
    down vote

    favorite












    I want to draw a shape that I want to reuse. This shape is defined by geometrical input parameters (distances, radii) in a newcommand. Thus I want to perform some operations to define for example the center of my shape for example.



    Maybe I am not treating thos problem correctly as I also want this shpae to work with the node proprties tu use the anchors and not only the origin of the shape for placement.



    Here my small code. Thank you in advance.



    documentclass[border=2mm]standalone %<--- standalone
    usepackagetikz


    newcommandhinge[3] % #1 = name , #2 = rotation angle

    draw[black, line width=1mm] (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;



    begindocument


    begintikzpicture
    hinge(0,0,1);
    draw[red] (0,0) circle (0.1);
    endtikzpicture
    enddocument









    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I want to draw a shape that I want to reuse. This shape is defined by geometrical input parameters (distances, radii) in a newcommand. Thus I want to perform some operations to define for example the center of my shape for example.



      Maybe I am not treating thos problem correctly as I also want this shpae to work with the node proprties tu use the anchors and not only the origin of the shape for placement.



      Here my small code. Thank you in advance.



      documentclass[border=2mm]standalone %<--- standalone
      usepackagetikz


      newcommandhinge[3] % #1 = name , #2 = rotation angle

      draw[black, line width=1mm] (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;



      begindocument


      begintikzpicture
      hinge(0,0,1);
      draw[red] (0,0) circle (0.1);
      endtikzpicture
      enddocument









      share|improve this question













      I want to draw a shape that I want to reuse. This shape is defined by geometrical input parameters (distances, radii) in a newcommand. Thus I want to perform some operations to define for example the center of my shape for example.



      Maybe I am not treating thos problem correctly as I also want this shpae to work with the node proprties tu use the anchors and not only the origin of the shape for placement.



      Here my small code. Thank you in advance.



      documentclass[border=2mm]standalone %<--- standalone
      usepackagetikz


      newcommandhinge[3] % #1 = name , #2 = rotation angle

      draw[black, line width=1mm] (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;



      begindocument


      begintikzpicture
      hinge(0,0,1);
      draw[red] (0,0) circle (0.1);
      endtikzpicture
      enddocument






      tikz-pgf macros






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 50 mins ago









      Willi

      1779




      1779




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You could also use a rectangular node but with a border and filling based on path picture option.



          documentclass[border=2mm]standalone %<--- standalone
          usepackagetikz
          usetikzlibrarypositioning

          begindocument
          begintikzpicture[hinge/.style 2 args=
          path picture=(path picture bounding box.north east)
          --([xshift=#2]path picture bounding box.north) arc(0:-180:#2)
          -,
          hinge/.default=5mm
          ]

          node[minimum width=4cm, minimum height=2cm,
          , hinge] (a) Some text inside;
          node[minimum width=2cm, minimum height=1cm,
          , hinge=fill=red!302mm, above right=0pt of a] (b) Test;

          node[minimum width=2cm, minimum height=1cm,
          , hinge=draw=green,fill=blue!302mm, right= of a] (c) Test 2;
          endtikzpicture
          enddocument


          enter image description here






          share|improve this answer



























            up vote
            2
            down vote













            I probably misread the question but if you want to give the thing node-like anchors, you may want to use a pic. In the following example, I give the thing the name X and draw a 45 degree line from its north east corner.



            documentclass[border=2mm]standalone %<--- standalone
            usepackagetikz
            tikzsetpics/.cd,
            hinge/.style args=#1/#2/#3code=
            draw[black, line width=1mm] (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;



            begindocument
            begintikzpicture
            pic[local bounding box=X] at (0,0) hinge=0/0/1;
            draw (X.north east) -- ++ (1,1);
            draw[red] (0,0) circle (0.1);
            endtikzpicture
            enddocument


            enter image description here






            share|improve this answer




















            • Ooh, interesting :)
              – Phelype Oleinik
              27 mins ago










            • +1 I had the same idea
              – Andrew
              6 mins ago

















            up vote
            1
            down vote













            When you create a command with newcommand, the arguments must be given within pairs of braces, so for the command to work you would need to use hinge001. To preserve the usage syntax and keep the command TikZ-like, you can use xparse:



            documentclass[border=2mm]standalone %<--- standalone
            usepackagetikz
            usepackagexparse

            NewDocumentCommandhinge
            > SplitArgument2, r() % #1 = name , #2 = rotation angle
            %
            hingeMAIN#1%

            newcommandhingeMAIN[3]%
            draw [black, line width = 1mm]
            (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;


            begindocument

            begintikzpicture
            hinge(0,0,1);
            draw[red] (0,0) circle (0.1);
            endtikzpicture
            enddocument


            The argument specification > SplitArgument2, r() says that there is a required argument delimited by () which will be pre-processed by the SplitArgument, which will split the input argument in 2+1 items separated by a ,. This command will transform (0,1,2) into 012, which is then passed to hingeMAIN, which expects braced arguments and does the drawing. The ; after the hinge command is not necessary.




            enter image description here







            share|improve this answer




















            • Thank you for your fast answer!!! I feel stupid because of the synthax of my command... anyway, is it the right way to make a macro-shape to reuse in tikz or is there an elegant way?
              – Willi
              36 mins ago










            • I think xparse is the best way to easily create custom-delimited commands. The other way to to that would be defhinge(#1,#2,#3)<stuff here>, but this would be much more fragile than the xparse version, so I think it's as elegant as it can get :)
              – Phelype Oleinik
              33 mins ago










            • My concern was why I could not perform #1+#2 in the drawing but you solved my problem with replacing hinge(1,2,3) by inge123
              – Willi
              24 mins ago

















            up vote
            0
            down vote













            I had the same idea as @marmot: you should use a pic for this -- but I really like Ignasi's approach too!



            Most of the solutions above do not seem to handle your requirement about node coordinates and the shapes are not styled further. It is easy to do both using pics, which you can read more about in section 18.2 of the tikz manual (version 3.0.1a).



            Specifically, you can create something like:



            enter image description here



            using the code:



            documentclass[border=2mm]standalone %<--- standalone
            usepackagetikz

            tikzset
            pics/hinge/.style args = #1,#2% name, style
            code =
            draw[black, line width=1mm, #2]
            (0,0)--++(1,0)--++(0:0) arc(0:180:-1) node[pos=0.5](#1)
            --++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)
            --++(-1,0)--cycle;




            begindocument

            begintikzpicture
            draw (4,4) pichinge=first,blue;
            pic at (0,0) hinge=second,red,rotate=30;
            draw (0,4) pichinge=third,scale=0.4;
            draw[thick,->](first)--(second);
            endtikzpicture

            enddocument


            As I have shown, you can place a pic using either a draw command or a pic command. I have define a pic called hinge that takes two mandatory arguments:



            • the node name, which is in the center of the pic

            • and any styling

            Both arguments to hinge are mandatory but you can omit one of them by using a comma:



            draw (4,0) pichinge=fourth,;


            is perfectly valid -- but be careful because you will get a nasty error message if you try:



            draw (4,0) pichinge=fourth;


            Finally, as pics are placed relative to the point that they are placed (using draw or pic), I dropped your (#1-#3,#2+#3). If this is really needed it is easy enough to add back in as pics can take many arguments, although at some point it becomes easier to use keys.






            share|improve this answer






















            • Thank you very much all for your fast answer. Pic seems to be dedicated to what I try to do!
              – Willi
              2 mins ago










            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%2f455807%2fdraw-a-shape-with-operation-on-parameters-in-tikz%23new-answer', 'question_page');

            );

            Post as a guest






























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            You could also use a rectangular node but with a border and filling based on path picture option.



            documentclass[border=2mm]standalone %<--- standalone
            usepackagetikz
            usetikzlibrarypositioning

            begindocument
            begintikzpicture[hinge/.style 2 args=
            path picture=(path picture bounding box.north east)
            --([xshift=#2]path picture bounding box.north) arc(0:-180:#2)
            -,
            hinge/.default=5mm
            ]

            node[minimum width=4cm, minimum height=2cm,
            , hinge] (a) Some text inside;
            node[minimum width=2cm, minimum height=1cm,
            , hinge=fill=red!302mm, above right=0pt of a] (b) Test;

            node[minimum width=2cm, minimum height=1cm,
            , hinge=draw=green,fill=blue!302mm, right= of a] (c) Test 2;
            endtikzpicture
            enddocument


            enter image description here






            share|improve this answer
























              up vote
              2
              down vote



              accepted










              You could also use a rectangular node but with a border and filling based on path picture option.



              documentclass[border=2mm]standalone %<--- standalone
              usepackagetikz
              usetikzlibrarypositioning

              begindocument
              begintikzpicture[hinge/.style 2 args=
              path picture=(path picture bounding box.north east)
              --([xshift=#2]path picture bounding box.north) arc(0:-180:#2)
              -,
              hinge/.default=5mm
              ]

              node[minimum width=4cm, minimum height=2cm,
              , hinge] (a) Some text inside;
              node[minimum width=2cm, minimum height=1cm,
              , hinge=fill=red!302mm, above right=0pt of a] (b) Test;

              node[minimum width=2cm, minimum height=1cm,
              , hinge=draw=green,fill=blue!302mm, right= of a] (c) Test 2;
              endtikzpicture
              enddocument


              enter image description here






              share|improve this answer






















                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                You could also use a rectangular node but with a border and filling based on path picture option.



                documentclass[border=2mm]standalone %<--- standalone
                usepackagetikz
                usetikzlibrarypositioning

                begindocument
                begintikzpicture[hinge/.style 2 args=
                path picture=(path picture bounding box.north east)
                --([xshift=#2]path picture bounding box.north) arc(0:-180:#2)
                -,
                hinge/.default=5mm
                ]

                node[minimum width=4cm, minimum height=2cm,
                , hinge] (a) Some text inside;
                node[minimum width=2cm, minimum height=1cm,
                , hinge=fill=red!302mm, above right=0pt of a] (b) Test;

                node[minimum width=2cm, minimum height=1cm,
                , hinge=draw=green,fill=blue!302mm, right= of a] (c) Test 2;
                endtikzpicture
                enddocument


                enter image description here






                share|improve this answer












                You could also use a rectangular node but with a border and filling based on path picture option.



                documentclass[border=2mm]standalone %<--- standalone
                usepackagetikz
                usetikzlibrarypositioning

                begindocument
                begintikzpicture[hinge/.style 2 args=
                path picture=(path picture bounding box.north east)
                --([xshift=#2]path picture bounding box.north) arc(0:-180:#2)
                -,
                hinge/.default=5mm
                ]

                node[minimum width=4cm, minimum height=2cm,
                , hinge] (a) Some text inside;
                node[minimum width=2cm, minimum height=1cm,
                , hinge=fill=red!302mm, above right=0pt of a] (b) Test;

                node[minimum width=2cm, minimum height=1cm,
                , hinge=draw=green,fill=blue!302mm, right= of a] (c) Test 2;
                endtikzpicture
                enddocument


                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 17 mins ago









                Ignasi

                88.6k4159296




                88.6k4159296




















                    up vote
                    2
                    down vote













                    I probably misread the question but if you want to give the thing node-like anchors, you may want to use a pic. In the following example, I give the thing the name X and draw a 45 degree line from its north east corner.



                    documentclass[border=2mm]standalone %<--- standalone
                    usepackagetikz
                    tikzsetpics/.cd,
                    hinge/.style args=#1/#2/#3code=
                    draw[black, line width=1mm] (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;



                    begindocument
                    begintikzpicture
                    pic[local bounding box=X] at (0,0) hinge=0/0/1;
                    draw (X.north east) -- ++ (1,1);
                    draw[red] (0,0) circle (0.1);
                    endtikzpicture
                    enddocument


                    enter image description here






                    share|improve this answer




















                    • Ooh, interesting :)
                      – Phelype Oleinik
                      27 mins ago










                    • +1 I had the same idea
                      – Andrew
                      6 mins ago














                    up vote
                    2
                    down vote













                    I probably misread the question but if you want to give the thing node-like anchors, you may want to use a pic. In the following example, I give the thing the name X and draw a 45 degree line from its north east corner.



                    documentclass[border=2mm]standalone %<--- standalone
                    usepackagetikz
                    tikzsetpics/.cd,
                    hinge/.style args=#1/#2/#3code=
                    draw[black, line width=1mm] (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;



                    begindocument
                    begintikzpicture
                    pic[local bounding box=X] at (0,0) hinge=0/0/1;
                    draw (X.north east) -- ++ (1,1);
                    draw[red] (0,0) circle (0.1);
                    endtikzpicture
                    enddocument


                    enter image description here






                    share|improve this answer




















                    • Ooh, interesting :)
                      – Phelype Oleinik
                      27 mins ago










                    • +1 I had the same idea
                      – Andrew
                      6 mins ago












                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    I probably misread the question but if you want to give the thing node-like anchors, you may want to use a pic. In the following example, I give the thing the name X and draw a 45 degree line from its north east corner.



                    documentclass[border=2mm]standalone %<--- standalone
                    usepackagetikz
                    tikzsetpics/.cd,
                    hinge/.style args=#1/#2/#3code=
                    draw[black, line width=1mm] (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;



                    begindocument
                    begintikzpicture
                    pic[local bounding box=X] at (0,0) hinge=0/0/1;
                    draw (X.north east) -- ++ (1,1);
                    draw[red] (0,0) circle (0.1);
                    endtikzpicture
                    enddocument


                    enter image description here






                    share|improve this answer












                    I probably misread the question but if you want to give the thing node-like anchors, you may want to use a pic. In the following example, I give the thing the name X and draw a 45 degree line from its north east corner.



                    documentclass[border=2mm]standalone %<--- standalone
                    usepackagetikz
                    tikzsetpics/.cd,
                    hinge/.style args=#1/#2/#3code=
                    draw[black, line width=1mm] (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;



                    begindocument
                    begintikzpicture
                    pic[local bounding box=X] at (0,0) hinge=0/0/1;
                    draw (X.north east) -- ++ (1,1);
                    draw[red] (0,0) circle (0.1);
                    endtikzpicture
                    enddocument


                    enter image description here







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 31 mins ago









                    marmot

                    66.5k471143




                    66.5k471143











                    • Ooh, interesting :)
                      – Phelype Oleinik
                      27 mins ago










                    • +1 I had the same idea
                      – Andrew
                      6 mins ago
















                    • Ooh, interesting :)
                      – Phelype Oleinik
                      27 mins ago










                    • +1 I had the same idea
                      – Andrew
                      6 mins ago















                    Ooh, interesting :)
                    – Phelype Oleinik
                    27 mins ago




                    Ooh, interesting :)
                    – Phelype Oleinik
                    27 mins ago












                    +1 I had the same idea
                    – Andrew
                    6 mins ago




                    +1 I had the same idea
                    – Andrew
                    6 mins ago










                    up vote
                    1
                    down vote













                    When you create a command with newcommand, the arguments must be given within pairs of braces, so for the command to work you would need to use hinge001. To preserve the usage syntax and keep the command TikZ-like, you can use xparse:



                    documentclass[border=2mm]standalone %<--- standalone
                    usepackagetikz
                    usepackagexparse

                    NewDocumentCommandhinge
                    > SplitArgument2, r() % #1 = name , #2 = rotation angle
                    %
                    hingeMAIN#1%

                    newcommandhingeMAIN[3]%
                    draw [black, line width = 1mm]
                    (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;


                    begindocument

                    begintikzpicture
                    hinge(0,0,1);
                    draw[red] (0,0) circle (0.1);
                    endtikzpicture
                    enddocument


                    The argument specification > SplitArgument2, r() says that there is a required argument delimited by () which will be pre-processed by the SplitArgument, which will split the input argument in 2+1 items separated by a ,. This command will transform (0,1,2) into 012, which is then passed to hingeMAIN, which expects braced arguments and does the drawing. The ; after the hinge command is not necessary.




                    enter image description here







                    share|improve this answer




















                    • Thank you for your fast answer!!! I feel stupid because of the synthax of my command... anyway, is it the right way to make a macro-shape to reuse in tikz or is there an elegant way?
                      – Willi
                      36 mins ago










                    • I think xparse is the best way to easily create custom-delimited commands. The other way to to that would be defhinge(#1,#2,#3)<stuff here>, but this would be much more fragile than the xparse version, so I think it's as elegant as it can get :)
                      – Phelype Oleinik
                      33 mins ago










                    • My concern was why I could not perform #1+#2 in the drawing but you solved my problem with replacing hinge(1,2,3) by inge123
                      – Willi
                      24 mins ago














                    up vote
                    1
                    down vote













                    When you create a command with newcommand, the arguments must be given within pairs of braces, so for the command to work you would need to use hinge001. To preserve the usage syntax and keep the command TikZ-like, you can use xparse:



                    documentclass[border=2mm]standalone %<--- standalone
                    usepackagetikz
                    usepackagexparse

                    NewDocumentCommandhinge
                    > SplitArgument2, r() % #1 = name , #2 = rotation angle
                    %
                    hingeMAIN#1%

                    newcommandhingeMAIN[3]%
                    draw [black, line width = 1mm]
                    (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;


                    begindocument

                    begintikzpicture
                    hinge(0,0,1);
                    draw[red] (0,0) circle (0.1);
                    endtikzpicture
                    enddocument


                    The argument specification > SplitArgument2, r() says that there is a required argument delimited by () which will be pre-processed by the SplitArgument, which will split the input argument in 2+1 items separated by a ,. This command will transform (0,1,2) into 012, which is then passed to hingeMAIN, which expects braced arguments and does the drawing. The ; after the hinge command is not necessary.




                    enter image description here







                    share|improve this answer




















                    • Thank you for your fast answer!!! I feel stupid because of the synthax of my command... anyway, is it the right way to make a macro-shape to reuse in tikz or is there an elegant way?
                      – Willi
                      36 mins ago










                    • I think xparse is the best way to easily create custom-delimited commands. The other way to to that would be defhinge(#1,#2,#3)<stuff here>, but this would be much more fragile than the xparse version, so I think it's as elegant as it can get :)
                      – Phelype Oleinik
                      33 mins ago










                    • My concern was why I could not perform #1+#2 in the drawing but you solved my problem with replacing hinge(1,2,3) by inge123
                      – Willi
                      24 mins ago












                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    When you create a command with newcommand, the arguments must be given within pairs of braces, so for the command to work you would need to use hinge001. To preserve the usage syntax and keep the command TikZ-like, you can use xparse:



                    documentclass[border=2mm]standalone %<--- standalone
                    usepackagetikz
                    usepackagexparse

                    NewDocumentCommandhinge
                    > SplitArgument2, r() % #1 = name , #2 = rotation angle
                    %
                    hingeMAIN#1%

                    newcommandhingeMAIN[3]%
                    draw [black, line width = 1mm]
                    (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;


                    begindocument

                    begintikzpicture
                    hinge(0,0,1);
                    draw[red] (0,0) circle (0.1);
                    endtikzpicture
                    enddocument


                    The argument specification > SplitArgument2, r() says that there is a required argument delimited by () which will be pre-processed by the SplitArgument, which will split the input argument in 2+1 items separated by a ,. This command will transform (0,1,2) into 012, which is then passed to hingeMAIN, which expects braced arguments and does the drawing. The ; after the hinge command is not necessary.




                    enter image description here







                    share|improve this answer












                    When you create a command with newcommand, the arguments must be given within pairs of braces, so for the command to work you would need to use hinge001. To preserve the usage syntax and keep the command TikZ-like, you can use xparse:



                    documentclass[border=2mm]standalone %<--- standalone
                    usepackagetikz
                    usepackagexparse

                    NewDocumentCommandhinge
                    > SplitArgument2, r() % #1 = name , #2 = rotation angle
                    %
                    hingeMAIN#1%

                    newcommandhingeMAIN[3]%
                    draw [black, line width = 1mm]
                    (#1-#3,#2+#3)--++(1,0)--++(0:0) arc(0:180:-1)--++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)--++(-1,0)--cycle;


                    begindocument

                    begintikzpicture
                    hinge(0,0,1);
                    draw[red] (0,0) circle (0.1);
                    endtikzpicture
                    enddocument


                    The argument specification > SplitArgument2, r() says that there is a required argument delimited by () which will be pre-processed by the SplitArgument, which will split the input argument in 2+1 items separated by a ,. This command will transform (0,1,2) into 012, which is then passed to hingeMAIN, which expects braced arguments and does the drawing. The ; after the hinge command is not necessary.




                    enter image description here








                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 41 mins ago









                    Phelype Oleinik

                    18.8k54173




                    18.8k54173











                    • Thank you for your fast answer!!! I feel stupid because of the synthax of my command... anyway, is it the right way to make a macro-shape to reuse in tikz or is there an elegant way?
                      – Willi
                      36 mins ago










                    • I think xparse is the best way to easily create custom-delimited commands. The other way to to that would be defhinge(#1,#2,#3)<stuff here>, but this would be much more fragile than the xparse version, so I think it's as elegant as it can get :)
                      – Phelype Oleinik
                      33 mins ago










                    • My concern was why I could not perform #1+#2 in the drawing but you solved my problem with replacing hinge(1,2,3) by inge123
                      – Willi
                      24 mins ago
















                    • Thank you for your fast answer!!! I feel stupid because of the synthax of my command... anyway, is it the right way to make a macro-shape to reuse in tikz or is there an elegant way?
                      – Willi
                      36 mins ago










                    • I think xparse is the best way to easily create custom-delimited commands. The other way to to that would be defhinge(#1,#2,#3)<stuff here>, but this would be much more fragile than the xparse version, so I think it's as elegant as it can get :)
                      – Phelype Oleinik
                      33 mins ago










                    • My concern was why I could not perform #1+#2 in the drawing but you solved my problem with replacing hinge(1,2,3) by inge123
                      – Willi
                      24 mins ago















                    Thank you for your fast answer!!! I feel stupid because of the synthax of my command... anyway, is it the right way to make a macro-shape to reuse in tikz or is there an elegant way?
                    – Willi
                    36 mins ago




                    Thank you for your fast answer!!! I feel stupid because of the synthax of my command... anyway, is it the right way to make a macro-shape to reuse in tikz or is there an elegant way?
                    – Willi
                    36 mins ago












                    I think xparse is the best way to easily create custom-delimited commands. The other way to to that would be defhinge(#1,#2,#3)<stuff here>, but this would be much more fragile than the xparse version, so I think it's as elegant as it can get :)
                    – Phelype Oleinik
                    33 mins ago




                    I think xparse is the best way to easily create custom-delimited commands. The other way to to that would be defhinge(#1,#2,#3)<stuff here>, but this would be much more fragile than the xparse version, so I think it's as elegant as it can get :)
                    – Phelype Oleinik
                    33 mins ago












                    My concern was why I could not perform #1+#2 in the drawing but you solved my problem with replacing hinge(1,2,3) by inge123
                    – Willi
                    24 mins ago




                    My concern was why I could not perform #1+#2 in the drawing but you solved my problem with replacing hinge(1,2,3) by inge123
                    – Willi
                    24 mins ago










                    up vote
                    0
                    down vote













                    I had the same idea as @marmot: you should use a pic for this -- but I really like Ignasi's approach too!



                    Most of the solutions above do not seem to handle your requirement about node coordinates and the shapes are not styled further. It is easy to do both using pics, which you can read more about in section 18.2 of the tikz manual (version 3.0.1a).



                    Specifically, you can create something like:



                    enter image description here



                    using the code:



                    documentclass[border=2mm]standalone %<--- standalone
                    usepackagetikz

                    tikzset
                    pics/hinge/.style args = #1,#2% name, style
                    code =
                    draw[black, line width=1mm, #2]
                    (0,0)--++(1,0)--++(0:0) arc(0:180:-1) node[pos=0.5](#1)
                    --++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)
                    --++(-1,0)--cycle;




                    begindocument

                    begintikzpicture
                    draw (4,4) pichinge=first,blue;
                    pic at (0,0) hinge=second,red,rotate=30;
                    draw (0,4) pichinge=third,scale=0.4;
                    draw[thick,->](first)--(second);
                    endtikzpicture

                    enddocument


                    As I have shown, you can place a pic using either a draw command or a pic command. I have define a pic called hinge that takes two mandatory arguments:



                    • the node name, which is in the center of the pic

                    • and any styling

                    Both arguments to hinge are mandatory but you can omit one of them by using a comma:



                    draw (4,0) pichinge=fourth,;


                    is perfectly valid -- but be careful because you will get a nasty error message if you try:



                    draw (4,0) pichinge=fourth;


                    Finally, as pics are placed relative to the point that they are placed (using draw or pic), I dropped your (#1-#3,#2+#3). If this is really needed it is easy enough to add back in as pics can take many arguments, although at some point it becomes easier to use keys.






                    share|improve this answer






















                    • Thank you very much all for your fast answer. Pic seems to be dedicated to what I try to do!
                      – Willi
                      2 mins ago














                    up vote
                    0
                    down vote













                    I had the same idea as @marmot: you should use a pic for this -- but I really like Ignasi's approach too!



                    Most of the solutions above do not seem to handle your requirement about node coordinates and the shapes are not styled further. It is easy to do both using pics, which you can read more about in section 18.2 of the tikz manual (version 3.0.1a).



                    Specifically, you can create something like:



                    enter image description here



                    using the code:



                    documentclass[border=2mm]standalone %<--- standalone
                    usepackagetikz

                    tikzset
                    pics/hinge/.style args = #1,#2% name, style
                    code =
                    draw[black, line width=1mm, #2]
                    (0,0)--++(1,0)--++(0:0) arc(0:180:-1) node[pos=0.5](#1)
                    --++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)
                    --++(-1,0)--cycle;




                    begindocument

                    begintikzpicture
                    draw (4,4) pichinge=first,blue;
                    pic at (0,0) hinge=second,red,rotate=30;
                    draw (0,4) pichinge=third,scale=0.4;
                    draw[thick,->](first)--(second);
                    endtikzpicture

                    enddocument


                    As I have shown, you can place a pic using either a draw command or a pic command. I have define a pic called hinge that takes two mandatory arguments:



                    • the node name, which is in the center of the pic

                    • and any styling

                    Both arguments to hinge are mandatory but you can omit one of them by using a comma:



                    draw (4,0) pichinge=fourth,;


                    is perfectly valid -- but be careful because you will get a nasty error message if you try:



                    draw (4,0) pichinge=fourth;


                    Finally, as pics are placed relative to the point that they are placed (using draw or pic), I dropped your (#1-#3,#2+#3). If this is really needed it is easy enough to add back in as pics can take many arguments, although at some point it becomes easier to use keys.






                    share|improve this answer






















                    • Thank you very much all for your fast answer. Pic seems to be dedicated to what I try to do!
                      – Willi
                      2 mins ago












                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    I had the same idea as @marmot: you should use a pic for this -- but I really like Ignasi's approach too!



                    Most of the solutions above do not seem to handle your requirement about node coordinates and the shapes are not styled further. It is easy to do both using pics, which you can read more about in section 18.2 of the tikz manual (version 3.0.1a).



                    Specifically, you can create something like:



                    enter image description here



                    using the code:



                    documentclass[border=2mm]standalone %<--- standalone
                    usepackagetikz

                    tikzset
                    pics/hinge/.style args = #1,#2% name, style
                    code =
                    draw[black, line width=1mm, #2]
                    (0,0)--++(1,0)--++(0:0) arc(0:180:-1) node[pos=0.5](#1)
                    --++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)
                    --++(-1,0)--cycle;




                    begindocument

                    begintikzpicture
                    draw (4,4) pichinge=first,blue;
                    pic at (0,0) hinge=second,red,rotate=30;
                    draw (0,4) pichinge=third,scale=0.4;
                    draw[thick,->](first)--(second);
                    endtikzpicture

                    enddocument


                    As I have shown, you can place a pic using either a draw command or a pic command. I have define a pic called hinge that takes two mandatory arguments:



                    • the node name, which is in the center of the pic

                    • and any styling

                    Both arguments to hinge are mandatory but you can omit one of them by using a comma:



                    draw (4,0) pichinge=fourth,;


                    is perfectly valid -- but be careful because you will get a nasty error message if you try:



                    draw (4,0) pichinge=fourth;


                    Finally, as pics are placed relative to the point that they are placed (using draw or pic), I dropped your (#1-#3,#2+#3). If this is really needed it is easy enough to add back in as pics can take many arguments, although at some point it becomes easier to use keys.






                    share|improve this answer














                    I had the same idea as @marmot: you should use a pic for this -- but I really like Ignasi's approach too!



                    Most of the solutions above do not seem to handle your requirement about node coordinates and the shapes are not styled further. It is easy to do both using pics, which you can read more about in section 18.2 of the tikz manual (version 3.0.1a).



                    Specifically, you can create something like:



                    enter image description here



                    using the code:



                    documentclass[border=2mm]standalone %<--- standalone
                    usepackagetikz

                    tikzset
                    pics/hinge/.style args = #1,#2% name, style
                    code =
                    draw[black, line width=1mm, #2]
                    (0,0)--++(1,0)--++(0:0) arc(0:180:-1) node[pos=0.5](#1)
                    --++(1,0)--++(0,-2.1)--++(-1,0)--++(0:0) arc(0:180:1)
                    --++(-1,0)--cycle;




                    begindocument

                    begintikzpicture
                    draw (4,4) pichinge=first,blue;
                    pic at (0,0) hinge=second,red,rotate=30;
                    draw (0,4) pichinge=third,scale=0.4;
                    draw[thick,->](first)--(second);
                    endtikzpicture

                    enddocument


                    As I have shown, you can place a pic using either a draw command or a pic command. I have define a pic called hinge that takes two mandatory arguments:



                    • the node name, which is in the center of the pic

                    • and any styling

                    Both arguments to hinge are mandatory but you can omit one of them by using a comma:



                    draw (4,0) pichinge=fourth,;


                    is perfectly valid -- but be careful because you will get a nasty error message if you try:



                    draw (4,0) pichinge=fourth;


                    Finally, as pics are placed relative to the point that they are placed (using draw or pic), I dropped your (#1-#3,#2+#3). If this is really needed it is easy enough to add back in as pics can take many arguments, although at some point it becomes easier to use keys.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 5 mins ago

























                    answered 10 mins ago









                    Andrew

                    28.1k34075




                    28.1k34075











                    • Thank you very much all for your fast answer. Pic seems to be dedicated to what I try to do!
                      – Willi
                      2 mins ago
















                    • Thank you very much all for your fast answer. Pic seems to be dedicated to what I try to do!
                      – Willi
                      2 mins ago















                    Thank you very much all for your fast answer. Pic seems to be dedicated to what I try to do!
                    – Willi
                    2 mins ago




                    Thank you very much all for your fast answer. Pic seems to be dedicated to what I try to do!
                    – Willi
                    2 mins ago

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f455807%2fdraw-a-shape-with-operation-on-parameters-in-tikz%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    One-line joke