beamer 中的 pgfkeys 和列

beamer 中的 pgfkeys 和列

我正在尝试引入一些宏,以便让我在 beamer 中处理列时更加轻松。这些宏应该采用键值对来自定义宽度、高度、对齐方式等。我在使用 pgfkeys 时遇到了无法正确更新/初始化的问题。

下面是我简化为仅使用列宽作为参数的最小示例。以下是有效和无效的内容:

  1. 当我使用两个小页面并指定列宽(例如\leftcolumn[width=0.5])时它可以工作;
  2. 使用 minipages 时,框架中的默认参数\leftcolumn不起作用(请尝试删除[width=0.5]);
  3. 使用columns环境(尝试取消注释该块% --- Macros using columns...仅适用于第一列,但不适用于第二列。

非常感谢您的帮助或建议!

\documentclass[11pt]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{etoolbox}   
\makeatletter

% Make sure the macros exist. They will hold the relative width.
\newcommand*\leftwidth{}
\newcommand*\rightwidth{}

\pgfkeys{
    /slide/.cd ,
    leftcolumn/width/.code = \def\leftwidth{#1} ,
    leftcolumn/width/.default = 0.6 ,
    rightcolumn/width/.code = \def\rightwidth{#1} ,
    rightcolumn/width/.default = 0.4 ,
}

%%%%% Either use the following 3 macros or uncomment the      %%%%
%%%%% 3 macros below (which use columns instead of minipages) %%%%
%%%%% and comment out the first 3 macros                      %%%%
% ------- Macros using minipages - they work OK -------
\newrobustcmd*\leftcolumn[1][]{
    \pgfqkeys{/slide/leftcolumn}{#1}%
    \begin{minipage}{\leftwidth\linewidth}
}

\newrobustcmd*\rightcolumn[1][]{
    \end{minipage}\hfill
    \pgfqkeys{/slide/rightcolumn}{#1}
    \begin{minipage}{\rightwidth\linewidth}
}

\renewrobustcmd*\endcolumn{\end{minipage}}

% % ------- Macros using columns - they don't work -------
% \newrobustcmd*\leftcolumn[1][]{
%     \begin{columns} 
%         \pgfqkeys{/slide/leftcolumn}{#1}%
%         \column{\leftwidth\linewidth}
%         left width: |\leftwidth| * linewidth\\[1em]  % just some output
% }
%
% \newrobustcmd*\rightcolumn[1][]{
%     \pgfqkeys{/slide/rightcolumn}{#1}
%     \column{\rightwidth\linewidth}
%     right width: |\rightwidth| * linewidth\\[1em]  % \rightwidth is empty!
% } 
%
% \renewrobustcmd*\endcolumn{
%     \end{columns}
% }

\makeatother

\begin{document}

\begin{frame}[t]{}
    Using two minipages works, except for the default values: removing the [width=0.5] does not fall back to the default value of 0.6.\\[1em]

    \leftcolumn[width=0.5]  % does not work without [...]!
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

    \rightcolumn[width=0.35]
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

    \endcolumn  
\end{frame}    
\end{document}

答案1

至于你的问题,我只会设置一些初始值。此外,我不会引入显式宏,而是使用\pgfkeysvalueof

\documentclass[11pt]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{etoolbox}   
\makeatletter


\pgfkeys{
    /slide/.cd ,
    leftcolumn/width/.initial = 0.55 ,
    rightcolumn/width/.initial = 0.4 ,
}

% ------- Macros using minipages - they work OK -------
\newrobustcmd*\leftcolumn[1][]{%
    \pgfkeys{/slide/leftcolumn/.cd,#1}%
    \begin{minipage}{\pgfkeysvalueof{/slide/leftcolumn/width}\linewidth}
}

\newrobustcmd*\rightcolumn[1][]{%
    \end{minipage}\hfill
    \pgfkeys{/slide/rightcolumn/.cd,#1}%
    \begin{minipage}{\pgfkeysvalueof{/slide/rightcolumn/width}\linewidth}
}

\renewrobustcmd*\endcolumn{\end{minipage}}


\makeatother

\begin{document}

\begin{frame}[t]
    \frametitle{Setting explicit values}
    Using two minipages works.\\[1em]

    \leftcolumn[width=0.5]  % does not work without [...]!
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

    \rightcolumn[width=0.35]
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

    \endcolumn  
\end{frame}    

\begin{frame}[t]
    \frametitle{Setting no values will use the initial values}
    Using two minipages works.\\[1em]

    \leftcolumn  % works without [...]
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

    \rightcolumn
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

    \endcolumn  
\end{frame}    

\end{document}

在此处输入图片描述

或者使用列。(由于 TeX 分组,有点棘手。)

\documentclass[11pt]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{etoolbox}   
\makeatletter


\pgfkeys{
    /slide/.cd ,
    leftcolumn/width/.initial = 0.55 ,
    rightcolumn/width/.initial = 0.4 ,
}

% ------- Macros using minipages - they work OK -------
\newrobustcmd*\leftcolumn[1][]{%
     \pgfkeys{/slide/leftcolumn/.cd,#1}%
    \begin{columns} 
    \column{\pgfkeysvalueof{/slide/leftcolumn/width}\linewidth}
        left width: |\pgfkeysvalueof{/slide/leftcolumn/width}| * linewidth\\[1em]  % just some output
}

\newrobustcmd*\rightcolumn[1][]{%
 \pgfkeys{/slide/rightcolumn/.cd,#1}% 
 \xdef\tmp{\pgfkeysvalueof{/slide/rightcolumn/width}}% smuggle out of group
 \column{\tmp\linewidth}
 \pgfkeys{/slide/rightcolumn/.cd,#1}% 
    right width: |\pgfkeysvalueof{/slide/rightcolumn/width}| * linewidth\\[1em]  % \rightwidth is empty!
} 

\renewrobustcmd*\endcolumn{%
    \end{columns}
}


\makeatother

\begin{document}

\begin{frame}[t]
    \frametitle{Setting explicit values}
    Using columns works.\\[1em]

    \leftcolumn[width=0.4]  % does not work without [...]!
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

    \rightcolumn[width=0.25]
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

    \endcolumn  
\end{frame}    

\begin{frame}[t]
    \frametitle{Setting no values will use the initial values}
    Using columns works.\\[1em]

    \leftcolumn  % works without [...]
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

    \rightcolumn
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

    \endcolumn  
\end{frame}    

\end{document}

在此处输入图片描述

答案2

textpos包装

    \documentclass[11pt]{beamer}
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}

    \usepackage[absolute,overlay
    %,showboxes
    ]{textpos}
    \TPGrid{1}{1}

    \begin{document}

    \begin{frame}
    \frametitle{columns with textpos package}
    \begin{textblock}{0.5}(0.05,0.1)%column with width of 0.5\paperwidth at 0.05 \paperwidth and 0.1\paperheight from the upper left corner
       Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    \end{textblock}
    %
    \begin{textblock}{0.35}(0.6,0.2)%column with width of 0.35\paperwidth at 0.6\paperwidth and 0.2\paperheight from the upper left corner
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
    \end{textblock} 
    \end{frame}    
    \end{document}

在此处输入图片描述

相关内容