3

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}

2 Answers 2

2

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


If you are using moloch, the updated fork of metropolis, you could redefine the frametitle to add logos:

\documentclass{beamer}
\usetheme{moloch}% modern fork of the metropolis theme 

\makeatletter
\setbeamertemplate{frametitle}{%
  \nointerlineskip%
  \begin{beamercolorbox}[%
      wd=\paperwidth,
      sep=0ex,
      leftskip=0.5cm,
      rightskip=0.0cm,
    ]{frametitle}%
    \begin{minipage}{.65\linewidth}
      \usebeamerfont{frametitle}%
      \vbox{}\vskip-0.4ex%
      \strut\insertframetitle\nolinebreak\strut\par%
      {%
        \ifx\insertframesubtitle\@empty%
          \else%
          {\usebeamerfont{framesubtitle}\vspace{-0.8ex}\usebeamercolor[fg]{framesubtitle}\strut\insertframesubtitle\strut\par}%
        \fi
      }%
    \end{minipage}%
    \hfill
    \begin{minipage}{.3\linewidth}
      \mbox{}\hfill\includegraphics[height=1cm]{example-image-duck}~\includegraphics[height=1cm,page=2]{example-image-duck}~
    \end{minipage}%
  \end{beamercolorbox}%
  \usebeamertemplate*{progress bar in head/foot}
}
\makeatother

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

enter image description here

4
  • Looks promising! As long as it works I do not mind the code. Is there a way to also align them horizontally without having a gap between the graphics?
    – pat-s
    Oct 14, 2016 at 14:08
  • 1
    @pat-s I introduced the gap with ~%, because it seems to be there in your image. Oct 14, 2016 at 14:12
  • Unfortunately, your solution makes progressbar in the frametitle disappear. Apr 22, 2017 at 21:30
  • @WolfRamovsky I have provided an additional solution for the special case of a progress bar under the frametitle. Apr 23, 2017 at 11:47
2

Update

Finally! The solution which doesn't require to copy-paste the current definition of the frametitle template definition, i.e. robust to this definition updates!

\newsavebox{\mybox}
\makeatletter
\addtobeamertemplate{frametitle}{
    \begin{lrbox}{\mybox}
        \begin{minipage}{1.00\textwidth}
}{
        \end{minipage}
    \end{lrbox}
    \nointerlineskip
    \usebox{\mybox}
    \tikzexternaldisable
    \tikz[remember picture,overlay,every node/.style={inner sep=0,outer sep=0}]
        \node[anchor=north east] at (current page.north east)
        {
            \includegraphics[height=\dimexpr\ht\mybox+\metropolis@frametitle@padding\relax]{example-image}
            ~%
            \includegraphics[height=\dimexpr\ht\mybox+\metropolis@frametitle@padding\relax]{example-image-a}
        }
    ;
    \tikzexternalenable
}
\makeatletter

Unfortunately, the \appendix still cancels the effect of this \addtobeamertemplate, so redo it after the \appendix command.

Old answer

The solution from the accepted answer breaks progressbar in frametitle for me. Progressbar just disappears. So I've rewritten it a bit:

\makeatletter
\@ifundefined{mybox}{%
    \newsavebox{\mybox}%
}{}%
\addtobeamertemplate{frametitle}{%
    \savebox{\mybox}{%
        \begin{beamercolorbox}[%
            wd=\paperwidth,%
            sep=0pt,%
            leftskip=\metropolis@frametitle@padding,%
            rightskip=\metropolis@frametitle@padding,%
        ]{frametitle}%
            \metropolis@frametitlestrut@start%
            \insertframetitle%
            \nolinebreak%
            \metropolis@frametitlestrut@end%
        \end{beamercolorbox}%
    }%
}{%
    \tikzexternaldisable%
    \tikz[remember picture,overlay,every node/.style={inner sep=0,outer sep=0}]
        \node[anchor=north east] at (current page.north east)
        {
            \includegraphics[height=\dimexpr\ht\mybox+\metropolis@frametitle@padding\relax]{example-image}
            ~%
            \includegraphics[height=\dimexpr\ht\mybox+\metropolis@frametitle@padding\relax]{example-image-a}
        }
    ;
}
\makeatother

It would be great if someone somehow replace hardcoded copy-pasted definition of frametitle beamer template inside \savebox with macro which expands into current definition of the template.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .