branch: externals/org-mathsheet commit c3b9570c2bd3756111946a4debda724527f92085 Author: Ian Martins <ia...@jhu.edu> Commit: Ian Martins <ia...@jhu.edu>
Use latex math formatting, allow exponent and sqrt --- example.org | 21 +++++++++++---------- mathsheet.org | 41 +++++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/example.org b/example.org index 728cdeaa43..3abbe060bd 100644 --- a/example.org +++ b/example.org @@ -50,16 +50,17 @@ |--------+-------+------------------------------------+--------| | 3 | 1 | x / ([2..4] + [a=0..5]) = [$a..10] | simple | | 3 | 2 | [$a*[2..10]] / x = [a=1,2,4] | simple | +| 1 | 3 | x^2 = sqrt([16 - $a] + [a=1..5]) | simple | #+BEGIN: problem-set :templates "algebra-1" :count 8 :instruction "Solve for x" -| problem | answer | -|-----------------+--------| -| x / (3 + 1) = 1 | x = 4 | -| x / (3 + 0) = 5 | x = 15 | -| x / (2 + 4) = 5 | x = 30 | -| x / (2 + 1) = 6 | x = 18 | -| 16 / x = 2 | x = 8 | -| 2 / x = 1 | x = 2 | -| 9 / x = 1 | x = 9 | -| 8 / x = 4 | x = 2 | +| problem | answer | +|--------------------+--------| +| x / (3 + 0) = 5 | x = 15 | +| x / (3 + 4) = 9 | x = 63 | +| x / (3 + 0) = 4 | x = 12 | +| 12 / x = 2 | x = 6 | +| 8 / x = 2 | x = 4 | +| 36 / x = 4 | x = 9 | +| 6 / x = 2 | x = 3 | +| x^2 = sqrt(13 + 3) | x = 2 | #+END: diff --git a/mathsheet.org b/mathsheet.org index 727252fd4b..3f18868811 100644 --- a/mathsheet.org +++ b/mathsheet.org @@ -1,9 +1,7 @@ * Goal The goal is to generate a math practice sheet made up of dynamic -problems that are defined based on flexible templates. The problem -distribution and order is also configurable. - -Similar to https://www.math-aids.com. +problems that are defined based on a set of flexible templates. The +problem distribution and order should also be configurable. * Problem Templates ** Overview This section contains some example templates. Each table defines a @@ -46,6 +44,11 @@ that generates the worksheet. Only the first three columns are used. * Code walkthrough ** Problem generation +*** Header +#+begin_src elisp :tangle mathsheet.el +;;; mathsheet.el --- Generate dynamic math worksheets -*- lexical-binding:t -*- +#+end_src + *** Dependencies This package needs [[https://elpa.gnu.org/packages/peg.html][peg]]. @@ -115,7 +118,7 @@ new field to the list when we close the current field. alg-vars) (with-peg-rules - ((stuff (* (or asn-var alg-var digit symbol field space))) + ((stuff (* (or asn-var math-func alg-var digit symbol field space))) (field open (opt assignment) stuff close) (space (* [space])) (open (region "[") @@ -149,9 +152,10 @@ new field to the list when we close the current field. (push (caar open-fields) (cadadr open-fields))) (push (pop open-fields) closed-fields) "."))) + (math-func (or "sqrt")) (letter [a-z]) (digit [0-9]) - (symbol (or "." "," "+" "-" "*" "/" "(" ")" "="))) + (symbol (or "." "," "+" "-" "*" "/" "^" "(" ")" "="))) (peg-run (peg stuff) (lambda (x) (message "failed %s" x)) @@ -540,12 +544,25 @@ solution for how to enumerate with circled numbers from [[https://latex.org/foru \end{document} #+end_src +*** Convert calc to latex +Convert a calc expression to latex format. + +#+name: convert-to-latex +#+begin_src elisp :tangle mathsheet.el + (defun ianxm/convert-to-latex (expr) + (let* ((calc-language 'latex) + (calc-expr (math-read-expr expr)) + (latex-expr (math-format-stack-value (list calc-expr 1 nil))) + (latex-expr-cleaned (replace-regexp-in-string (rx "1:" (* space)) "" latex-expr))) + (concat "$" latex-expr-cleaned "$"))) +#+end_src *** Write PDF This writes the generated into a local file and runs ~texi2pdf~ to -convert it to a pdf. We save it as ~worksheet.tex~ and the final -worksheet is named ~worksheet.pdf~. Each execution will overwrite the -same file. +convert it to a pdf. We save it as ~[template-name].tex~ and the final +worksheet is named ~[template-name].pdf~. Each execution with the same +template name will overwrite the same file. + #+begin_src elisp :results silent :tangle mathsheet.el (defun ianxm/gen-worksheet (template-name instruction problems) @@ -563,16 +580,16 @@ same file. (dolist (row problems) (if (cadddr row) (insert (format"\\CircledItem %s\\vspace{4cm}\n" - (car row))) + (ianxm/convert-to-latex (car row)))) (insert (format"\\CircledItem %s = \\rule[-.2\\baselineskip]{2cm}{0.4pt}\n" - (car row))))) + (ianxm/convert-to-latex (car row)))))) (goto-char (point-min)) (search-forward "<<answers>>") (replace-match "") (dolist (row problems) (insert (format "\\CircledItem %s\n" - (cadr row))))) + (ianxm/convert-to-latex (cadr row)))))) (shell-command (concat "texi2pdf " template-name ".tex") (get-buffer-create "*Standard output*"))) #+end_src