3

I wanted to make a shape called "dynamometer" like rectangle, circle or so on with some anchors and options. But I can't figure out how to achieve such goal.

I've taken the dynamometer shape from this topic and to make it a shape I've some questions.

  1. How to declare a shape for TikZ? Such that I'm allowed to write \node[dynamo, rectangle, F={65~N}] at (5,0).
  2. In a declared shape, how to create nodes?
  3. In a declared shape, how to define options as "rectangle" or "F={65~N}"?

Here is "my" MWE :

\documentclass{standalone}

\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        %%
        %%  Key wanted :    - Newton value to label the red marker.
        %%                      - Option to use the rectangle or circle boundaries shape.
        %%                      - Node wanted :     - red marker node
        %%                                              - bottom of the hook
        %%                                              - Upper part of the hook
        %%                                              - center of the hook
        %%                                              - upper part of the ring
        %%                                              - 
        
        %% Ring
        \draw[line width=6pt] (2,24) circle (1.5);
        \draw[line width=3pt] (2,22.5) -- ++(0,-3);
        
        %% Hook
        \draw[line width=6pt, gray, rounded corners] (2,9) -- ++(0,-1.5) arc (90:360:1.7);
        
        %% Shape of the dynamometer
%       \draw[line width=3pt, fill=lightgray] (4,20) arc (0:180:2) -- ++(0,-10) arc (180:360:2) -- cycle;   %% bords en cercle
        \draw[line width=3pt, fill=lightgray, rounded corners] (3.5,21) rectangle ++(-3,-12);                   %% bord rectangle arrondi
        
        %% Scale
        % Vertical line
        \draw[line width=3pt] (2,20) -- ++(0,-10); 
        
        % Graduation with red marker
        \foreach \i in {0,...,20}{
            \ifnum\i=5
            \draw[line width=5pt, red] 
            \else
            \draw[line width=0.5pt] 
            \fi
            (1.5,{10+0.5*\i}) -- ++(1,0);
        }
    \end{tikzpicture}
\end{document}
5
  • 2
    Circuitikz makes it relatively easy to create shapes, mostly because the nodes do not get larger when you add text. I have some tutorials on the subject on elfsoft2000.com/projects/index.htm Mar 25 at 15:52
  • 1
    Does this answer your question? Dynamometer - ask for improvement suggestions
    – Sebastiano
    Mar 25 at 19:48
  • 2
    @Sebastiano I understand that the OP wants to create a node shape or something similar in order to reproduce this drawing several times. Hence, in my view, this is not a duplicate of the linked question, but rather it is a follow-up question. Mar 26 at 8:02
  • @JasperHabicht I have retract the closure vote.
    – Sebastiano
    Mar 26 at 10:46
  • 2
    @sebastiano I wanted to learn more about creating shapes to reproduce several times the figure. Initially, I used a \newcommand. A bit ugly. And my learning goal was over the dynamometer.
    – Piroooh
    Mar 26 at 20:09

2 Answers 2

4

You could create a node shape (you might want to look into the PGF/TikZ manual to learn about how to do this in general) if you want to reuse this drawing several times (what I assume is your aim actually), but I am not sure whether a shape is flexible enough to meet your requirements. In my opinion, it is better to create a pic from it, since it would allow you to change the marker position easily:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\tikzset{
    pics/dynamometer/.style={
        code={
            \tikzset{dynamometer/.cd, #1}
            
            %% Ring
            \draw[line width=6pt] (2,24) circle (1.5);
            \draw[line width=3pt] (2,22.5) -- ++(0,-3);
            \coordinate (-ring-top) at (2,25.5);
        
            %% Hook
            \draw[line width=6pt, gray, rounded corners] (2,9) -- ++(0,-1.5) arc (90:360:1.7);
            \coordinate (-hook-top) at (2,7.5);
            \coordinate (-hook-center) at (2,5.8);
            \coordinate (-hook-bottom) at (2,4.1);
        
            %% Shape of the dynamometer
            \draw[line width=3pt, fill=lightgray, rounded corners] (3.5,21) rectangle ++(-3,-12); 
            
            %% Scale
            % Vertical line
            \draw[line width=3pt] (2,20) -- ++(0,-10); 
            
            % Graduation with red marker
            \foreach \i in {0,...,20} {
                \draw[line width=0.5pt] (1.5,{10+0.5*\i}) -- ++(1,0);
            }
            \coordinate (-marker) at (2,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \draw[line width=5pt, red] (1.5,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}}) -- ++(1,0);
        }
    },
    dynamometer/marker position/.initial={5}
}

