2

I want to write a custom environment that handles some layouts that default beamer slides aren't designed to do.

I use xparse's \NewDocumentEnvironment to achieve this, but have problems passing down the label.

If I do something like this:

\documentclass{beamer}
\usepackage{xparse}

\NewDocumentEnvironment{slide}{ o +b }
{%
    \begin{frame}[environment=slide,\IfValueT{#1}{#1}]
        % not important implementation details

        #2
    \end{frame}
}{}

\begin{document}

\begin{slide}[label=slide-label-a]
    test
\end{slide}

\end{document}

I get ! Package keyval Error: label=slide-label-a undefined. because the whole option is parsed as a key.

If I do something like this, where I only pass the label's value, it works but generates a lot of empty labels (for every frame where I don't use one):

\documentclass{beamer}
\usepackage{xparse}

\NewDocumentEnvironment{slide}{ o +b }
{%
    \begin{frame}[environment=slide,label=\IfValueT{#1}{#1}]
        % not important implementation details

        #2
    \end{frame}
}{}

\begin{document}

\begin{slide}[slide-label-a]
    test
\end{slide}

\end{document}

The optimal solution would be the first variant, so I can pass arbitrary keyval options to beamer. But if that doesn't work a second option would be to have a good way to set individual options.

3
  • 1
    (hiding frames in new environments can result in surprising problems down the line. Depending on what layout changes you are going to make, a new frame option might be the safer approach) Sep 21 at 9:23
  • 1
    (If your tex distribution is up-to-date, you don't need to load \usepackage{xparse}) Sep 21 at 9:36
  • good to know, that I don't need to load xparse. Yeah, I first tried it with options, but the problem is that beamer inherently assumes the frame title to be at the top of the frame. I want to achieve a look similar to reveal.js where the title is set directly above the content (everything including the title is centered horizontally and vertically). The only way I found to achieve this is to disable the header, wrap frame and insert the title above the given frame content. Do you see another option?
    – fhred
    Sep 21 at 10:40

1 Answer 1

2

You can avoid the problem by using an empty default value for your optional argument. This way you can always pass it to the frame:

\documentclass{beamer}


\NewDocumentEnvironment{slide}{ O{} +b }
{    \begin{frame}[environment=slide,#1]
        % not important implementation details

        #2
    \end{frame}
}{}

\begin{document}

\begin{slide}
    test \ref{slide-label-a}
\end{slide}


\begin{slide}[label=slide-label-a]
    test
\end{slide}


\end{document}
1
  • Thanks! Just as a follow-up for my understanding: So I assume the macros that check the options actually cause the issue here? Presumably, because they cause grouping?
    – fhred
    Sep 21 at 10:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .