[Tex/LaTex] Drawing and Labeling a Triangle for a Demonstration of the Double-Angle Formula of Cosines

pgfplotstikz-pgf

I have a display of two triangles, but the triangle drawn in red is unintentional. TikZ is misinterpreting the coordinates for point C. The polar coordinates for C is given as \coordinate (C) at (120:5);. TikZ is plotting the point in Quadrant III.

Also, TikZ is misinterpreting the positions at which I want the label for the vertex A and the labels for the angles to be typeset. The command to place A is

\node at ({2.5mm*sqrt(2)/2},{-2.5mm*sqrt(2)/2}){$A$};

and the commands to place $\theta$ are

\coordinate (label_for_theta) at (60:4mm);

\node[font=\footnotesize] at (label_for_theta){$\theta$};.

It seems to me that TikZ is placing these labels using the canvas coordinate system.

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings,backgrounds,patterns}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}


\begin{tikzpicture}

\begin{axis}[width=5in,axis equal image,
    axis lines=middle,
    xmin=-5,xmax=5,
    ymin=-1.5,ymax=5,
    restrict y to domain=-1.5:5,
    enlargelimits={abs=0.5cm},
    xtick={\empty},ytick={\empty},
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

%A triangle is drawn on the Cartesian plane. One side of the triangle is along
%the positive x-axis, and another side of the triangle is drawn in Quadrant II.
\coordinate (A) at (0,0);
\coordinate (B) at (3.5,0);
\coordinate (C) at (120:5);
\draw[red] (A) -- (B) -- (C) -- cycle;

%The labels for A and B are typeset.
\node at ({2.5mm*sqrt(2)/2}:{-2.5mm*sqrt(2)/2}){$A$};
\node at (3.5,-2.5mm){$B$};

%The point C' = C is placed using Cartesian coordinates.
\coordinate (C') at (-2.5,{5*sin(120)});
\draw[fill] (C') circle (1.5pt);
\draw (A) -- (C');
\draw (B) -- (C');
%The label for C' = C is typeset.
\coordinate (label_C_left) at ($(C')!-4mm!(B)$);
\coordinate (label_C_right) at ($(C')!-4mm!(A)$);
\coordinate (label_C) at ($(label_C_left)!0.5!(label_C_right)$);
\node[blue] at ($(C')!2.5mm!(label_C)$){$C$};

%Angles are drawn for $\theta$ and its supplement.
\draw[draw=blue] (A) ++(120:4mm) arc (120:0:4mm);
\coordinate (label_for_theta) at (60:6.5mm);
\node[font=\footnotesize] at (label_for_theta){$\theta$};
\draw[draw=blue,dash dot] (A) ++(180:6mm) arc (180:120:6mm);
\coordinate (label_for_supplement_to_theta) at (150:8.5mm);
\node[font=\footnotesize] at (label_for_supplement_to_theta){$\pi - \theta$};


%A right-angle mark is drawn.
\coordinate (U) at ($(-2.5,0)!3mm!45:(A)$);
\draw[dash dot] (U) -- ($(-2.5,0)!(U)!(A)$);
\draw[dash dot] (U) -- ($(-2.5,0)!(U)!(C')$);


\draw[dashed] (C') -- (-2.5,0);

%Braces indicating the distances that C' is from the axes are typeset. To give
%them the appearance of being typeset over the axes, they are first typeset
%in white with a line width of 2pt, which is 10 times the thickness of the
%brace that is actually typeset.
\draw[draw=white,line width=4pt,decorate,decoration={brace,raise=5pt,amplitude=5pt}] (-2.5,0) -- (C');
\draw[decorate,decoration={brace,raise=5pt,amplitude=5pt}] (-2.5,0) -- (C');
\draw[draw=white,line width=4pt,decorate,decoration={brace,raise=5pt,amplitude=5pt,mirror}] (-2.5,0) -- (A);
\draw[decorate,decoration={brace,raise=5pt,amplitude=5pt,mirror}] (-2.5,0) -- (A);


\coordinate (label_for_5_sin_theta) at ($({5*cos(120)},{2.5*sin(120)}) + (-2.5mm-10pt,0pt)$);
\node[anchor=east] at (label_for_5_sin_theta){$r\sin\theta$};
\coordinate (label_for_5_cos_theta) at (-1.25,-2.5mm-10pt);
\node at (label_for_5_cos_theta){$r\cos\theta$};

\end{axis}
\end{tikzpicture}


\end{document}

Best Answer

You cannot straightforwardly use dimensions when specifying coordinates within an axis environment. Since the specification for C doesn't use a dimension, it is correctly placed, but other coordinates are misplaced relative to the intended origin.

To see that this is not an effect of TikZ itself, simply remove the axis environment and typeset the remaining code within the raw tikzpicture environment.

The reason this does not work in the axis environment is because there is, in general, no one-one correspondence between the coordinate systems used by TikZ and those configured by pgfplots. See page 350, section 4.27 TikZ Interoperability for details.

One option is to simply dispense with pgfplots as you are not really using it except to draw the axes:

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{amsmath}
\usepackage{amsfonts}
\usetikzlibrary{calc,arrows.meta,positioning}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}

\begin{tikzpicture}
  \draw [{Latex[]}-{Latex[]}] (-5.5,0) -- (5.5,0) node [below right] {$x$};
  \draw [{Latex[]}-{Latex[]}] (0,-1.5) -- (0,5.5) node [above right] {$y$};

  %A triangle is drawn on the Cartesian plane. One side of the triangle is along
  %the positive x-axis, and another side of the triangle is drawn in Quadrant II.
  \coordinate (A) at (0,0);
  \coordinate (B) at (3.5,0);
  \coordinate (C) at (120:5);
  \draw[draw=red] (A) node [below right] {$A$} -- (B) -- (C) -- cycle;

  %The label for B is typeset.
  \node at (3.5,-2.5mm){$B$};

  %The point C' = C is placed using Cartesian coordinates.
  \coordinate (C') at (-2.5,{5*sin(120)});
  \draw[fill] (C') circle (1.5pt);
  \draw (A) -- (C');
  \draw (B) -- (C');
  %The label for C' = C is typeset.
  \coordinate (label_C_left) at ($(C')!-4mm!(B)$);
  \coordinate (label_C_right) at ($(C')!-4mm!(A)$);
  \coordinate (label_C) at ($(label_C_left)!0.5!(label_C_right)$);
  \node[blue] at ($(C')!2.5mm!(label_C)$){$C$};

  %Angles are drawn for $\theta$ and its supplement.
  \draw[draw=blue] (A) ++(120:4mm) arc (120:0:4mm);
  \coordinate (label_for_theta) at (60:6mm);
  \node[font=\footnotesize] at (label_for_theta){$\theta$};
  \draw[draw=blue,dash dot] (A) ++(180:6mm) arc (180:120:6mm);
  \coordinate (label_for_supplement_to_theta) at (150:9.5mm);
  \node[font=\footnotesize] at (label_for_supplement_to_theta){$\pi - \theta$};

  %A right-angle mark is drawn.
  \coordinate (U) at ($(-2.5,0)!3mm!45:(A)$);
  \draw[dash dot] (U) -- ($(-2.5,0)!(U)!(A)$);
  \draw[dash dot] (U) -- ($(-2.5,0)!(U)!(C')$);

  \draw[dashed] (C') -- (-2.5,0);

  %Braces indicating the distances that C' is from the axes are typeset. To give
  %them the appearance of being typeset over the axes, they are first typeset
  %in white with a line width of 2pt, which is 10 times the thickness of the
  %brace that is actually typeset.
  \draw[draw=white,line width=4pt,decorate,decoration={brace,raise=5pt,amplitude=5pt}] (-2.5,0) -- (C');
  \draw[decorate,decoration={brace,raise=5pt,amplitude=5pt}] (-2.5,0) -- (C');
  \draw[draw=white,line width=4pt,decorate,decoration={brace,raise=5pt,amplitude=5pt,mirror}] (-2.5,0) -- (A);
  \draw[decorate,decoration={brace,raise=5pt,amplitude=5pt,mirror}] (-2.5,0) -- (A);

  \coordinate (label_for_5_sin_theta) at ($({5*cos(120)},{2.5*sin(120)}) + (-2.5mm-10pt,0pt)$);
  \node[anchor=east] at (label_for_5_sin_theta){$r\sin\theta$};
  \coordinate (label_for_5_cos_theta) at (-1.25,-2.5mm-10pt);
  \node at (label_for_5_cos_theta){$r\cos\theta$};
\end{tikzpicture}
\end{document}

non-plotted axes

Another option is to specify the coordinates differently. Although polar coordinates using absolute dimensions do not work within the axis environment you've defined, specifying them without using absolute dimensions does:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{amsmath}
\usepackage{amsfonts}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings,backgrounds,patterns}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[width=5in,axis equal image,
    axis lines=middle,
    xmin=-5,xmax=5,
    ymin=-1.5,ymax=5,
    restrict y to domain=-1.5:5,
    enlargelimits={abs=0.5cm},
    xtick={\empty},ytick={\empty},
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
    ]

    %A triangle is drawn on the Cartesian plane. One side of the triangle is along
    %the positive x-axis, and another side of the triangle is drawn in Quadrant II.
    \draw [draw= red] (0,0) coordinate (A) node [below right] {$A$} -- (3.5,0) coordinate (B) node [below] {$B$} -- (120:5) coordinate (C) -- cycle;

    %The point C' = C is placed using Cartesian coordinates.
    \draw (A) --  (-2.5,{5*sin(120)}) coordinate (C') -- (B);
    \draw[fill] (C') circle (1.5pt);
    %The label for C' = C is typeset.
    \coordinate (label_C_left) at ($(C')!-4mm!(B)$);
    \coordinate (label_C_right) at ($(C')!-4mm!(A)$);
    \coordinate (label_C) at ($(label_C_left)!0.5!(label_C_right)$);
    \node[blue] at ($(C')!2.5mm!(label_C)$){$C$};

    %Angles are drawn for $\theta$ and its supplement.
    \draw[draw=blue] (120:0.4) arc (120:0:0.4);
    \coordinate (label_for_theta) at (60:0.4);
    \node[font=\footnotesize, anchor=south west] at (label_for_theta){$\theta$};
    \draw[draw=blue,dash dot] (180:.6) arc (180:120:.6);
    \coordinate (label_for_supplement_to_theta) at (150:.6);
    \node[font=\footnotesize, anchor=south east] at (label_for_supplement_to_theta){$\pi - \theta$};

    %A right-angle mark is drawn.
    \draw[dash dot] ($(-2.5,0)!3mm!45:(A)$) coordinate (U) -- ($(-2.5,0)!(U)!(A)$);
    \draw[dash dot] (U) -- ($(-2.5,0)!(U)!(C')$);

    \draw[dashed] (C') -- (-2.5,0);

    %Braces indicating the distances that C' is from the axes are typeset. To give
    %them the appearance of being typeset over the axes, they are first typeset
    %in white with a line width of 2pt, which is 10 times the thickness of the
    %brace that is actually typeset.
    \draw[draw=white,line width=4pt,decorate,decoration={brace,raise=5pt,amplitude=5pt}] (-2.5,0) -- (C');
    \draw[decorate,decoration={brace,raise=5pt,amplitude=5pt}] (-2.5,0) -- (C');
    \draw[draw=white,line width=4pt,decorate,decoration={brace,raise=5pt,amplitude=5pt,mirror}] (-2.5,0) -- (A);
    \draw[decorate,decoration={brace,raise=5pt,amplitude=5pt,mirror}] (-2.5,0) -- (A);

    \coordinate (label_for_5_sin_theta) at ($({5*cos(120)},{2.5*sin(120)}) + (-2.5mm-10pt,0pt)$);
    \node[anchor=east] at (label_for_5_sin_theta){$r\sin\theta$};
    \coordinate (label_for_5_cos_theta) at (-1.25,-2.5mm-10pt);
    \node at (label_for_5_cos_theta){$r\cos\theta$};

  \end{axis}
\end{tikzpicture}
\end{document}

non-dimensions in polar coordinates

It is true that you can usually use dimensions to say e.g. (60:6mm) in an ordinary tikzpicture environment.

Related Question