1

My question

How can I improve this \newcommand that enables me to have

  1. a centered figure with captions in mode article
  2. a centered graph with a title in beamer
  3. The title in beamer is the caption in article

enter image description here enter image description here

%------------    %0= Article 1= beamer    --------------
\def\model{1} 
%----------------load the documentclass ----------------
\if 0\model
  \documentclass{article}
  \usepackage[envcountsect]{beamerarticle}
\else
  \documentclass{beamer}
\fi
\usepackage{tikz}
\usepackage{graphicx}

\newcommand{\hackGraph}[3]{
  \if 0\model
    \begin{figure}[htp]
      \centering
      #1
      \caption{#3}
    \end{figure}
  \else
    \begin{frame}{#3}
      \centering
      #2
    \end{frame}
  \fi
}

\begin{document}

\hackGraph
  {\includegraphics[width=0.5\textwidth]{example-image}}
  {\includegraphics[width=0.8\textwidth]{example-image}}
  {Figure with caption or frame with title} 

\end{document}

context

I use beamer and beameraticle to generate either Notes or slides. Following the "no figure in beamer" principle, I created \hackGraph for all my includegraphics and \input{TikZ.tex}

For instance

\hackGraph
  {\resizebox{0.5\textwidth}{!}{\input{TikZ.tex}}}
  {\resizebox{0.8\textwidth}{!}{\input{TikZ.tex}}}
  {TikZ figure with caption or frame with title} 

works as well to wrap/resize my figures in TikZ.

But I see while using it that I don't do things the right way.

The code seems too repetitive. How could it be improved ?

9
  • 2
    Your \hackGraph already fits your requirement, and you are asking for possible improvements? May 22, 2020 at 4:07
  • Does the width constraint (0.5\textwidth for beamerarticle and 0.8\textwidth for beamer) fixed for every image and tikz graph, or variable from image to image? May 22, 2020 at 4:10
  • 2
    Your \model switch is very close to beamer's \mode internal macro. Have a look at the beameruseguide section 21.3. Also, while it was formerly advised to not use figure environments in beamer, they can actually be used transparently (the only difference is that they do not float) so there is no reason for you to avoid this environment in your presentation.
    – BambOo
    May 22, 2020 at 8:21
  • 1
    Side-notes : why do you use the frame title as a caption ? Otherwise, either with articles or presentations, I rarely compile tikzpictures inside my documents. At the end of the day it takes much more time, especially if you have complex graphs like pgfplots axes. I prefer to include them with a good old \includegraphics.
    – BambOo
    May 22, 2020 at 8:21
  • @muzimuzhiZ I am indeed looking for potential improvements. So far, I input manually the percentage of textwidth/textwidth for each figure. But i imagine there is a smarter way to adapt automatically the size (aspect ratio ?).
    – JeT
    May 22, 2020 at 8:22

1 Answer 1

1

A little late, but I only found your question one month ago in search for something similar. Since then, I have worked out what I was looking for and I believe it is in line with what you need/requested.

It was important to me that I would NOT define my own switch, but instead use the mode<presentation>{} or mode<article>{} commands (since they are available already). But because those commands are provided by beamer, they do not work in article mode until you have loaded \usepackage{beamerarticle}. In other words, you cannot use mode<article>{\usepackage{beamerarticle}}. However, you can use:

\@ifclassloaded{article}{\usepackage{beamerarticle}}

Effectively, this can become a conditional statement which ensures that you load beamerarticle, only when you typeset your documentclass as article.

While I am aware that the figure environment should not be used in beamer, I actually need it in article mode and (thanks to the [H] option provided by the float package) it works really well for me - in both modes. In addition (similar to what you were looking for), it was important to me to have one single .tex file that produces both, a presentation and a full-blown article (with proper figure environment).

To use the figure caption in other places (like the frame title), you can use the cleveref package (according to this post).

%%%%%%%%%%%%%%%%%%%% 
% Create more complete clever ref to use
% figure label for auto generation frame title 
%%%%%%%%%%%%%%%%%%%% 
\mode<presentation>{
\let\Chyperref\Cref % Save original command under a new name
\renewcommand{\Cref}[1]{\hyperlink{#1}{\Chyperref{#1}}} % Redefine \cref command and explicitly add the hyperlink. 
\newcommand*{\fullref}[1]{\Cref{#1}:~\nameref{#1}}
}
%%%%%%%%%%%%%%%%%%%% 
\mode<article>{
\newcommand*{\fullref}[1]{\hyperref[{#1}]{\Cref*{#1}: \nameref*{#1}}}
}
%%%%%%%%%%%%%%%%%%%% 

Because the hyperlink is slightly different in beamer mode than it is in article mode, it is defined seperately (That way, you can also make adjustments for each individual output version, depending on your needs).

Now you can use the captionsetup command from the caption package to make the standard figure caption disappear.

\captionsetup[figure]{
labelformat=empty,
textformat=empty,
}

If need to be, you can set this per mode (for example use \mode<presentation>{\captionsetup[figure]{...}} if you only want to adjust the caption in beamer). Likewise, you could also change this setting anywhere throughout the document.

With those settings in place, you can now use a frame like this:

\begin{frame}
\frametitle{\fullref{fig: my label}}

\begin{figure}[H]
\includegraphics{example-image}
\caption{My Caption}
\label{fig: my label}
\end{figure}

\end{frame}

And finally, here is a full minimal working example, for your review. Note that the switch comes in form of % in lines 1 and 2.

Full MWE:

\documentclass{beamer}  % Comment out for article
%\documentclass{article}    % Uncomment line for article

%%%%%%%%%%%%%%%%%%%%
% Begin Load Packages %%%%%%%
%%%%%%%%%%%%%%%%%%%%
% Packages needed in article mode 
\makeatletter
\@ifclassloaded{article}{
\usepackage{beamerarticle}
\usepackage{graphicx}   % Automatically loaded in Beamer, but not in Article mode
\usepackage{hyperref}   % Automatically loaded in Beamer, but not in Article mode
}
\makeatother

% Packages needed in both modes
\usepackage{float}      % Allows use of figure environment  in Beamer (using [H]) 
\usepackage{caption}    % Needed to adjust caption
\usepackage{cleveref}   % Needed to adjust cross referencing 
%%%%%%%%%%%%%%%%%%%%
% End Load Packages %%%%%%%%
%%%%%%%%%%%%%%%%%%%% 



%%%%%%%%%%%%%%%%%%%%
% Begin Custom Commands %%%%%
%%%%%%%%%%%%%%%%%%%% 
%
%%%%%%%%%%%%%%%%%%%% 
% Create more complete clever ref to use
% figure label for auto generation frame title 
%%%%%%%%%%%%%%%%%%%% 
\mode<presentation>{
% Idea stolen from https://tex.stackexchange.com/questions/266080/cleveref-hyperlink-not-working-in-beamer/266109#266109
\let\Chyperref\Cref % Save original command under a new name
\renewcommand{\Cref}[1]{\hyperlink{#1}{\Chyperref{#1}}} % Redefine \cref command and explicitly add the hyperlink. 
\newcommand*{\fullref}[1]{\Cref{#1}:~\nameref{#1}}
}
%%%%%%%%%%%%%%%%%%%% 
\mode<article>{
\newcommand*{\fullref}[1]{\hyperref[{#1}]{\Cref*{#1}: \nameref*{#1}}}
}
%%%%%%%%%%%%%%%%%%%% 


%%%%%%%%%%%%%%%%%%%%
% Make the standard caption disappear
%%%%%%%%%%%%%%%%%%%%
% Can be encased in \mode<presentation>{} 
% to use standard caption in article mode
%
%\mode<presentation>{
\captionsetup[figure]{
labelformat=empty,
textformat=empty,
}
%}
%
%%%%%%%%%%%%%%%%%%%%
% End Custom Commands %%%%%%
%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%% 
% Standard fix for 2018 LaTeX release
%%%%%%%%%%%%%%%%%%%% 
\makeatletter
\let\@@magyar@captionfix\relax
\makeatother
%%%%%%%%%%%%%%%%%%%% 


\begin{document}


\begin{frame}
\frametitle{\fullref{fig: my label 1}}

\begin{figure}[H]
\centering
\includegraphics[height=6cm]{example-image}
\caption{My First Figure Caption}
\label{fig: my label 1}
\end{figure}

\end{frame}

\mode<article>{
In my workflow, I need the figure caption in the frame title (in beamer) 
and above the image (in article). However, this can be easily adjusted 
through the ``mode switch'' 
}


\mode<article>{
\captionsetup[figure]{
labelformat=simple,
textformat=simple,
}}


\begin{frame}
\mode<presentation>{\frametitle{ \fullref{fig: my label 2}}}
\mode<article>{\frametitle{My Second Frame}}

\begin{figure}[H]
\centering
\includegraphics[height=6cm]{example-image}
\caption{My Second Figure Caption}
\label{fig: my label 2}
\end{figure}

\end{frame}

\end{document}

This works really well for me, but I would love to hear your and the community feedback, as I am always interested in improvements or simpler methods. This is the most minimalistic version that I could come up with, but if there is a better method, I would love to know and use that.

Example of Beamer Output:

Beamer Mode

Example of Article Output:

Article Mode

You must log in to answer this question.

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