Beamer:使用 \label 块添加垂直空间

Beamer:使用 \label 块添加垂直空间

\label当向类中的块添加时beamer,会在内容前插入额外的垂直空间。

使用默认块模板时不会发生这种情况,但使用rounded模板设置会引入空间。

据我所知,apgfpicture用于圆角块,据我所知,tcolorbox包也使用pgfpicter。事实上,对于tcolorboxes,也添加了垂直空间。所以我的猜测是,它与此有关。


我怎样才能避免这种行为?

一个简单但不太干净的解决方法是添加一个负面的\vspace,但我宁愿了解这个的起源......

期待您的回答:)


梅威瑟:

\documentclass[9pt]{beamer}

\setbeamercolor{block body}{bg=red!5}
\setbeamercolor{block title}{fg=black,bg=red!40}

\usepackage{tcolorbox}

\begin{document}
    \begin{frame}
        \begin{block}{Regular block}\label{thm:1}
            I have a label.
        \end{block}
        \begin{block}{Regular block}
            I don't have a label.
        \end{block}

        \setbeamertemplate{blocks}[rounded][shadow=false]

        \begin{block}{Round block}\label{thm:2}%
            I have a label.
        \end{block}
        \begin{block}{Round block}
            I don't have a label.
        \end{block}

        \begin{tcolorbox}[title={tcolorbox}]\label{thm:3}
            I have a label.
        \end{tcolorbox}
        \begin{tcolorbox}[title={tcolorbox}]
            I don't have a label.
        \end{tcolorbox}
    \end{frame}
\end{document}

生成:在此处输入图片描述


更新:按照@koleygr的建议,我打开了一个问题在 GitHub 上的 repo中beamer

答案1

这是通过重新定义环境来修复的:

\documentclass[9pt]{beamer}
\setbeamercolor{block body}{bg=red!5}
\setbeamercolor{block title}{fg=black,bg=red!40}

\usepackage{tcolorbox}
\newlength\myFSize
\newlength\myFontSize
\makeatletter
\def\ReadFSize{%
\setlength{\myFSize}{\f@size pt}
\setlength{\myFontSize}{0.5\myFSize}}
\makeatother

\newcounter{emptyLabelCounter}

\newenvironment{myRBlock}[2]{\def\X{#2}\begin{block}{#1}\ifx\X\empty\stepcounter{emptyLabelCounter}\label{labelEmpty:\X}\else\label{#2}\fi\ReadFSize\vskip-\myFontSize\par}{\end{block}\vskip-\myFontSize}
\newenvironment{myTcolorbox}[2][]{\ReadFSize\par\begin{tcolorbox}[#1]\ifx\X\empty\stepcounter{emptyLabelCounter}\label{labelEmpty:\X}\else\label{#2}\fi\vskip-0.6\myFontSize}{\end{tcolorbox}}

\begin{document}
    \begin{frame}
        \begin{block}{Regular block}\label{thm:1}%
            I have a label.
        \end{block}
        \begin{block}{Regular block}
            I don't have a label.
        \end{block}

        \setbeamertemplate{blocks}[rounded][shadow=true]

        \begin{myRBlock}{Round block}{thm:2}
            I have a label.
        \end{myRBlock}
        \Large
        \begin{myRBlock}{Round block}{}
            I don't have a label.
        \end{myRBlock}

        \begin{myTcolorbox}[title={tcolorbox}]{thm:3}%
            I have a label.
        \end{myTcolorbox}
        \begin{myTcolorbox}[title={tcolorbox}]{}
            I don't have a label.
        \end{myTcolorbox}
    \end{frame}
\end{document}

PS:我为每个定义添加了一个标签参数,如果为空则添加一个自动标签。

在此处输入图片描述

答案2

使用风险自负(来自https://github.com/josephwright/beamer/issues/541#issuecomment-552993755):

\documentclass[9pt]{beamer}

\setbeamercolor{block body}{bg=red!5}
\setbeamercolor{block title}{fg=black,bg=red!40}

\usepackage{tcolorbox}

\makeatletter
\def\beamer@inserttarget#1{%
  \ifbeamer@inframe%
    #1%
  \else% defer to next frame
    \expandafter\gdef\expandafter\beamer@framehypertargets\expandafter{\beamer@framehypertargets#1}%
  \fi%
}
\makeatother

\begin{document}
    \begin{frame}
        \begin{block}{Regular block}\label{thm:1}
            I have a label.
        \end{block}
        \begin{block}{Regular block}
            I don't have a label.
        \end{block}

        \setbeamertemplate{blocks}[rounded][shadow=false]

        \begin{block}{Round block}\label{thm:2}%
            I have a label.
        \end{block}
        \begin{block}{Round block}
            I don't have a label.
        \end{block}

        \begin{tcolorbox}[title={tcolorbox}]\label{thm:3}
            I have a label.
        \end{tcolorbox}
        \begin{tcolorbox}[title={tcolorbox}]
            I don't have a label.
        \end{tcolorbox}
    \end{frame}
\end{document}

在此处输入图片描述

相关内容