设计自定义标题页模板时如何检测日期字段是否为空?

设计自定义标题页模板时如何检测日期字段是否为空?

我正在尝试创建自定义标题页,但我无法编写宏来检测日期字段是否为空。如果是空的,block则应直接绕过日期输入。

在此处输入图片描述

上面的图片看起来很奇怪,因为显示了日期块标题,但实际日期是空的。当然,对于这种情况,我可以手动删除日期块,但我仍然希望这能自动完成。我还想知道如何确定某个字段是否为空。

\documentclass[aspectratio=169, xcolor={x11names}]{beamer}


\usecolortheme{rose}

    \setbeamercolor{itemize item}{fg=black}

\useoutertheme[]{tree}

\useinnertheme{inmargin}

    \setbeamersize{text margin left=2mm, text margin right=2mm}

    \newlength{\sidebarWidth}
    \setlength{\sidebarWidth}{0.2\paperwidth}

    \setbeamersize{sidebar width left=\sidebarWidth, sidebar width right=0cm}

\usefonttheme{structurebold}


\title{The Main Title of The Presentation}
\subtitle{Short Title Goes here}
\author{Al-Motasem Aldaoudeyeh}
\institute{North Dakota State University}
\date{}

\setbeamertemplate{headline}{}

\setbeamertemplate{date}{nothing}

\setbeamercolor{block title}{fg=blue}
\setbeamerfont{title page}{shape=\bfseries}


% Custome title page
\setbeamertemplate{title page}{
    \vspace*{\stretch{3}}
    \begin{block}{}
        \usebeamercolor[fg]{title page}\usebeamerfont{block title}\usebeamertemplate{block title}
        \inserttitle
    \end{block}
    \begin{block}{}
        \usebeamercolor[fg]{title page}\usebeamerfont{block title}\usebeamertemplate{block title}
        \large \insertsubtitle
    \end{block}
    \vspace{\stretch{2}}
    \begin{block}{Author}
        \insertauthor
    \end{block}
    \begin{block}{Institute}
        \insertinstitute
    \end{block}
    \begin{block}{Date}
        \insertdate
    \end{block}
    \vspace{\stretch{5}}
                            }

\begin{document}

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

\end{document}

答案1

如果日期确实是空的(例如,如果您的文档包含\date{}),那么您可以使用以下方法进行测试

\ifx\insertdate\empty%
\else%
   \begin{block}{Date}
       \insertdate
   \end{block}   
\fi

请注意,如果您的文档不包含任何\date命令,则默认显示当前日期。

\documentclass[aspectratio=169, xcolor={x11names}]{beamer}

\usecolortheme{rose}
\setbeamercolor{itemize item}{fg=black}
\useoutertheme[]{tree}
\useinnertheme{inmargin}

\setbeamersize{text margin left=2mm, text margin right=2mm}
\newlength{\sidebarWidth}
\setlength{\sidebarWidth}{0.2\paperwidth}

\setbeamersize{sidebar width left=\sidebarWidth, sidebar width right=0cm}

\usefonttheme{structurebold}


\title{The Main Title of The Presentation}
\subtitle{Short Title Goes here}
\author{Al-Motasem Aldaoudeyeh}
\institute{North Dakota State University}
\date{}

\setbeamertemplate{headline}{}

\setbeamertemplate{date}{nothing}

\setbeamercolor{block title}{fg=blue}
\setbeamerfont{title page}{shape=\bfseries}


% Custome title page
\setbeamertemplate{title page}{
    \vspace*{\stretch{3}}
    \begin{block}{}
        \usebeamercolor[fg]{title page}\usebeamerfont{block title}\usebeamertemplate{block title}
        \inserttitle
    \end{block}
    \begin{block}{}
        \usebeamercolor[fg]{title page}\usebeamerfont{block title}\usebeamertemplate{block title}
        \large \insertsubtitle
    \end{block}
    \vspace{\stretch{2}}
    \begin{block}{Author}
        \insertauthor
    \end{block}
    \begin{block}{Institute}
        \insertinstitute
    \end{block}
    \ifx\insertdate\empty%
    \else%
       \begin{block}{Date}
           \insertdate
       \end{block}   
    \fi
    \vspace{\stretch{5}}
                            }


\begin{document}

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

\end{document}

在此处输入图片描述

答案2

这是一个答案适用于空日期或“白色”日期

\documentclass[aspectratio=169, xcolor={x11names}]{beamer}

% Added code:
\makeatletter
\newcommand{\AddDateBox}{\xdef\mystring{\insertdate}\sbox1{\hbox{\mystring}}\sbox2{\hbox{\space}}\ifdim\wd1=\wd2\relax\else\ifx\insertdate\empty\relax\else\begin{block}{Date}\insertdate\end{block}\fi\fi}
\makeatother


\usecolortheme{rose}

\setbeamercolor{itemize item}{fg=black}

\useoutertheme[]{tree}

\useinnertheme{inmargin}

    \setbeamersize{text margin left=2mm, text margin right=2mm}

    \newlength{\sidebarWidth}
    \setlength{\sidebarWidth}{0.2\paperwidth}

    \setbeamersize{sidebar width left=\sidebarWidth, sidebar width right=0cm}

\usefonttheme{structurebold}


\title{The Main Title of The Presentation}
\subtitle{Short Title Goes here}
\author{Al-Motasem Aldaoudeyeh}
\institute{North Dakota State University}
\date{}

\setbeamertemplate{headline}{}

\setbeamertemplate{date}{nothing}

\setbeamercolor{block title}{fg=blue}
\setbeamerfont{title page}{shape=\bfseries}


% Custome title page
\setbeamertemplate{title page}{
    \vspace*{\stretch{3}}
    \begin{block}{}
        \usebeamercolor[fg]{title page}\usebeamerfont{block title}\usebeamertemplate{block title}
        \inserttitle
    \end{block}
    \begin{block}{}
        \usebeamercolor[fg]{title page}\usebeamerfont{block title}\usebeamertemplate{block title}
        \large \insertsubtitle
    \end{block}
    \vspace{\stretch{2}}
    \begin{block}{Author}
        \insertauthor
    \end{block}
    \begin{block}{Institute}
        \insertinstitute
    \end{block}
    \AddDateBox
    \vspace{\stretch{5}}
                            }

\begin{document}

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

\end{document}

相关内容