Skip to content

Instantly share code, notes, and snippets.

@noelbautista91
Last active June 5, 2017 06:32
Show Gist options
  • Save noelbautista91/5eeb1ec96f238782479a07013edbd73b to your computer and use it in GitHub Desktop.
Save noelbautista91/5eeb1ec96f238782479a07013edbd73b to your computer and use it in GitHub Desktop.
A very basic example on how to automate writing math questions + answers formatted in a LaTeX document.
"""
A very basic example on how to automate writing math questions + answers
formatted in a LaTeX document.
This proof of concept project shows that one could quickly substitute numbers,
provide a correct answer, and generate a properly formatted LaTeX document with
a simple command.
Compare that to the tedious process of manually copy-pasting questions, modifying the given
variables, and computing for the answers!
Input:
`python precalc-example.py -2 -3 0 -5`
Output:
```
\begin(illustration)
Determine the distance between points $A = (-2, -3)$ and $B = (0, -5)$.
\begin(sol)
Let $x_(1) = -2$, $x_(2) = 0$, $y_(1) = -3$ and $y_(2) = -5$:
\begin(align*)
D(A, B) &= \sqrt( (x_(2) - x_(1))^2 + (y_(2) - y_(1))^(2)) &&
\text(\scriptsize (Distance.formula) ) \\
&= \sqrt( (0 - -2)^(2) + (-5 - -3)^(2)) &&\text(\scriptsize(Substitute the values) ) \\
&= \sqrt( (2)^(2) + (-2)^(2) &&\text(\scriptsize (Subtract the terms) ) \\
&= \sqrt( (4 + 4) &&\text(\scriptsize (Simplify) ) \\
&\approx 2.8284271247461903\ \text(units) &&\text(\scriptsize (Finalanswer) )
\end(align*)
(\bf Conclusion):\\
This means that the distance between the two points $A = (-2, 0)$ and $B =
(-3, -5) is approximately equal to 2.8284$ units.
\end(sol)
\end(illustration)
```
"""
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('a1', type=int)
parser.add_argument('b1', type=int)
parser.add_argument('a2', type=int)
parser.add_argument('b2', type=int)
args = parser.parse_args()
A1 = args.a1
A2 = args.a2
B1 = args.b1
B2 = args.b2
final_answer = (((A2-A1)**2 + (B2-B1)**2))**(1/2.0)
problem = rf'''\begin(illustration)
Determine the distance between points $A = ({A1}, {B1})$ and $B = ({A2}, {B2})$.
\begin(sol)
Let $x_(1) = {A1}$, $x_(2) = {A2}$, $y_(1) = {B1}$ and $y_(2) = {B2}$:
\begin(align*)
D(A, B) &= \sqrt( (x_(2) - x_(1))^2 + (y_(2) - y_(1))^(2)) &&
\text(\scriptsize (Distance.formula) ) \\
&= \sqrt( ({A2} - {A1})^(2) + ({B2} - {B1})^(2)) &&\text(\scriptsize(Substitute the values) ) \\
&= \sqrt( ({A2-A1})^(2) + ({B2-B1})^(2) &&\text(\scriptsize (Subtract the terms) ) \\
&= \sqrt( ({(A2-A1)**2} + {(B2-B1)**2}) &&\text(\scriptsize (Simplify) ) \\
&\approx {final_answer}\ \text(units) &&\text(\scriptsize (Finalanswer) )
\end(align*)
(\bf Conclusion):\\
This means that the distance between the two points $A = ({A1}, {A2})$ and $B =
({B1}, {B2}) is approximately equal to {round(final_answer, 4)}$ units.
\end(sol)
\end(illustration)
'''
print(problem % args.__dict__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment