3

I'm creating a presentation with a lot of frames with one figure per slide. So instead of having hundreds of the same code over and over again I created a simple \newcommand for it where one of the argument is the figure-name which I also use as the label for the figure. But when I use figure subfolders the argument is: subfolderone/subfoldertwo/figurename. But I only want to use figurename as the figure label.

Is it possible to break the #1 argument by "/" and use only the last one for labeling? Else... I'll just make another input argument.

\documentclass{beamer}
\usepackage{graphicx}

\newcommand{\figcaptiontitlecrop}[7]{%
    \begin{frame}[b]{#3}
        \begin{figure}
            \centering
            \includegraphics[trim=#4mm #5mm #6mm #7mm, clip]{#1}
            \caption{#2}%
            \label{fig:#1}%
        \end{figure}
    \end{frame}
}

\begin{document}
\figcaptiontitlecrop{filename}{Caption Text}{Frame Title Here}{0}{0}{0}{0}
%\figcaptiontitlecrop{figures/subfolder/filename}{Caption Text}{Frame Title Here}{0}{0}{0}{0}
\end{document}
  • 2
    SplitList from xparse could help or a key-value interface. A user command with 7 arguments is error-prone! – user31729 Feb 2 '17 at 16:33
  • 1
    Since #4-#7 are trim arguments, it may be easier to just collapse that into a single {0mm 0mm 0mm 0mm} argument. – Teepeemm Feb 2 '17 at 17:16
3

A command with seven arguments should be avoided. A key-value approach is better:

\documentclass{beamer}
\usepackage{graphicx}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\beamerfigure}{mm}
 {
  \begin{frame}[b]
  \keys_set:nn { bjartmar/beamerfigure } { #1 }
  \frametitle{\l_bjartmar_beamerfigure_title_tl}
  \begin{figure}
    \centering
    \use:x
     {
      \exp_not:N \includegraphics [
        \l_bjartmar_beamerfigure_options_tl,
        trim=\l_bjartmar_beamerfigure_tl,
        clip
      ]
    }{\l_bjartmar_beamerfigure_path_tl / \l_bjartmar_beamerfigure_name_tl}
    \caption{#2}
    \label{fig:\l_bjartmar_beamerfigure_name_tl}
    \end{figure}
  \end{frame}
}
\keys_define:nn { bjartmar/beamerfigure }
 {
  title   .tl_set:N  = \l_bjartmar_beamerfigure_title_tl,
  path    .tl_set:N  = \l_bjartmar_beamerfigure_path_tl,
  path    .initial:n = {.},
  name    .tl_set:N  = \l_bjartmar_beamerfigure_name_tl,
  options .tl_set:N  = \l_bjartmar_beamerfigure_options_tl,
  trim    .tl_set:N  = \l_bjartmar_beamerfigure_tl,
  trim    .initial:n = 0~0~0~0,
 }
\ExplSyntaxOff

\begin{document}

\beamerfigure{
  name=donald-duck,
  options={height=.5\textheight},
  trim=0 5 0 5,
}{This is Donald Duck}

\beamerfigure{
  path=/usr/local/texlive/2016/texmf-dist/tex/latex/mwe/,
  name=example-grid-100x100pt,
}{Another figure}

\end{document}

If no path is specified, the current directory is used. As you see, you can also specify additional options for \includegraphics.

enter image description here

  • Brilliant! Happy to learn something new. I'll defiantly use this and the method in the future! I don't know why, but I had to move \keys_set:nn { bjartmar/beamerfigure } { #1 } outside the frame environment to make the frametitle to work. – Bjartmar Feb 2 '17 at 21:13
  • @bjartmar Strange. But in that case, add also \group_begin: at the start and \group_end: at the end, or the variables would be clobbered. You're not using the deprecated syntax \begin{frame}{frame title}, are you? – egreg Feb 2 '17 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.