josephwright / beamer

A LaTeX class for producing presentations and slides
Other
1.35k stars 139 forks source link

beamer template 'footline': vertical alignment of frame numbers not consistent #840

Closed user227621 closed 1 year ago

user227621 commented 1 year ago

Description of the bug

When using \setbeamertemplate{footline}[frame number] (or \setbeamertemplate{footline}[page number]), the vertical alignment of the frame numbers is not consistent, depending on the font. Consider the following example:

\documentclass{beamer}

\usepackage{noto-sans}

\setbeamertemplate{footline}[frame number]

\newcommand\emptyframe{\begin{frame}\end{frame}}

\begin{document}

\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe

\end{document}

When you look closely at the frame numbers, you'll see that the frame numbers on pages 3, 5, 6, 8, 9 and 10 are slightly shifted upwards.

This can happen with fonts where the digits do not all have the same height and depth. In Computer Modern Sans, all the digits do have the same height and depth, but this is not true for many other fonts.

Possible Fix

Do \setbox\mybox=\hbox{0123456789}, then define a command \mystrut similar to \strut, but with \vrule \@height\ht\mybox \@depth\dp\mybox \@width\z@ inside \strutbox, and insert this command at an appropriate place, possibly inside the beamer template page number in head/foot, like this:

\defbeamertemplate{page number in head/foot}{totalframenumber}{%
    \setbox\beamer@tempbox=\hbox{\inserttotalframenumber\,/\,\inserttotalframenumber}%
    \beamer@tempdim=\wd\beamer@tempbox%
    \makebox[\beamer@tempdim][r]{\insertframenumber\,/\,\inserttotalframenumber\mystrut}%
}

(Replace \mybox and \mystrut with appropriate internal names, of course)

samcarter commented 1 year ago

Could you maybe add a screenshot of your result? The alignment looks fine to me:

Screenshot 2023-03-07 at 10 45 34
user227621 commented 1 year ago

@samcarter The vertical shift is visible on your screenshot when you zoom in: There is no space between the red line and "2/11", but there is a tiny space (one pixel high) between the red line and "3/11".

samcarter commented 1 year ago

I think I understand what you mean, thanks for reporting!

Personally I don't think a pixel at several hundred percent zoom level is worth increasing the code complexity. Making the code more complex has potential drawbacks, like making it slower and harder to maintain.

Screenshot 2023-03-07 at 12 02 27
samcarter commented 1 year ago

Meanwhile @Skillmon found a more drastic example:


\documentclass[]{beamer}

\usepackage[oldstyle]{kpfonts}

\geometry{papersize={1cm,1cm}}

\setbeamertemplate{navigation symbols}{}

\setbeamertemplate{footline}[frame number]

\newcommand\emptyframe{\begin{frame}\end{frame}}

\begin{document}

\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe
\emptyframe

\end{document}
user227621 commented 1 year ago

@samcarter \usepackage[oldstyle]{noto-sans} would have been a more drastic example, too... Thanks for fixing this issue!

samcarter commented 1 year ago

@user227621 You're welcome!

Yes, the examples with oldstyle where pretty convincing - with them I didn't even have to recompile texstudio in order to allow more then 400% zoom level just to be able to see a hint of a problem :)

user227621 commented 1 year ago

@samcarter I just noticed that there is a missing percent sign in \defbeamertemplate{footline}{frame number}:

\defbeamertemplate{footline}{frame number}
{% <----- MISSING
  \hfill%
  \usebeamercolor[fg]{page number in head/foot}%
  \usebeamerfont{page number in head/foot}%
  \setbeamertemplate{page number in head/foot}[totalframenumber]%
  \strut\usebeamertemplate*{page number in head/foot}\kern1em\vskip1.4pt%
}

I don't know if this could produce spurious spaces, but it would probably be best if you added the missing percent sign.

samcarter commented 1 year ago

The % is not necessary there.

user227621 commented 1 year ago

However, the % is present in the similar definition of \defbeamertemplate{footline}{page number}:

\defbeamertemplate{footline}{page number}
{%
  \hfill%
  \usebeamercolor[fg]{page number in head/foot}%
  \usebeamerfont{page number in head/foot}%
  \setbeamertemplate{page number in head/foot}[totalpagenumber]%
  \strut\usebeamertemplate*{page number in head/foot}\kern1em\vskip1.4pt%
}
samcarter commented 1 year ago

Just try it out :) And even if it would add a space (which is not the case), it would be swallowed by the \hfill.

car222222 commented 1 year ago

It is still "good practice to add % in such cases: as there may not always be an \hfill (or some other trick) to swallow it!