2

I have the following tikz code which I based on https://tex.stackexchange.com/a/13952/15360. Now I want to have the spring, the damper and the 3 arrows have labels. Like for example, F for force and k as spring constant, and d as damper constant.

Do I have to use fixed coordinates? \node[draw] at (0,0) {$F$};?

Or can I do this more clever? Because at the moment the spring is not a node so I have no coordinates of it...

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,patterns,decorations.pathmorphing,decorations.markings}

\begin{document}
\begin{tikzpicture}[every node/.style={draw,outer sep=0pt,thick}]

\tikzstyle{spring}=[thick,decorate,decoration={zigzag,pre length=0.3cm,post
length=0.3cm,segment length=6}]

\tikzstyle{damper}=[thick,decoration={markings,  
  mark connection node=dmp,
  mark=at position 0.5 with 
  {
    \node (dmp) [thick,inner sep=0pt,transform shape,rotate=-90,minimum
width=15pt,minimum height=3pt,draw=none] {};
    \draw [thick] ($(dmp.north east)+(2pt,0)$) -- (dmp.south east) -- (dmp.south
west) -- ($(dmp.north west)+(2pt,0)$);
    \draw [thick] ($(dmp.north)+(0,-5pt)$) -- ($(dmp.north)+(0,5pt)$);
  }
}, decorate]

\tikzstyle{ground}=[fill,pattern=north east lines,draw=none,minimum
width=0.75cm,minimum height=0.3cm]

\node (M1) [minimum width=1.5cm, minimum height=1.5cm] {$m_1$};
\node (M2) at (3,0) [minimum width=1.5cm, minimum height=1.5cm] {$m_2$};
\draw[spring] ($(M1.east) - (0,0.5)$) -- ($(M2.west) - (0,0.5)$);
\draw[damper] ($(M1.east) + (0,0.5)$) -- ($(M2.west) + (0,0.5)$);
\draw[thick, dashed] ($(M1.north west)$) -- ($(M1.north west) + (0,1)$);
\draw[thick, dashed] ($(M2.north west)$) -- ($(M2.north west) + (0,1)$);
\draw[ultra thick, -latex] ($(M2.north west) + (0,0.75)$) -- 
                           ($(M2.north west) + (1,0.75)$);
\draw[ultra thick, -latex] ($(M1.north west) + (0,0.75)$) -- 
                           ($(M1.north west) + (1,0.75)$);
\draw[ultra thick, -latex] ($(M1.west) - (1,0)$) -- ($(M1.west)$); 

\end{tikzpicture}

\end{document}

1 Answer 1

5

If I understand your question correctly this is fairly easy to do. I have included two examples below by modifying your code, by labeling your spring and one arrow. E.g. after

\draw[spring] ($(M1.east) - (0,0.5)$) -- ($(M2.west) - (0,0.5)$) 

You can add

node [midway,above] {$k$};

which places a node above and between

($(M1.east) - (0,0.5)$) and ($(M2.west) - (0,0.5)$)

The following code

 \documentclass{standalone}
 \usepackage{tikz}
 \usetikzlibrary{calc,patterns,
                 decorations.pathmorphing,
                 decorations.markings}

 \begin{document}
 \begin{tikzpicture}

 \tikzstyle{spring}=[thick,decorate,decoration={zigzag,pre length=0.3cm,post
 length=0.3cm,segment length=6}]

 \tikzstyle{damper}=[thick,decoration={markings,  
   mark connection node=dmp,
   mark=at position 0.5 with 
   {
     \node (dmp) [thick,inner sep=0pt,transform shape,rotate=-90,minimum
 width=15pt,minimum height=3pt,draw=none] {};
     \draw [thick] ($(dmp.north east)+(2pt,0)$) -- (dmp.south east) -- (dmp.south
 west) -- ($(dmp.north west)+(2pt,0)$);
     \draw [thick] ($(dmp.north)+(0,-5pt)$) -- ($(dmp.north)+(0,5pt)$);
   }
 }, decorate]

 \tikzstyle{ground}=[fill,pattern=north east lines,draw=none,minimum
 width=0.75cm,minimum height=0.3cm]

 \node[draw,outer sep=0pt,thick] (M1) [minimum width=1.5cm, minimum height=1.5cm] {$m_1$};
 \node[draw,outer sep=0pt,thick] (M2) at (3,0) [minimum width=1.5cm, minimum height=1.5cm] {$m_2$};
 \draw[spring] ($(M1.east) - (0,0.5)$) -- ($(M2.west) - (0,0.5)$) 
 node [midway,above] {$k$};
 \draw[damper] ($(M1.east) + (0,0.5)$) -- ($(M2.west) + (0,0.5)$);
 \draw[thick, dashed] ($(M1.north west)$) -- ($(M1.north west) + (0,1)$);
 \draw[thick, dashed] ($(M2.north west)$) -- ($(M2.north west) + (0,1)$);
 \draw[ultra thick, -latex] ($(M2.north west) + (0,0.75)$) -- 
                            ($(M2.north west) + (1,0.75)$)
                            node [midway, below] {$F_x$};
 \draw[ultra thick, -latex] ($(M1.north west) + (0,0.75)$) -- 
                            ($(M1.north west) + (1,0.75)$);
 \draw[ultra thick, -latex] ($(M1.west) - (1,0)$) -- ($(M1.west)$); 

 \end{tikzpicture}
 \end{document}

gives enter image description here .

Note that I moved the styling of your boxed nodes from the tikzpictureenvironment to the nodes itself to avoid having boxes surrounding the labels.

Also, if you add the positioning library:

\usetikzlibrary{positioning}

You can position new nodes relative to your existing named nodes:

\node (label1) [below=of M1] {A label};

For more information regarding placement of nodes you could take a look at chapter 16.5 in the PGF manual.

1
  • Thank you very much. When I have more time, I think I will try to create a package for this. With these basics shapes.
    – WG-
    Nov 20, 2012 at 18:35

You must log in to answer this question.

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