Skip to content

[Bug]: leading whitespace in legend and labels ignored with usetex=True #24166

Open
@lukashergt

Description

@lukashergt

Bug summary

As raised in a question on stackoverflow, none of the typical latex commands for horizontal white space (e.g. ~, \,, \quad, \hphantom{whitespace}) seem to work at the start of a text label when using text.usetex=True.

The commands work as expected in the middle of a text label.

Code for reproduction

import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'serif'
plt.rcParams['text.usetex'] = True
plt.rcParams['mathtext.fontset'] = 'cm'

fig = plt.figure(figsize=(4, 3))
plt.plot([], label="~ tilde $\\sim$")
plt.plot([], label="\, comma \\textbackslash,")
plt.plot([], label="\quad \\textbackslash quad")
plt.plot([], label="\hphantom{whitespace} \\textbackslash hphantom")
plt.plot([], label=".\hphantom{whitespace} ugly leading dot")
plt.plot([], label=".whitespace ugly leading dot")
plt.xlabel("\hphantom{whitespace} xlabel")
plt.legend()

Actual outcome

As you can see in the image below, none of the horizontal white space commands seem to be doing anything as long as they are at the head of the legend label or xlabel.

They all work fine when starting with some other character. Compare legend labels 4, 5, and 6. \hphantom{whitespace} doesn't do anything in label 4, but in label 5 after a leading dot it correctly produces exactly the amount of white space that matches the horizontal space of the word whitespace as shown in label 6.

no leading whitespace

Expected outcome

The latex commands ~, \,, \quad, \hphantom{whitespace} should produce horizontal white space even when at the start of a text label.

System

  • Operating system: Manjaro Linux
  • Matplotlib Version: 3.6.1
  • Matplotlib Backend: module://matplotlib_inline.backend_inline
  • Python version: 3.10.7
  • Jupyter version: 6.4.12 (notebook), 3.4.7 (lab)
  • Installation: pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions