Claude helped me make a long division worksheet generator using LuaLaTex

 I spent about 45 minutes with Claude and some manual editing and ended up with the following file that can be compiled with LuaLaTex to produce 10 pages of long division fact problems.  There are variables at the top of the page to allow you to specify the fact ranges you want.


% ============================================================
%  Long Division Worksheet — 10 Random Pages
%  Compile with: lualatex long_division_random.tex
% ============================================================
\documentclass[10pt]{article}
\usepackage[margin=0.5in, top=0.55in, bottom=0.5in]{geometry}
\usepackage{longdivision}
\usepackage{array}
\usepackage{pgfmath}
\usepackage{pgffor}    % provides \foreach

% ============================================================
%  USER SETTINGS — edit these four values
% ============================================================
\newcommand{\MinQuotient}{2}    % smallest possible dividend
\newcommand{\MaxQuotient}{12}   % largest  possible dividend
\newcommand{\MinDivisor}{2}      % smallest possible divisor
\newcommand{\MaxDivisor}{12}     % largest  possible divisor
\newcommand{\NumPages}{10}       % number of worksheet pages
% ============================================================

% ---- Lua: generate NumPages*50 unique evenly-divisible problems.
% Each problem stored as \dvdP_N and \dvsP_N  (page P, problem N).
\directlua{
  math.randomseed(os.time())

  local minQuo  = tonumber("\MinQuotient")
  local maxQuo  = tonumber("\MaxQuotient")
  local minDvs  = tonumber("\MinDivisor")
  local maxDvs  = tonumber("\MaxDivisor")
  local npages  = tonumber("\NumPages")

  for p = 1, npages do
    for n = 1, 50 do
      local dvs  = math.random(minDvs, maxDvs)
local quo = math.random(minQuo, maxQuo)
      local dvd = dvs * quo
      tex.sprint("\\expandafter\\newcommand\\csname dvd" .. p .. "x" .. n .. "\\endcsname{" .. dvd .. "}")
      tex.sprint("\\expandafter\\newcommand\\csname dvs" .. p .. "x" .. n .. "\\endcsname{" .. dvs .. "}")
    end
  end
}

% ---- Accessors: #1=page, #2=problem index
\newcommand{\getQuotient}[2]{\csname dvd#1x#2\endcsname}
\newcommand{\getDivisor}[2] {\csname dvs#1x#2\endcsname}

% ---- Worksheet page macro (argument = page number)
\newcommand{\worksheetpage}[1]{%
  \begin{center}
    {\large \textbf{Long Division Practice}} \quad {\small (Sheet #1 of \NumPages)}\\[3pt]
    {Divisors: \MinDivisor--\MaxDivisor}\\[5pt]
    Name:\;\underline{\hspace{5.5cm}}\quad
    Date:\;\underline{\hspace{3cm}}\quad
    Score:\;\underline{\hspace{1.8cm}}
  \end{center}
  \vspace{0.15cm}
  \begin{center}
  % 5 columns x 10 rows = 50 problems per page
  \foreach \r in {1,...,10}{%
    \pgfmathtruncatemacro{\na}{(\r-1)*5+1}%
    \pgfmathtruncatemacro{\nb}{(\r-1)*5+2}%
    \pgfmathtruncatemacro{\nc}{(\r-1)*5+3}%
    \pgfmathtruncatemacro{\nd}{(\r-1)*5+4}%
    \pgfmathtruncatemacro{\ne}{(\r-1)*5+5}%
    \begin{minipage}[t]{0.10\textwidth}\centering\vspace{0pt}%
      \longdivision[stage=0]{\getQuotient{#1}{\na}}{\getDivisor{#1}{\na}}\end{minipage}\hfill%
    \begin{minipage}[t]{0.10\textwidth}\centering\vspace{0pt}%
      \longdivision[stage=0]{\getQuotient{#1}{\nb}}{\getDivisor{#1}{\nb}}\end{minipage}\hfill%
    \begin{minipage}[t]{0.10\textwidth}\centering\vspace{0pt}%
      \longdivision[stage=0]{\getQuotient{#1}{\nc}}{\getDivisor{#1}{\nc}}\end{minipage}\hfill%
    \begin{minipage}[t]{0.10\textwidth}\centering\vspace{0pt}%
      \longdivision[stage=0]{\getQuotient{#1}{\nd}}{\getDivisor{#1}{\nd}}\end{minipage}\hfill%
    \begin{minipage}[t]{0.10\textwidth}\centering\vspace{0pt}%
      \longdivision[stage=0]{\getQuotient{#1}{\ne}}{\getDivisor{#1}{\ne}}\end{minipage}\\[0.8cm]%
  }
  \end{center}
}

% ---- Answer key page macro (argument = page number)
\newcommand{\answerkeypage}[1]{%
  \begin{center}
    {\large \textbf{Answer Key}} \quad {\small (Sheet #1 of \NumPages)}\\[3pt]
    {\small Quotients: \MinDividend--\MaxDividend \quad Divisors: \MinDivisor--\MaxDivisor}
  \end{center}
  \vspace{0.15cm}
  \begin{center}
  \foreach \r in {1,...,10}{%
    \pgfmathtruncatemacro{\na}{(\r-1)*5+1}%
    \pgfmathtruncatemacro{\nb}{(\r-1)*5+2}%
    \pgfmathtruncatemacro{\nc}{(\r-1)*5+3}%
    \pgfmathtruncatemacro{\nd}{(\r-1)*5+4}%
    \pgfmathtruncatemacro{\ne}{(\r-1)*5+5}%
    \begin{minipage}[t]{0.10\textwidth}\centering\vspace{0pt}%
      \longdivision{\getQuotient{#1}{\na}}{\getDivisor{#1}{\na}}\end{minipage}\hfill%
    \begin{minipage}[t]{0.10\textwidth}\centering\vspace{0pt}%
      \longdivision{\getQuotient{#1}{\nb}}{\getDivisor{#1}{\nb}}\end{minipage}\hfill%
    \begin{minipage}[t]{0.10\textwidth}\centering\vspace{0pt}%
      \longdivision{\getQuotient{#1}{\nc}}{\getDivisor{#1}{\nc}}\end{minipage}\hfill%
    \begin{minipage}[t]{0.10\textwidth}\centering\vspace{0pt}%
      \longdivision{\getQuotient{#1}{\nd}}{\getDivisor{#1}{\nd}}\end{minipage}\hfill%
    \begin{minipage}[t]{0.10\textwidth}\centering\vspace{0pt}%
      \longdivision{\getQuotient{#1}{\ne}}{\getDivisor{#1}{\ne}}\end{minipage}\\[1.3cm]%
  }
  \end{center}
}

\pagestyle{empty}

\begin{document}

% ---- Emit all worksheet pages, then all answer key pages ----
\foreach \p in {1,...,\NumPages}{%
  \worksheetpage{\p}%
  \newpage
}

%\foreach \p in {1,...,\NumPages}{%
%  \answerkeypage{\p}%
%  \ifnum\p<\NumPages\newpage\fi
%}

\end{document}

Comments

Popular posts from this blog

Transparent xmonad

Convert WebVTT file to SRT format (VTT to SRT)