pgf-tikz / pgf

A Portable Graphic Format for TeX
https://pgf-tikz.github.io/
1.08k stars 104 forks source link

Text placement/alignment issue #1312

Closed IjhUDWzf closed 3 months ago

IjhUDWzf commented 3 months ago

Brief outline of the bug

When text is aligned to bottom and a minus is present the vertical alignment does not match the same text without the minus.

image

Context: I use pgf backend of matplotlib and noticed that ticks labels are positioned incorrectly. While investigating the issue I found out that labels below the graph (aligned to top) are fine while labels above the graph (aligned to bottom) are incorrect. Moreover, the coordinates produced by matplotlib do seem to be ok.

Version of pgf: v3.1.8b

Minimal working example (MWE)

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{pgfpicture}%
\begin{pgfscope}%
\pgftext[x=1in,y=1in,,bottom]{\(\displaystyle {\ensuremath{-}3}\)}%
\pgftext[x=1.1in,y=1in,,bottom]{\(\displaystyle {3}\)}%
\end{pgfscope}%
\end{pgfpicture}%

\end{document}
muzimuzhi commented 3 months ago

Pgf acts as expected. Surprisingly, minus math character - in the default math symbol font has a non-zero depth, hence aligning at bottom makes $-3$ higher than $3$.

Maybe using bottom option of \pgftext to align plot labels is not a good choice. Similarly, top won't ensure result of aligning at baseline if height of \pgftexts differ (for instance when old-style numbers are used). The base option is what's needed here, but then one has to use estimated shiftings.

\documentclass{article}
\showboxbreadth=\maxdimen
\showboxdepth=\maxdimen

\begin{document}
\setbox0=\hbox{$3$}
\showbox0

\setbox0=\hbox{$-$}
\showbox0
\end{document}
> \box0=
\hbox(6.44444+0.0)x5.00002
.\mathon
.\OT1/cmr/m/n/10 3
.\mathoff

> \box0=
\hbox(5.83333+0.83333)x7.7778
.\mathon
.\OMS/cmsy/m/n/10 ^^@
.\mathoff
IjhUDWzf commented 3 months ago

Thank you @muzimuzhi for explaining. When I replaced bottom with base in the code generated by matplotlib the alignment seems to be perfect even without any shifts (even when I only replace bottom with base for negative numbers). The issue seems to be on the matplotlib side then.