[Tex/LaTex] Beamer metropolis: align image in frametitle/set image height to frametitle height

beamerbeamer-metropolisgraphicshorizontal alignmentvertical alignment

I am trying to align certain images in the headline of Beamers Metropolis theme using the tikz package. (May be there are even better approaches to do so.)

Right now, I align them using yshiftand xshift. However, they are not perfectly aligned using this manual approach and hence look odd on high resolution presentation in which you see the offset:

enter image description here

\usepackage{tikz}
% headline images
\addtobeamertemplate{frametitle}{}{%
\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north east,yshift=4.24pt, xshift = 4.2pt] at (current page.north east) {\includegraphics[height=1cm]{img/image2.png}};
\end{tikzpicture}}

\addtobeamertemplate{frametitle}{}{%
\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north east,yshift=4.13pt, xshift = -64.8pt] at (current page.north east) {\includegraphics[height=1cm]{img/image1.png}};
\end{tikzpicture}}

The frametitle is defined as follows according to the metropolis doc:

\newlength{\metropolis@frametitle@padding}
\setlength{\metropolis@frametitle@padding}{2.2ex}
\newcommand{\metropolis@frametitlestrut@start}{
\rule{0pt}{\metropolis@frametitle@padding +%
\totalheightof{% \ifcsdef{metropolis@frametitleformat}{\metropolis@frametitleformat X}{X}% }%
}% }
\newcommand{\metropolis@frametitlestrut@end}{
 \rule[-\metropolis@frametitle@padding]{0pt}{\metropolis@frametitle@padding}
}
\defbeamertemplate{frametitle}{plain}{%
  \nointerlineskip%
  \begin{beamercolorbox}[%
      wd=\paperwidth,%
      sep=0pt,%
      leftskip=\metropolis@frametitle@padding,%
      rightskip=\metropolis@frametitle@padding,%
    ]{frametitle}%
 \metropolis@frametitlestrut@start\insertframetitle\metropolis@frametitlestrut@end%
  \end{beamercolorbox}%
}

Possible solutions for me would be:

  1. Set height of images to height of frame title and do a manual horizontal alignment
  2. Set both heights to a fixed value (e.g. 1cm)
  3. A totally different approach of image placement in the frame title

MWE (Compiled with XeLaTeX):

\documentclass[unknownkeysallowed]{beamer}\usepackage[]{graphicx}\usepackage[]{color}
\usetheme[numbering = fraction]{metropolis} 

\usepackage[american]{babel}

% frametitle images
\addtobeamertemplate{frametitle}{}{%
\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north east,yshift=4.24pt, xshift = 4.2pt] at (current page.north east) {\includegraphics[height=1cm]{example-image-b}};
\end{tikzpicture}}

\addtobeamertemplate{frametitle}{}{%
\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north east,yshift=4.13pt, xshift = -44.8pt] at (current page.north east) {\includegraphics[height=1cm]{example-image-c}};
\end{tikzpicture}}

\begin{document}

% ##############################################################################
\section{Introduction} 

% ------------------------------------------------------------------------------
\begin{frame}{Introduction}
test
\end{frame}

\end{document}

Best Answer

The following solution is a bit ugly, as it first measures the height of the frametitle without the logo and then uses this height for the logo, so basically all code is there two times ...

\documentclass[unknownkeysallowed]{beamer}
%\usepackage{graphicx}
%\usepackage{color}
\usetheme[numbering = fraction]{metropolis} 

\usepackage[american]{babel}

\makeatletter
\newsavebox{\mybox}
\setbeamertemplate{frametitle}{%
  \nointerlineskip%
  \savebox{\mybox}{%
      \begin{beamercolorbox}[%
          wd=\paperwidth,%
          sep=0pt,%
          leftskip=\metropolis@frametitle@padding,%
          rightskip=\metropolis@frametitle@padding,%
        ]{frametitle}%
      \metropolis@frametitlestrut@start\insertframetitle\metropolis@frametitlestrut@end%
      \end{beamercolorbox}%
    }
  \begin{beamercolorbox}[%
      wd=\paperwidth,%
      sep=0pt,%
      leftskip=\metropolis@frametitle@padding,%
      rightskip=\metropolis@frametitle@padding,%
    ]{frametitle}%
  \metropolis@frametitlestrut@start\insertframetitle\metropolis@frametitlestrut@end%
  \hfill%
  \raisebox{-\metropolis@frametitle@padding}{\includegraphics[height=\dimexpr\ht\mybox+\metropolis@frametitle@padding\relax]{example-image}}%
  ~%
    \raisebox{-\metropolis@frametitle@padding}{\includegraphics[height=\dimexpr\ht\mybox+\metropolis@frametitle@padding\relax]{example-image-a}}%
    \hspace*{-\metropolis@frametitle@padding}
  \end{beamercolorbox}%
}
\makeatother

\begin{document}
\begin{frame}
\frametitle{Introduction}
test
\end{frame}
\end{document}

enter image description here


In case one would like to combine this solution with a progress bar under the frametitle:

\documentclass[unknownkeysallowed]{beamer}
%\usepackage{graphicx}
%\usepackage{color}
\usetheme[numbering = fraction, progressbar=frametitle]{metropolis} 

\usepackage[american]{babel}

\makeatletter
\newsavebox{\mybox}
\setbeamertemplate{frametitle}{%
  \nointerlineskip%
  \savebox{\mybox}{%
      \begin{beamercolorbox}[%
          wd=\paperwidth,%
          sep=0pt,%
          leftskip=\metropolis@frametitle@padding,%
          rightskip=\metropolis@frametitle@padding,%
        ]{frametitle}%
      \metropolis@frametitlestrut@start\insertframetitle\metropolis@frametitlestrut@end%
      \end{beamercolorbox}%
    }
  \begin{beamercolorbox}[%
      wd=\paperwidth,%
      sep=0pt,%
      leftskip=\metropolis@frametitle@padding,%
      rightskip=\metropolis@frametitle@padding,%
    ]{frametitle}%
  \metropolis@frametitlestrut@start\insertframetitle\metropolis@frametitlestrut@end%
  \hfill%
  \raisebox{-\metropolis@frametitle@padding}{\includegraphics[height=\dimexpr\ht\mybox+\metropolis@frametitle@padding\relax]{example-image}}%
  ~%
    \raisebox{-\metropolis@frametitle@padding}{\includegraphics[height=\dimexpr\ht\mybox+\metropolis@frametitle@padding\relax]{example-image-a}}%
    \hspace*{-\metropolis@frametitle@padding}
  \end{beamercolorbox}%
}

\addtobeamertemplate{frametitle}{}{%
    \usebeamertemplate*{progress bar in head/foot}
}
\makeatother

\begin{document}
\begin{frame}
\frametitle{Introduction}
test
\end{frame}

\begin{frame}
\frametitle{Introduction}
test
\end{frame}

\end{document}

enter image description here