# how to represent method for finding LCM (least common multiple) in LaTeX

How can we draw the ladder method for LCM (least common multiple) in LaTeX, as shown in the figure below,

If possible it'd be nice to have different colours for different lines and numbers, as shown

Note: I'd prefer to stick to amsmath or mathtools if possible.

Here's a solution that employs a two-column array environment. (LuaLaTeX is needed only for the calculation of the LCM as the product of the individual factors.)

\documentclass{article}
\usepackage{colortbl}

\begin{document}
$\setlength\extrarowheight{2pt} \begin{array}{@{}l|l@{}} 2 & 48,72,108\\ \cline{2-2} 2 & 24,36,54\\ \cline{2-2} 3 & 12,18,27\\ \cline{2-2} \arrayrulecolor{red} \color{red}3 & 4,6,9\\ \cline{2-2} \color{red}2 & 4,2,3\\ \cline{2-2} \multicolumn{1}{c}{} & 2,1,3 \end{array}$
The \textsc{lcm} of $48$, $72$, and $108$ is the product
$\verb|2*2*3*3*2*2*1*3|=\directlua{tex.sprint(2*2*3*3*2*2*1*3)}$.
\end{document}

• No LuaLaTeX? ;-)
– user31729
Commented Dec 31, 2017 at 21:42
• @ChristianHupfer - Happy New Year to you too!! :-)
– Mico
Commented Dec 31, 2017 at 23:18
• Happy New Year!
– user31729
Commented Dec 31, 2017 at 23:49
• @ChristianHupfer - You made me do it: I added a LuaLaTeX angle...
– Mico
Commented Jan 1, 2018 at 3:44
• Sighhhhhhhhhhhhhhhhhh....
– user31729
Commented Jan 1, 2018 at 8:00

Most likely I do not understand your question correctly, but it seems to me that you want to color some elements of a table. If so, then you may start with this code.

\documentclass{article}
\usepackage{colortbl}
\usepackage[table]{xcolor}
\begin{document}
\begin{tabular}{l|rrr}
\rowcolor{red!40} 3  & 12 & 15 & 18\\
\hline
\rowcolor{blue!30} 2  & 4 & 5 & 6\\
\hline
&  2 & \textcolor{blue!70}{5} & 3
\end{tabular}
\end{document}


To produce something along the lines of your second picture, you may try

\documentclass{article}
\usepackage{colortbl}
\usepackage[table]{xcolor}
\begin{document}
\begin{tabular}{lrrr}
\textcolor{red}{3}  & \multicolumn{1}{!{\color{red}\vline}r}{12} & 15 & 18\\
\arrayrulecolor{red}\cline{2-4}
\textcolor{red}{2} &    \multicolumn{1}{!{\color{red}\vline}r}{4} & 5 & 6\\
\arrayrulecolor{red}\cline{2-4}
&   2 & 5 & 3
\end{tabular}
\end{document}


• I have edited the original post. Hope it is clear now. pls check the second image. Commented Dec 31, 2017 at 20:32