5

I wanted to add a logo at the top right corner of my frames so I edited the frametitle with \addtobeamertemplate{frametitle} and added the logo using a TikZ \node (as explained in this answer), like this:

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

I would like to adjust the height of the logo so that it matches exactly the height of the frametitle box.

enter image description here

Currently I can only do this by trial and error, changing the image height and the yshift value of the \node. This makes getting the desired result tricky and very time consuming.

As far as I know, the frametitle box is created with the beamercolorbox environment (e.g. here and here). I would like to know if there is a command that gets the height of the frametitle box, so that I can assign it to the logo height like this:

\includegraphics[height=\frametitleheight]{example-image}

Here is my MWE:

\documentclass{beamer}
\usetheme{Madrid}

\usepackage{tikz}

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

\begin{document}

\begin{frame}
\frametitle{Frame title}
Hello world!
\end{frame}

\end{document}

2 Answers 2

3

You can measure the height of the frametitle colour box with the help of the \settoheight macro. Basically you could duplicate the content of the frametitle there, but to avoid the duplication of the content, just save the frametitle color box in a \savebox and re-use this savebox both in the frametitle template and in measuring the height.

\documentclass{beamer}
\usetheme{Madrid}

\usepackage{tikz}

\makeatletter
\setbeamertemplate{frametitle}{%
  \ifbeamercolorempty[bg]{frametitle}{}{\nointerlineskip}%
  \@tempdima=\textwidth%
  \advance\@tempdima by\beamer@leftmargin%
  \advance\@tempdima by\beamer@rightmargin%
  \newsavebox{\mybox}% <- NEW
  \sbox{\mybox}{% <- NEW
      \begin{beamercolorbox}[sep=0.3cm,left,wd=\the\@tempdima]{frametitle}
        \usebeamerfont{frametitle}%
        \vbox{}\vskip-1ex%
        \if@tempswa\else\csname beamer@fteleft\endcsname\fi%
        \strut\insertframetitle\strut\par%
        {%
          \ifx\insertframesubtitle\@empty%
          \else%
          {\usebeamerfont{framesubtitle}\usebeamercolor[fg]{framesubtitle}\insertframesubtitle\strut\par}%
          \fi
        }%
        \vskip-1ex%
        \if@tempswa\else\vskip-.3cm\fi% set inside beamercolorbox... evil here...
      \end{beamercolorbox}%
     }% <- NEW
     \usebox{\mybox}% <- NEW
     \newlength{\frametitelheight}% <- NEW
     \settoheight{\frametitelheight}{\usebox{\mybox}}% <- NEW
}
\makeatother

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

\begin{document}

\begin{frame}
\frametitle{Frame title}
Hello world!
\end{frame}

\end{document}
4
  • Thank you for your answer. I'm having difficulties to understand what you did. Could you please break it down for me? Why do you change the template of the presentation with \setbeamertemplate{frametitle}? Nov 15, 2017 at 13:43
  • @codeaviator I redefined the template to add the height measurement there, the rest of the code is identical to the original definition of the theme you use. Nov 15, 2017 at 13:51
  • Would it be possible to find the yshift value of the TikZ \node automatically? Thank you. Nov 16, 2017 at 14:50
  • The above results into error when one uses environment \begin{block} \end{block}. The error is \mybox is already defined. So can you please tell how to use this along with \begin{block} \end{block}
    – user61681
    Feb 24, 2021 at 15:41
1

I managed to fix several bugs in samcarters solution:

  1. resolved the mybox error mentioned in the comments
  2. added the height of the header to match for themes like Berlin or Darmstadt
  3. Fixed the yshift mentioned in the comments

enter image description here

\documentclass{beamer}
\usetheme{Darmstadt}
\usepackage{tikz}

\makeatletter
\newlength{\frametitleheight}% <- NEW
\newsavebox{\beamer@titlebox}% <- NEW

\setbeamertemplate{frametitle}{%
  \ifbeamercolorempty[bg]{frametitle}{}{\nointerlineskip}%
  \@tempdima=\textwidth%
  \advance\@tempdima by\beamer@leftmargin%
  \advance\@tempdima by\beamer@rightmargin%

  \sbox{\beamer@titlebox}{% <- NEW
      \begin{beamercolorbox}[sep=0.3cm,left,wd=\the\@tempdima]{frametitle}
        \usebeamerfont{frametitle}%
        \vbox{}\vskip-1ex%
        \if@tempswa\else\csname beamer@fteleft\endcsname\fi%
        \strut\insertframetitle\strut\par%
        {%
          \ifx\insertframesubtitle\@empty%
          \else%
          {\usebeamerfont{framesubtitle}\usebeamercolor[fg]{framesubtitle}\insertframesubtitle\strut\par}%
          \fi
        }%
        \vskip-1ex%
        \if@tempswa\else\vskip-.3cm\fi% set inside beamercolorbox... evil here...
      \end{beamercolorbox}%
     }% <- NEW
     \usebox{\beamer@titlebox}% <- NEW
\settoheight{\frametitleheight}{\usebox{\beamer@titlebox}}% <- NEW
\addtolength{\frametitleheight}{\headheight}
}
\makeatother

\addtobeamertemplate{frametitle}{}{%
\begin{tikzpicture}[remember picture,overlay]
\node[inner sep=0, outer sep=0, anchor=north east] at (current page.north east) {\includegraphics[height=\frametitleheight, keepaspectratio]{example-image}};
\end{tikzpicture}
}

\begin{document}

\begin{frame}{Frame Title}
Hello world!
\end{frame}

\begin{frame}
Hello world!
\end{frame}

\begin{frame}{Frame Title}
Hello world!
\end{frame}

\end{document}

If you only want to match the headline, for instance when some frames have no title, but only the headline, use instead

\addtobeamertemplate{headline}{}{%
\begin{tikzpicture}[remember picture,overlay]
\node[inner sep=0, outer sep=0, anchor=north east] at (current page.north east) {\includegraphics[height=\headheight, keepaspectratio]{example-image}};
\end{tikzpicture}
}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.