66import tempfile
77import subprocess
88
9- def compile_solution (content : str ) -> bool :
9+ def validate_latex_content (content : str ) -> bool :
1010 """
11- Compile a LaTeX document to check if the content has valid LaTeX syntax.
11+ Validate a LaTeX document by attempting to compile it, checking for syntax validity .
1212
1313 Args:
1414 content (str): The LaTeX content to check.
@@ -17,17 +17,14 @@ def compile_solution(content: str) -> bool:
1717 bool: True if compilation is successful, False otherwise.
1818 """
1919 with tempfile .TemporaryDirectory () as temp_dir :
20- tex_file = Path (temp_dir ) / "temp_solution.tex "
20+ tex_file = Path (temp_dir ) / "temp_solution"
2121 doc = Document ()
2222 doc .append (NoEscape (content ))
23- doc .generate_tex (str (tex_file ))
2423 try :
25- result = subprocess .run (
26- ["pdflatex" , "-output-directory" , temp_dir , tex_file ],
27- capture_output = True , text = True
28- )
29- return result .returncode == 0
30- except subprocess .CalledProcessError as e :
24+ # Attempt to compile the LaTeX document to PDF
25+ doc .generate_pdf (str (tex_file ), clean_tex = True )
26+ return True
27+ except Exception as e :
3128 print (f"Error compiling LaTeX content: { e } " )
3229 return False
3330
@@ -92,20 +89,16 @@ def generate_solutions_pdf(
9289 key = lambda x : int (x .stem .split ("_" )[0 ][1 :])
9390 )
9491
95- # Validate each solution before adding to the document
92+ sol_files_with_issues = []
93+ # Add each solution to document
9694 for sol_file in solution_files :
9795 print (f"Validating solution: { sol_file .name } " )
9896 content = sol_file .read_text (encoding = "utf-8" )
99-
100- if not compile_solution (content ):
101- print (f"Error: Solution '{ sol_file .name } ' has LaTeX syntax errors. Please fix it." )
102- return
103-
104- print ("All solutions are valid. Generating the final PDF..." )
10597
106- # Add each solution to document
107- for sol_file in solution_files :
108- content = sol_file .read_text (encoding = "utf-8" )
98+ if not validate_latex_content (content ):
99+ print (f"Error: Solution '{ sol_file .name } ' has LaTeX syntax errors. Please fix it." )
100+ sol_files_with_issues .append (sol_file )
101+ continue
109102 doc .append (NoEscape (content ))
110103 doc .append (NoEscape (r"\newpage" ))
111104
0 commit comments