Be aware that longtable
- makes use of the
table-counter.
- causes the
\caption-command to write entries for the list of tables (the .lot-file) and to make use of the table-counter.
These aspects are "hard-wired" both into the longtable environment and into the patch for the longtable-environment provided by the hyperref package.
In other words:
Unfortunately there is no \usecounter and no \uselistof for longtable-environments.
With your example-boxes you might wish
- to use another counter than the
table-counter
- not to have entries for example-boxes merged into the list of tables but to have a separate list of example boxes.
As things are somewhat "hardwired" in the longtable-package, if you want to be accurate, you need to derive your own style-file/your own package, containing your own variant of the longtable-environment where each occurrence of the usage of the table-counter is replaced by usage of the examplebox-counter, and where each attempt at writing to the .lot-file is replaced by attempts at writing to the list-of-exampleboxes-file (.loEB-file in the example below).
That package should also take into account patches as provided by the hyperref-package.
If you don't wish to be accurate, you can within the examplebox-environment
- redirect using the infrastructure of the
table-counter to using the infrastructure of the examplebox-counter. Be aware that macros like \hyper@makecurrent, which use the counter-name directly for deriving names of anchors for hyperlinks, need to be patched so that they crank out the usage of the name of the table-counter and replace it by usage of the name of the examplebox-counter.
- redirect writing to the .lot-file to writing to another file, e.g., to a .loEB-file. In order to achieve this, the macro
\addcontentsline can be patched to crank out cases where entries shall go to the .lot-file / the \ext@table-file.
Spontaneously I see two important reasons why this redirection-approach is not accurate:
This redirection-approach breaks nesting inside your examplebox-environment things other than examplebox-environments that also use the table-counter and/or that also write entries for the list of tables/for the .lot-file.
This redirection-approach might break \autoreferencing tables and other things where the table-counter was used for creating the corresponding referencing-label for cases where the auto-reference is to occur/to take place "inside" the examplebox-environment.
Thus a rule of thumb: With the code below don't nest tables and other things that make use of the table-counter or that do write to the list of tables inside the examplebox-environment.
In case you can live with this, here is some code:
\documentclass{report}
\usepackage{caption}
\usepackage{longtable}
\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{colortbl}
\makeatletter
%% Use examplebox-stuff instead of table-stuff:
%%
%% Inside the examplebox-environment
%%
%% - \hyper@makecurrent needs to redirect using the table-counter
%% to using the examplebox-counter. The following things are needed
%% for patching \hyper@makecurrent inside the examplebox environment:
\newcommand\MySavedhyper@makecurrent{}%
\AtBeginDocument{\let\MySavedhyper@makecurrent=\hyper@makecurrent}%
\newcommand\Mytablestring{table}%
\newcommand\MyPatchedhyper@makecurrent[1]{%
\begingroup
\long\def\@tempa{#1}%
\expandafter\endgroup
\ifx\@tempa\Mytablestring
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\MySavedhyper@makecurrent{examplebox}}%
{\MySavedhyper@makecurrent{#1}}%
}%
%% - \addcontentsline needs to redirect writing to the lot-file
%% to writing to the loEB-file (list of Example Boxes).
%% The following things are needed for patching \addcontentsline
%% inside the examplebox environment:
\newcommand\MySavedAddcontentsline{}%
\AtBeginDocument{\let\MySavedAddcontentsline=\addcontentsline}%
\newcommand\Mylotstring{lot}%
\newcommand\Mylotstringb{\csname ext@table\endcsname}%
\newcommand\MyPatchedAddcontentsline[1]{%
\begingroup
\long\def\@tempa{#1}%
\ifx\@tempa\Mylotstring
\expandafter\@secondoftwo
\else
\expandafter\@firstofone
\fi
{%
\ifx\@tempa\Mylotstringb
\expandafter\@secondoftwo
\else
\expandafter\@firstoftwo
\fi
{\endgroup\MySavedAddcontentsline{#1}}%
}%
{\endgroup\MySavedAddcontentsline{loEB}}%
}%
%%
%% \ExampleboxStuffInsteadOfTableStuff performs redirecting and patching.
%%
\newcommand\ExampleboxStuffInsteadOfTableStuff{%
% Redirect LaTeX 2e-kernel-counter-infrastructure-stuff of
% the table counter to the examplebox counter
\let\c@table=\c@examplebox
\let\thetable=\theexamplebox
\let\p@table=\p@examplebox
\let\cl@table=\cl@examplebox
% Redirect hyperref-counter-infrastructure-stuff and \autoref-stuff
% of the table counter to the examplebox counter:
\let\theHtable=\theHexamplebox
% \let\tableautorefname=\exampleboxautorefname
\let\tablename=\exampleboxname %<- in case \tableautorefname is undefined,
% this will break \autoref-erencing tables
% inside the examplebox-environment!
% patch for hyperref's longtable patch =
% patch \hyper@makecurrent to crank out usage of the table-counter:
\let\hyper@makecurrent=\MyPatchedhyper@makecurrent
% patch \addcontentsline to crank out usage of lot:
\let\addcontentsline=\MyPatchedAddcontentsline
}%
%% examplebox environment
%%
\definecolor{examplebox}{gray}{0.95} %define the examplebox color
\newcounter{examplebox}
\newcommand*{\exampleboxautorefname}{Example box}
\newcommand*{\exampleboxname}{Example box}
\newcommand*\listexampleboxname{List of example boxes}%
\newenvironment{examplebox}{%
\ExampleboxStuffInsteadOfTableStuff
\longtable
}{\endlongtable}
%% \listofexampleboxes
%%
\newcommand\listofexampleboxes{%
\if@twocolumn
\@restonecoltrue
\onecolumn
\else
\@restonecolfalse
\fi
\chapter*{\listexampleboxname}%
\@mkboth{\MakeUppercase\listexampleboxname}%
{\MakeUppercase\listexampleboxname}%
\@starttoc{loEB}%
\if@restonecol\twocolumn\fi
}%
\makeatother
\begin{document}
\listoftables\newpage
\listofexampleboxes\newpage
\autoref{box:test} is the label of this examplebox.\\
The current counter for examplebox is \theexamplebox.
% A column of type p{<width>} from the array package is
% used to make enumerate and itemize work.
\begin{examplebox}{|p{5cm}|} \hline
\rowcolor{examplebox}%
\label{box:test}%
\begin{itemize}
\item A
\item B
\item C
\end{itemize}
\begin{enumerate}
\item A
\item B
\item C
\end{enumerate}
Hello there.\\ \hline
\caption{Caption of this example box.}
\end{examplebox}
The current counter for examplebox is now \theexamplebox.
\begin{table}[h]
\begin{center}
\begin{tabular}{| c c c |}
\hline
1&2&3\\ \hline
4&5&6\\ \hline
\end{tabular}
\caption{Random table caption}
\end{center}
\end{table}
The current counter for examplebox is still \theexamplebox.
\end{document}

refstepcounteryou are already there if you can convincelongtableto take the counter value ofexampleboxinszead of it the table counter. But maybe there is a more elegant method. – moewe Jul 16 '18 at 11:32ltcaption's\LTcaptypeorlongtable*can also help you... – moewe Jul 16 '18 at 11:35longtable.tcolorboxormdframedspring to mind. – moewe Jul 16 '18 at 11:43\LTcaptioncan be made to apply only to yourexampleboxand it would also solve the referencing problem if done properly I believe. I would investigate further, but I can't really be bothered to guess your code snippets into a fully compilable document... – moewe Jul 16 '18 at 11:59longfigurepackage but never released it. I will see if I still get the source since you only need to renamelongfiguretoexampleboxto have a solution. Please hold the line... – Jul 16 '18 at 12:19