0

How can I define a function call \StepFrame that accepts default arguments, as well as optional arguments for more complex behavior? I would like to define a LaTeX command \StepFrame that behaves differently depending on the number of arguments passed to it. Specifically, I want:

  1. A default case, where the command is called with two arguments (title and image file), and it generates a frame with a single image.
  2. An optional case, where the command is called with three arguments (title, main image, and overlay image), and it generates a frame with the second image overlaid on the first image.

Here's a basic version of my code:

\documentclass{beamer}

\newcommand{\StepFrame}[2]
{   
    
    %% default case:
    \begin{frame}{#1}
        \includegraphics[width=\textwidth]{#2}
    \end{frame}
    
    %% optinal case:
    %\begin{frame}{#1}
    %   \includegraphics[width=\textwidth]{example-image}\llap{\includegraphics[width=0.4\textwidth]{example-image}}
    %\end{frame}
}

\begin{document}
    
    %% call the function \StepFrame with default arguments (single image)
    \StepFrame{my title 1}{example-image}
    \StepFrame{my title 2}{example-image}
    \StepFrame{my title 3}{example-image}
    
    %% call the function \StepFrame with optional arguments (image inside another image)
    %\StepFrame{my title 4}{example-image}{example-image-b}
    %\StepFrame{my title 5}{example-image}{example-image-b}
    %\StepFrame{my title 6}{example-image}{example-image-b}

\end{document}
2
  • 2
    Why not using standard LaTeX syntax: \StepFrame{my title 4}{example-image}[example-image-b], with brackets for optional arguments? Commented yesterday
  • 1
    It's a bit simpler if the optional argument comes first: \StepFrame[example-image-b]{my title 4}{example-image}, if that makes sense in your code. Commented yesterday

1 Answer 1

5

Use [] for optional arguments

enter image description here

\documentclass{beamer}

\NewDocumentCommand\StepFrame{mmo}{%
    \begin{frame}{#1}
        \includegraphics[width=\textwidth]{#2}%
    \IfNoValueF{#3}{%
      \llap{\includegraphics[width=0.4\textwidth]{#3}}}%
     \end{frame}%
}

\begin{document}
    
    %% call the function \StepFrame with default arguments (single image)
    \StepFrame{my title 1}{example-image}
    \StepFrame{my title 2}{example-image}
    \StepFrame{my title 3}{example-image}
    
    %% call the function \StepFrame with optional arguments (image inside another image)
    \StepFrame{my title 4}{example-image}[example-image-b]
    \StepFrame{my title 5}{example-image}[example-image-b]
    \StepFrame{my title 6}{example-image}[example-image-b]

\end{document}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.