1

I am messing with tcolorboxes as environments to store tables, images, etc. The boxes themselves are numbered by section.box (with the built in tcolorbox numbering) and I had the idea of marking the multiple tabulars/images (counted by \captionof} in each box by the box count, so I would have something like Table 1.3a in Box 1.3, or Figure 2.4b in Box 2.4. Now I've read about \newcounter, \refstepcounter, and \renewcommand (for table or tabular) but I just can't seem to be able to wrap my head around how I would go about achieving this. Please advise.

\documentclass{article}

\usepackage[margin=2cm]{geometry}

\usepackage{graphicx} \usepackage{xcolor} \usepackage{capt-of} \usepackage{tcolorbox} \newtcolorbox[auto counter,number within=section]{cusbox}[2][]{% before skip=6pt, after skip=6pt,% halign upper=center,halign lower=center,% colbacktitle=green!15!white,coltitle=black,colback=green!5!white,colframe=green!75!black,% leftrule=2mm,rightrule=2mm,titlerule=1mm,% toptitle=1mm,bottomtitle=1mm,center title,title=Box \thetcbcounter: #2,#1}

\usepackage{lipsum}

\begin{document} \section{Tcolorbox}

\lipsum[1]

\begin{cusbox}[sidebyside,label=box:table]{The Tables Have Turned} \begin{tabular}{ccc} \textbf{one} & \textbf{two} & \textbf{three} \ \hline 5646 & 5651 & 65654 \ 56645 & 465 & ksldk \end{tabular} \captionof{table}{Actual data (not really)}\label{table:table1} \tcblower \begin{tabular}{ccc} \textbf{one} & \textbf{two} & \textbf{three} \ \hline 5646 & 5651 & 65654 \ 56645 & 465 & ksldk \end{tabular} \captionof{table}{Actual data (not really)}\label{table:table2} \end{cusbox}

\lipsum[2]

\end{document}

Plergux
  • 874

2 Answers2

2

Something like \renewcommand{\thetable}{\thetcbcounter\alph{table}} will change the format for tables, but you need to reset the counter for table after every tcolorbox to start from a in every new box. This can be done with before upper={\setcounter{table}{0}}.

\documentclass{article}

\usepackage[margin=2cm]{geometry}

\usepackage{graphicx} \usepackage{xcolor} \usepackage{capt-of} \usepackage{tcolorbox} \newtcolorbox[auto counter,number within=section]{cusbox}[2][]{% before upper={\setcounter{table}{0}}, before skip=6pt, after skip=6pt,% halign upper=center,halign lower=center,% colbacktitle=green!15!white,coltitle=black,colback=green!5!white,colframe=green!75!black,% leftrule=2mm,rightrule=2mm,titlerule=1mm,% toptitle=1mm,bottomtitle=1mm,center title,title=Box \thetcbcounter: #2,#1}

\usepackage{lipsum}

\renewcommand{\thetable}{\thetcbcounter\alph{table}}

\begin{document} \section{Tcolorbox}

\lipsum[1]

\begin{cusbox}[sidebyside,label=box:table]{The Tables Have Turned} \begin{tabular}{ccc} \textbf{one} & \textbf{two} & \textbf{three} \ \hline 5646 & 5651 & 65654 \ 56645 & 465 & ksldk \end{tabular} \captionof{table}{Actual data (not really)}\label{table:table1} \tcblower \begin{tabular}{ccc} \textbf{one} & \textbf{two} & \textbf{three} \ \hline 5646 & 5651 & 65654 \ 56645 & 465 & ksldk \end{tabular} \captionof{table}{Actual data (not really)}\label{table:table2} \end{cusbox}

\lipsum[2]

\begin{cusbox}[sidebyside,label=box:table]{The Tables Have Turned} \begin{tabular}{ccc} \textbf{one} & \textbf{two} & \textbf{three} \ \hline 5646 & 5651 & 65654 \ 56645 & 465 & ksldk \end{tabular} \captionof{table}{Actual data (not really)}\label{table:table1} \tcblower \begin{tabular}{ccc} \textbf{one} & \textbf{two} & \textbf{three} \ \hline 5646 & 5651 & 65654 \ 56645 & 465 & ksldk \end{tabular} \captionof{table}{Actual data (not really)}\label{table:table2} \end{cusbox} \end{document}

enter image description here

Ignasi
  • 136,588
  • This looks good, but I have one question: If I wanted to do images (or quotations or whatever) I would have to add one entry (along the lines of " before upper={\setcounter{table}{0}},") for each in the tcolorbox definition? (for example "before upper={\setcounter{image}{0}},"). – Plergux Mar 29 '21 at 16:04
  • 1
    @Plergux If you don't reset the counter you will get Table 1.2a, Table 1.2b, Table 1.3c, Table 1.3d, ... If you want to mix figures, tables and whatever in any box, you need to reset all of them, but if you define different boxes for each kind of contents, you only need to reset one counter. It's up to you. – Ignasi Mar 29 '21 at 17:09
  • I see. It's a choice of box=content or box+content. Gotcha! Thank you. – Plergux Mar 29 '21 at 17:45
1

This is another option --- more manual and it has no the fancy things that table has (maybe you can try to find a way to integrate this with newfloat).

What I have done is: let the tcolorboxes use a known counter, and define a new counter that resets with the first one. Then I manually defined the caption-like command.

\documentclass{article}

\usepackage[margin=2cm]{geometry}

\usepackage{graphicx} \usepackage{xcolor} \usepackage{capt-of} \usepackage{tcolorbox} \newcounter{boxes} \newtcolorbox[use counter=boxes,number within=section]{cusbox}[2][]{% before skip=6pt, after skip=6pt,% halign upper=center,halign lower=center,% colbacktitle=green!15!white,coltitle=black,colback=green!5!white,colframe=green!75!black,% leftrule=2mm,rightrule=2mm,titlerule=1mm,% toptitle=1mm,bottomtitle=1mm,center title,title=Box \thetcbcounter: #2,#1% }

\newcounter{intable}[boxes] \renewcommand{\theintable}{\theboxes\alph{intable}} \newcommand{\myintable}[1]{% \refstepcounter{intable}\par\textbf{Table \theintable} #1% }

\usepackage{lipsum}

\begin{document} \section{Tcolorbox}

\lipsum[1]

\begin{cusbox}[sidebyside,label=box:table]{The Tables Have Turned} \begin{tabular}{ccc} \textbf{one} & \textbf{two} & \textbf{three} \ \hline 5646 & 5651 & 65654 \ 56645 & 465 & ksldk \end{tabular} \myintable{Actual data (not really)}\label{intable:table1} \tcblower \begin{tabular}{ccc} \textbf{one} & \textbf{two} & \textbf{three} \ \hline 5646 & 5651 & 65654 \ 56645 & 465 & ksldk \end{tabular} \myintable{Actual data (not really)}\label{intable:table2} \end{cusbox}

\bigskip

\textbf{References:} \ref{intable:table1} and \ref{intable:table2}

\bigskip \lipsum[1]

\begin{cusbox}[sidebyside,label=box:table2]{The Tables Have Turned} \begin{tabular}{ccc} \textbf{one} & \textbf{two} & \textbf{three} \ \hline 5646 & 5651 & 65654 \ 56645 & 465 & ksldk \end{tabular} \myintable{Actual data (not really)}\label{intable:table3} \tcblower \begin{tabular}{ccc} \textbf{one} & \textbf{two} & \textbf{three} \ \hline 5646 & 5651 & 65654 \ 56645 & 465 & ksldk \end{tabular} \myintable{Actual data (not really)}\label{intable:table4} \end{cusbox}

\textbf{New References:} \ref{intable:table3} and \ref{intable:table4}

\textbf{Old References:} \ref{intable:table1} and \ref{intable:table2}

\end{document}

enter image description here

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • Well, I want to avoid floats. :p but if I also wanted to do images I could use the same code, just change \newcounter{intable}[boxes] to \newcounter{inimage}[boxes], \renewcommand{\theintable}... to \renewcommand{\theinimage}... et cetera? And then use \myinimage{caption} with \includegraphics inside the box? – Plergux Mar 29 '21 at 16:02
  • 1
    Yes, I suppose --- the only problem is that you do not have automatically list of "intables" or things like that. – Rmano Mar 29 '21 at 16:03
  • I was only planning to have a list of boxes anyway. But I'll see how this project evolves :) – Plergux Mar 29 '21 at 16:05
  • 1
    Well, I discoverd this for the list-of-whatever: https://tex.stackexchange.com/questions/61086/how-to-create-my-own-list-of-things (there is a package for everything, it seems...) – Rmano Mar 29 '21 at 16:13
  • Sweet! Thanks. I'll definitely have a look at that. :) – Plergux Mar 29 '21 at 16:45