Search code examples
tikzxelatex

LateX Tikz: How can I rotate a line


I tried to rotate a line, several lines, with scope, but I don't know how.

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[]
\coordinate (A) at (0,0);
\coordinate (B) at (4,0);
\coordinate (C) at (6,2);
\coordinate (D) at (2,2);
\begin{scope}[rotate=30]
    \draw (A) -- (B) --(C) -- (D) --cycle;
\end{scope}
\begin{scope}[rotate=60]
\draw (2,0) ellipse (3 cm and 1.5cm);
\end{scope}
\end{tikzpicture}
\end{document}

I'm expecting a rotated parallelogram by 30°. I tried a rotation of an ellipse. This works.


Solution

  • The problem is that you are using absolute coordinates. If you replace them by relative coordinates, you'll get rotated shapes:

    \documentclass[tikz]{standalone}
    \usepackage{tikz}
    \usetikzlibrary{calc}
    \usetikzlibrary{arrows}
    \begin{document}
    \begin{tikzpicture}[]
    \coordinate (A) at (0,0);
    \coordinate (B) at (4,0);
    \coordinate (C) at (6,2);
    \coordinate (D) at (2,2);
    \begin{scope}[rotate=30]
        \draw (A) -- ++(0,1) -- ++(1,0) -- ++(0,-1) --cycle;
        \draw (D) rectangle ++(3,3);
    \end{scope}
    \begin{scope}[rotate=60]
    \draw (2,0) ellipse (3 cm and 1.5cm);
    \end{scope}
    \end{tikzpicture}
    \end{document}
    

    Now if you wanted to keep your absolute coordinates, you could transform the canvas:

    \documentclass[tikz]{standalone}
    \usepackage{tikz}
    \usetikzlibrary{calc}
    \usetikzlibrary{arrows}
    \begin{document}
    \begin{tikzpicture}[]
    \coordinate (A) at (0,0);
    \coordinate (B) at (4,0);
    \coordinate (C) at (6,2);
    \coordinate (D) at (2,2);
    \begin{scope}[transform canvas={rotate=30}]
        \draw (A) -- (B) --(C) -- (D) --cycle;
    \end{scope}
    \begin{scope}[rotate=60]
    \draw (2,0) ellipse (3 cm and 1.5cm);
    \end{scope}
    \end{tikzpicture}
    \end{document}