Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Bug]: multialignment='right' in ax.text() with path_effects breaks when using LaTeX package \usepackage[T1]{fontenc} #28675

Copy link
Copy link
Closed
@yjzhu-solar

Description

@yjzhu-solar
Issue body actions

Bug summary

When I plot a multiline text using ax.text() with path_effects under TeX environment with package \usepackage[T1]{fontenc} the multiline coalignment to right breaks.

Code for reproduction

import matplotlib
print(matplotlib.__version__)
import matplotlib.pyplot as plt
from matplotlib import rc_context, patheffects as path_effects
print(matplotlib.get_backend())

with rc_context({'text.usetex': True, 'font.family': 'serif', 'font.serif':'cm',
                 'text.latex.preamble': r'\usepackage[T1]{fontenc}'
                 }):
    fig, ax = plt.subplots(layout='constrained')
    text1 = ax.text(0, 0.95, r'\textbf{aaaaaaaaaaaaaaa}'
            "\n" + r'\textbf{{\quad\textsc{aaaaaa}}}',
            transform=ax.transAxes, fontsize=12, va='top', ha='left',linespacing=1.2,ma='left',
            color='green', )
    
    text2 = ax.text(0, 0.8, r'\textbf{aa}'
            "\n" + r'\textbf{{aaaaaaaaaaaaaaa}}',
            transform=ax.transAxes, fontsize=12, va='top', ha='left',linespacing=1.2,ma='right',
            color='red', path_effects=[path_effects.Stroke(linewidth=1.2, foreground='black'),
                                path_effects.Normal()])
    
    text3 = ax.text(1, 0.95, r'\textbf{aa}'
            "\n" + r'\textbf{{aaaaaaaaaaaaaaa}}',
            transform=ax.transAxes, fontsize=12, va='top', ha='right',linespacing=1.2,ma='right',
            color='red',)
    
    text4 = ax.text(1, 0.8, r'\textbf{aa}'
            "\n" + r'\textbf{{aaaaaaaaaaaaaaa}}',
            transform=ax.transAxes, fontsize=12, va='top', ha='right',linespacing=1.2,ma='right',
            color='red', path_effects=[path_effects.Stroke(linewidth=1.2, foreground='black'),
                                path_effects.Normal()])
    
    ax.set_title(r'With \textbackslash usepackage[T1]\{fontenc\}')

    plt.show()

with rc_context({'text.usetex': True, 'font.family': 'serif', 'font.serif':'cm',
                 }):
    fig, ax = plt.subplots(layout='constrained')
    text1 = ax.text(0, 0.95, r'\textbf{aaaaaaaaaaaaaaa}'
            "\n" + r'\textbf{{\quad\textsc{aaaaaa}}}',
            transform=ax.transAxes, fontsize=12, va='top', ha='left',linespacing=1.2,ma='left',
            color='green', )
    
    text2 = ax.text(0, 0.8, r'\textbf{aa}'
            "\n" + r'\textbf{{aaaaaaaaaaaaaaa}}',
            transform=ax.transAxes, fontsize=12, va='top', ha='left',linespacing=1.2,ma='right',
            color='red', path_effects=[path_effects.Stroke(linewidth=1.2, foreground='black'),
                                path_effects.Normal()])
    
    text3 = ax.text(1, 0.95, r'\textbf{aa}'
            "\n" + r'\textbf{{aaaaaaaaaaaaaaa}}',
            transform=ax.transAxes, fontsize=12, va='top', ha='right',linespacing=1.2,ma='right',
            color='red',)
    
    text4 = ax.text(1, 0.8, r'\textbf{aa}'
            "\n" + r'\textbf{{aaaaaaaaaaaaaaa}}',
            transform=ax.transAxes, fontsize=12, va='top', ha='right',linespacing=1.2,ma='right',
            color='red', path_effects=[path_effects.Stroke(linewidth=1.2, foreground='black'),
                                path_effects.Normal()])
    
    ax.set_title(r'Without \textbackslash usepackage[T1]\{fontenc\}')

    plt.show()

Actual outcome

The ma='right' breaks for the bottom two texts with path_effects.
image

Expected outcome

I would expect something similar without \usepackage[T1]{fontenc} below, but somehow I really want to include it because it provides some additional support, for example, nesting \textsc{} in \textbf{}.
image

Additional information

No response

Operating system

WSL 2.2.4.0/Ubuntu 22.04.3 LTS

Matplotlib Version

3.9.1

Matplotlib Backend

qtagg

Python version

3.12.2

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.