自动将 beamercolorbox 高度设置为 frametitle 上的姊妹 beamercolorbox

自动将 beamercolorbox 高度设置为 frametitle 上的姊妹 beamercolorbox

我正在尝试创建一个自定义的 Beamer 主题。但是,我想在框架标题旁边添加一些颜色,但我无法自动调整栏的高度。我尝试通过添加一些跳过量来解决这个问题,如果所有标题的高度相同(一行),它就会起作用。但如果这种情况发生变化,主题就不起作用了。

那么,如何才能将左侧颜色框的高度调整为右侧标题的高度。

\documentclass{beamer}
\definecolor{secinhead}{RGB}{249,196,95}
\definecolor{titlebg}{RGB}{51,51,51}

\setbeamercolor{secsubsec}{fg=secinhead,bg=secinhead}
\setbeamercolor{frametitle}{fg=secinhead,bg=titlebg}

\makeatletter
\setbeamertemplate{frametitle}{%
  \nointerlineskip%
  \usebeamerfont{headline}%
  \nointerlineskip%
  \hbox{\hspace{-0.09\paperwidth}%
  \begin{beamercolorbox}[wd=0.1\paperwidth,vmode]{secsubsec}%
    \newdimen\titleheight%
    \setbox0=\hbox{\usebeamerfont*{frametitle}\insertframetitle}
    \titleheight=\ht0 \advance\titleheight by \dp0%
    \vskip-.5pt%
    \vskip\titleheight%
    \ifx\insertframesubtitle\@empty%
      \strut\par%
    \else%
      \setbox0=\hbox{\usebeamerfont*{framesubtitle}\insertframesubtitle}%
      \titleheight=\ht0 \advance\titleheight by \dp0%
      \par{%
      \vskip\titleheight%
      \strut\par%
      \vskip-.65ex%
      }%
    \fi%
    \usebeamerfont{headline}%
    \vskip.5ex%
  \end{beamercolorbox}%
  \begin{beamercolorbox}[wd=0.99\paperwidth,leftskip=.3cm,rightskip=.3cm plus1fil,vmode]{frametitle}%
    \vskip.5ex%
    \usebeamerfont*{frametitle}\insertframetitle%
    \ifx\insertframesubtitle\@empty%
      \strut\par%
    \else%
      \par{\usebeamerfont*{framesubtitle}{\usebeamercolor[fg]{framesubtitle}\insertframesubtitle}\strut\par}%
    \fi%%
    \usebeamerfont{headline}%
    \vskip.5ex%
  \end{beamercolorbox}%
  }
  \nointerlineskip
}
\makeatother

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

\begin{frame}{longer title test to demonstrate it is not working}
content...
\end{frame}

\begin{frame}{even longer longer title test to demonstrate it is not working}
content...
\end{frame}
\end{document}

在此处输入图片描述

请看左图,所有黄色标记都有不同的高度。

答案1

使用的解决方案tcolorbox包裹:

\documentclass{beamer}
\usepackage[many]{tcolorbox}

\definecolor{secinhead}{RGB}{249,196,95}
\definecolor{titlebg}{RGB}{51,51,51}

\setbeamercolor{secsubsec}{fg=secinhead,bg=secinhead}
\setbeamercolor{frametitle}{fg=secinhead,bg=titlebg}

\makeatletter
\setbeamertemplate{frametitle}{%
  \nointerlineskip%
  \usebeamerfont{headline}%
  \nointerlineskip%
  \hbox{\hspace{-0.09\paperwidth}%
  \begin{tcolorbox}[
    enhanced,
    boxrule=15pt,
    colframe=secinhead,
    arc=0pt,
    outer arc=0pt,
    colback=titlebg,
    colupper=secinhead,
    width=\paperwidth+2mm,
    toprule=0pt,
    bottomrule=0pt,
    rightrule=0pt,
    left=15pt,
  ]%
    {\usebeamercolor{frametitle}\usebeamerfont*{frametitle}\strut\insertframetitle\strut}
    \ifx\insertframesubtitle\@empty%
      \strut\par%
    \else%
     \par{\usebeamerfont*{framesubtitle}{\usebeamercolor[fg]{framesubtitle}\insertframesubtitle}\par}%
    \fi
  \end{tcolorbox}%
  }
  \nointerlineskip
}
\makeatother

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

\begin{frame}{longer title test to demonstrate it is working}
content...
\end{frame}

\begin{frame}{even longer longer title test to demonstrate it is working even for several lines}{And even with a subtitle}
content...
\end{frame}

\end{document}

在此处输入图片描述

相关内容