答案1
定义一个禁用反步进和 ToC 编写的环境。这允许很大的自由度:
\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}
分段标题的计数器存储在 处\begin{quietsections}
,并在 期间恢复\end{quietsections}
。
下面显示了如何对 进行操作\section
,但很容易将其扩展到其他部分单位:
\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}
答案2
更新最后,自动恢复任意计数器!
使用dummycounter
与上部级别重置的和\let\c@subsection\c@dummycounter
,即明确使用计数寄存器c@dummycounter
而不是c@subsection
如果在小页面内部使用它,则\let
操作不会泄漏到外面(因为它minipage
是一个环境,也是一个组)。
\subsubsection
请注意,我的方法在开箱即用时不会有用。这需要做更多的工作!
适当设置tocdepth
计数器将防止出现虚假的子部分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}
更新
xassoccnt
带有 的版本--\BackupCounterValues
这可以维护各种计数器的值并自动阻止进入ToC
、Lof
和Lot
。
的未来版本xassoccnt
将会支持此类开箱即用的功能——我已将其放入我的待办事项清单中!
由于拳击,使用figure
将无法工作!table
\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}