TikZ-PGF – Reproducing a Figure from Wikipedia’s Entry on Pythagorean Theorem

drawtikz-pgf

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.

Best Answer

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}