Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

When using 3D coordinates in TikZ, is there a way of changing the direction of the Z vector so that it doesn't point at a 45 degree angle in the X-Y plane? Given a drawing of a cube like the following, I would like a nice way to shift the face at the back around without having to tweak the coordinates.

\begin{tikzpicture}
\draw (0,0,0) -- (1,0,0) -- (1,1,0) -- (0,1,0) -- cycle;
\draw (0,0,1) -- (1,0,1) -- (1,1,1) -- (0,1,1) -- cycle;
\draw (0,0,0) -- (0,0,1); \draw (1,0,0) -- (1,0,1); \draw (0,1,0) -- (0,1,1); \draw (1,1,0) -- (1,1,1); 
\end{tikzpicture}

Looking at the manual, I was only able to find the ability to change the length of the Z vector, not its direction.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You can specify the unit vector for the z-direction with the z key, making the z-vector appear at a given angle in the XY-plane.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[z={(60:0.5cm)}]
    \draw (0,0,0) -- (1,0,0) -- (1,1,0) -- (0,1,0) -- cycle;
    \draw (0,0,1) -- (1,0,1) -- (1,1,1) -- (0,1,1) -- cycle;
    \draw (0,0,0) -- (0,0,1); \draw (1,0,0) -- (1,0,1); \draw (0,1,0) -- (0,1,1); \draw (1,1,0) -- (1,1,1);
\end{tikzpicture}

\begin{tikzpicture}[z={(3cm,1.5cm)}]
    \draw (0,0,0) -- (1,0,0) -- (1,1,0) -- (0,1,0) -- cycle;
    \draw (0,0,1) -- (1,0,1) -- (1,1,1) -- (0,1,1) -- cycle;
    \draw (0,0,0) -- (0,0,1); \draw (1,0,0) -- (1,0,1); \draw (0,1,0) -- (0,1,1); \draw (1,1,0) -- (1,1,1);
\end{tikzpicture}
\end{document}

Output

share|improve this answer
    
Aha - this is exactly what I needed. Where I was going wrong was by not putting the coordinate in "z={(3cm,1.5cm)}" in braces. Thanks. –  Ashley Montanaro May 9 at 14:18

You can use rotate around y=<angle>. Similarly there is rotate around x=<angle> and rotate around z=<angle>.

\documentclass{article}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
\draw (0,0,0) -- (1,0,0) -- (1,1,0) -- (0,1,0) -- cycle;
\draw (0,0,1) -- (1,0,1) -- (1,1,1) -- (0,1,1) -- cycle;
\draw (0,0,0) -- (0,0,1); \draw (1,0,0) -- (1,0,1);
\draw (0,1,0) -- (0,1,1); \draw (1,1,0) -- (1,1,1);
\draw[red] (0,0,0) -- (0,0,2)node {$z$};
\end{tikzpicture}
  \begin{tikzpicture}[rotate around y=45]
\draw (0,0,0) -- (1,0,0) -- (1,1,0) -- (0,1,0) -- cycle;
\draw (0,0,1) -- (1,0,1) -- (1,1,1) -- (0,1,1) -- cycle;
\draw (0,0,0) -- (0,0,1); \draw (1,0,0) -- (1,0,1); 
\draw (0,1,0) -- (0,1,1); \draw (1,1,0) -- (1,1,1);
\draw[red] (0,0,0) -- (0,0,2)node {$z$};
\end{tikzpicture}
  \begin{tikzpicture}[rotate around x=45]
\draw (0,0,0) -- (1,0,0) -- (1,1,0) -- (0,1,0) -- cycle;
\draw (0,0,1) -- (1,0,1) -- (1,1,1) -- (0,1,1) -- cycle;
\draw (0,0,0) -- (0,0,1); \draw (1,0,0) -- (1,0,1);
\draw (0,1,0) -- (0,1,1); \draw (1,1,0) -- (1,1,1);
\draw[red] (0,0,0) -- (0,0,2)node {$z$};
\end{tikzpicture}
  \begin{tikzpicture}[rotate around z=5]
\draw (0,0,0) -- (1,0,0) -- (1,1,0) -- (0,1,0) -- cycle;
\draw (0,0,1) -- (1,0,1) -- (1,1,1) -- (0,1,1) -- cycle;
\draw (0,0,0) -- (0,0,1); \draw (1,0,0) -- (1,0,1);
\draw (0,1,0) -- (0,1,1); \draw (1,1,0) -- (1,1,1);
\draw[red] (0,0,0) -- (0,0,2)node {$z$};
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
Thanks, this is helpful but for my application setting the vector directly as described by the other answer turns out to be more useful. –  Ashley Montanaro May 9 at 14:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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