[Tex/LaTex] Using \hfill in a beamer \frametitle in the wuerzburg theme

beamerhorizontal alignment

I'm using a set of beautiful beamer templates developed by Christian Gogolin and I'm struggling to get the desired effect using the \hfill command in \frametitle{} in order to have a beamer button aligned completely to the right in the frame title.

Here is a MWE, using Gogolin's own wuerzburg beamer outer theme:

\documentclass[xcolor={x11names,svgnames,dvipsnames}]{beamer} 
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\useoutertheme[nofootline]{wuerzburg}
\usecolortheme{shark}
%\setbeamertemplate{frametitle}[default][none] % uncommenting this gives proper placement but overrides the frametitle definition of the outer theme. 
\begin{document}

\begin{frame}
\frametitle{This is the frame title \hfill \beamergotobutton{right}}
Some text
\end{frame}
\end{document}

This is the result:

<code>Example frame with unaligned button</code>

The "right" beamer button is not aligned right as expected; its absolute position also changes based on the lenght of the title text.

I tried to implement the advice given on google groups: using \frametitle{Title \hspace{1 filll} Logo} (1 filll or 1 fill both do not work).

Using \setbeamertemplate{frametitle}[default][none] gives proper placement but overrides the frametitle definition of the outer theme.

I imagine that the fix would involve the definition of frametitle template in the wuerzburg theme:

\defbeamertemplate*{frametitle}{wuerzburg theme}[1][left]
{
  \nointerlineskip
  \ifbeamercolorempty[bg]{frametitle}{\vskip0.5ex}{%
    \hbox to\textwidth{\hskip-\Gm@lmargin\pgfuseshading{beamer@frametitletopmshade}\hskip-\Gm@rmargin}
    \nointerlineskip%
  }%
  \@tempdima=\textwidth%
  \advance\@tempdima by\beamer@leftmargin%
  \advance\@tempdima by\beamer@rightmargin%
  \begin{beamercolorbox}[sep=0.3cm,#1,wd=\the\@tempdima]{frametitle}
    \usebeamerfont{frametitle}%
    \vbox{}\vskip-1.7ex%
    \if@tempswa\else\csname beamer@fte#1\endcsname\fi%
    \strut\insertframetitle\strut\par%
    {%
      \ifx\insertframesubtitle\@empty%
      \else%
      {\usebeamerfont{framesubtitle}\usebeamercolor[fg]{framesubtitle}\insertframesubtitle\strut\par}%
      \fi
    }%
    \vskip-1.3ex%
    \if@tempswa\else\vskip-.3cm\fi% set inside beamercolorbox... evil here...
  \end{beamercolorbox}%
  \nointerlineskip
  \ifbeamercolorempty[bg]{frametitle}{}{%
    \hbox to\textwidth{\hskip-\Gm@lmargin\pgfuseshading{beamer@frametitlebottomshade}\hskip-\Gm@rmargin}%
    \vskip-1.5pt%
  }%
}

But I can't figure out what goes wrong here: the text width seems to be correct and I don't understand the role of \strut\par around \insertframetitle.

Any help is appreciated.

Best Answer

You can use \hspace{0pt plus 1 filll} instead of \hfill:

\frametitle{This is the frame title \hspace{0pt plus 1 filll}  \beamergotobutton{right}}

The environment beamercolorboxhas predefined left and right margins which are defined with the skip 0pt plus 1fill. If you are using \hfill the word is centered for the right part. To show the behaviour you can use the following mwe:

\documentclass[]{article} 
\parindent0pt
\usepackage{showframe}
\begin{document}

\null Word \hfill word \par


\null Word \hfill word \hfill\null\par

\null\hspace{0pt plus 1fill} Word \hfill word \hspace{0pt plus 1fill}\null\par

\null\hspace{0pt plus 1fill} Word \hspace{0pt plus 1filll} word \hspace{0pt plus 1fill}\null\par
\end{document}

enter image description here

egreg wrote an answer to this special registers: What is the difference between 'fil' and 'fill'?