我怎样才能制作一个“假”的子部分编号?

我怎样才能制作一个“假”的子部分编号?

我正在编写一个 LaTeX 文档,其中包含有关如何构造另一个 LaTeX 文档的示例。但是,我无法找到一种方法来使示例中的子节 a) 不包含在目录中,并且 b) 其数字不会随主要部分而增加。

\include并且\input都增加,所以我无法使用它们。

这是我当前的 MWE,使用两个文件和\include

\fbox{
\begin{minipage}{\textwidth}

\include{EX1}

\end{minipage}
}

示例 1:

\subsection{Fake Subsection}

Some text.

尝试创建一个虚假的小节:

答案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这可以维护各种计数器的值并自动阻止进入ToCLofLot

的未来版本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}

在此处输入图片描述

相关内容