How can I make a "fake" subsection number?

Define an environment within which counter stepping and ToC-writing is disabled. This allows great freedom:

enter image description here

\documentclass{article}

\usepackage{xparse}

\newcounter{savesection}
\newcounter{savesubsection}
\newcounter{savesubsubsection}
\newenvironment{quietsections}
  {% Store sectional counters
   \setcounter{savesection}{\value{section}}%
   \setcounter{savesubsection}{\value{subsection}}%
   \setcounter{savesubsubsection}{\value{subsubsection}}%
   \renewcommand{\refstepcounter}[1]{}% Remove \refstepcounter functionality
   \renewcommand{\addcontentsline}[3]{}% Remove \addcontentslie functionality
  }{%
   % Restore sectional counters
   \setcounter{section}{\value{savesection}}%
   \setcounter{subsection}{\value{savesubsection}}%
   \setcounter{subsubsection}{\value{savesubsubsection}}%
  }

\begin{document}

\tableofcontents

\section{A section}
\section*{Another section}

\bigskip

\noindent
\fbox{%
  \begin{minipage}{\dimexpr\linewidth-2\fboxsep-2\fboxrule}
    \begin{quietsections}
      \setcounter{section}{4}
      \section{A fake section}
      \setcounter{subsection}{3}
      \subsection{Another fake subsection}
      \subsection*{More subsections}
    \end{quietsections}
  \end{minipage}%
}

\bigskip

\section{Yet another section}

\end{document}

Counters for sectional headings are stored at \begin{quietsections}, and restored during \end{quietsections}.


The following shows how it can be done for \section, but it is easy to extend it to other sectional units:

enter image description here

\documentclass{article}

\usepackage{xparse}

\NewDocumentCommand{\fakesection}{s m o m}{{%
  \renewcommand{\refstepcounter}[1]{}% Remove functionality of \refstepcounter
  \renewcommand{\addcontentsline}[3]{}% Remove functionality of \addcontentsline
  \renewcommand{\thesection}{#2}% Set subsection counter
  \IfBooleanTF{#1}
    {\section*{#4}}
    {\IfValueTF{#3}
       {\section[#3]{#4}}
       {\section{#4}}%
    }
}}

\begin{document}

\tableofcontents

\section{A section}
\section*{Another section}

\bigskip

\noindent
\fbox{%
  \begin{minipage}{\dimexpr\linewidth-2\fboxsep-2\fboxrule}
    \fakesection{5}{A fake section}
    \fakesection*{X}{Another fake section}
  \end{minipage}%
}

\bigskip

\section{Yet another section}

\end{document}

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}

enter image description here

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}

enter image description here