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 would like to achieve the following diagram (may be colored) wit Tikz. enter image description here
Here is what I have done till now:
i)Drawing a 3d cube with Tikz
ii)Get the Molecular layer from here
Here is my code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\cubex}{4}
\pgfmathsetmacro{\cubey}{4}
\pgfmathsetmacro{\cubez}{4}
\draw (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}
\\[1cm]
\begin{tikzpicture}
  \def\nuPi{3.1459265}
  \foreach \i in {11,10,...,0}{% This one doesn't matter
    \foreach \j in {5,4,...,0}{% This will crate a membrane
                               % with the front lipids visible
      % top layer
      \pgfmathsetmacro{\dx}{rand*0.1}% A random variance in the x coordinate
      \pgfmathsetmacro{\dy}{rand*0.1}% A random variance in the y coordinate,
                                     % gives a hight fill to the lipid
      \pgfmathsetmacro{\rot}{rand*0.1}% A random variance in the
                                      % molecule orientation
      \shade[ball color=red] ({\i+\dx+\rot},{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)}) circle(0.45);
      \shade[ball color=gray] (\i+\dx,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-0.9}) circle(0.45);
      \shade[ball color=gray] (\i+\dx-\rot,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-1.8}) circle(0.45);
      % bottom layer
      \pgfmathsetmacro{\dx}{rand*0.1}
      \pgfmathsetmacro{\dy}{rand*0.1}
      \pgfmathsetmacro{\rot}{rand*0.1}
      \shade[ball color=gray] (\i+\dx+\rot,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-2.8}) circle(0.45);
      \shade[ball color=gray] (\i+\dx,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-3.7}) circle(0.45);
      \shade[ball color=red] (\i+\dx-\rot,{0.5*\j+\dy+0.4*sin(\i*\nuPi*10)-4.6}) circle(0.45);
    }
  }
\end{tikzpicture}
\end{document} 

enter image description here

How do I fix the layer onto the cube is my ultimate question and drawing the inner cube as in the diagram (preferably with axes)

share|improve this question
1  
Frankly, you'd better use asymptote for that kind of stuff. –  cjorssen May 12 at 7:23
1  
Given the example at the top of your post and the two pictures at the bottom, it is not clear to me what you try to achieve. –  cjorssen May 12 at 7:25
    
@cjorssen, I want to draw the diagram as in the first figure . But I am able to draw the cube and the layer separately. How do I merge these two? –  subham soni May 12 at 7:26
    
@subhamsoni: you can try using the overlay option for the cube. –  Claudio Fiandrino May 12 at 7:27
    
@cjorssen but the layer isn't structured –  subham soni May 12 at 7:30
add comment

3 Answers 3

up vote 5 down vote accepted

I tried to exclude complex bits of code and maths. This is the brute force attack.

\documentclass{article}
\usepackage{tikz}
%\usetikzlibrary{calc,fadings,decorations.pathreplacing}

\tikzset{My Line Style/.style={ultra thick, blue, fill=yellow!10, fill opacity=0.5,join=round}}

\begin{document}
\begin{tikzpicture}[rotate around x=5] 
    \foreach \x in {0,1,2,3,4,5,6}{% 
      \foreach \z in {0,1,2,3,4,5,6}{%      
      \shade[ball color=gray] (\x,0,\z) circle(0.5);
      }
    }
    \foreach \x in {0.5,1.5,2.5,3.5,4.5,5.5}{%
      \foreach \z in {0,1,2,3,4,5,6}{%
      \shade[ball color=gray] (\x,{sqrt(0.74)},\z) circle(0.5);
      }
    }
    \foreach \x in {0,1,2,3,4,5,6}{%
      \foreach \z in {0,1,2,3,4,5,6}{%
      \shade[ball color=gray] (\x,{2*sqrt(0.74)},\z) circle(0.5);
      }
    }
    \foreach \x in {0.5,1.5,2.5,3.5,4.5,5.5}{%
      \foreach \z in {0,1,2,3,4,5,6}{%
      \shade[ball color=gray] (\x,{3*sqrt(0.74)},\z) circle(0.5);
      }
    }
    \foreach \x in {0,1,2,3,4,5,6}{%
      \foreach \z in {0,1,2,3,4,5,6}{%
      \shade[ball color=gray] (\x,{4*sqrt(0.74)},\z) circle(0.5);
      }
    }
    \foreach \x in {0.5,1.5,2.5,3.5,4.5,5.5}{%
      \foreach \z in {0,1,2,3,4,5,6}{%
      \shade[ball color=gray] (\x,{5*sqrt(0.74)},\z) circle(0.5);
      }
    }
    \foreach \x in {0,1,2,3,4,5,6}{%
      \foreach \z in {0,1,2,3,4,5,6}{%
      \shade[ball color=gray] (\x,{6*sqrt(0.74)},\z) circle(0.5);
      }
    }

    \draw [My Line Style] (6,{4*sqrt(0.74)},6) -- (4,{4*sqrt(0.74)},6) -- (4,{6*sqrt(0.74)},6) -- 
                          (6,{6*sqrt(0.74)},6) -- cycle;
    \draw [My Line Style] (4,{6*sqrt(0.74)},6) -- (4,{6*sqrt(0.74)},4) -- (6,{6*sqrt(0.74)},4) --
                          (6,{6*sqrt(0.74)},6) -- cycle;
    \draw [My Line Style] (6,{4*sqrt(0.74)},6) -- (6,{4*sqrt(0.74)},4) -- (6,{6*sqrt(0.74)},4) --
                          (6,{6*sqrt(0.74)},6) -- cycle;
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
add comment

You can simply move the drawing of the cube into the same tikzpicture as the molecules. Also, I would not recommend using a random number generator to place the molecules since you want to draw over them. Instead place them on a grid to get the crystal structure.

Below is something that should allow you to achieve what you want by tweaking the parameters:

enter image description here

Notes:

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.pathreplacing}

\tikzset{My Line Style/.style={ultra thick, blue, fill=yellow!10, fill opacity=0.5, join=round}}

\begin{document}
\begin{tikzpicture}
    \newcommand*{\Radius}{0.45}%
    \newcommand*{\ZFactor}{1.5*\Radius}%
    \newcommand*{\YFactor}{2.0*\Radius}%
    \newcommand*{\XFactor}{2.0*\Radius}%

    \foreach \z in {0,...,4} {
      \pgfmathsetmacro{\ZCoord}{\ZFactor*\z}
      \foreach \x in {0,...,10} {
          \pgfmathsetmacro{\XCoord}{\YFactor*\x}
          \foreach \y in {0,...,6} {
              \pgfmathsetmacro{\YCoord}{\XFactor*\y}
              \shade[ball color=gray!20] (\XCoord,\YCoord,\ZCoord) circle(\Radius);
          }
      }
  }

  %% Select the ones to highlight
    \foreach \z in {2,...,4} {
      \pgfmathsetmacro{\ZCoord}{\ZFactor*\z}
      \foreach \x in {4,...,6} {
          \pgfmathsetmacro{\XCoord}{\YFactor*\x}
          \foreach \y in {3,...,5} {
              \pgfmathsetmacro{\YCoord}{\XFactor*\y}
              \shade[ball color=red!40] (\XCoord,\YCoord,\ZCoord) circle(\Radius);
          }
      }
  }


  %% Draw the cube
  \begin{scope}[shift={(3.5*\XFactor,2.5*\YFactor,-4*\ZFactor)}]%xshift=4*\Radius cm, yshift=4*\Radius]
        \pgfmathsetmacro{\cubex}{2*\XFactor}
        \pgfmathsetmacro{\cubey}{2*\YFactor}
        \pgfmathsetmacro{\cubez}{3*\ZFactor}
        \draw [My Line Style] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
        \draw [My Line Style] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
        \draw [My Line Style] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
    \end{scope}
\end{tikzpicture}
\end{document} 
share|improve this answer
    
But the shape is not a perfect cube :( –  subham soni May 12 at 7:38
    
Huh? It is a cube. If you mean to say that it does not align with the molecules, then you just need to adjust your coordinates. –  Peter Grill May 12 at 7:41
    
No @Peter Grill. See the picture. The molecules form a perfect cube. –  subham soni May 12 at 7:43
1  
@subhamsoni: actually Peter is telling you that he provided you an example: if you adjust the coordinates of the cube you will be able to align with the molecules. Otherwise you have to change way to draw molecules.. –  Claudio Fiandrino May 12 at 7:53
    
@ClaudioFiandrino got it –  subham soni May 12 at 8:18
show 1 more comment

Another answer, which IMHO reflects more accurately the crystal structure.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.pathreplacing}
\begin{document}

% You can tweak these
\colorlet{odd plane}{black!20}
\colorlet{even plane}{black!20}
\def\ballradius{0.45}


\def\DrawRow#1#2{
    \foreach \x in {0,...,#2}
       \shade[ball color=ball] ($(#1) +(\x, 0,0)$) circle(\ballradius);
}
\def\DrawOddPlane#1{ 
  \pgfmathsetmacro{\aux}{#1-1}
  \colorlet{ball}{odd plane}
  \foreach \z in {0,...,#1} {
      \DrawRow{0,0,\z}{#1}
      \if\z#1\relax\else
      \DrawRow{0.5,0,\z+0.5}{\aux}
      \fi
  }
}
\def\DrawEvenPlane#1{ 
  \pgfmathsetmacro{\aux}{#1-1}
  \colorlet{ball}{even plane}
  \foreach \z in {0,...,#1} {
      \DrawRow{0.5,0,\z}{\aux}
      \if\z#1\relax\else
      \DrawRow{0,0,\z+0.5}{#1}
      \fi
  }
}

\begin{tikzpicture}
   \foreach \y in {0,...,3} {
      \begin{scope}[yshift=\y cm]
          \DrawOddPlane{3}
      \end{scope}
      \if\y3\relax\else
      \begin{scope}[yshift=\y cm + 0.5cm]
          \DrawEvenPlane{3}
      \end{scope}
      \fi
  }
    \pgfmathsetmacro{\cubex}{1}
    \pgfmathsetmacro{\cubey}{1}
    \pgfmathsetmacro{\cubez}{1}
    \draw (3,3,3) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
    \draw (3,3,3) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
    \draw (3,3,3) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}
\end{document} 

Produces:

Result

You can tweak the color of the balls for even and odd planes, and the ball radius, for example:

% You can tweak these
\colorlet{odd plane}{black!20}
\colorlet{even plane}{green!20}
\def\ballradius{0.3}

Result:

Result2

share|improve this answer
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.