I've been trying to draw these squares using Inkspace, but I'm having trouble exporting it to LaTex. Now I want to draw it using Tikz but I'm learning Tikz. Does anyone have an idea how to draw it?
5 Answers
Without calculating the positioning:
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[very thick]
% Right square
\node[fill=lightgray!50,draw=black, minimum size=2cm, inner sep=0pt] (as) {$a^2$};
\node[draw=black,minimum width=2cm, minimum height=1cm, inner sep=0pt, above=-\pgflinewidth of as] (abh) {$ab$};
\node[draw=black,minimum width=1cm, minimum height=2cm, inner sep=0pt, right=-\pgflinewidth of as] (abv) {$ab$};
\node[fill=blue!20,draw=blue, minimum size=1cm, inner sep=0pt, above right=-\pgflinewidth and -\pgflinewidth of as] {$b^2$};
% Side labels
\node[anchor=east] at (as.west) {$a$};
\node[anchor=north] at (as.south) {$a$};
\node[anchor=east] at (abh.west) {$b$};
\node[anchor=north] at (abv.south) {$b$};
% Square label
\node[xshift=.5cm, above=1.2cm of as] {$(a+b)^2=a^2+b^2+2ab$};
% Left square
\node[fill=lightgray!50,draw=black, minimum size=2cm, inner sep=0pt, right=2cm of as] (asq) {$a^2$};
\node[fill=blue!20,draw=blue, minimum size=1cm, inner sep=0pt, above right=-\pgflinewidth and -\pgflinewidth of asq] (bsq) {$b^2$};
% Side labels
\node[anchor=east] at (asq.west) {$a$};
\node[anchor=north] at (asq.south) {$a$};
\node[anchor=east] at (bsq.west) {$b$};
\node[anchor=north] at (bsq.south) {$b$};
% Square label
\node[xshift=.5cm, above=1.2cm of asq] {$a^2+b^2$};
\end{tikzpicture}
\end{document}
With defined node styles and using the calc
library and node labels:
\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,
positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 0pt,
gN/.style = {% gray Node
draw, fill=gray!30, minimum size=22mm,
inner sep=0pt, outer sep=0pt,
node contents={$a^2$}},
bN/.style = {% blue Node
draw=blue, fill=blue!30, minimum size=11mm,
inner sep=0pt, outer sep=0pt,
node contents={$b^2$}},
hN/.style = {draw, minimum height=11mm, minimum width=22mm,
inner sep=0pt, outer sep=0pt,
node contents={$ba$}},
vN/.style = {draw, minimum height=22mm, minimum width=11mm,
inner sep=0pt, outer sep=0pt,
node contents={$ab$}},
]
\node (a) [gN, label=left:$a$, label=below:$a$];
\node (ba) [hN,above=of a, label=left:$b$];
\node (ab) [vN,right=of a, label=below:$b$];
\node (b) [bN,above=of ab];
\node[above=2mm of {$(ba.north west)!0.5!(b.north east)$}] {$(a+b)^2=a^2+b^2+2ab$};
\scoped[xshift=44mm]
{
\node (a) [gN, label=left:$a$, label=below:$a$];
\node (b) [bN,above right=of a,
label=left:$b$, label=below:$b$];
\node[above=2mm of {$(a.west |- b.north)!0.5!(b.north east)$}] {$a^2+b^2$};
}
\end{tikzpicture}
\end{document}
A quick try with MetaPost, for whom it may interest, included in a LuaLaTeX program:
\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
beginfig(1);
a = 2.75cm; c = a+b = 1.4a;
path first_square, second_square;
first_square = origin -- (a,0) -- (a,a) -- (0,a) -- cycle;
second_square = (a,a) -- (c,a) -- (c,c) -- (a,c) --- cycle;
fill first_square withcolor .8white;
draw first_square;
label("$a^2$", center first_square);
fill second_square withcolor .25[white,blue];
draw second_square withcolor blue;
label("$b^2$", center second_square);
label.bot("$a$", (.5a,0)); label.lft("$a$", (0,.5a));
picture pict; pict=currentpicture;
draw (a,0) -- (c,0) -- (c,a);
draw (0,a) -- (0,c) -- (a,c);
for M = (a+.5b,.5a), (.5a,a+.5b):
label("$ab$", M);
endfor;
label.bot("$b$", (a+.5b,0));
label.lft("$b$", (0,a+.5b));
labeloffset:= 8bp;
label.top("$(a+b)^2 = a^2 + 2ab + b^2$", c*(.5,1));
draw image(draw pict;
labeloffset := 3bp;
label.bot("$b$", (a+.5b,a));
label.lft("$b$", (a,a+.5b));
labeloffset:= 8bp;
label.top("$a^2+b^2$", c*(.5,1)))
shifted (1.4c,0);
endfig;
\end{mplibcode}
\end{document}
tkz-euclide
is now a tool dedicated to Euclidean geometry but it is possible to divert it from its main objective. Here the advantage is that once the fixed points are given, no more coordinates are used, then the nodes are determined for the first figure and can be reused for the second. I used a few lines of TikZ
to place the labels correctly.
\documentclass{standalone}
\usepackage{tkz-euclide}
\tkzSetUpLine[thick]
\begin{document}
\begin{tikzpicture}
\tkzDefPoints{0/0/A,6/0/B,4/0/E}
\tkzDefSquare(A,E)\tkzGetPoints{F}{G}
\tkzDefSquare(A,B)\tkzGetPoints{C}{D}
\tkzDefPointBy[projection= onto B--C](F) \tkzGetPoint{I}
\tkzDefPointBy[projection= onto C--D](F) \tkzGetPoint{J}
\tkzFillPolygon[gray!30,opacity=.3](A,E,F,G)
\tkzFillPolygon[blue!30,opacity=.3](F,I,C,J)
\tkzDrawPolygon(A,B,C,D)
\tkzDrawPolygon(A,E,F,G)
\tkzDrawPolygon[blue](F,I,C,J)
\path (E)--node{$a^2$} (G) ;
\path (F)--node{$b^2$} (C) ;
\path (F)--node{$ba$} (D) ;
\path (F)--node{$ab$} (B) ;
\tkzLabelSegment[below](A,E){$a$}
\tkzLabelSegment[below](E,B){$b$}
\tkzLabelSegment[left](A,G){$a$}
\tkzLabelSegment[left](G,D){$b$}
\tkzLabelSegment[above](D,C){$(a+b)^2=a^2+b^2+2ab$}
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}[scale=1,line width=1pt]
\tkzFillPolygon[gray!30,opacity=.3](A,E,F,G)
\tkzFillPolygon[blue!30,opacity=.3](F,I,C,J)
\tkzDrawPolygon(A,E,F,G)
\tkzDrawPolygon[blue](F,I,C,J)
\path (E)--node{$a^2$} (G) ;
\path (F)--node{$b^2$} (C) ;
\tkzLabelSegment[below](A,E){$a$}
\tkzLabelSegment[below](F,I){$b$}
\tkzLabelSegment[left](A,G){$a$}
\tkzLabelSegment[left](F,J){$b$}
\tkzLabelSegment[above](D,C){$a^2+b^2$}
\end{tikzpicture}
\end{document}
Not Ideal, could be improved:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[very thick]
\fill [lightgray!50,draw=black] (0,0) rectangle (4,4) ;
\fill [white,draw=black] (0,4) rectangle (4,5);
\fill [blue!20,draw=blue] (4,4) rectangle (5,5);
\fill [white,draw=black] (4,0) rectangle (5,4);
\fill [lightgray!50,draw=black] (8,0) rectangle (12,4) ;
\fill [blue!20,draw=blue] (12,4) rectangle (13,5);
\node at (2,2) {$a^2$};
\node at (-0.5,2) {$a$};
\node at (-0.5,4.5) {$b$};
\node at (2,4.5) {$ab$};
\node at (4.5,2) {$ab$};
\node at (4.5,4.5) {$b^2$};
\node at (2,-0.5) {$a$};
\node at (4.5,-0.5) {$b$};
\node at (2.5,6) {$(a+b)^2=a^2+b^2+2ab$};
\node at (7.5,2) {$a$};
\node at (10,-0.5) {$a$};
\node at (10,2) {$a^2$};
\node at (12.5,4.5) {$b^2$};
\node at (11.5,4.5) {$b$};
\node at (12.5,3.5) {$b$};
\node at (10.5,6) {$a^2+b^2$};
\end{tikzpicture}
\end{document}
.pdf_tex
file frequently contains the last\includegraphics
command which tries to include a non-existing page. It’s a known bug and what you have to do is to open that file and remove the offending\includegraphics
command(s).