11import pathlib
22import subprocess
33import sys
4+ from typing import Optional
45
56
6- def run_pdftex (latex_file_path : pathlib .Path ) -> pathlib .Path :
7- """Run TinyTeX with the given $\\ LaTeX$ file to render the PDF.
7+ def run_latex (
8+ latex_file_path : pathlib .Path , local_latex_command : Optional [str ] = None
9+ ) -> pathlib .Path :
10+ """Run `pdftex` with the given $\\ LaTeX$ file to render the PDF. If a local LaTeX
11+ command is provided, it will be called instead of `pdftex` of TinyTeX.
812
913 Args:
1014 latex_file_path: The path to the $\\ LaTeX$ file.
15+ local_latex_command: The local LaTeX command to use. If provided, it will be
16+ called instead of `pdftex` of TinyTeX.
1117
1218 Returns:
1319 The path to the rendered PDF file.
@@ -17,21 +23,49 @@ def run_pdftex(latex_file_path: pathlib.Path) -> pathlib.Path:
1723 message = f"The file { latex_file_path } doesn't exist!"
1824 raise FileNotFoundError (message )
1925
20- tinytex_binaries_directory = (
21- pathlib .Path (__file__ ).parent / "tinytex-release" / "TinyTeX" / "bin"
22- )
26+ if local_latex_command :
27+ executable = local_latex_command
2328
24- executables = {
25- "win32" : tinytex_binaries_directory / "windows" / "pdflatex.exe" ,
26- "linux" : tinytex_binaries_directory / "x86_64-linux" / "pdflatex" ,
27- "darwin" : tinytex_binaries_directory / "universal-darwin" / "pdflatex" ,
28- }
29+ # check if the command is working:
30+ try :
31+ subprocess .run (
32+ [executable , "--version" ],
33+ stdout = subprocess .DEVNULL , # don't capture the output
34+ stderr = subprocess .DEVNULL , # don't capture the error
35+ check = True ,
36+ )
37+ except FileNotFoundError as e :
38+ message = (
39+ f"{ executable } isn't installed! Please install LaTeX and try again (or"
40+ " don't use the [bright_black]--use-local-latex-command[/bright_black]"
41+ " option)."
42+ )
43+ raise FileNotFoundError (message ) from e
44+ else :
45+ tinytex_binaries_directory = (
46+ pathlib .Path (__file__ ).parent / "tinytex-release" / "TinyTeX" / "bin"
47+ )
48+
49+ executables = {
50+ "win32" : tinytex_binaries_directory / "windows" / "pdflatex.exe" ,
51+ "linux" : tinytex_binaries_directory / "x86_64-linux" / "pdflatex" ,
52+ "darwin" : tinytex_binaries_directory / "universal-darwin" / "pdflatex" ,
53+ }
2954
30- if sys .platform not in executables :
31- message = f"TinyTeX doesn't support the platform { sys .platform } !"
32- raise OSError (message )
55+ if sys .platform not in executables :
56+ message = f"TinyTeX doesn't support the platform { sys .platform } !"
57+ raise OSError (message )
3358
34- executable = executables [sys .platform ]
59+ executable = executables [sys .platform ]
60+
61+ # check if the executable exists:
62+ if not executable .is_file ():
63+ message = (
64+ f"The TinyTeX executable ({ executable } ) doesn't exist! If you are"
65+ " cloning the repository, make sure to clone it recursively to get the"
66+ " TinyTeX binaries. See the developer guide for more information."
67+ )
68+ raise FileNotFoundError (message )
3569
3670 # Before running LaTeX, make sure the PDF file is not open in another program,
3771 # that wouldn't allow LaTeX to write to it. Remove the PDF file if it exists,
@@ -63,6 +97,17 @@ def run_pdftex(latex_file_path: pathlib.Path) -> pathlib.Path:
6397 if latex_process .returncode != 0 :
6498 latex_file_path_log = latex_file_path .with_suffix (".log" ).read_text ()
6599
100+ if local_latex_command :
101+ message = (
102+ f"The local LaTeX command { local_latex_command } couldn't render"
103+ " this LaTeX file into a PDF. Check out the log file"
104+ f" { latex_file_path .with_suffix ('.log' )} in the output directory"
105+ " for more information. It is printed below:\n \n "
106+ )
107+
108+ message = message + latex_file_path_log
109+ raise RuntimeError (message )
110+
66111 message = (
67112 "Failed to render the PDF file. Check out the details in the log file:"
68113 f" { latex_file_path .with_suffix ('.log' )} \n \n "
0 commit comments