# Drawing spheres

The question is simple: how to draw the next figue?

Of couse, I want to draw it using LaTeX libraries. I have a simple code for genearating a pdf version with a software bus I want to do the same with LaTeX.

Drawing circles is pretty easy but it would be better potitioning spheres in a 3d mesh. I know pgfplot can draw meshes.

It is straightforward to draw such things with asymptote.

\documentclass[border=2pt]{standalone}
\usepackage{asypictureB}
\begin{document}
\begin{asypicture}{name=AsyTest}
import graph3;
import solids;
size(8cm,6cm);
settings.render = 4;
currentprojection=orthographic(30,100,25);

material greensphere=material(  grey+green,       yellow,     black,       green);
material bluesphere=material(  grey+blue,       yellow,     black,       blue);

material m_eyeB=material(diffusepen=gray(0.001), emissivepen=gray(0.01), specularpen=gray(0.9),shininess=0.15);

for (int i=1; i <4; i+=1)
{
for (int j=1; j <4; j+=1)
{
for (int k=1; k <4; k+=1)
{
draw(surface(sphere(i*X+j*Y+k*Z,0.2)),surfacepen=greensphere);
}
draw(i*X+j*Y -- i*X+j*Y+3Z,gray);
draw(i*X+j*Z -- i*X+j*Z+3Y,gray);
draw(i*Y+j*Z -- i*Y+j*Z+3X,gray);
}
}

for (int i=1; i <3; i+=1)
{
for (int j=1; j <3; j+=1)
{
for (int k=1; k <3; k+=1)
{
draw(surface(sphere(0.5*(X+Y+Z)+i*X+j*Y+k*Z,0.3)),surfacepen=bluesphere);
}
}
}

draw(3X-2Y -- 4X-2Y,Arrow3, L=Label("$x_1$",position=EndPoint));
draw(3X-2Y -- 3X-Y,Arrow3, L=Label("$x_2$", position=EndPoint));
draw(3X-2Y -- 3X-2Y+Z,Arrow3, L=Label("$x_3$", position=EndPoint));

\end{asypicture}
\end{document}


It is also possible to draw this with TikZ but this has no 3D engine and one has to make sure to draw the objects in the "correct" order.

\documentclass[tikz,border=2pt]{standalone}
\begin{document}
\begin{tikzpicture}
% lattice
\foreach \X in {1,...,4}
{
\foreach \Y in {1,...,4}
{
\draw[gray] (-\X,-\Y,-1) -- (-\X,-\Y,-4);
\draw[gray] (-\X,-1,-\Y) -- (-\X,-4,-\Y);
\draw[gray] (-1,-\X,-\Y) -- (-4,-\X,-\Y);
}
}
% blue balls
\foreach \X in {1,2}
{
\foreach \Y in {1,2}
{
\foreach \Z in {1,2}
{
}
}
}
% green balls
\foreach \X in {1,...,3}
{
\foreach \Y in {1,...,3}
{
\foreach \Z in {1,...,3}
{

Note that this picture is not yet in perfect order, one would have to draw the green and blue balls one after the other. Note also that with tikz-3dplot you can rotate the view and so on more easily, but the fundamental problem of having to adjust things by hand will persist.