From d113e01a95d6a1944596311c41f17b7ef5074e53 Mon Sep 17 00:00:00 2001 From: Wessel Bruinsma Date: Thu, 7 Apr 2022 19:02:41 +0100 Subject: [PATCH] Add checks to `tex` and `pdfcrop` --- wbml/plot.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/wbml/plot.py b/wbml/plot.py index 9528ffc..810bd19 100644 --- a/wbml/plot.py +++ b/wbml/plot.py @@ -1,10 +1,12 @@ +import subprocess from functools import wraps -import subprocess import lab as B import matplotlib.pyplot as plt from plum import Dispatcher +from .warning import warn_upmodule + __all__ = ["patch", "tex", "tweak", "style", "pdfcrop"] _dispatch = Dispatcher() @@ -76,7 +78,13 @@ def patched_f(*args, **kw_args): def tex(): """Use TeX for rendering.""" - plt.rcParams["text.usetex"] = True # Use TeX for rendering. + if subprocess.call("which latex", shell=True) == 0: + plt.rcParams["text.usetex"] = True # Use TeX for rendering. + else: + warn_upmodule( + "No LaTeX installation found, so LaTeX will not be used to render " + "`matplotlib` figures." + ) def tweak( @@ -189,4 +197,10 @@ def pdfcrop(path): Args: path (str): Path of PDF. """ - subprocess.call(["pdfcrop", path, path]) + if subprocess.call("which pdfcrop", shell=True) == 0: + subprocess.call(["pdfcrop", path, path]) + else: + warn_upmodule( + "External tool `pdfcrop` cannot be found. " + "Skipping the application of `pdfcrop`." + )