File size: 2,481 Bytes
5f38e95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
r"""Captioned figure export for inclusion in LaTeX manuscripts.

A :class:`CaptionedFigure` is a lightweight pairing of a Matplotlib figure
with its caption, label, and the inclusion stanza that a LaTeX document
should use to bring it in. :func:`save_captioned_figure` writes both the
figure file (PDF + PNG side by side --- PDF for the manuscript, PNG for
slide decks and previews) and an accompanying ``.tex`` snippet that the
user can ``\input`` from their paper without copy-pasting captions.
"""

from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING

if TYPE_CHECKING:                                           # pragma: no cover
    from matplotlib.figure import Figure


@dataclass(slots=True)
class CaptionedFigure:
    """A figure plus its scholarly metadata."""

    figure: "Figure"
    caption: str
    label: str
    width_textwidth: float = 0.95           # \\includegraphics[width=<this>\\textwidth]


_TEX_TEMPLATE = r"""\begin{{figure}}[!htb]
  \centering
  \includegraphics[width={width:.3f}\textwidth]{{{filename}}}
  \caption{{{caption}}}
  \label{{{label}}}
\end{{figure}}
"""


def save_captioned_figure(
    cf: CaptionedFigure,
    out_dir: str | Path,
    *,
    stem: str | None = None,
    also_png: bool = True,
) -> dict[str, Path]:
    """Write the figure as PDF (+ optional PNG) plus a LaTeX inclusion snippet.

    Returns a dict mapping the written role (``"pdf"``, ``"png"``, ``"tex"``)
    to its on-disk path. The PDF and PNG share the same basename, derived
    from ``stem`` if supplied or from ``cf.label`` otherwise.
    """
    out = Path(out_dir)
    out.mkdir(parents=True, exist_ok=True)
    base = stem or _safe_stem(cf.label)

    pdf_path = out / f"{base}.pdf"
    cf.figure.savefig(pdf_path)
    written: dict[str, Path] = {"pdf": pdf_path}

    if also_png:
        png_path = out / f"{base}.png"
        cf.figure.savefig(png_path, dpi=300)
        written["png"] = png_path

    tex_path = out / f"{base}.tex"
    tex_path.write_text(
        _TEX_TEMPLATE.format(
            width=cf.width_textwidth,
            filename=base + ".pdf",
            caption=cf.caption,
            label=cf.label,
        )
    )
    written["tex"] = tex_path
    return written


def _safe_stem(label: str) -> str:
    """Turn a LaTeX label like ``fig:tension-track`` into a safe filename stem."""
    return label.replace(":", "_").replace("/", "_").replace(" ", "_")