Skip to content

Instantly share code, notes, and snippets.

@tecoholic
Last active February 7, 2018 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tecoholic/81cfc4d1047eaf720cd2c36ee5b1b61c to your computer and use it in GitHub Desktop.
Save tecoholic/81cfc4d1047eaf720cd2c36ee5b1b61c to your computer and use it in GitHub Desktop.
Flash Card Generator - Python LaTeX
Question 1
Answer 1
Question 2
Answer 2
#!/usr/bin/env python
import argparse
def make_cards(filename, subject, chapter):
template = """\documentclass[avery5371,grid,frame]{flashcards}
\geometry{margin=0.75cm}
\setlength{\cardwidth}{10cm}
\setlength{\cardheight}{6.5cm}
\setlength{\cardmargin}{0.3cm}
\\renewcommand{\cardrows}{4}
\\renewcommand{\cardcolumns}{2}
\usepackage{amsmath}
\cardfrontstyle[\LARGE\slshape]{headings}
\\begin{document}
"""
if subject:
template += "\n\cardfrontfoot{{{}}}".format(subject)
f = open(filename, 'r')
intext = f.read()
cards = intext.split("\n\n")
for card in cards:
question, answer = card.strip().split("\n", 1)
if "__" in question:
question = question.replace("__", "\\underline{\hspace{1cm}}")
if chapter:
template += "\n\\begin{flashcard}["+chapter+"]{"+question+"}"
else:
template += "\n\\begin{flashcard}{"+question+"}"
template += "\n "+answer.replace("\n", "\\\\\n")
template += "\n\end{flashcard}\n"
template += "\n\end{document}"
if not chapter:
chapter = filename.split('.')[0]
with open(chapter.replace(" ", "_")+".tex", 'w') as output:
output.write(template)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("filename", help="input filename")
parser.add_argument(
"subject", help="subject appears in the front of every card")
parser.add_argument(
"--chapter", help="chapter name appears in the front of every card")
args = parser.parse_args()
make_cards(args.filename, args.subject, args.chapter)
@tecoholic
Copy link
Author

# Generating the tex file from the input text file
python topanga.py --chapter Chapter sample_cardlist.txt Subject

# Generating the PDF
xelatex Chapter.tex

Output

screen shot 2018-02-07 at 10 53 06 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment