Skip to content

Commit 2703b16

Browse files
authored
feat: allow more com customization (#600)
1 parent be19ece commit 2703b16

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

src/mplhep/label.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
import re
45
from typing import TYPE_CHECKING, Any
56

67
import matplotlib as mpl
@@ -141,6 +142,20 @@ def dist(tup):
141142
)
142143

143144

145+
def _parse_com(com):
146+
"""Parse center-of-mass energy into value and unit."""
147+
if com is None:
148+
return "13", "TeV"
149+
if isinstance(com, str):
150+
match = re.match(r"(\d+\.?\d*)\s*(.*)", com.strip())
151+
return (
152+
(match.group(1), match.group(2))
153+
if match and match.group(2)
154+
else (com, "TeV")
155+
)
156+
return str(com), "TeV"
157+
158+
144159
def _lumi_line(
145160
*,
146161
year: str | float | None = None,
@@ -150,19 +165,13 @@ def _lumi_line(
150165
com: str | float | None = None,
151166
) -> str:
152167
"""Format luminosity line for standard layout."""
153-
# Set default values
154-
com_str = str(com) if com is not None else "13"
168+
com_str, com_unit = _parse_com(com)
155169
year_str = f", {year}" if year is not None else ""
156170

157171
if lumi is not None:
158-
# Format luminosity with unit
159-
lumi_str = lumi_format.format(lumi)
160-
lumi_with_unit = f"{lumi_str} $\\mathrm{{{lumi_unit}}}$"
161-
_lumi = f"{lumi_with_unit}{year_str} ({com_str} TeV)"
162-
else:
163-
_lumi = f"$\\ ${year_str} ({com_str} TeV)"
164-
165-
return _lumi.rstrip()
172+
lumi_str = f"{lumi_format.format(lumi)} $\\mathrm{{{lumi_unit}}}$"
173+
return f"{lumi_str}{year_str} ({com_str} {com_unit})"
174+
return f"$\\ ${year_str} ({com_str} {com_unit})"
166175

167176

168177
def _lumi_line_atlas(
@@ -174,21 +183,13 @@ def _lumi_line_atlas(
174183
com: str | float | None = None,
175184
) -> str:
176185
"""Format luminosity line for ATLAS-style layout."""
177-
# Format center-of-mass energy
178-
if com is not None:
179-
com_str = f"$\\sqrt{{s}} = \\mathrm{{{com}\\ TeV}}$"
180-
else:
181-
com_str = "$\\sqrt{s} = \\mathrm{13\\ TeV}$"
186+
com_str, com_unit = _parse_com(com)
187+
com_latex = f"$\\sqrt{{s}} = \\mathrm{{{com_str}\\ {com_unit}}}$"
182188

183189
if lumi is not None:
184-
# Format luminosity with unit
185-
lumi_str = lumi_format.format(lumi)
186-
lumi_with_unit = f"{lumi_str} $\\mathrm{{{lumi_unit}}}$"
187-
_lumi = f"{com_str}, {lumi_with_unit}"
188-
else:
189-
_lumi = com_str
190-
191-
return _lumi.rstrip()
190+
lumi_str = f"{lumi_format.format(lumi)} $\\mathrm{{{lumi_unit}}}$"
191+
return f"{com_latex}, {lumi_str}"
192+
return com_latex
192193

193194

194195
def _fontsize_to_points(fontsize: str | float) -> float:

0 commit comments

Comments
 (0)