22

(a+b)^3

Someone posted it on math.stackexchange without code or source. It's an expanded form of (a+b)^3

I would like to write it in Latex, but am clueless as from where to even begin with or which package to use to accomplish this.

Black Mild
  • 17,569

2 Answers2

51

Welcome to TeX.SE!!!

Here is a possibility. For this you'll need TikZ package an its libraries 3d and perspective. What I'm proposing is to create two macros. The first one, simplecube draws a rectangular cuboid given its dimensions and its line styles. And the second one, cubeab draws the desired result using the first macro to draw the eight cuboids separated (or not), given the dimensions and the separation between them.

Something like this:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{3d}          % for 'canvas is...' options
\usetikzlibrary{perspective} % for '3d view' option

\tikzset { linea/.style={draw=red}, lineb/.style={draw=blue}, }

\newcommand{\simplecube}[7]% origin, dimension x, dimension y, dimension z, style x, style y, style z { \begin{scope}[shift={#1}] \fill[white ,canvas is xy plane at z=#4] (0,0) rectangle (#2,#3); \fill[gray!40,canvas is yz plane at x=#2] (0,0) rectangle (#3,#4); \fill[gray!10,canvas is xz plane at y=#3] (0,0) rectangle (#2,#4); \foreach\i/\j in {0/1, 1/1, 1/0} { \draw[line#5] (0,#3\i,#4\j) --++ (#2,0,0); \draw[line#6] (#2\i,0,#4\j) --++ (0,#3,0); \draw[line#7] (#2\i,#3\j,0) --++ (0,0,#4); } \end{scope} }

\newcommand{\cubeab}[4]% origin, a, b, separation { \begin{scope}[shift={#1}] \simplecube{(0 ,0 ,0 )}{#2}{#2}{#2}{a}{a}{a} \simplecube{(#2+#4,0 ,0 )}{#3}{#2}{#2}{b}{a}{a} \simplecube{(0 ,#2+#4,0 )}{#2}{#3}{#2}{a}{b}{a} \simplecube{(#2+#4,#2+#4,0 )}{#3}{#3}{#2}{b}{b}{a} \simplecube{(0 ,0 ,#2+#4)}{#2}{#2}{#3}{a}{a}{b} \simplecube{(#2+#4,0 ,#2+#4)}{#3}{#2}{#3}{b}{a}{b} \simplecube{(0 ,#2+#4,#2+#4)}{#2}{#3}{#3}{a}{b}{b} \simplecube{(#2+#4,#2+#4,#2+#4)}{#3}{#3}{#3}{b}{b}{b} \end{scope} }

\begin{document} \begin{tikzpicture}[3d view={115}{30},line cap=round,line join=round] \def\a{3.2} \def\b{1.2} \cubeab{(0,0,0)}{\a}{\b}{0} \cubeab{(0,9,0)}{\a}{\b}{1.5} \end{tikzpicture} \end{document}

enter image description here

Edit 1: With two figures, as requested.

\documentclass {article}
\usepackage    {lipsum}      % dummy text
\usepackage    {showframe}   % just for this example
\usepackage    {subcaption}
\usepackage    {tikz}
\usetikzlibrary{3d}          % for 'canvas is...' options
\usetikzlibrary{perspective} % for '3d view' option

\tikzset { linea/.style={draw=red}, lineb/.style={draw=blue}, }

\newcommand{\simplecube}[7]% origin, dimension x, dimension y, dimension z, style x, style y, style z { \begin{scope}[shift={#1}] \fill[white ,canvas is xy plane at z=#4] (0,0) rectangle (#2,#3); \fill[gray!40,canvas is yz plane at x=#2] (0,0) rectangle (#3,#4); \fill[gray!10,canvas is xz plane at y=#3] (0,0) rectangle (#2,#4); \foreach\i/\j in {0/1, 1/1, 1/0} { \draw[line#5] (0,#3\i,#4\j) --++ (#2,0,0); \draw[line#6] (#2\i,0,#4\j) --++ (0,#3,0); \draw[line#7] (#2\i,#3\j,0) --++ (0,0,#4); } \end{scope} }

\newcommand{\cubeab}[4]% origin, a, b, separation { \begin{scope}[shift={#1}] \simplecube{(0 ,0 ,0 )}{#2}{#2}{#2}{a}{a}{a} \simplecube{(#2+#4,0 ,0 )}{#3}{#2}{#2}{b}{a}{a} \simplecube{(0 ,#2+#4,0 )}{#2}{#3}{#2}{a}{b}{a} \simplecube{(#2+#4,#2+#4,0 )}{#3}{#3}{#2}{b}{b}{a} \simplecube{(0 ,0 ,#2+#4)}{#2}{#2}{#3}{a}{a}{b} \simplecube{(#2+#4,0 ,#2+#4)}{#3}{#2}{#3}{b}{a}{b} \simplecube{(0 ,#2+#4,#2+#4)}{#2}{#3}{#3}{a}{b}{b} \simplecube{(#2+#4,#2+#4,#2+#4)}{#3}{#3}{#3}{b}{b}{b} \end{scope} }

\begin{document} \lipsum[1]

\begin{figure}[h]\centering \def\a{3.2} \def\b{1.2} \begin{subfigure}[b]{0.45\textwidth}\centering % b = bottom alignment \begin{tikzpicture}[3d view={115}{30},line cap=round,line join=round,scale=0.5] \cubeab{(0,0,0)}{\a}{\b}{0} \end{tikzpicture} \caption{Subpicture 1, sep=0}\label{fig:figB} \end{subfigure} \begin{subfigure}[b]{0.45\textwidth}\centering % b = bottom alignment \begin{tikzpicture}[3d view={115}{30},line cap=round,line join=round,scale=0.5] \cubeab{(0,0,0)}{\a}{\b}{1.5} \end{tikzpicture} \caption{Subpicture 2, sep=1.5}\label{fig:figB} \end{subfigure} \caption{Pictures 1 and 2}\label{fig:figAB} \end{figure}

\lipsum[2] \end{document}

enter image description here

Edit 2: A beamer animation, just for fun.

\documentclass {beamer}
\usepackage    {tikz}
\usetikzlibrary{3d}          % for 'canvas is...' options
\usetikzlibrary{perspective} % for '3d view' option

\setbeamertemplate{navigation symbols}{}

\tikzset { linea/.style={draw=red}, lineb/.style={draw=blue}, }

\newcommand{\simplecube}[7]% origin, dimension x, dimension y, dimension z, style x, style y, style z { \begin{scope}[shift={#1}] \fill[white ,canvas is xy plane at z=#4] (0,0) rectangle (#2,#3); \fill[gray!40,canvas is yz plane at x=#2] (0,0) rectangle (#3,#4); \fill[gray!10,canvas is xz plane at y=#3] (0,0) rectangle (#2,#4); \foreach\i/\j in {0/1, 1/1, 1/0} { \draw[line#5] (0,#3\i,#4\j) --++ (#2,0,0); \draw[line#6] (#2\i,0,#4\j) --++ (0,#3,0); \draw[line#7] (#2\i,#3\j,0) --++ (0,0,#4); } \end{scope} }

\newcommand{\cubeab}[4]% origin, a, b, separation { \begin{scope}[shift={#1}] \simplecube{(0 ,0 ,0 )}{#2}{#2}{#2}{a}{a}{a} \simplecube{(#2+#4,0 ,0 )}{#3}{#2}{#2}{b}{a}{a} \simplecube{(0 ,#2+#4,0 )}{#2}{#3}{#2}{a}{b}{a} \simplecube{(#2+#4,#2+#4,0 )}{#3}{#3}{#2}{b}{b}{a} \simplecube{(0 ,0 ,#2+#4)}{#2}{#2}{#3}{a}{a}{b} \simplecube{(#2+#4,0 ,#2+#4)}{#3}{#2}{#3}{b}{a}{b} \simplecube{(0 ,#2+#4,#2+#4)}{#2}{#3}{#3}{a}{b}{b} \simplecube{(#2+#4,#2+#4,#2+#4)}{#3}{#3}{#3}{b}{b}{b} \end{scope} }

\begin{document} \begin{frame} \begin{figure}\centering \begin{tikzpicture}[3d view={115}{30},scale=0.75,line cap=round,line join=round] \def\a{3.2} \def\b{1.2} \foreach\i in {1,...,29} { \pgfmathsetmacro\j{15-int(abs(15-\i))} \only<\i> { \cubeab{(0,0,0)}{\a}{\b}{0.2*\j-0.2} } } \end{tikzpicture} \end{figure} \end{frame} \end{document}

enter image description here

Juan Castaño
  • 28,426
14

Update Here is an Asymptote solution to illustrate (a+b)^3 = a^3 + 3a^2b + 3ab^2 + b^3 with a crucial update on light (see its documentation). Without lights, for real 3D objects, some sides of the object will be dark. To overcome, one way is using opacity; however, we can put several light sources at different positions

light White=light(new pen[] {rgb(0.38,0.38,0.45),rgb(0.6,0.6,0.67),
rgb(0.5,0.5,0.57)},specularfactor=3,
  new triple[] {(5,5,5),(0,5,5),(-0.5,0,2)});

and turn them on

currentlight=White;

The number t is for shifting boxes.

enter image description here

// http://asymptote.ualberta.ca/
// To illustrate (a+b)^3 = a^3 + 3a^2b + 3ab^2 + b^3
unitsize(1cm);
import three;
currentprojection=orthographic(3,2,1,center=true,zoom=.8);
//currentprojection=orthographic(0,10,0,zoom=.8);
light White=light(new pen[] {rgb(0.38,0.38,0.45),rgb(0.6,0.6,0.67),
                             rgb(0.5,0.5,0.57)},specularfactor=3,
  new triple[] {(5,5,5),(0,5,5),(-0.5,0,2)});

currentlight=White;

real a=3.2, b=1.5; path3[] p=unitbox; surface q=unitcube; void mybox(triple A, triple B, pen fillpen=nullpen, pen drawpen=nullpen,triple shifting=O){ real s=(abs(B-A))/sqrt(3);
draw(shift(shifting)shift(A)scale3(s)q,fillpen+opacity(1)); draw(shift(shifting)box(A,B),drawpen); } triple A=(-a,-a,-a); // lower vertex triple B=(b,b,b); // upper vertex pen pena=lightyellow; // for a^3 pen penb=pink; // for b^3 pen pena2b=brown; // for 3 a^2 b pen penab2=darkcyan; // for 3 a b^2 real t=.6; // for shifting boxes

mybox(A,O,pena,pena); mybox(O,B,penb,penb,(t,t,t));

// 3 a^2 b draw(shift(t,-t,-t)box(O,(b,-a,-a)),pena2b); draw(shift(-t,-t,t)box(O,(-a,-a,b)),pena2b); draw(shift(-t,t,-t)*box(O,(-a,b,-a)),pena2b);

// 3 a b^2 draw(shift(t,t,-t)box(O,(b,b,-a)),penab2); draw(shift(-t,t,t)box(O,(-a,b,b)),penab2); draw(shift(t,-t,t)*box(O,(b,-a,b)),penab2);

With t=0 - no shifting

enter image description here

Here is an advantage of Asymptote: projection - with

currentprojection=orthographic(0,10,0,zoom=.8);

we get a projection of the figure illustrating 2D version, that is (a+b)^2 = a^2 + 2ab + b^2.

enter image description here

Black Mild
  • 17,569