调整徽标高度以匹配 \frametitle 框高度

调整徽标高度以匹配 \frametitle 框高度

我想在我的框架的右上角添加一个徽标,所以我编辑了,frametitle\addtobeamertemplate{frametitle}使用 TikZ 添加徽标\node(如这个答案), 像这样:

\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}}

我想调整徽标的高度,以便它与frametitle盒子的高度完全匹配。

在此处输入图片描述

目前我只能通过反复试验来做到这一点,改变图像heightyshift的值\node。这使得获得所需结果变得棘手且非常耗时。

据我所知,该frametitle框是通过beamercolorbox环境创建的(例如这里这里)。我想知道是否有一个命令可以获取框的高度frametitle,以便我可以像这样将其分配给徽标高度:

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

这是我的 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}

答案1

您可以借助宏来测量框架标题颜色框的高度\settoheight。基本上,您可以在那里复制框架标题的内容,但为了避免内容重复,只需将框架标题颜色框保存在 a 中,\savebox然后在框架标题模板和测量高度时重新使用此保存框。

\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}

答案2

我设法修复了 samcarters 解决方案中的几个错误:

  1. 解决了mybox评论中提到的错误
  2. 增加了标题的高度以匹配柏林或达姆施塔特等主题
  3. 修复了yshift评论中提到的问题

在此处输入图片描述

\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}

如果您只想匹配headline,例如当某些框架没有标题,只有标题时,请使用

\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}
}

在此处输入图片描述

相关内容