10

How do I draw an angle with a label between two lines when the lines are not necessarily drawn in the same \draw call?

I need to draw an angle with a label, theta, between the y-axis and the pendulum string (see picture below).

Other suggestions/improvements of my code/diagram are welcome.

Current code:

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{calc,patterns}
\begin{document}
\begin{tikzpicture}
\coordinate (origo) at (0,0);
\coordinate (pivot) at (1,5);

% draw axes
\fill[black] (origo) circle (0.05);
\draw[thick,gray,->] (origo) -- ++(4,0) node[black,right] {$x$};
\draw[thick,gray,->] (origo) -- ++(0,-4) node[black,below] {$y$};

% draw roof
\fill[pattern = north east lines] ($ (origo) + (-1,0) $) rectangle ($ (origo) + (1,0.5) $);
\draw[thick] ($ (origo) + (-1,0) $) -- ($ (origo) + (1,0) $);

\draw[thick] (origo) -- ++(300:3) coordinate (bob);
\fill (bob) circle (0.2);
\end{tikzpicture}
\end{document}

Current output:

enter image description here

For illustration, I want something like this in my diagram:

enter image description here

23

You can use the angles library which defines a pic for this purpose. The quotes library is used for ease of labelling.

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{calc,patterns,angles,quotes}
\begin{document}
  \begin{tikzpicture}
    \coordinate (origo) at (0,0);
    \coordinate (pivot) at (1,5);

    % draw axes
    \fill[black] (origo) circle (0.05);
    \draw[thick,gray,->] (origo) -- ++(4,0) node[black,right] {$x$};
    \draw[thick,gray,->] (origo) -- ++(0,-4) node (mary) [black,below] {$y$};

    % draw roof
    \fill[pattern = north east lines] ($ (origo) + (-1,0) $) rectangle ($ (origo) + (1,0.5) $);
    \draw[thick] ($ (origo) + (-1,0) $) -- ($ (origo) + (1,0) $);

    \draw[thick] (origo) -- ++(300:3) coordinate (bob);
    \fill (bob) circle (0.2);

    \pic [draw, ->, "$\theta$", angle eccentricity=1.5] {angle = mary--origo--bob};
  \end{tikzpicture}
\end{document}

marked angle

If you want the angle in red with two-way arrows, just modify the last line:

\pic [draw=red, <->, "$\theta$", angle eccentricity=1.5] {angle = mary--origo--bob};

modified marked angle

EDIT (Respond to query in comments)

To change the colour of the label as well, just set the text key:

\pic [draw=red, text=blue, <->, "$\theta$", angle eccentricity=1.5] {angle = mary--origo--bob};

modified modified marked angle

  • 2
    May I ask why you called the node mary? :-) – user66178 Dec 22 '14 at 2:54
  • @teelf Why did you call the other one bob? I don't really know, is the answer. For some reason 'Bob' -> 'Other name?' got me 'Mary'. Why? – cfr Dec 22 '14 at 3:01
  • 1
    @teelf Didn't you ask how to change the colour of the marker? It seems to have disappeared but, just in case, see edit above ;). – cfr Dec 22 '14 at 3:04
  • 1
    @Casimir Try changing angle eccentricity. – cfr Jan 20 '17 at 1:21
  • 1
    I tried out your solution and it works great. For other users i have two tips: 1. If you use angle=(0,0)--(1,0)--(0,-1) with points and not coordinates your latex code will throw an error. You need to add a coordinate for each point of your angle. 2. If you wish to fill the angle with a certain color you can use the fill option in the same style as in \draw. – michip96 Jun 11 '18 at 13:40
4

Just for comparison, here's a version in plain Metapost with some explanatory comments.

a swinging pendulum

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);

% first define the unit to use
u = 1cm;

% now define the paths and points
% next define the axes, the bob position, and the path of the pendulum
path xx, yy, pendulum; pair bob;
xx = (left -- 3 right) scaled u;
yy = (origin -- 4 down) scaled u;

theta = 24;
bob = 3 down scaled u rotated theta;
pendulum = origin -- bob;

% also define an angle mark, rotated to start on the yy axis and go as far as the pendulum
% this assumes theta is positive by the way
path angle_mark; angle_mark = fullcircle rotated 270 scaled 3/2u cutafter pendulum;

% now we can get on with drawing

% first do the striped fill for the roof area
path roof_area; roof_area = unitsquare shifted 1/2 left xscaled 2u yscaled 1/2u;
picture roof_fill;   
roof_fill = image(for x=-2u step 1/8u until 2u: draw (left--right) scaled 2u rotated 45 shifted (x,0); endfor);
clip roof_fill to roof_area;
draw roof_fill;

% now draw the axes in grey
drawarrow xx withcolor .5 white; label.rt (btex $x$ etex, point 1 of xx);
drawarrow yy withcolor .5 white; label.bot(btex $y$ etex, point 1 of yy);
% and the bottom of the roof area in black
draw subpath(0,1) of roof_area;
% draw the pendulum, the pivot at the origin, and the bob on the end of the pendulum
draw pendulum;
fill fullcircle scaled dotlabeldiam;
fill fullcircle scaled 1/3u shifted bob;

% now the angle mark - note it will lie on top of the axis and the pendulum
% if this bothers you, draw it first
ahlength := 2.5; 
drawarrow angle_mark withcolor .54 red;

% finally do the label, attached to the angle_mark
label(btex $\theta$ etex, point 1/2 of angle_mark + (1,-6)) withcolor .67 blue;

% and some optional go-faster stripes for the bob
for i=2 step 2 until 10:
  draw quartercircle scaled 1/3u rotated (135+theta-i) shifted (bob rotated -i) withcolor (i/10)*white;
endfor
endfig;
end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy