6

I am a novice TeX user, so please forgive any ignorance shown here. I am drafting a document for my students to explain the basic proofs of the Pythagorean theorem. I understand it is possible to produce the figure below using tikz, but I am not proficient enough to do it.

enter image description here

I am interested in reproducing the figure without the Pythagorean equation below it. I was also interested in changing colors. My aim is to create it myself so as not to plagiarize, and it also seems like a good skill to learn. Could someone please help me draw the figure using tikz? I thank all helpers.

5
13

It seems there is no plain TikZ yet. Here is my plain TikZ way. The code is quite lengthy but simple and highly customized (so you are free to customize ^^). I explain a bit:

  • \a, \b, \c are three sides,
  • pyth is the style for legendary;
  • Most of nodes in the picture is in midway position, so I put nodes={black,midway} in a scope;
  • Colors can be taken consistently for 2 parts of the whole figure.
  • The code for the left part is similar to the one for the right code. In each part, the code for each of right triangles are similar to each others. So, I just copy code of one triangle and make minor changes. The loop \foreach is not suitable in this situation. \fill[triangle1] (0,\a)--++(-90:\a) node[right]{$a$}--++(0:\b) node[above]{$b$}--cycle node[above]{$c$};

The structure of the code is as follows.

\documentclass[tikz,border=5mm]{standalone}
\usepackage{amsmath,amssymb}
\begin{document}
\begin{tikzpicture}[pyth/.style={magenta,scale=1.5}]
% 3 sides of a right triangle
\def\a{1.5}     
\def\b{3}
\pgfmathsetmacro{\c}{sqrt(\a*\a+\b*\b)}

% colors for 4 triangles
\colorlet{triangle1}{cyan}
\colorlet{triangle2}{green}
\colorlet{triangle3}{orange!50}
\colorlet{triangle4}{teal!50}
        
\begin{scope}[nodes={black,midway}]
\begin{scope}
% code for the left part
\end{scope}
            
\begin{scope}[shift={(\a+\b+1,0)}]
% code for the right part
\end{scope}
\end{scope}

% legendary     
\path (current bounding box.south)  node[below,pyth]{$\boldsymbol{a^2+b^2=c^2}$};
\end{tikzpicture}
\end{document}

enter image description here

Complete code

\documentclass[tikz,border=5mm]{standalone}
\usepackage{amsmath,amssymb}
\begin{document}
\begin{tikzpicture}[pyth/.style={magenta,scale=1.5}]
\def\a{1.5}     
\def\b{3}
\pgfmathsetmacro{\c}{sqrt(\a*\a+\b*\b)}
\colorlet{triangle1}{cyan}
\colorlet{triangle2}{green}
\colorlet{triangle3}{orange!50}
\colorlet{triangle4}{teal!50}

\begin{scope}[nodes={black,midway}]
% the left part
\begin{scope}
\fill[triangle1] (0,\a)
--++(-90:\a) node[right]{$a$}
--++(0:\b)   node[above]{$b$}
--cycle      node[above]{$c$};

\fill[triangle2] (\b,0)
--++(0:\a)  node[above]{$a$}
--++(90:\b) node[left]{$b$}
--cycle     node[left]{$c$};

\fill[triangle3] (\a+\b,\b)
--++(90:\a)  node[left]{$a$}
--++(180:\b) node[below]{$b$}
--cycle      node[below]{$c$};

\fill[triangle4] (\a,\a+\b)
--++(180:\a) node[below]{$a$}
--++(-90:\b) node[right]{$b$}
--cycle      node[right]{$c$};

\draw[thick] (0,0) rectangle (\a+\b,\a+\b) node[pyth]{$\boldsymbol{c^2}$};
\end{scope}

% the right part
\begin{scope}[shift={(\a+\b+1,0)}]
\fill[triangle1] (\a,\a)
--++(-90:\a) node[right]{$a$}
--++(0:\b)   node[above]{$b$}
--cycle;
    
\fill[triangle2] (0,\a)
--++(0:\a)  node[above]{$a$}
--++(90:\b) node[left]{$b$}
--cycle;
    
\fill[triangle3] (\a+\b,0)
--++(90:\a)  node[left]{$a$}
--++(180:\b) node[below]{$b$}
--cycle      node[right]{$c$};
    
\fill[triangle4] (\a,\a+\b)
--++(180:\a) node[below]{$a$}
--++(-90:\b) node[right]{$b$}
--cycle      node[above left]{$c$};
    
\draw[thick] (0,0) rectangle (\a+\b,\a+\b);
\path (0,0)
rectangle ++(\a,\a) node[pyth]{$\boldsymbol{a^2}$}
rectangle ++(\b,\b) node[pyth]{$\boldsymbol{b^2}$};
\end{scope}
\end{scope}

\path (current bounding box.south)  node[below,pyth]{$\boldsymbol{a^2+b^2=c^2}$};
\end{tikzpicture}
\end{document}
9

It also can be done easily with pstricks:

\documentclass[border=6pt]{standalone}
\usepackage[x11names,svgnames]{xcolor}%
\usepackage{pst-node}
\usepackage{relsize} 

\begin{document}

\begin{pspicture}(-3,-3)(11,3)
%\psset{PointSymbol=none, PointName=none}
\pnodes(-3,3){A}(3,3){B}(3,-3){C}(-3,-3){D}
\pnodes(-1,3){A1}(3,1){B1}(1,-3){C1}(-3,-1){D1}
\pnodes(5,3){E}(11,3){F}(11,-3){G}(5,-3){H}
\pnodes(7,3){E1}(11,-1){F1}(7,-3){G1}(7,-1){K}(5,-1){H1}
%%Coloured triangles
\psset{linecolor=LightSteelBlue2}
\pspolygon*(D1)(A)(A1) \pspolygon*(E)(E1)(H1)
\psset{linecolor=DeepSkyBlue3}
\pspolygon*(D1)(D)(C1)\pspolygon*(G)(G1)(K)
\psset{linecolor=DarkSeaGreen2}
\pspolygon*(A1)(B)(B1)\pspolygon*(G)(F1)(K)
\psset{linecolor=ForestGreen!90}
\pspolygon*(C1)(C)(B1)\pspolygon*(K)(E1)(H1)
\psset{linecolor=black}
\psframe(A)(C) \psframe(E)(G)
%% Labels
\psset{linestyle=none, labelsep=1pt}
\ncline{A1}{C1}\ncput{$\mathlarger{c^2}$}
\ncline{F}{K}\ncput{$\mathlarger{a^2}$}
\ncline{H}{K}\ncput{$\mathlarger{b^2}$}
\foreach \s/\t in {A/A1,B/B1,C/C1,D/D1, E/E1/, F1/G,G1/K, K/H1}{\ncline{\s}{\t}\nbput{$\scriptstyle a$}}
\foreach \s/\t in {D1/A, A1/B, B1/C, C1/D, H1/E, E1/K, K/F1,G/G1}{\ncline{\s}{\t}\nbput{$\scriptstyle b$}}
\foreach \s/\t in {D1/A1, A1/B1, B1/C1, C1/D1, E1/H1, K/G}{\ncline{\s}{\t}\nbput{$\scriptstyle c$}}
\end{pspicture}

\end{document}

enter image description here

7
  • thank you so much. When I try to use your code, it throws an error: option clash for package xcolor
    – kroner
    Aug 3 at 18:09
  • 1
    Did you load pstricks (or one package of the family) before loading xcolorwith its options?
    – Bernard
    Aug 3 at 18:13
  • thank you. I am not familiar with TeX packages, I just copied your code into my document and trying to understand how it works. Could you please help me make it work?
    – kroner
    Aug 3 at 18:16
  • 1
    I think so. Try not loading xcolor, and loading the colour options with the document class.
    – Bernard
    Aug 3 at 18:20
  • thank you, how would I do that?
    – kroner
    Aug 3 at 18:20
6

Just for fun, an animated (beamer) version.

\documentclass {beamer}
\usepackage    {tikz}
\usetikzlibrary{calc}

% beamer configuration
\setbeamertemplate{navigation symbols}{}
\usefonttheme     {serif}

\def\n{20}  % transitions (half)
\def\a{1.5}
\def\b{3}
\pgfmathsetmacro\l{\a+\b} % square lenght

\definecolor{color1}{HTML}{1F78B4}
\definecolor{color2}{HTML}{33A02C}
\definecolor{color3}{HTML}{B2DF8A}
\definecolor{color4}{HTML}{A6CEE3}

\tikzset
{%
  pics/triangle/.style={%
    code={%
      \fill[pic actions] (0,\a) |- (\b,0) -- cycle;
      \node[black] at (0.2,0.5*\a)            {\strut$a$};
      \node[black] at (0.5*\b,0.2)            {\strut$b$};
      \node[black] at (0.5*\b+0.1,0.5*\a+0.1) {\strut$c$};
    }},
}


\begin{document}
\begin{frame}{The Pythagorean theorem}
\begin{figure}\centering
\begin{tikzpicture}
% coordinates
\coordinate (A1) at (0,0);
\coordinate (A2) at (\l,0);
\coordinate (A3) at (\l,\l);
\coordinate (A4) at (0,\l);
\coordinate (B1) at (\a,0);
\coordinate (B2) at (\a,\a);
\coordinate (B3) at (\l,\a);
\coordinate (B4) at (0,\l);
% triangles
\foreach\i in{1,...,\n} \foreach\j in {1,...,4}
{%
  \pgfmathtruncatemacro\ii{2*\n+1-\i}
  \only<\i,\ii>
  {%
    \pic[rotate=90*\j-90,color\j] at ($(A\j)!{(\i-1)/(\n-1)}!(B\j)$) {triangle};
  }
}
% labels
\pgfmathtruncatemacro\na{\n-1}
\pgfmathtruncatemacro\nb{\n+1}
\pgfmathtruncatemacro\nc{2*\n-1}
\only<-2,\nc-> {\node at ($(A1)!0.5!(A3)$) {\LARGE$c^2$};}
\only<\na-\nb>
{%
  \node at (0.5*\a,0.5*\a)       {\LARGE$a^2$};
  \node at (\a+0.5*\b,\a+0.5*\b) {\LARGE$b^2$};
}
\node at (0.5*\l,-1) {\LARGE$c^2=a^2+b^2$};
% square
\draw (A1) rectangle (A3);
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}

enter image description here

4

Just be comparison with Asymptote!

Compile at http://asymptote.ualberta.ca/

unitsize(cm);

pair A=(0,0),B=(4,4);
real a=1,b=3;
defaultpen(fontsize(9pt));

// Define your triangles
picture triangle(pen color, real poslabelb=0.5, bool labelc=true)
{
  picture pic;
  fill(pic,(A+a*dir(90))--A--(A+b*dir(0))--cycle,color);
  label(pic,Label("$a$",LeftSide,Shift),(A+a*dir(90))--A);
  label(pic,Label("$b$",Relative(poslabelb),LeftSide,Shift),A--(A+b*dir(0)));
  label(pic,(labelc) ? Label("$c$",LeftSide,Shift) : "",(A+a*dir(90))--(A+b*dir(0)));
  return pic;
}

picture pic1,pic2;
pen qq=opacity(.8);

add(pic1,triangle(blue+qq));
add(pic1,rotate(90,0.5*(A+B))*triangle(green+qq));
add(pic1,rotate(180,0.5*(A+B))*triangle(red+qq));
add(pic1,rotate(270,0.5*(A+B))*triangle(gray+qq));
draw(pic1,box(A,B));
label(pic1,scale(1.5)*"$c^2$",0.5*(A+B));

pair vector=(A+b*dir(0))-(A+a*dir(90));

add(pic2,shift(a,0)*triangle(blue+qq,0.3,false));
add(pic2,shift(-vector)*rotate(90,0.5*(A+B))*triangle(green+qq));
add(pic2,shift(0,-b)*rotate(180,0.5*(A+B))*triangle(red+qq));
add(pic2,rotate(270,0.5*(A+B))*triangle(gray+qq,0.35,false));
draw(pic2,box(A,B));
label(pic2,scale(1.5)*"$a^2$",0.5*(A+(a,a)));
label(pic2,scale(1.5)*"$b^2$",0.5*((a,a)+B));

// Add two pictures to currentpicture (by default)
add(pic1);
add(shift(5,0)*pic2);
label(scale(1.5)*"$c^2=a^2+b^2$",(4.5,-.5));

shipout(bbox(2mm,Fill(pink+opacity(.5)))); // For background

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.