|
1 | 1 | import matplotlib as mpl
|
| 2 | +import re |
2 | 3 | from matplotlib.patches import ArrowStyle
|
3 | 4 |
|
4 | 5 | from . import _color
|
5 | 6 |
|
| 7 | +def escape_text(text): |
| 8 | + """ |
| 9 | + Escapes certain patterns in a given text to make them compatible with |
| 10 | + LaTeX formatting, especially for SI units. |
| 11 | +
|
| 12 | + Parameters: |
| 13 | + - text (str): The input string that needs to be processed for LaTeX- |
| 14 | + compatible escapes. |
| 15 | +
|
| 16 | + Returns: |
| 17 | + - str: The text with escaped patterns suitable for LaTeX rendering. |
| 18 | +
|
| 19 | + The function primarily performs the following conversions: |
| 20 | + 1. Converts percentages, e.g., "45%", "45.5 %", to the LaTeX SI |
| 21 | + unit format: "\\SI{45}{\\percent}". |
| 22 | + 2. Fixes potential issues with the "\\SI" unit for the plus-minus |
| 23 | + notation. |
| 24 | + 3. Corrects ", s" patterns to ", \\SI{\\s}". |
| 25 | +
|
| 26 | + Note: |
| 27 | + This function assumes that the input text is likely to contain numeric |
| 28 | + values that need to be presented using SI notation in LaTeX. For |
| 29 | + correct rendering, the siunitx LaTeX package should be included in |
| 30 | + the document. |
| 31 | +
|
| 32 | + Example: |
| 33 | + Given the input: "The efficiency is 45% and the tolerance is |
| 34 | + +-\\SI{2}{\\degree}, s" |
| 35 | + The output will be: "The efficiency is \\SI{45}{\\percent} and |
| 36 | + the tolerance is \\SI{+-2}{\\degree}, \\SI{\s}" |
| 37 | + """ |
| 38 | + return re.sub(r"(\d+(\.\d+)?)\s?%", r"\\SI{\1}{\\percent}", |
| 39 | + text).replace("+-\\SI{", "\\SI{+-").replace(", s", |
| 40 | + ", \\SI{\\s}") |
| 41 | + |
6 | 42 |
|
7 | 43 | def draw_text(data, obj):
|
8 | 44 | """Paints text on the graph."""
|
|
0 commit comments