2

Trying to draw a mechanical system of two coupled pendulums, I used the code:

\documentclass[11pt]{article}

\usepackage{tikz,pgfplots}

\usetikzlibrary{intersections,calc,patterns,angles,quotes,}

\pgfplotsset{compat=1.11} \usetikzlibrary{arrows.meta} \usetikzlibrary{ decorations.markings, decorations.pathmorphing, decorations.pathreplacing, calc, patterns, positioning } \tikzset{ spring/.style={thick,decorate,decoration={zigzag,pre length=0.3cm,post length=0.3cm,segment length=6}}, blank/.style={draw=none,fill=none,pos=0.5}, ground/.style={fill,pattern=north east lines,draw=none,minimum width=0.5cm,minimum height=0.3cm}, damper/.style={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=10pt,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,-3pt)$) -- ($(dmp.north)+(0,3pt)$); } }, decorate }, box/.style={draw,thick,minimum width=1cm, minimum height=1cm} }

\begin{document}

\begin{figure}[htbp] \centering \begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1}, extended line/.style={shorten >=-#1,shorten <=-#1}, extended line/.default=0cm]

\node (wall) [ground, minimum width=4cm] {}; \draw (wall.south east) -- (wall.south west);

\pgfmathsetmacro{\myAngle}{30}

\coordinate (pivot1) at (-1,-0.16);
\draw[thick,-] (pivot1) -- ++ (0,-4) 
node (bob1) [draw,fill=gray!40,circle,minimum size=6pt,inner sep=6pt]{$$}
node (resort1) [midway,left]{$$};

\coordinate (pivot2) at (1,-0.16);
\draw[thick,-] (pivot2) -- ++ (0,-4) 
node (bob2) [draw,fill=gray!40,circle,minimum size=6pt,inner sep=6pt]{$$}
node (resort2) [midway,right]{$$};

\draw[spring] (resort1) -- (resort2) node[blank,above,yshift=0mm] {$ $};

\draw[decorate,decoration={brace,amplitude=.3cm}] (pivot2.west) -- (resort2.west) node[blank,right] {$l$};
 \draw[decorate,decoration={brace,mirror,amplitude=.7cm}] (pivot1.east) to ($(bob1)+(0.02,0.475)$) node[blank,left,xshift=-1.65cm,yshift=-1.9cm] {$L$};

\end{tikzpicture} \end{figure}

\begin{figure}[htbp] \centering \begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1}, extended line/.style={shorten >=-#1,shorten <=-#1}, extended line/.default=0cm]

\node (wall) [ground, minimum width=4cm] {}; \draw (wall.south east) -- (wall.south west);

\pgfmathsetmacro{\myAngle}{30}

\coordinate (pivot1) at (-1,-0.16);
\draw[thick,densely dotted,-] (pivot1) -- ++ (0,-1) node (vertical1) [black,below]{$ $};
\draw[thick] (pivot1) -- ++ (270+\myAngle:4cm)
node (bob1) [draw,fill=gray!40,circle,minimum size=6pt,inner sep=6pt]{$ $}
node (spring1) [midway,left]{$$};
\pic [draw, -,&quot;$\theta_1$&quot;, angle eccentricity=1.5] {angle = vertical1--pivot1--bob1};

\coordinate (pivot2) at (1,-0.16);
\draw[thick,densely dotted,-] (pivot2) -- ++ (0,-1) node (vertical2) [black,below]{$ $};
\draw[thick] (pivot2) -- ++(270+\myAngle:4cm)
node (bob2) [draw,fill=gray!40,circle,minimum size=6pt,inner sep=6pt]{$ $}
node (spring2) [midway,right]{$$};
\pic [draw, -,font=\small, &quot;$\theta_2$&quot;, angle eccentricity=1.5] {angle = vertical2--pivot2--bob2};

\draw[spring] (spring1) -- (spring2) node[blank,above,xshift=-4mm,yshift=0mm] {$ $};

\draw [thick,densely dotted,extended line, name path=A] (spring1) -- (spring2);
\draw [thick,densely dotted,extended line,name path=E] ($(spring1)!1cm!(spring2)$) -- ($(spring1)!-.88cm!(spring2)$);

\draw [thick,densely dotted,extended line, name path=B] ($(spring1)!(pivot1)!(spring2)$) -- (pivot1);

\draw [thick,densely dotted,extended line, name path=C] ($(spring1)!(pivot2)!(spring2)$) -- (pivot2);

\draw[decorate,decoration={brace,amplitude=.3cm}] (pivot1.east) -- (spring1.east) node[blank,right] {$l$};

\node[name intersections={of= E and B}] (P1) at (intersection-1) {$ $};
\node[name intersections={of= A and C}] (P2) at (intersection-1) {$ $};

\draw[decorate,decoration={brace,mirror,amplitude=.25cm}] ($(P1)-(0,0)$) to ($(spring1)+(0.15,0)$) node[blank,below,xshift=-0.5cm,yshift=-2.1cm] {$x$};

\draw[decorate,decoration={brace,amplitude=.25cm}] (spring2) to ($(P2)+(0,0)$) node[blank,below,xshift=1.5cm,yshift=-2.1cm] {$y$};
\draw[decorate,decoration={brace,amplitude=.7cm}] (pivot2.east) to ($(bob2)+(-0.25,0.4)$) node[blank,left,xshift=2.95cm,yshift=-1.3cm] {$L$};

\end{tikzpicture} \end{figure}

\end{document}

and the output is:

enter image description here

There are some issues in these figures.

  1. The second figure is not centered like the first one.
  2. The braces over the length L of the strings does not connect correctly the pivots and the bobs.
  3. The caption of the braces over the length l of the segment connecting the pivots and the points where the spring is attached is not positioned correctly.
  4. I wonder if there a way to set automatically the ends of the braces denoted as x and y.

How could I fix them?

Torbjørn T.
  • 206,688
Cris
  • 1,189
  • 2
    Should the two figures appear one immediately after the other, with no captions or anything else in between? If so you could fix the misalignment for example by placing both in the same tikzpicture. (The second one is centered, but the figure as a whole is centered, and it's wider than the first one because of the pendulum going out right.) – Torbjørn T. Jul 04 '20 at 10:01
  • 1
    For 2), what is "correctly"? Should the braces end at the top of the bob? Because you've explicitly told TikZ not to do that with the coordinate calculations such as ($(bob2)+(-0.25,0.4)$). – Torbjørn T. Jul 04 '20 at 10:06
  • 1
    You could just make the top wider so that the second image fits under it, or add something like \path (-8,0) (8,0); to each tikzpicture. – John Kormylo Jul 04 '20 at 14:48
  • @TorbjørnT. The figures should appear immediately after the other, exactly like in your solution code. Indeed, I saw it later, the figures are centered, but I was misled by the greater width of the second one. Of course, correctly meant that the braces end where the bobs are attached to the strings. – Cris Jul 04 '20 at 15:49
  • @JohnKormylo Thanks for your answer. I used the solution based on \begin{scope}[yshift=-6cm] \ \end{scope}. – Cris Jul 04 '20 at 15:52

1 Answers1

4
  1. The second figure is centered, the "walls" in the two diagrams aren't aligned because the second diagram is wider than the first. To get them aligned you can for example put both diagrams in the same tikzpicture, using a scope to shift the second one down:

     \begin{tikzpicture}
     <code for first diagram>
    

    \begin{scope}[yshift=-6cm] <code for second diagram> \end{scope} \end{tikzpicture}

    In this case you must remove the first \end{tikzpicture}\end{figure} and the second \begin{figure}\centering\begin{tikzpicture} of course.

    If you want to keep separate figures, and have the rightmost pendulum not be taken into account for the centering, you can add

     \useasboundingbox (wall.north west) rectangle ([yshift=-4cm]wall.east);
    

    immediately after \node (wall) [ground, minimum width=4cm] {}; in the second diagram.

    Both are demonstrated in the code below.

  2. You have explicitly told TikZ to stop the braces short of the bobs, with e.g. ($(bob1)+(0.02,0.475)$). Use just (bob1) and the brace stops where it should. (If I understand correctly.)

  3. When using to, any nodes that are to be placed along the path must be placed between to and the next coordinate. I.e. you need

     \draw (0,0) to node {foo} (2,2);
    

    and not

     \draw (0,0) to (2,2) node[midway] {foo};
    

    The latter case will not work, and this is why you need all those xshifts and yshifts.

    When using -- instead of to, however, \draw (0,0) -- (2,2) node[midway] {foo}; does work, and the foo node is placed at the halfway point.

  4. You can use perpendicular coordinates (TikZ: What EXACTLY does the the |- notation for arrows do?) to define coordinates at the left endpoints of the braces. For example,

    \coordinate (c1) at (pivot1 |- spring1);
    

    and then draw the brace from c1 to spring1.

    You've made this more complicated than it needs be I think, by defining coordinates such as c1 you don't need the vertical1 coordinates, or intersections, or coordinate calculations.

Here is a modified version of your code. In addition to some of the things mentioned above, I made the spring and resort nodes into coordinates (so you don't have to worry about the anchors etc. they're just points), and I moved the common style definitions to the \tikzset in the preamble, and made a new style for the bob nodes. Oh, and I removed some empty nodes as well, if they're not used all they do is clutter the code.

output of code below

\documentclass[11pt]{article}

\usepackage{tikz} \usetikzlibrary{ angles, quotes, arrows.meta, decorations.markings, decorations.pathmorphing, decorations.pathreplacing, calc, patterns, positioning } \tikzset{ spring/.style={thick,decorate,decoration={zigzag,pre length=0.3cm,post length=0.3cm,segment length=6}}, blank/.style={draw=none,fill=none,pos=0.5}, ground/.style={fill,pattern=north east lines,draw=none,minimum width=0.5cm,minimum height=0.3cm}, damper/.style={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=10pt,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,-3pt)$) -- ($(dmp.north)+(0,3pt)$); } }, decorate }, box/.style={draw,thick,minimum width=1cm, minimum height=1cm}, dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1}, extended line/.style={shorten >=-#1,shorten <=-#1}, extended line/.default=0cm, bob/.style={draw,fill=gray!40,circle,minimum size=6pt,inner sep=6pt} }

\begin{document}

\begin{figure}[htbp] \centering \begin{tikzpicture}

\node (wall) [ground, minimum width=4cm] {}; \draw (wall.south east) -- (wall.south west);

\coordinate (pivot1) at (-1,-0.16);
\draw[thick,-] (pivot1) -- ++ (0,-4) 
  node (bob1) [bob]{}
  coordinate [midway] (resort1);

\coordinate (pivot2) at (1,-0.16);
\draw[thick,-] (pivot2) -- ++ (0,-4) 
  node (bob2) [bob]{}
  coordinate [midway] (resort2);

\draw[spring] (resort1) -- (resort2);

\draw[decorate,decoration={brace,amplitude=.35cm}] (pivot2) -- node[right=3mm] {$l$} (resort2);
 \draw[decorate,decoration={brace,mirror,amplitude=.35cm}] (pivot1) to node[left=3mm] {$L$} (bob1);

\end{tikzpicture} \end{figure}

\begin{figure}[htbp] \centering \begin{tikzpicture} \node (wall) [ground, minimum width=4cm] {};

\useasboundingbox (wall.north west) rectangle ([yshift=-4cm]wall.east);

\draw (wall.south east) -- (wall.south west);

\pgfmathsetmacro{\myAngle}{30}

\coordinate (pivot1) at (-1,-0.16);
\draw[thick] (pivot1) -- ++ (270+\myAngle:4cm)
  node (bob1) [bob]{}
  coordinate [midway] (spring1);

\coordinate (c1) at (pivot1|-spring1);

\pic [draw, -,&quot;$\theta_1$&quot;, angle eccentricity=1.5] {angle = c1--pivot1--bob1};

\coordinate (pivot2) at (1,-0.16);
\draw[thick] (pivot2) -- ++(270+\myAngle:4cm)
  node (bob2) [bob]{}
  coordinate [midway] (spring2);
\coordinate (c2) at (pivot2|-spring2);

\pic [draw, font=\small, &quot;$\theta_2$&quot;, angle eccentricity=1.5] {angle = c2--pivot2--bob2};

\draw[spring] (spring1) -- (spring2);

\draw [thick, densely dotted] (pivot1) |- (spring1);
\draw [thick, densely dotted] (pivot2) |- (spring2);

\draw[decorate,decoration={brace,mirror,amplitude=.25cm}] (c1) -- node[below=2mm] {$x$} (spring1);
\draw[decorate,decoration={brace,mirror,amplitude=.25cm}] (c2) -- node[below=2mm] {$y$} (spring2);

\draw[decorate,decoration={brace,amplitude=.35cm}] (pivot1) --node[blank,right=3mm,yshift=2mm] {$l$} (spring1);

\draw[decorate,decoration={brace,amplitude=.35cm}] (pivot2) -- node[blank,right=3mm,yshift=2mm] {$L$}(bob2) ;

\end{tikzpicture} \end{figure}

%% both in the same tikzpicture/figure might be more appropriate

\begin{figure}[htbp] \centering \begin{tikzpicture}

\node (wall) [ground, minimum width=4cm] {}; \draw (wall.south east) -- (wall.south west);

\coordinate (pivot1) at (-1,-0.16);
\draw[thick,-] (pivot1) -- ++ (0,-4) 
  node (bob1) [bob]{}
  coordinate [midway] (resort1);

\coordinate (pivot2) at (1,-0.16);
\draw[thick,-] (pivot2) -- ++ (0,-4) 
  node (bob2) [bob]{}
  coordinate [midway] (resort2);

\draw[spring] (resort1) -- (resort2);

\draw[decorate,decoration={brace,amplitude=.35cm}] (pivot2) -- node[right=3mm] {$l$} (resort2);
 \draw[decorate,decoration={brace,mirror,amplitude=.35cm}] (pivot1) to node[left=3mm] {$L$} (bob1);

\begin{scope}[yshift=-5.3cm] \node (wall) [ground, minimum width=4cm] {};

\useasboundingbox (wall.north west) rectangle ([yshift=-4cm]wall.east);

\draw (wall.south east) -- (wall.south west);

\pgfmathsetmacro{\myAngle}{30}

\coordinate (pivot1) at (-1,-0.16);
\draw[thick] (pivot1) -- ++ (270+\myAngle:4cm)
  node (bob1) [bob]{}
  coordinate [midway] (spring1);

\coordinate (c1) at (pivot1|-spring1);

\pic [draw, -,&quot;$\theta_1$&quot;, angle eccentricity=1.5] {angle = c1--pivot1--bob1};

\coordinate (pivot2) at (1,-0.16);
\draw[thick] (pivot2) -- ++(270+\myAngle:4cm)
  node (bob2) [bob]{}
  coordinate [midway] (spring2);
\coordinate (c2) at (pivot2|-spring2);

\pic [draw, font=\small, &quot;$\theta_2$&quot;, angle eccentricity=1.5] {angle = c2--pivot2--bob2};

\draw[spring] (spring1) -- (spring2);

\draw [thick, densely dotted] (pivot1) |- (spring1);
\draw [thick, densely dotted] (pivot2) |- (spring2);

\draw[decorate,decoration={brace,mirror,amplitude=.25cm}] (c1) -- node[below=2mm] {$x$} (spring1);
\draw[decorate,decoration={brace,mirror,amplitude=.25cm}] (c2) -- node[below=2mm] {$y$} (spring2);

\draw[decorate,decoration={brace,amplitude=.35cm}] (pivot1) --node[blank,right=3mm,yshift=2mm] {$l$} (spring1);

\draw[decorate,decoration={brace,amplitude=.35cm}] (pivot2) -- node[blank,right=3mm,yshift=2mm] {$L$}(bob2) ;

\end{scope}

\end{tikzpicture} \end{figure}

\end{document}

Torbjørn T.
  • 206,688
  • 1
    Thank you so very much for your thorough solution! I really didn't know how to use the perpendicular coordinates -|/|-. It's much simpler and more elegant. Indeed, the intersections or coordinate computations are not needed. I also understood how to use correctly 'to' or '--' to set a node between two points. – Cris Jul 04 '20 at 16:13