Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
lukashergt opened this issue Oct 14, 2022 · 5 comments
Open

Comments

@lukashergt
Copy link

lukashergt commented Oct 14, 2022

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
@timhoffm
Copy link
Member

This is likely due to dvipng -T tight being used in TexManager.make_png()`. My guess is that we are fundamentally not able to preserve leading whitespace. But people with more TeX/dvi knowledge may know better.

@anntzer
Copy link
Contributor

anntzer commented Oct 17, 2022

My guess is that we need to somehow hook the new-paragraph (\everypar) mechanism of tex to make it lay a marker there so that we know where the intended start-of-line is, but there's probably a bit of engineering needed (especially as we don't want this to affect displaymath, which is normally rendered at the center of the page, i.e. very far from the normal paragraph start position).

@zweihuehner
Copy link

Is there a update on this? I have a similar problem with adding leading whitespace to the tick labels.

@tacaswell
Copy link
Member

@zweihuehner For ticklabels I think you can right-align the text and get the effect I suspect you want.

I do not think this is a regression (it has always been like this) and is unfortunately not a high priority issue. For this to move forward we need someone to look into the latex -> dvi pipeline and sort out how to extract the intended bounding box (including white space).

@pjgorski
Copy link

A workaround. I also had a similar issue for axis label. I solved it by moving the whole label.

ax2.set_ylabel('Ylabel', y=0.4)

I guess it is more difficult to do it for legend texts, but maybe it is still possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants