如何使 pgfooclass 在 beamer 演示中持久存在?

如何使 pgfooclass 在 beamer 演示中持久存在?

我想定义一个 tikz 图像元素,以便在 beamer 演示中重复使用。我尝试将其定义为 pgfooclass,但这似乎是因为我只能在已定义类的框架中实例化该类的对象,同样,我只能在已实例化的框架中使用实例化的对象。

如果我尝试在框架中实例化在框架外定义的类的对象,则会收到“未知类”错误。我可以在框架外实例化此类对象,但当我尝试在框架内使用该对象时,会收到“未定义的控制序列”错误。

已解决:问题出在ignorenonframetextbeamer 选项上。事实证明,pgfooclasses 可以在 之前定义\begin{document},然后可以在框架中使用,即使使用ignorenonframetext选项

例子:

\documentclass[ignorenonframetext]{beamer}
%\documentclass{beamer}

\usepackage{tikz}
\usepgfmodule{oo}


\author{bli}

% defining a class before the document
\pgfooclass{outdocclass}{
    \method outdocclass(){}    
    \method draw(){
        \node (outdoc) at (0,0) {outdoc};
    }
}

\begin{document}

% defining a class outside a frame
\pgfooclass{outclass}{
    \method outclass(){}    
    \method draw(){
        \node (out) at (0,0) {out};
    }
}
% instantiating an object of this class outside a frame works
%\pgfoonew \outobject=new outclass()

\begin{frame}{Class outclass is defined outside frames, and outdocclass before the beginning of the document}

Class outclass is instantiated as outobject outside frames

Class outdocclass is instantiated as outdocobject in this frame
% instantiating an object of the outocclass
\pgfoonew \outdocobject=new outdocclass()

\begin{tikzpicture}
    \node at (0,1) {object outobject can be used in this picture};
    %\outobject.draw()
% if the option ignorenonframetext is used:
%! Undefined control sequence.
%\beamer@doifinframe ... this picture}; \outobject
%                                                  .draw() \end {tikzpicture}...
%l.50 \end{frame}
    \node[text width=0.75\textwidth] at (0,-1) {object outdocobject can be used in this picture, even with the ignorenonframetext beamer option};
    \outdocobject.draw()
\end{tikzpicture}

\end{frame}


\end{document}

答案1

正如 Andrew Stacey 所建议的,可以使用两种技术使 pgfooclasses 在整个 beamer 演示过程中可用,即使在ignorenonframetext使用 beamer 选项时也是如此。

一种方法是在 之前定义类\begin{document},因为ignorenonframetext在这个阶段 尚未活跃。

另一个是在某种模式下定义类\mode<all>{...},从而暂时忽略该选项。

以下是使用这两种技术的示例:

\documentclass[ignorenonframetext]{beamer}

\usepackage{tikz}
\usepgfmodule{oo}


\author{bli, with suggestions from Andrew Stacey}

% defining a class before the document begins
\pgfooclass{outdocclass}{
    \method outdocclass(){}    
    \method draw(){
        \node (outdoc) at (-1,0) {outdoc};
    }
}

\begin{document}

% having the outside frame class definition taken into account in presentation mode,
% despite the ignorenonframetext option
\mode<all>{
% defining a class outside a frame
\pgfooclass{outframeclass}{
    \method outframeclass(){}    
    \method draw(){
        \node (outframe) at (1,0) {outframe};
    }
}

% instantiating an object of this class outside a frame
\pgfoonew \outframeobject=new outframeclass()
}

\begin{frame}{Class outframeclass is defined outside frames, and outdocclass before the beginning of the document}

% instantiating an object of the outdocclass
outdocclass is instantiated as outdocobject in this frame.\\
This is possible even with the \texttt{ignorenonframetext} option
because this option takes effect only after the document has begun.
\pgfoonew \outdocobject=new outdocclass()

\begin{tikzpicture}
    \node[text width=0.75\textwidth] at (0,1) {object outobject can be used in this picture,
    because it was instantiated in \texttt{mode<all>}};
    \outframeobject.draw()
    \node[text width=0.75\textwidth] at (0,-1) {object outdocobject can be used in this picture};
    \outdocobject.draw()
\end{tikzpicture}

\end{frame}


\end{document}

请注意,实例化可以在其他地方进行。重要的是,类定义可以从实例化发生的地方“访问”,并且生成的对象可以从使用的地方“访问”。

相关内容