7

Is there a way to define vectors in TikZ? By that I mean the ability to add and scalar multiply coordinate tuples, for example for drawing a parallelogram:

% Just pseudocode:
P := (0,1);
Q := (1,1);
\draw[-] (0,0) -- P -- (P+Q) -- Q -- (0,0);
8

Coordinates are already some kind of vectors. Library calc provides the addition of coordinates:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\tikz\draw
  (0, 1) coordinate (P)
  (1, 1) coordinate (Q)
  (0, 0) -- (P) -- ($(P) + (Q)$) -- (Q) -- cycle
;
\end{document}

Result

BTW: Closed paths should use cycle to get a proper line join at the starting point.

  • Thank you very much, that is an excellent answer! Also thanks for the cycle hint! – flawr Oct 26 '16 at 21:19
  • A follow up question: Is there some way to use the defined P and Q in more than one (that particular) \draw command? – flawr Oct 26 '16 at 21:56
  • @flawr For more than one \draw command, use environment use environment tikzpicture. – Heiko Oberdiek Oct 27 '16 at 4:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.