latexcaptionbeamer

LaTeX Beamer -- How to create numbered table captions without the table environment


I'm trying to create a numbered table caption in beamer without using the table environment. The reason for not using the table environment stems from the possibility of horizontal aligning my tables.

I'm using a command "\myCaptionGrey" to create a table caption, but I don't understand how I can add a numbering.

Here's my MWE: (Side note: For my needs I like to use the NiceTabular environment)

\documentclass[aspectratio=169,xcolor=table,12pt]{beamer}
% compile in pdfLaTeX

\usepackage{caption}
\usepackage{booktabs}
\usepackage{nicematrix}
    \NiceMatrixOptions{cell-space-limits = 2pt}

\definecolor{tableYellowHeaderBg}{RGB}{250,191,18}
\definecolor{tableYellowCellBg}{RGB}{255,242,204}
\definecolor{tableYellowLines}{RGB}{250,191,18}
\definecolor{tableYellowCellFg}{RGB}{127,127,127}

% table caption
\newcommand{\myCaptionGrey}[2]{%
  {%
    \usebeamercolor[fg]{tableGreyCellFg}%
    \usebeamerfont*{caption name}%
    #1%
    \usebeamertemplate{caption label separator}%
  }%
  \usebeamerfont*{caption name}
  #2\par
}

\newcommand{\myCaptionYellow}[2]{%
  {%
    \usebeamercolor[fg]{UnthaTableYellowCellFg}%
    \usebeamerfont*{caption name}%
    #1%
    \usebeamertemplate{caption label separator}%
  }%
  \usebeamerfont*{caption name}
  #2\par
}

\begin{document}

    \begin{frame}[t]{Tables}

        \raggedleft
        \myCaptionYellow{Table}{This is a table caption.}
        
        \begin{NiceTabular}[c]{wc{1.0cm}wc{1.0cm}wc{1.0cm}wc{1.0cm}}[rules/color=tableYellowLines]
        \CodeBefore
            \rowcolor{tableYellowHeaderBg}{1}
            \rowcolors{2}{}{tableYellowCellBg}
        \Body
            \toprule[1.5pt]
            \RowStyle{\color{white}}H1 & H2 & H3 & H4  \\
            \arrayrulecolor{white}\toprule[1.5pt]
            \RowStyle[nb-rows=*]{\color{tableYellowCellFg}}Cell 1 & Cell 2 & Cell 3 & Cell 5 \\
            \midrule
            Cell 5 & Cell 6 & Cell 7 & Cell 8 \\
            \midrule
            Cell 9 & Cell 10 & Cell 11 & Cell 12 \\
            \bottomrule
        \end{NiceTabular}
        
    \end{frame}

\end{document}

Solution

  • Here a variation which includes the figure/table number (please note that the syntax is now slightly different, the first argument should be table or figure in lowercase).

    I would also suggest to not repeat the same definition for multiple different colours. Instead you could change the caption name beamer colour.

    \documentclass[aspectratio=169,xcolor=table,12pt]{beamer}
    % compile in pdfLaTeX
    
    \usepackage{caption}
    \usepackage{booktabs}
    \usepackage{nicematrix}
       \NiceMatrixOptions{cell-space-limits = 2pt}
    
    \definecolor{tableYellowHeaderBg}{RGB}{250,191,18}
    \definecolor{tableYellowCellBg}{RGB}{255,242,204}
    \definecolor{tableYellowLines}{RGB}{250,191,18}
    \definecolor{tableYellowCellFg}{RGB}{127,127,127}
    
    % table caption
    
    \newcommand{\myCaption}[2]{%
     {%
       \refstepcounter{#1}%
       \usebeamercolor[fg]{caption name}%
       \usebeamerfont*{caption name}%
       \csname#1name\endcsname~\csname the#1\endcsname
       \usebeamertemplate{caption label separator}%
     }%
     \usebeamerfont*{caption name}
     #2\par
    }
    
    
    \begin{document}
    
       \begin{frame}[t]{Tables}
    
           \raggedleft
           \setbeamercolor{caption name}{fg=tableYellowCellFg}
           \myCaption{table}{This is a table caption.}
           
           \begin{NiceTabular}[c]{wc{1.0cm}wc{1.0cm}wc{1.0cm}wc{1.0cm}}[rules/color=tableYellowLines]
           \CodeBefore
               \rowcolor{tableYellowHeaderBg}{1}
               \rowcolors{2}{}{tableYellowCellBg}
           \Body
               \toprule[1.5pt]
               \RowStyle{\color{white}}H1 & H2 & H3 & H4  \\
               \arrayrulecolor{white}\toprule[1.5pt]
               \RowStyle[nb-rows=*]{\color{tableYellowCellFg}}Cell 1 & Cell 2 & Cell 3 & Cell 5 \\
               \midrule
               Cell 5 & Cell 6 & Cell 7 & Cell 8 \\
               \midrule
               Cell 9 & Cell 10 & Cell 11 & Cell 12 \\
               \bottomrule
           \end{NiceTabular}
           
           \raggedright
           \myCaption{table}{This is a table caption.}
           
           \begin{NiceTabular}[c]{wc{1.0cm}wc{1.0cm}wc{1.0cm}wc{1.0cm}}[rules/color=tableYellowLines]
           \CodeBefore
               \rowcolor{tableYellowHeaderBg}{1}
               \rowcolors{2}{}{tableYellowCellBg}
           \Body
               \toprule[1.5pt]
               \RowStyle{\color{white}}H1 & H2 & H3 & H4  \\
               \arrayrulecolor{white}\toprule[1.5pt]
               \RowStyle[nb-rows=*]{\color{tableYellowCellFg}}Cell 1 & Cell 2 & Cell 3 & Cell 5 \\
               \midrule
               Cell 5 & Cell 6 & Cell 7 & Cell 8 \\
               \midrule
               Cell 9 & Cell 10 & Cell 11 & Cell 12 \\
               \bottomrule
           \end{NiceTabular}
           
       \end{frame}
    
    \end{document}
    

    enter image description here