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.

I want to draw a cuboid in another transparent cuboid with TikZ. Therefore I've created a \newcommand to draw a cuboid.

\documentclass[11pt, halfparskip]{scrartcl}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}
\usepackage{tikz}

\begin{document}
\newcommand{\cuboid}[7]{
    % bottom
    \draw[#7] (#1, #2, #3) -- ++(#4, 0, 0) -- ++(0, 0, #6) -- ++(-#4, 0, 0) -- cycle;
    % back
    \draw[#7] (#1, #2, #3) -- ++(#4, 0, 0) -- ++(0, #5, 0) -- ++(-#4, 0, 0) -- cycle;
    % left
    \draw[#7] (#1, #2, #3) -- ++(0, 0, #6) -- ++(0, #5, 0) -- ++(0, 0, -#6) -- cycle;
    % right
    \draw[#7] (#1+#4, #2, #3) -- ++(0, 0, #6) -- ++(0, #5, 0) -- ++(0, 0, -#6) -- cycle;
    % front
    \draw[#7] (#1, #2, #3+#6) -- ++(#4, 0, 0) -- ++(0, #5, 0) -- ++(-#4, 0, 0) -- cycle;
    % top
    \draw[#7] (#1, #2+#5, #3) -- ++(#4, 0, 0) -- ++(0, 0, #6) -- ++(-#4, 0, 0) -- cycle;
}
\begin{tikzpicture}


\tikzstyle{bottomfill} = [thick, fill=lightgray, fill opacity=.5]
\tikzstyle{camerafill} = [thick, fill=blue!20]

\cuboid{0}{0}{0}{4}{4}{4}{bottomfill}
\cuboid{1}{1}{1}{1}{1}{1}{camerafill}

\draw[->, thick] (2, 1.5, 1.5) -- ++(4, 0, 0);

\end{tikzpicture}
\end{document}

But the result looks strange (The inner cuboid seems to be outside the transparent cuboid; compare the cuboid and and the arrow of the second image)

enter image description here

The result looks right if I'm adding the planes in the right z-order (but that's exhausting)

\documentclass[11pt, halfparskip]{scrartcl}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\tikzstyle{bottomfill} = [thick, fill=lightgray, fill opacity=.5]
\tikzstyle{camerafill} = [thick, fill=blue!20]

% 1 bottom
\draw[bottomfill] (0, 0, 0) -- ++(4, 0, 0) -- ++(0, 0, 4) -- ++(-4, 0, 0) -- cycle;
% 1 back
\draw[bottomfill] (0, 0, 0) -- ++(4, 0, 0) -- ++(0, 4, 0) -- ++(-4, 0, 0) -- cycle;
% 1 left
\draw[bottomfill] (0, 0, 0) -- ++(0, 0, 4) -- ++(0, 4, 0) -- ++(0, 0, -4) -- cycle;

% 2 bottom
\draw[camerafill] (1, 1, 1) -- ++(1, 0, 0) -- ++(0, 0, 1) -- ++(-1, 0, 0) -- cycle;
% 2 back
\draw[camerafill] (1, 1, 1) -- ++(1, 0, 0) -- ++(0, 1, 0) -- ++(-1, 0, 0) -- cycle;
% 2 left
\draw[camerafill] (1, 1, 1) -- ++(0, 0, 1) -- ++(0, 1, 0) -- ++(0, 0, -1) -- cycle;
% 2 right
\draw[camerafill] (1+1, 1, 1) -- ++(0, 0, 1) -- ++(0, 1, 0) -- ++(0, 0, -1) -- cycle;
% 2 front
\draw[camerafill] (1, 1, 1+1) -- ++(1, 0, 0) -- ++(0, 1, 0) -- ++(-1, 0, 0) -- cycle;
% 2 top
\draw[camerafill] (1, 1+1, 1) -- ++(1, 0, 0) -- ++(0, 0, 1) -- ++(-1, 0, 0) -- cycle;

\draw[thick] (2, 1.5, 1.5) -- ++(2, 0, 0);

% 1 right
\draw[bottomfill] (0+4, 0, 0) -- ++(0, 0, 4) -- ++(0, 4, 0) -- ++(0, 0, -4) -- cycle;
% 1 front
\draw[bottomfill] (0, 0, 0+4) -- ++(4, 0, 0) -- ++(0, 4, 0) -- ++(-4, 0, 0) -- cycle;
% 1 top
\draw[bottomfill] (0, 0+4, 0) -- ++(4, 0, 0) -- ++(0, 0, 4) -- ++(-4, 0, 0) -- cycle;

\draw[->, thick] (4, 1.5, 1.5) -- ++(2, 0, 0);
\end{tikzpicture}
\end{document}

With the result

enter image description here

My question: is there a possibility in TikZ to draw an object in a transparent object with without adding the shapes in the right z-order?

share|improve this question
    
Welcome to TeX.SX. Note that you don't have to sign with your name since it automatically appears in the lower right corner of your post. –  Claudio Fiandrino Mar 6 '13 at 14:26
    
Could you elaborate on what exactly "looks strange"? I don't see much of a difference between the output of your first code and that of your second code... –  Jubobs Mar 6 '13 at 17:46
    
I've updated the drawings and added an arrow to make it better visible. –  ClemensK Mar 6 '13 at 21:17
add comment

1 Answer

up vote 5 down vote accepted

What about an Asymptote solution?

\documentclass{standalone}
\usepackage{asymptote}

\begin{document}

\begin{asy}[width=8cm,height=8cm]
import three;

currentprojection=perspective(3,6.5,5);
currentlight=(3,7,8);

surface cube1 = scale3(1)*shift((-0.5,-0.5,-0.5))*unitcube;
surface cube2 = scale3(2)*shift((-0.5,-0.5,-0.5))*unitcube;

draw(cube1,lightgrey);
draw(cube2,lightblue+opacity(0.2));

\end{asy}
\end{document}

Compile with pdflatex file.tex, asy file-1.asy and a few more pdflatex.

enter image description here

share|improve this answer
    
Super, that's what I was looking for! Asymptote seems to have better 3d-capabilities than TikZ. There are also nice 3d-examples for TikZ on texample.net but they often use some tricks. –  ClemensK Mar 6 '13 at 20:55
add comment

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.