Skip to content

Commit

Permalink
Pass extra commands as an arg to _get_preamble
Browse files Browse the repository at this point in the history
These commands are run before using the underscore package.
  • Loading branch information
voidstarstar committed May 10, 2024
1 parent 7d20c7c commit e2d1aa7
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ def _get_documentclass():
else:
return _DOCUMENTCLASS

Check warning on line 43 in lib/matplotlib/backends/backend_pgf.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backends/backend_pgf.py#L43

Added line #L43 was not covered by tests

def _get_preamble():
def _get_preamble(commands=None):

Check failure on line 45 in lib/matplotlib/backends/backend_pgf.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 E302 expected 2 blank lines, found 1 Raw Output: ./lib/matplotlib/backends/backend_pgf.py:45:1: E302 expected 2 blank lines, found 1
"""Prepare a LaTeX preamble based on the rcParams configuration."""
if commands is None:
commands = []
font_size_pt = FontProperties(
size=mpl.rcParams["font.size"]
).get_size_in_points()
Expand All @@ -68,6 +70,7 @@ def _get_preamble():
for family in ["serif", "sans\\-serif", "monospace"]]
)
] + [r"\fi"] if mpl.rcParams["pgf.rcfonts"] else []),
*commands,
# Documented as "must come last".
mpl.texmanager._usepackage_if_not_loaded("underscore", option="strings"),
])
Expand Down Expand Up @@ -201,12 +204,13 @@ class LatexManager:
@staticmethod
def _build_latex_header():
latex_header = [
_get_preamble(),
# Include TeX program name as a comment for cache invalidation.
# TeX does not allow this to be the first line.
rf"% !TeX program = {mpl.rcParams['pgf.texsystem']}",
# Test whether \includegraphics supports interpolate option.
r"\usepackage{graphicx}",
_get_preamble(commands=[
# Include TeX program name as a comment for cache invalidation.
# TeX does not allow this to be the first line.
rf"% !TeX program = {mpl.rcParams['pgf.texsystem']}",
# Test whether \includegraphics supports interpolate option.
r"\usepackage{graphicx}",
]),
r"\begin{document}",
r"\typeout{pgf_backend_query_start}",
]
Expand Down Expand Up @@ -832,11 +836,12 @@ def print_pdf(self, fname_or_fh, *, metadata=None, **kwargs):
"\n".join([
r"\PassOptionsToPackage{pdfinfo={%s}}{hyperref}" % pdfinfo,
r"\PassOptionsToPackage{%s}{geometry}" % geometry_options,
_get_preamble(),
r"\usepackage{hyperref}",
r"\usepackage{geometry}",
r"\geometry{reset, %s}" % geometry_options,
r"\usepackage{pgf}",
_get_preamble(inserted_lines=[
r"\usepackage{hyperref}",
r"\usepackage{geometry}",
r"\geometry{reset, %s}" % geometry_options,
r"\usepackage{pgf}",
]),
r"\begin{document}",
r"\centering",
r"\input{figure.pgf}",
Expand Down Expand Up @@ -945,11 +950,12 @@ def _write_header(self, width_inches, height_inches):
latex_header = "\n".join([
r"\PassOptionsToPackage{pdfinfo={%s}}{hyperref}" % pdfinfo,
r"\PassOptionsToPackage{%s}{geometry}" % geometry_options,
_get_preamble(),
r"\usepackage{hyperref}",
r"\usepackage{geometry}",
r"\geometry{reset, %s}" % geometry_options,
r"\usepackage{pgf}",
_get_preamble(inserted_lines=[
r"\usepackage{hyperref}",
r"\usepackage{geometry}",
r"\geometry{reset, %s}" % geometry_options,
r"\usepackage{pgf}",
]),
r"\setlength{\parindent}{0pt}",
r"\begin{document}%",
])
Expand Down

0 comments on commit e2d1aa7

Please sign in to comment.