1

I am trying to create a beamer template where everything is top-aligned by default. For those slides without a title, I would like to add a top margin automatically. The CASE 2 in the MWE below is what I come up with. Unfortunately, it fails to increase top margin. In comparison, I included CASE 1, which works properly in terms of adding margin only. I am not sure why putting the minipage inside some if block will change the output.

Output of CASE 1:

enter image description here

Output of CASE 2:

enter image description here

\documentclass[t]{beamer}
\usepackage{expl3}

\ExplSyntaxOn
% CASE 1:
% works normally
\setbeamertemplate{headline}{
  \begin{minipage}[t][1cm][t]{0.8\paperwidth}
  abc\par
  def\par
  ghi\par
  \end{minipage}  
}

% CASE 2:
% not working
\setbeamertemplate{headline}{
  % get frame number
  \tl_set:Nx \l_tmpa_tl {\insertframenumber}
  % do not add margin to the title page
  \tl_if_eq:NnF \l_tmpa_tl {1} {
    \cs_if_exist:NT \insertframetitle {
      \hcoffin_set:Nn \l_tmpa_coffin {\insertframetitle}
      \dim_compare:nNnT {\coffin_wd:N \l_tmpa_coffin} < {0.1pt} {
        % add spacing when there is no title
        \begin{minipage}[t][1cm][t]{0.8\paperwidth}
        abc\par
        def\par
        ghi\par
        \end{minipage}  
      }
    }
  }
}


\ExplSyntaxOff
\begin{document}

\maketitle

\begin{frame}{title}
conten...
\end{frame}

\begin{frame}
content...
\end{frame}

\end{document}


8
  • Excuse me, but what exactly are you trying to achieve? – Skillmon Oct 29 '20 at 19:21
  • And is there a particular reason to use a coffin instead of a box? – Skillmon Oct 29 '20 at 19:24
  • @Skillmon As I said, I want to add margins to frames without titles automatically so that the content is not so close to the top border. As for the use of l3coffins, it is merely a way to determine if the frame has a title. At this moment, I cannot figure out a more direct approach. – Alan Xiang Oct 30 '20 at 0:24
  • The coffin is meant to be a structure to be typeset/joined with other coffins with some handles. That's an unnecessary overhead if you just want to see whether something has a width when typeset. Instead you could use \hbox_set:Nn and \box_wd:N (that's all I meant with my second comment). – Skillmon Oct 30 '20 at 7:09
  • And should that margin have typeset contents? Or is it just some white space? – Skillmon Oct 30 '20 at 7:13
2

This wasn't created by me, I just adapted it to fit this use case. If you think this deserves an upvote, you should really upvote the original answer by @samcarter.

The following reuses ideas from @samcarter (but patches the environment, like proposed there).

We patch beamer's internals to add a noframetitle template that gets evoked when there is no \frametitle in a frame. This noframetitle template will add some white space. You can modify that template to your liking, I just put a \rule there with width 0pt (so it is invisible), the height I got from trial-and-error to match the spacing of a frame with a title.

\documentclass[t]{beamer}

\makeatletter
\patchcmd\endbeamer@frameslide
  {\setbox\beamer@frametitlebox=\box\voidb@x}
  {%
    % no change for title page
    \ifnum\c@framenumber=1
      \setbox\beamer@frametitlebox=\box\voidb@x
    \else
      \setbox\beamer@frametitlebox=\vbox{%
        \vbox{}%
        {\parskip0pt\usebeamertemplate***{noframetitle}\vskip0.25em}%
      }%
    \fi
  }
  {}
  {\GenericError{}{Error: Failed to patch beamer's frameslide}{}{}}
\makeatother
\setbeamertemplate{noframetitle}{\rule{0pt}{6.7mm}}

\begin{document}

\maketitle

\begin{frame}{title}
  content\ldots
\end{frame}

\begin{frame}
  content\ldots
\end{frame}

\end{document}

enter image description here

enter image description here

enter image description here

2
  • Thanks! This is very clever, and you must have deep understanding with respect to how beamer works internally. I need to achieve this because I am replicating one of the existing PowerPoint templates with LaTeX. Surprisingly it is nontrivial to add some small margin. – Alan Xiang Oct 30 '20 at 12:07
  • @AlanXiang no, I don't. @samcarter has, I just know a bit about TeX programming, beamer is way above my head (everything is different in it because it basically has to change everything for its job). And I know your pain, I had to recreate such a template a few years ago myself (that's when I asked and @samcarter provided above hack). – Skillmon Oct 30 '20 at 12:37

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.