Skip to content

Instantly share code, notes, and snippets.

@arivero
Created August 19, 2025 10:46
Show Gist options
  • Select an option

  • Save arivero/d14b85fafae4e0ed26e399c784d052bb to your computer and use it in GitHub Desktop.

Select an option

Save arivero/d14b85fafae4e0ed26e399c784d052bb to your computer and use it in GitHub Desktop.
multiplicaciones para niños
\documentclass[12pt,a4paper]{article}
\usepackage[margin=0.8in]{geometry}
\usepackage{amsmath}
\usepackage{array}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}
% ---- unified N×M renderer (tight alignment) ----
\newcommand{\boxw}{0.8} % cm
\newcommand{\boxh}{1.0} % cm
% lengths / digits (pure TeX)
\newcommand{\NumberLen}[2]{%
\begingroup
\count0=#1\relax \ifnum\count0<0 \multiply\count0 by -1\fi
\count2=0\relax
\ifnum\count0=0 \count2=1\else
\loop \advance\count2 by 1 \divide\count0 by 10 \ifnum\count0>0 \repeat
\fi
\xdef#2{\the\count2}%
\endgroup
}
\newcommand{\ExtractDigit}[3]{%
\begingroup
\count20=#1\relax \count30=#2\relax \count22=1
\loop\ifnum\count30>0 \multiply\count22 by 10 \advance\count30 by -1\repeat
\divide\count20 by \count22
\count24=\count20 \divide\count24 by 10 \multiply\count24 by 10
\advance\count20 by -\count24
\xdef#3{\the\count20}%
\endgroup
}
% Draw N dotted boxes with the **right edge** at (X,Y), extending left
\newcommand{\PlaceBoxes}[3]{% N, X, Y (TikZ calc is loaded)
\pgfmathtruncatemacro{\N}{#1}
\ifnum\N>0
\pgfmathtruncatemacro{\NmOne}{\N-1}
\foreach \i in {0,...,\NmOne}{%
\draw[dotted,thick] ($(#2,#3)+(-\i,0)$) rectangle ++(-1,-1);
}%
\fi
}
% Unified problem macro (3rd arg ignored)
\newcommand{\multproblem}[2]{%
\begin{minipage}[t]{0.48\textwidth}
%we need to anchor the top left of this minipage:
\text{ }
\begin{center}
\begin{tikzpicture}[every node/.style={font=\Huge}, x=\boxw cm, y=\boxh cm]
\def\A{#1}\def\B{#2}%
\NumberLen{\A}{\alen}\NumberLen{\B}{\blen}%
\edef\RES{\number\numexpr \A*\B\relax}%
\NumberLen{\RES}{\slen}%
\count51=\alen\relax \count52=\blen\relax
\ifnum\count51>\count52 \edef\maxab{\alen}\else\edef\maxab{\blen}\fi
% --- Tunables for spacing (in box-height units) ---
\def\ruleY{-2.0} % y of the first rule
\def\gap{0.12} % tiny gap from rule to boxes
\def\rowskip{0.12} % tiny gap between rows of boxes
\def\sumbuffer{0.20} % tiny gap above the sum rule
% Top numbers, right-aligned to the result width
\node[anchor=east] at (\slen, 0.00) {#1};
\node[anchor=east] at (\slen, -1.20) {$\times$ #2};
% First rule: width = max(len A, len B)
\pgfmathtruncatemacro{\ruleStart}{\slen - \maxab}
\draw[thick] (\ruleStart, \ruleY) -- (\slen, \ruleY);
% Partial products (one row per digit of B, right->left)
\count50=\blen \advance\count50 by -1 \edef\lastk{\the\count50}%
\foreach \k in {0,...,\lastk}{%
\ExtractDigit{\B}{\k}{\dval}%
\ifnum\dval=0
\edef\lenk{1}% one “0” box
\else
\edef\PP{\number\numexpr \A*\dval\relax}%
\NumberLen{\PP}{\lenk}%
\fi
% top edge of this row: just under the rule, then step down per row
\pgfmathsetmacro{\rowY}{\ruleY - \gap - (\k)*(1.0+\rowskip)}
\pgfmathtruncatemacro{\rightx}{\slen - \k}
\PlaceBoxes{\lenk}{\rightx}{\rowY}
}
% Sum rule and final answer (width = len(A*B)), tightly aligned
\ifnum\blen>1\relax
\pgfmathsetmacro{\sumY}{\ruleY - \gap - (\blen)*(1.0+\rowskip) - \sumbuffer}
\draw[thick] (0,\sumY) -- (\slen,\sumY);
\pgfmathsetmacro{\ansY}{\sumY - \gap}
\PlaceBoxes{\slen}{\slen}{\ansY}
\fi
\end{tikzpicture}
\end{center}
\end{minipage}
}
% ============================= Document =============================
\begin{document}
%\Large\textbf{Multiplication Practice - Expanded Format}
\vspace{0.4cm}
\multproblem{23}{14}
\hfill
\multproblem{31}{22}
\vspace{1.7cm}
\multproblem{42}{13}
\hfill
\multproblem{123}{21}
\vspace{1.7cm}
\multproblem{142}{13}
\hfill
\multproblem{215}{321}
\newpage
\vspace{0.5cm}
\multproblem{234}{6} % 3×1 included here
\hfill
\multproblem{8}{567}
\vspace{3cm}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment