Find a file in the texmf tree using kpathsea_. The kpathsea library, provided by most existing TeX distributions, both on Unix-like systems and on Windows (MikTeX), is invoked via a long-lived luatex process if luatex is installed, or via kpsewhich otherwise. .. _kpathsea: htt
(filename)
| 1275 | |
| 1276 | @lru_cache |
| 1277 | def find_tex_file(filename): |
| 1278 | """ |
| 1279 | Find a file in the texmf tree using kpathsea_. |
| 1280 | |
| 1281 | The kpathsea library, provided by most existing TeX distributions, both |
| 1282 | on Unix-like systems and on Windows (MikTeX), is invoked via a long-lived |
| 1283 | luatex process if luatex is installed, or via kpsewhich otherwise. |
| 1284 | |
| 1285 | .. _kpathsea: https://www.tug.org/kpathsea/ |
| 1286 | |
| 1287 | Parameters |
| 1288 | ---------- |
| 1289 | filename : str or path-like |
| 1290 | |
| 1291 | Raises |
| 1292 | ------ |
| 1293 | FileNotFoundError |
| 1294 | If the file is not found. |
| 1295 | """ |
| 1296 | |
| 1297 | # we expect these to always be ascii encoded, but use utf-8 |
| 1298 | # out of caution |
| 1299 | if isinstance(filename, bytes): |
| 1300 | filename = filename.decode('utf-8', errors='replace') |
| 1301 | |
| 1302 | try: |
| 1303 | lk = _LuatexKpsewhich() |
| 1304 | except (FileNotFoundError, OSError): |
| 1305 | lk = None # Fallback to directly calling kpsewhich, as below. |
| 1306 | |
| 1307 | if lk: |
| 1308 | path = lk.search(filename) |
| 1309 | else: |
| 1310 | if sys.platform == 'win32': |
| 1311 | # On Windows only, kpathsea can use utf-8 for cmd args and output. |
| 1312 | # The `command_line_encoding` environment variable is set to force |
| 1313 | # it to always use utf-8 encoding. See Matplotlib issue #11848. |
| 1314 | kwargs = {'env': {**os.environ, 'command_line_encoding': 'utf-8'}, |
| 1315 | 'encoding': 'utf-8'} |
| 1316 | else: # On POSIX, run through the equivalent of os.fsdecode(). |
| 1317 | kwargs = {'env': {**os.environ}, |
| 1318 | 'encoding': sys.getfilesystemencoding(), |
| 1319 | 'errors': 'surrogateescape'} |
| 1320 | kwargs['env'].update( |
| 1321 | MT_VARTEXFONTS=str(Path(mpl.get_cachedir(), "vartexfonts"))) |
| 1322 | |
| 1323 | try: |
| 1324 | path = cbook._check_and_log_subprocess( |
| 1325 | ['kpsewhich', '-mktex=pk', filename], _log, **kwargs, |
| 1326 | ).rstrip('\n') |
| 1327 | except (FileNotFoundError, OSError, RuntimeError): |
| 1328 | path = None |
| 1329 | |
| 1330 | if path: |
| 1331 | return path |
| 1332 | else: |
| 1333 | raise FileNotFoundError( |
| 1334 | f"Matplotlib's TeX implementation searched for a file named " |
no test coverage detected
searching dependent graphs…