2. Equations 1 step diagramsο
The python file to make a 1-step invop diagram is below.
The python file, invop_diagram_maker.py, when run, will ask for these inputs:
Choose the arithmetic process:
"Enter 1, 2, 3, 4 or 5 for +, -, X, /, random for 1st process"
Choose the file name base:
"Enter the base filename to be added to the prefix :"
The prefix will be βinvolp1β for standard operations.
The filename will have β_qβ added for the question diagram and β_ansβ for the answer diagram.
2.1. Example 1-step invop diagramο
2.2. 1-step invop diagram: pythonο
1from pathlib import Path
2import subprocess
3import time
4import magick_pdf_to_png
5import invop_functions as iof
6
7
8currfile_dir = Path(__file__).parent
9tex_template_path = currfile_dir / "invop_template.tex"
10texans_template_path = currfile_dir / "invop_template.tex"
11tex_diagram_template_path = currfile_dir / "invop_diagram_template.tex"
12
13
14def convert_to_pdf(tex_path, currfile_dir, aux_path):
15 """
16 Converts a TeX file to PDF format using pdfLaTeX.
17
18 Args:
19 tex_path (str): The path to the TeX file.
20 currfile_dir (str): The path to the directory where the TeX file is located.
21 aux_path (str): The path to the directory where auxiliary files will be stored.
22
23 Returns:
24 subprocess.CompletedProcess: A subprocess.CompletedProcess object containing information about the completed process.
25
26 Raises:
27 FileNotFoundError: If the TeX file does not exist.
28 subprocess.CalledProcessError: If pdfLaTeX returns a non-zero exit code.
29 """
30 result = subprocess.run(
31 [
32 "pdfLaTeX",
33 tex_path,
34 "-output-directory",
35 currfile_dir,
36 "-aux-directory",
37 aux_path,
38 ],
39 stdout=subprocess.PIPE,
40 )
41
42
43# % end modify values for invop
44# tex_keys = []
45tex_keys_q = ['line1_LHS', 'line1_RHS', 'line2_LHSq', 'line2_RHSq', 'line3_LHS', 'line3_RHSq']
46
47
48def make1_diagram(tex_diagram_template_txt, num):
49 tex_diagram_template_txt_ans = tex_diagram_template_txt
50 kv = iof.get_1step_process_dict(num)
51 for key, value in kv.items():
52 tex_diagram_template_txt_ans = tex_diagram_template_txt_ans.replace(
53 "<<" + key + ">>", value
54 )
55 for key, value in kv.items():
56 if key in tex_keys_q:
57 tex_diagram_template_txt = tex_diagram_template_txt.replace(
58 "<<" + key + ">>", value
59 )
60 else:
61 tex_diagram_template_txt = tex_diagram_template_txt.replace(
62 "<<" + key + ">>", kv[f'{key}q']
63 )
64 return tex_diagram_template_txt, tex_diagram_template_txt_ans
65
66
67def main():
68 num = input("Enter 1, 2, 3, 4 or 5 for +, -, X, /, random \n")
69 if num.strip().isdigit():
70 num = int(num)
71 if not num in [1, 2, 3, 4, 5]:
72 num = 5 # random by default
73 else:
74 num = 5 # random by default
75 filename = input("Enter the base filename to be added to the prefix invop1_: \n")
76 if not filename:
77 filename = "1" # "invop1_1_q and invop1_1_ans as default file"
78 # set names of files that are made
79 # questions
80 tex_output_path = currfile_dir / f"invop1_{filename}_q.tex"
81 pdf_path = currfile_dir / f"invop1_{filename}_q.pdf"
82 png_path = currfile_dir / f"invop1_{filename}_q.png"
83 aux_path = currfile_dir / "temp"
84 # answers
85 tex_output_path_ans = currfile_dir / f"invop1_{filename}_ans.tex"
86 pdf_path_ans = currfile_dir / f"invop1_{filename}_ans.pdf"
87 png_path_ans = currfile_dir / f"invop1_{filename}_ans.png"
88
89 # Read in the LaTeX template file
90 with open(tex_template_path, "r") as infile:
91 tex_template_txt = infile.read()
92 # Read in the LaTeX template file for answers
93 with open(texans_template_path, "r") as infile:
94 tex_template_txt_ans = infile.read()
95 # Read in the LaTeX diagram template file
96 with open(tex_diagram_template_path, "r") as infile:
97 tex_diagram_template_txt = infile.read()
98
99 # Generate the <<diagram>> replacement tex
100 diagram_text, diagram_text_ans = make1_diagram(tex_diagram_template_txt, num)
101 # Replace the <<diagram>> placeholder in the LaTeX template
102 tex_template_txt = tex_template_txt.replace("<<diagram>>", diagram_text)
103 tex_template_txt_ans = tex_template_txt_ans.replace("<<diagram>>", diagram_text_ans)
104 # Write the question diagram tex to an output file
105 with open(tex_output_path, "w") as outfile:
106 outfile.write(tex_template_txt)
107 # Write the answer diagram tex to an output file
108 with open(tex_output_path_ans, "w") as outfile:
109 outfile.write(tex_template_txt_ans)
110
111 # Wait for the files to be created
112 time.sleep(1)
113 # convert to pdf
114 convert_to_pdf(tex_output_path, currfile_dir, aux_path)
115 convert_to_pdf(tex_output_path_ans, currfile_dir, aux_path)
116
117 # Wait for the files to be created
118 time.sleep(1)
119 # convert to png
120 magick_pdf_to_png.convert_pdf_to_png(pdf_path, png_path)
121 magick_pdf_to_png.convert_pdf_to_png(pdf_path_ans, png_path_ans)
122
123
124if __name__ == "__main__":
125 print("starting")
126 main()
127 print("finished")