Get the width, total height and descent (in TeX points) for a TeX command's output in the current LaTeX environment.
(self, tex)
| 326 | return self._get_box_metrics(_escape_and_apply_props(text, prop)) |
| 327 | |
| 328 | def _get_box_metrics(self, tex): |
| 329 | """ |
| 330 | Get the width, total height and descent (in TeX points) for a TeX |
| 331 | command's output in the current LaTeX environment. |
| 332 | """ |
| 333 | # This method gets wrapped in __init__ for per-instance caching. |
| 334 | self._stdin_writeln( # Send textbox to TeX & request metrics typeout. |
| 335 | # \sbox doesn't handle catcode assignments inside its argument, |
| 336 | # so repeat the assignment of the catcode of "^" and "%" outside. |
| 337 | r"{\catcode`\^=\active\catcode`\%%=\active\sbox0{%s}" |
| 338 | r"\typeout{\the\wd0,\the\ht0,\the\dp0}}" |
| 339 | % tex) |
| 340 | try: |
| 341 | answer = self._expect_prompt() |
| 342 | except LatexError as err: |
| 343 | # Here and below, use '{}' instead of {!r} to avoid doubling all |
| 344 | # backslashes. |
| 345 | raise ValueError("Error measuring {}\nLaTeX Output:\n{}" |
| 346 | .format(tex, err.latex_output)) from err |
| 347 | try: |
| 348 | # Parse metrics from the answer string. Last line is prompt, and |
| 349 | # next-to-last-line is blank line from \typeout. |
| 350 | width, height, offset = answer.splitlines()[-3].split(",") |
| 351 | except Exception as err: |
| 352 | raise ValueError("Error measuring {}\nLaTeX Output:\n{}" |
| 353 | .format(tex, answer)) from err |
| 354 | w, h, o = float(width[:-2]), float(height[:-2]), float(offset[:-2]) |
| 355 | # The height returned from LaTeX goes from base to top; |
| 356 | # the height Matplotlib expects goes from bottom to top. |
| 357 | return w, h + o, o |
| 358 | |
| 359 | |
| 360 | @functools.lru_cache(1) |
no test coverage detected