You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在自定义JuanLesPins风格Beamer头部行间添加渐变效果?

实现Beamer JuanLesPins主题头部渐变效果

要给定制的headline添加平滑渐变背景,beamercolorbox本身不支持直接设置渐变,但可以通过PGF/TikZ的渐变功能实现,以下是具体方案:

关键思路

  • 使用\pgfdeclarehorizontalshading定义水平渐变,匹配标题、章节、子章节栏的高度和主题颜色
  • 在每个beamercolorbox中先填充渐变背景,再放置内容
  • 保留原有的条件判断逻辑,仅在存在对应内容时显示栏位

修改后的完整代码

\documentclass{beamer}

\usetheme{JuanLesPins}
\usepackage{tikz}
\usetikzlibrary{shadings}

\makeatletter
% 定义各栏目的水平渐变:从左侧主题色平滑过渡到浅色调
\pgfdeclarehorizontalshading{title_head_shading}{\dimexpr2ex+1ex\relax}{%
  color(0pt)=(title in head/foot.bg);
  color(\paperwidth)=(title in head/foot.bg!20!white)
}

\pgfdeclarehorizontalshading{section_head_shading}{\dimexpr2ex+1ex\relax}{%
  color(0pt)=(section in head/foot.bg);
  color(\paperwidth)=(section in head/foot.bg!20!white)
}

\pgfdeclarehorizontalshading{subsection_head_shading}{\dimexpr2ex+1ex\relax}{%
  color(0pt)=(subsection in head/foot.bg);
  color(\paperwidth)=(subsection in head/foot.bg!20!white)
}

\setbeamertemplate{headline}{%
  % 标题栏:带渐变背景
  \begin{beamercolorbox}[wd=\paperwidth,ht=2ex,dp=1ex]{title in head/foot}
    \pgfuseshading{title_head_shading}% 填充渐变背景
    \vbox to 2ex{\vfil\hspace*{2ex}\inserttitle\hspace*{2ex}\vfil}% 内容垂直居中
  \end{beamercolorbox}
  
  % 仅当存在章节时显示章节栏
  \ifx\insertsectionhead\@empty
  \else
    \begin{beamercolorbox}[wd=\paperwidth,ht=2ex,dp=1ex]{section in head/foot}
      \pgfuseshading{section_head_shading}
      \vbox to 2ex{\vfil\hspace*{4ex}\insertsectionhead\hspace*{2ex}\vfil}
    \end{beamercolorbox}
    
    % 仅当存在子章节时显示子章节栏
    \ifx\insertsubsectionhead\@empty
    \else
      \begin{beamercolorbox}[wd=\paperwidth,ht=2ex,dp=1ex]{subsection in head/foot}
        \pgfuseshading{subsection_head_shading}
        \vbox to 2ex{\vfil\hspace*{6ex}\insertsubsectionhead\hspace*{2ex}\vfil}
      \end{beamercolorbox}
    \fi
  \fi
}
\makeatother

\title{My Custom Beamer Theme}
\subtitle{Subtitle}

\begin{document}

\begin{frame}
    \titlepage
\end{frame}

\section{First section}
\begin{frame}
    This frame is the only one of the section.
\end{frame}

\section{Second section}
\subsection{First subsection} 
\begin{frame}
    Now look, the subsection line was added in the header.
\end{frame}

\subsection{Second subsection} 
\begin{frame}
    It's still here, nice.
\end{frame}

\end{document}

细节说明

  • 渐变定义:\pgfdeclarehorizontalshading的第二个参数是渐变高度,和beamercolorbox的ht+dp保持一致(2ex+1ex),避免出现错位。
  • 颜色过渡:使用主题原生的title in head/foot.bg等颜色,通过!20!white混合白色实现平滑渐变,你可以根据原版效果调整比例(比如改成!50!white或直接white)。
  • 内容居中:用\vbox包裹内容并设置垂直居中,确保文字不会被渐变背景覆盖。
  • 兼容性:该代码可直接在pdflatex下编译,无需更换编译器。

内容的提问来源于stack exchange,提问作者Ennpeh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 05:27:02