2

This question already has an answer here:

enter image description here

I'd like to draw a cube in a cube using TikZ as the above. I know how to draw a 3-dimensional vector space, with a line. But, I don't know how to draw a cube in TikZ. What code should I use for it?

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikz}
\draw %?
\end{tikz}
\end{document}

marked as duplicate by Andrew, Romain Picot, Maarten Dhondt, egreg tikz-pgf Apr 11 '16 at 10:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You can highlight code in your post using back-ticks. For code-blocks indent them by four spaces or use the {} on the gui. Btw, some people take exception to questions of the form "Please draw this for me". You will get more help if you post some code showing what you have tried and give a minimal working example. – Andrew Apr 11 '16 at 8:35
  • @Andrew What does "gui" mean? – buzzee Apr 11 '16 at 8:37
  • @buzzee - Graphical User Interface, in this case your LaTeX editor. – Karlo Apr 11 '16 at 8:44
  • @buzzee By the gui I mean the (graphical user) web interface that you used to write your question. One of the editor buttons at the top of the screen looks like {} this is can be used to highlight blocks of code. – Andrew Apr 11 '16 at 8:47
  • Do you need a perspective projection like in your example images (note how no two lines are actually parallel) or would an isometric projection (where all parallel lines in 3D space would also be parallel in the 2D image) be acceptable. The latter is a lot easier to do with TikZ. – wrtlprnft Apr 11 '16 at 9:22
0

Here is a solution using tikz. The "viewing angle" that tikz uses is a little different to that in the question.

enter image description here

Here is the code:

\documentclass{article}
\usepackage{tikz}


\begin{document}

    \begin{tikzpicture}[inside/.style={fill=cyan!80,draw opacity=0.5,thick},
                        outside/.style={thick},
                        every node/.style={font=\tiny}]
        \draw[inside](-1,-1,-1)--(-1,1,-1)--(-1,1,1)--(-1,-1,1)--(-1,-1,-1);
        \draw[inside](-1,-1,-1)--(1,-1,-1)--(1,-1,1)--(-1,-1,1)--(-1,-1,-1);
        \draw[inside](-1,-1,-1)--(1,-1,-1)--(1,1,-1)--(-1,1,-1)--(-1,-1,-1);
        \draw[inside](1,1,1)--(1,-1,1)--(1,-1,-1)--(1,1,-1)--(1,1,1);
        \draw[inside](1,1,1)--(-1,1,1)--(-1,1,-1)--(1,1,-1)--(1,1,1);
        \draw[inside](1,1,1)--(-1,1,1)--(-1,-1,1)--(1,-1,1)--(1,1,1);
        %% inside frame - this shouldn't be necessary...
        \draw[gray](-1,-1,-1)--(-1,1,-1)--(-1,1,1)--(-1,-1,1)--(-1,-1,-1);
        \draw[gray](-1,-1,-1)--(1,-1,-1)--(1,-1,1)--(-1,-1,1)--(-1,-1,-1);
        \draw[gray](-1,-1,-1)--(1,-1,-1)--(1,1,-1)--(-1,1,-1)--(-1,-1,-1);
        %%
        \draw[outside](-2,-2,-2)--(-2,2,-2)--(-2,2,2)--(-2,-2,2)--(-2,-2,-2);
        \draw[outside](-2,-2,-2)--(2,-2,-2)--(2,-2,2)--(-2,-2,2)--(-2,-2,-2);
        \draw[outside](-2,-2,-2)--(2,-2,-2)--(2,2,-2)--(-2,2,-2)--(-2,-2,-2);
        \draw[outside](2,2,2)--(2,-2,2)--(2,-2,-2)--(2,2,-2)--(2,2,2);
        \draw[outside](2,2,2)--(-2,2,2)--(-2,2,-2)--(2,2,-2)--(2,2,2);
        \draw[outside](2,2,2)--(-2,2,2)--(-2,-2,2)--(2,-2,2)--(2,2,2);
        \foreach \x in {-1,0,1} {
             \draw(\x,-2,2)node[below]{$\x$}--++(0,0,-0.2);
             \draw(-2,\x,2)node[left]{$\x$}--++(0.2,0,0);
             \draw(2,-2,\x)node[right]{$\x$}--++(-0.2,0,0);
        }
    \end{tikzpicture}

\end{document}

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