\begin{document}
    \begin{tikzpicture}
        %%
        %%  Key wanted :    - Newton value to label the red marker.
        %%                  - Option to use the rectangle or circle boundaries shape.
        %%                  - Node wanted :     - red marker node
        %%                                      - bottom of the hook
        %%                                      - Upper part of the hook
        %%                                      - center of the hook
        %%                                      - upper part of the ring
        %%                                      
        
        \pic (mymeter) at (0,0) {dynamometer};
        
        \node[circle, inner sep=3pt, fill=yellow] at (mymeter-ring-top) {};
        \node[circle, inner sep=3pt, fill=green] at (mymeter-marker) {};
        \node[circle, inner sep=3pt, fill=cyan] at (mymeter-hook-top) {};
        \node[circle, inner sep=3pt, fill=blue] at (mymeter-hook-center) {};
        \node[circle, inner sep=3pt, fill=magenta] at (mymeter-hook-bottom) {};

        \pic at (5,0) {dynamometer={marker position=15}};
    \end{tikzpicture}
\end{document}

enter image description here

I don't know what unit the scale displays, so I opted for a neutral marker position as option to the pic.


If you want to have a highly stylable variant, you could maybe use this (which also extends the hook according to the marker position):

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\tikzset{
    pics/dynamometer/.style={
        code={
            \tikzset{dynamometer/.cd, #1}
            
            %% Ring
            \draw[dynamometer/ring] (2,24) circle[radius=1.5];
            \draw[dynamometer/ring connection] (2,22.5) -- ++(0,-3);
            \coordinate (-ring-top) at (2,25.5);
        
            %% Hook
            \draw[dynamometer/hook] (2,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}}) -- ++(0,-11.5) arc[start angle=90, end angle=360, radius=1.7];
            \coordinate (-hook-top) at (2,{-1.5+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \coordinate (-hook-center) at (2,{-3.2+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \coordinate (-hook-bottom) at (2,{-4.9+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
        
            %% Shape of the dynamometer
            \draw[dynamometer/meter] (3.5,21) rectangle ++(-3,-12); 
            
            %% Scale
            % Vertical line
            \draw[dynamometer/scale] (2,20) -- ++(0,-10); 
            
            % Graduation with red marker
            \foreach \i in {0,...,20} {
                \draw[dynamometer/tick] (1.5,{10+0.5*\i}) -- ++(1,0);
            }
            \coordinate (-marker) at (2,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \draw[dynamometer/marker] (1.5,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}}) -- ++(1,0);
        }
    },
    dynamometer/marker position/.initial={5},
    dynamometer/ring/.style={line width=6pt},
    dynamometer/ring connection/.style={line width=3pt},
    dynamometer/hook/.style={line width=6pt, gray, rounded corners},
    dynamometer/meter/.style={line width=3pt, fill=lightgray, rounded corners},
    dynamometer/scale/.style={line width=3pt},
    dynamometer/tick/.style={line width=0.5pt},
    dynamometer/marker/.style={line width=5pt, red},
}

\begin{document}
    \begin{tikzpicture}
        %%
        %%  Key wanted :    - Newton value to label the red marker.
        %%                  - Option to use the rectangle or circle boundaries shape.
        %%                  - Node wanted :     - red marker node
        %%                                      - bottom of the hook
        %%                                      - Upper part of the hook
        %%                                      - center of the hook
        %%                                      - upper part of the ring
        %%                                      
        
        \pic (mymeter) at (0,0) {dynamometer};
        
        \node[circle, inner sep=3pt, fill=yellow] at (mymeter-ring-top) {};
        \node[circle, inner sep=3pt, fill=green] at (mymeter-marker) {};
        \node[circle, inner sep=3pt, fill=cyan] at (mymeter-hook-top) {};
        \node[circle, inner sep=3pt, fill=blue] at (mymeter-hook-center) {};
        \node[circle, inner sep=3pt, fill=magenta] at (mymeter-hook-bottom) {};

        \pic at (5,0) {dynamometer={marker position=20, meter/.append style={sharp corners, fill=white}, hook/.append style={draw=blue}}};
    \end{tikzpicture}
\end{document}

enter image description here


Added a few options for a label:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{siunitx}

\tikzset{
    pics/dynamometer/.style={
        code={
            \tikzset{dynamometer/.cd, #1}
            
            %% Ring
            \draw[dynamometer/ring] (2,24) circle[radius=1.5];
            \draw[dynamometer/ring connection] (2,22.5) -- ++(0,-3);
            \coordinate (-ring-top) at (2,25.5);
        
            %% Hook
            \draw[dynamometer/hook] (2,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}}) -- ++(0,-11.5) arc[start angle=90, end angle=360, radius=1.7];
            \coordinate (-hook-top) at (2,{-1.5+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \coordinate (-hook-center) at (2,{-3.2+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \coordinate (-hook-bottom) at (2,{-4.9+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
        
            %% Shape of the dynamometer
            \draw[dynamometer/meter] (3.5,21) rectangle ++(-3,-12); 
            
            %% Scale
            % Vertical line
            \draw[dynamometer/scale] (2,20) -- ++(0,-10); 
            
            % Graduation with red marker
            \foreach \i in {0,...,20} {
                \draw[dynamometer/tick] (1.5,{10+0.5*\i}) -- ++(1,0);
            }
            \coordinate (-marker) at (2,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \draw[dynamometer/marker] (1.5,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}}) -- ++(1,0);
            \node[dynamometer/marker label] at (-marker) (-marker-label) {\pgfkeysvalueof{/tikz/dynamometer/marker label content}};
        }
    },
    dynamometer/marker position/.initial={5},
    dynamometer/ring/.style={line width=6pt},
    dynamometer/ring connection/.style={line width=3pt},
    dynamometer/hook/.style={line width=6pt, gray, rounded corners},
    dynamometer/meter/.style={line width=3pt, fill=lightgray, rounded corners},
    dynamometer/scale/.style={line width=3pt},
    dynamometer/tick/.style={line width=0.5pt},
    dynamometer/marker/.style={line width=5pt, red},
    dynamometer/marker label/.style={right=0.5cm},
    dynamometer/marker label content/.initial={},
}

\begin{document}
    \begin{tikzpicture}
        %%
        %%  Key wanted :    - Newton value to label the red marker.
        %%                  - Option to use the rectangle or circle boundaries shape.
        %%                  - Node wanted :     - red marker node
        %%                                      - bottom of the hook
        %%                                      - Upper part of the hook
        %%                                      - center of the hook
        %%                                      - upper part of the ring
        %%                                      
        
        \pic (mymeter) at (0,0) {dynamometer={marker label content={\Huge\qty{65}{\newton}}, marker label/.append style={circle, fill=yellow}}};
    \end{tikzpicture}
\end{document}

enter image description here

3
  • Thank you for your quick and complete answer ! I'll explore it to make sure I understand each step of construction. You made more than what I expected. May I come back with some questions about the code later ? First, why \pic instead of shape in \node[dynamo] ? Second, how to add an option to write the value 65N ? I've tried myself with ` \coordinate[label=right:\pgfkeysvalueof{dynamometer/marker label}] (-mark) at (2,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});` but the label doesn't appear with marker label={\color{black}\HUGE $65~N$}} although borders increase.
    – Piroooh
    Mar 25 at 17:01
  • 1
    @Piroooh As for your question why I used a pic: A pic is much more flexible than a node, especially considering the fact that you might want to flexibly shift the marker (and the hook). Also, this way, you can easily style different parts of the drawing. For such use cases, I would argue that a pic is just the right thing. As for the label, of course you could easily add \node[right of=mymeter-mark] {65 N}; after having drawn the pic (with the name mymeter) ... However, I edited my answer to include a (stylable) label node. Mar 25 at 19:18
  • Such a bit late but after many times using it I can say... It works great !! I couldn't figure out the difference between pic and node and you helped me with it too. Thanks for all :) !
    – Piroooh
    Apr 16 at 13:59
5

Like this:

enter image description here

Code:

\documentclass[border=10pt]{standalone}
\usepackage{tikz,siunitx,calc}
\usetikzlibrary{decorations.pathmorphing,patterns}
\sisetup{round-mode = places, round-precision = 3}
\begin{document}
    
    \begin{tikzpicture}[scale=.65]
        \draw[gray,line width=12pt] (0,0) arc (180:0:2);
        \filldraw[brown] (-1,0) rectangle (5,-22);
            \node[white] at (2.1,-1.5) (a) {\bfseries \large Range: 0-1.800 kg};
            \node[white] at (2.1,-2.3) (a) {\bfseries 8 mm $\rightarrow$ 100 g};
            \draw[decoration={aspect=0.2, segment length=1mm, amplitude=2mm,coil},decorate] (2.08,-3) -- (2.05,-4);
            \draw[gray,line width=10pt] (2.1,-4)--(2.1,-21);
            \draw[cyan,line width=20pt,opacity=.4] (2.1,-3)--(2.1,-21);
            \foreach \i in {0,1,...,18}{
                \pgfmathsetmacro\h{-4-.8*\i};
                \pgfmathsetmacro\j{-4.5-.8*\i};
                %\pgfmathsetmacro{\j}{int(90-\i)}
                \pgfmathsetmacro{\k}{.1*\i};
                \draw[white,line width=3pt] (1.6,\h)--(1.2,\h) node[left] (\i) {\bfseries \large $\i$};
                \draw[white,line width=3pt] (2.6,\h)--(3.0,\h) node[right] (\i) {\bfseries \large \num{\k}};
                }
        \draw[red,line width=3,-latex] (1.5,-4)--(2.7,-4);
        \draw[gray,line width=10pt,rounded corners] (2.1,-22)--(2.1,-23) arc (90:350:2);
    \end{tikzpicture}
        
\end{document}
1
  • The shape of the first dynamometer is less important than the question "how to make it a shape ?". I wanted to learn about creating new commands for TikZ/PGF.
    – Piroooh
    Mar 26 at 20:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .