Package: pytask-latex
pytask-latexpytask-latex allows you to compile LaTeX documents with pytaskIt also useslatex-dependency-scannerto
automatically infer the dependencies of the LaTeX document such as images,
bibliographies and other.texfiles which are necessary to compile the LaTeX document.Installationpytask-latex is available onPyPIandAnaconda.org. Install it with$pipinstallpytask-latex#or$condainstall-cconda-forgepytask-latexYou also need to havelatexmkinstalled which determines the necessary number of
compilation steps (hereis an
explanation for what latexmk achieves). To test whether it is installed, type the
following on the command line$latexmk--helpIf an error is shown instead of a help page, you can installlatexmkwith one of the
popular LaTeX distributions, likeTeX Live,MiKTeX,MacTeXor others.UsageCompiling your PDF can be as simple as writing the following task.frompathlibimportPathfrompytaskimportmark@mark.latex(script=Path("document.tex"),document=Path("document.pdf"))deftask_compile_latex_document():passUse@mark.latexto indicate that this task compiles a LaTeX document. Thescriptand
thedocumentkeywords provide absolute paths or paths relative to the task module to
the LaTeX file and the compiled document.Dependencies and ProductsDependencies and products can be added as usual. Read thistutorial.For example, with the@taskdecorator. (The choice of the kwarg name, herepath, is
arbitrary.)frompytaskimportmarkfrompytaskimporttaskfrompathlibimportPath@task(kwargs={"path":Path("path_to_another_dependency.tex")})@mark.latex(script=Path("document.tex"),document=Path("document.pdf"))deftask_compile_latex_document():passCustomizing the compilationpytask-latex uses latexmk by default to compile the document because it handles most
use-cases automatically. The following is equivalent to a bare@mark.latexdecorator.@mark.latex(script=Path("document.tex"),document=Path("document.pdf"),compilation_steps="latexmk",)deftask_compile_latex_document():...The@mark.latexdecorator has a keyword argument calledcompilation_stepswhich
accepts which accepts strings or list of strings pointing to internally implemented
compilation steps. Using strings will use the default configuration of this compilation
step. It is equivalent to the following.frompytask_lateximportcompilation_stepsascs@mark.latex(script=Path("document.tex"),document=Path("document.pdf"),compilation_steps=cs.latexmk(options=("--pdf","--interaction=nonstopmode","--synctex=1","--cd")),)deftask_compile_latex_document():...In this example,compilation_steps.latexmkis a compilation step constructor which
accepts a set of options and creates a compilation step function.You can pass different options to change the compilation process with latexmk. Here is
an example for generating a.dvi.@mark.latex(script=Path("document.tex"),document=Path("document.pdf"),compilation_steps=cs.latexmk(options=("--dvi","--interaction=nonstopmode","--synctex=1","--cd")),)deftask_compile_latex_document():...compilation_step.latexmk(options)generates a compilation step which is a function
with the following signature:frompathlibimportPathimportsubprocessdefcustom_compilation_step(path_to_tex:Path,path_to_document:Path)->None:...subproces.run(...,check=True)You can also pass your custom compilation step with the same signature to thecompilation_stepskeyword argument of the decorator.Each compilation step receives the path to the LaTeX source file and the path to the
final document which it uses to call some program on the command line to run another
step in the compilation process.In the future, pytask-latex will provide more compilation steps for compiling
bibliographies, glossaries and the like.Repeating tasks with different scripts or inputsYou can compile multiple LaTeX documents as well as compiling a single.texdocument
with different command line arguments.The following task compiles two latex documents.foriinrange(2):@task@mark.latex(script=Path(f"document_{i}.tex"),document=Path(f"document_{i}.pdf"))deftask_compile_latex_document():passIf you want to compile the same document with different command line options, you have
to include the latex decorator in the parametrization. Pass a dictionary for possible
compilation steps and their options.forformat_in("pdf","dvi"):@task@mark.latex(script=Path("document.tex"),document=Path(f"document.{format_}"),compilation_steps=cs.latexmk((f"--{format_}","--interaction=nonstopmode","--synctex=1","--cd")),)deftask_compile_latex_document():passConfigurationinfer_latex_dependenciespytask-latex tries to scan your LaTeX document for included files with the help oflatex-dependency-scannerif
the following configuration value is true which is also the default.[tool.pytask.ini_options]infer_latex_dependencies=trueSince the package is in its early development phase and LaTeX provides a myriad of ways
to include files as well as providing shortcuts for paths (e.g.,\graphicspath), there
are definitely some rough edges left. File an issue here or in the other project in case
of a problem.ChangesConsult therelease notesto find out about what is new.