UPDATE at the end, with automatic restoration of arbitrary counters!!!!
Use a dummycounter
that resets with the upper section level and \let\c@subsection\c@dummycounter
, i.e. explicitly use the count register c@dummycounter
instead of c@subsection
If this is used inside of the minipage, the \let
operation does not leak outside (since minipage
is an environment and as such a group).
Please note, that my approach won't be useful for \subsubsection
out of the box then. This requires more work!
Setting the tocdepth
counter appropiately will prevent the appearance of the fake subsection in the ToC
!
\documentclass{article}
\newcounter{dummycounter}[section]%
\usepackage{blindtext}
\makeatletter
\newcommand{\example}[1]{%
\fbox{%
\begin{minipage}{\textwidth}
\addtocontents{toc}{\setcounter{tocdepth}{-2}}
\let\c@subsection\c@dummycounter%
#1
\end{minipage}
\addtocontents{toc}{\setcounter{tocdepth}{3}}
}%
}
\makeatother
\begin{document}
\tableofcontents
\section{Foo}
\subsection{Real subsection}
\example{\subsection{Fake subsection}\blindtext}
\subsection{Another real subsection}
\section{Other foo}
\subsection{Real subsection}
\example{\subsection{Fake subsection}\blindtext}
\example{\subsection{Fake subsection}\blindtext}
\subsection{Real subsection}
\subsection{Real subsection}
\end{document}

Update
An xassoccnt
version with \BackupCounterValues
-- This maintains the values of various counters and automatically prevents entries to the ToC
, Lof
and Lot
.
A future version of xassoccnt
will support such features out of the box -- I have put it on my to-do-list!
Using figure
and table
won't work, due to the boxing!
\documentclass{article}
\usepackage{blindtext}
\usepackage{tcolorbox}
\usepackage{xassoccnt}
\usepackage{caption}
\usepackage{hyperref}
\newcounter{storedtocdepth}
\ExplSyntaxOn
\newcommand{\preventcft}[1][toc,lof,lot]{%
\clist_set:Nn \l_tmpa_clist {#1}%
\clist_map_inline:Nn \l_tmpa_clist {%
\addtocontents{##1}{\setcounter{tocdepth}{-2}}%
}%
}
\newcommand{\restorecft}[1][toc,lof,lot]{%
\clist_set:Nn \l_tmpa_clist {#1}%
\clist_map_inline:Nn \l_tmpa_clist {%
\addtocontents{##1}{\setcounter{tocdepth}{\number\value{storedtocdepth}}}%
}%
}
\ExplSyntaxOff
\NewDocumentCommand{\example}{O{resetbackup=true}+m}{%
\BackupCounterValues[#1]{subsection,subsubsection,figure,table}%
\CopyDocumentCounters{tocdepth}{storedtocdepth}%
\begin{tcolorbox}[code={\preventcft}]%
#2%
\end{tcolorbox}%
\RestoreAllCounterValues
\restorecft%
}
\begin{document}
\tableofcontents
\listoffigures
\listoftables
\section{Foo}
\subsection{Real subsection}
\begin{figure}
\begin{center}
\caption{A beautiful duck outside}
\includegraphics[scale=0.3]{ente}
\end{center}
\end{figure}
\example{\subsection{Fake subsection}
\begin{center}
\includegraphics[scale=0.3]{ente}%
\captionof{figure}{A beautiful duck}
\end{center}
\blindtext
}
\subsection{Another real subsection}
\section{Other foo}
\example{\subsection{Fake subsection}\blindtext}
\subsection{Another real subsection of 2nd section}
\subsubsection{Foo subsubsection}
\example{
\subsection{Fake subsection}\blindtext
\begin{center}
\begin{tabular}{*{5}{l}}
\hline
1 & 2 & 3 & 4 & 5 \tabularnewline
\hline
\end{tabular}
\captionof{table}{An internal tabular}
\end{center}
\subsubsection{Fake subsubsection}
\begin{center}
\begin{tabular}{*{5}{l}}
\hline
1 & 2 & 3 & 4 & 5 \tabularnewline
\hline
\end{tabular}
\captionof{table}{Another internal tabular}
\end{center}
\subsubsection{Another fake subsubsection}\subsubsection{Imaginary subsubsection}}
\subsubsection{Real world subsubsection}
\subsection{Yet another real subsection of 2nd section}
\subsection{more real subsections}
\end{document}

\include
does introduce a new page everytime, I assume you don't want that really! – user31729 Jun 24 '16 at 16:40