4

I have a code to draw a simple vector diagram with tikZ

\documentclass[tikz, border=3mm]{standalone}
\usepackage{amsmath, amssymb}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
  \coordinate (v1) at (0,0);
  \coordinate (v2) at (1,1);
  \coordinate (v3) at (0,2.5);

  \draw[->] (v1) -- node[right] {$\vec{a}$} (v2);
  \draw[->] (v2) -- node[right] {$\vec{b}$} (v3);
  \draw[->] (v1) -- node[left] {$\vec{c}$} (v3);

\end{tikzpicture}
\end{document}

In addition I would like to draw the angle between vectors a and b. This requires 1) extending the line of vector a a bit 2) drawing the arc corresponding to the angle 3) placing the angle label

The final output should be the following:

enter image description here

What additions to the code are required?

Viesturs
  • 7,895

2 Answers2

3

You can use the calc library to calculate a new coordinate at the extension of the v1--v2 line, and then use the angles library as described in one of the questions mentioned by samcarter.

\documentclass[tikz, border=3mm]{standalone}
\usepackage{amsmath, amssymb}
\usetikzlibrary{calc,angles,quotes}

\begin{document}
\begin{tikzpicture}
  \coordinate (v1) at (0,0);
  \coordinate (v2) at (1,1);
  \coordinate (v3) at (0,2.5);
  % add coordinate at the extension of the line from v1 to v2
  \coordinate (v2-2) at ($(v1)!1.2!(v2)$); 

  \draw[->] (v1) -- node[right] {$\vec{a}$} (v2);
  \draw[->] (v2) -- node[right] {$\vec{b}$} (v3);
  \draw[->] (v1) -- node[left] {$\vec{c}$} (v3);

  % draw line and angle
  \draw [very thin] (v2) -- (v2-2)
     pic [draw,angle radius=2mm,angle eccentricity=1.7, "$\theta$" font=\scriptsize] {angle=v2-2--v2--v3};

\end{tikzpicture}
\end{document}

enter image description here

Torbjørn T.
  • 206,688
  • I get the following error when compiling the code: – Viesturs May 29 '17 at 18:05
  • ! Missing \endcsname inserted. \theta l.19 ...icity=1.7, ``$\theta$'', font=\scriptsize] {angle=v2-2--v2--v3}; – Viesturs May 29 '17 at 18:05
  • @Viesturs You're not using the same code. You should have " (the single character) at both sides of the \theta, while you have two backticks before it and two ' after it. – Torbjørn T. May 29 '17 at 18:13
  • Perhaps your editor autocorrected the quote marks to proper LaTeX quotes, but that is not what you want here. See e.g. https://tex.stackexchange.com/a/175971/586 – Torbjørn T. May 29 '17 at 18:14
  • This answer helped me reset the double quotation marks to U+0022 - https://emacs.stackexchange.com/questions/14068/insert-left-and-right-double-quotation-marks-instead-of-ordinary-quotation-mark – Viesturs May 29 '17 at 18:20
  • Can I nudge the placement of the angle label $theta$? – Viesturs Oct 28 '17 at 17:41
  • 1
    @Viesturs "$\theta$"{xshift=1mm, yshift=1mm font=\scriptsize} – Torbjørn T. Oct 28 '17 at 17:49
2

You can get the angle of a line using \pgfmathanglebetweenpoints.

\documentclass[tikz, border=3mm]{standalone}
\usepackage{amsmath, amssymb}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}
  \coordinate (v1) at (0,0);
  \coordinate (v2) at (1,1);
  \coordinate (v3) at (0,2.5);

  \pgfmathanglebetweenpoints{\pgfpointanchor{v1}{center}}{\pgfpointanchor{v2}{center}}
  \edef\angA{\pgfmathresult}
  \pgfmathanglebetweenpoints{\pgfpointanchor{v2}{center}}{\pgfpointanchor{v3}{center}}
  \edef\angB{\pgfmathresult}

  \draw[->] (v1) -- node[right] {$\vec{a}$} (v2);
  \draw[->] (v2) -- node[right] {$\vec{b}$} (v3);
  \draw[->] (v1) -- node[left] {$\vec{c}$} (v3);

  \draw[very thin] (v2)-- +(\angA:0.5);
  \draw[very thin] (v2) +(\angA:0.25) arc (\angA:\angB:0.25) node[pos=0.5,above=-1pt]{\footnotesize$\theta$};  
\end{tikzpicture}
\end{document}

enter image description here

StefanH
  • 13,823