6

I am trying to create a cube with the corners properly marked. I have the following MWE:

\documentclass{standalone}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{microtype}
\usepackage{mathtools}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \coordinate (o) at (0,0,0);
  \draw[->] (o) -- (5,0,0) node[right] {\(x\)};
  \draw[->] (o) -- (0,5,0) node[above] {\(y\)};
  \draw[->] (o) -- (0,0,5) node[left] {\(z\)};

  \node[left] (a) at (0,0,4) {\(a\)};
  \node[left] (b) at (0,4,4) {\(b\)};
  \node[right] (c) at (4,4,4) {\(c\)};
  \node[below] (d) at (4,0,4) {\(c\)};
  \node[right] (e) at (4,0,0) {\(e\)};
  \node[right] (f) at (4,4,0) {\(f\)};
  \node[above] (g) at (0,4,0) {\(g\)};
  \node[below] (h) at (0,0,0) {\(h\)};

  \begin{scope}[red]
    \draw (a) -- (b);
    \draw (b) -- (c);
    \draw (c) -- (d);
    \draw (d) -- (e);
    \draw (e) -- (f);
    \draw (f) -- (g);
    \draw[dashed] (g) -- (h);
    \draw[dashed] (h) -- (a);
  \end{scope}
\end{tikzpicture}
\end{document}

Unfortunately, this creates a cube with strangely bent lines (as the lines appear to be going to the center of the letters rather than the coordinates specified):

How can I make the lines actually go to the coordinates specified and have the text be shifted?

1 Answer 1

4

You are using nodes as coordinates for your red lines. This is considered the center of nodes, which don't lie on the "coordinates". Instead, use coordinates with labels as you can see in MWE below and you will obtain the following picture:

enter image description here

\documentclass[tikz,
               border=3mm]{standalone}
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    \usepackage{microtype}
    \usepackage{mathtools}

\begin{document}
    \begin{tikzpicture}
\coordinate (o) at (0,0,0);
    \draw[->] (o) -- (5,0,0) node[right] {\(x\)};
    \draw[->] (o) -- (0,5,0) node[above] {\(y\)};
    \draw[->] (o) -- (0,0,5) node[left] {\(z\)};
% here I change node to coordinate ...
\coordinate[label=left: $a$] (a) at (0,0,4);
\coordinate[label=left: $b$] (b) at (0,4,4);
\coordinate[label=right:$c$] (c) at (4,4,4);
\coordinate[label=below:$d$] (d) at (4,0,4);
\coordinate[label=below:$e$] (e) at (4,0,0);
\coordinate[label=right:$f$] (f) at (4,4,0);
\coordinate[label=left: $g$] (g) at (0,4,0);
\coordinate[label=below:$h$] (h) at (0,0,0);

    \begin{scope}[red]
\draw   (a) -- (b)  (b) -- (c)   (c) -- (d)
        (d) -- (e)  (e) -- (f)   (f) -- (g);
\draw[dashed]       (g) -- (h)  (h) -- (a);
  \end{scope}
\end{tikzpicture}
\end{document}

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.