2

I have to label the vertices of the following solid (cube) so that the labels do not overlap with boundaries of the solid.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \n/\x/\l/\p in{
1/{( 0  , 0)}/{$x_1y_1z_1$}/below,
2/{( 4, 0)}/{$x_1y_2z_1$}/below,
3/{( .8, 1)}/{$x_1y_2z_2$}/right,
4/{( 4.8, 1)}/{$x_1y_1z_2$}/right,
5/{( 0, 4)}/{$x_2y_2z_1$}/above,
6/{( 4, 4)}/{$x_2y_1z_1$}/above,
7/{( 0.8, 4.8)}/{$x_2y_1z_2$}/above,
8/{( 4.8, 4.8)}/{$x_2y_2z_2$}/above
}
{
        \node[inner sep=2pt,circle,draw,fill,label={\p:\l}] (\n) at \x {};
    }
\draw (1) -- (2) -- (6) -- (5) -- (1);
\draw (5) -- (6) -- (8) -- (7) -- (5);
\draw (2) -- (4) -- (8) -- (6) -- (2);
\draw[dashed] (1) -- (3) -- (4);
\draw[dashed] (3) -- (7);
\end{tikzpicture}
\end{document}
4

Having the labels "not overlapping the boundaries" is possibility subjective. Is this sufficient?

enter image description here

This was achieved by the following small changes to your code:

\documentclass[border=5mm,tikz]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\foreach \n/\x/\l/\p in{
1/{( 0  , 0)}/{$x_1y_1z_1$}/below,
2/{( 4, 0)}/{$x_1y_2z_1$}/below,
3/{( .8, 1)}/{$x_1y_2z_2$}/south east,
4/{( 4.8, 1)}/{$x_1y_1z_2$}/right,
5/{( 0, 4)}/{$x_2y_2z_1$}/left,
6/{( 4, 4)}/{$x_2y_1z_1$}/south west,
7/{( 0.8, 4.8)}/{$x_2y_1z_2$}/above,
8/{( 4.8, 4.8)}/{$x_2y_2z_2$}/above
}
{
        \node[inner sep=2pt,circle,draw,fill,label={\p:\l}] (\n) at \x {};
    }
\draw (1) -- (2) -- (6) -- (5) -- (1);
\draw (5) -- (6) -- (8) -- (7) -- (5);
\draw (2) -- (4) -- (8) -- (6) -- (2);
\draw[dashed] (1) -- (3) -- (4);
\draw[dashed] (3) -- (7);
\end{tikzpicture}

\end{document}

I have only changed some of the \p values in the loop, using south east and south west, and above instead of below, in a few places. If this isn't what you want then please give more details about how you want the labels to be placed.

Btw, it is much easier to draw a tikz cube if you use 3D coordinates. For example, the code

\documentclass[border=5mm,tikz]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
  \draw[thick](2,2,0)--(0,2,0)--(0,2,2)--(2,2,2)--(2,2,0)--(2,0,0)--(2,0,2)--(0,0,2)--(0,2,2);
  \draw[thick](2,2,2)--(2,0,2);
  \draw[gray](2,0,0)--(0,0,0)--(0,2,0);
  \draw[gray](0,0,0)--(0,0,2);
\end{tikzpicture}

\end{document}

produces

enter image description here

1
  • This is enough. Thank you very much, sir .@ Przemysław Scherwentke Feb 7 '17 at 7:28

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.