使用 tcolorbox 包的列表,没有 Chapter 和 Section 环境效果

使用 tcolorbox 包的列表,没有 Chapter 和 Section 环境效果

我想listings使用tcolorbox包但不使用计数ChapterSection环境。参见 MWE,我section在列表中使用了环境,这会影响以下部分的计数器。如果有人能帮助我 listings使用tcolorbox包但不使用计数ChapterSection环境,我将不胜感激。谢谢


输出


在此处输入图片描述


平均能量损失


\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%
\usepackage{xcolor}

\lstset{ % General setup for the package
    language={[LaTeX]TeX},
    basicstyle=\small\sffamily,
    numbers=left,
    numberstyle=\tiny,
    frame=tb,
    tabsize=4,
    columns=fixed,
    showstringspaces=false,
    showtabs=false,
    keepspaces,
    commentstyle=\color{red},
    keywordstyle=\color{blue}
}%



\tcbset{listing engine={listings}}


\usepackage[english]{babel}
\usepackage{blindtext}

\begin{document}

\section{First Section}

\blindtext

% Redefine the document environment within a group
\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\begin{tcblisting}{colback=red!5!white,colframe=red!25,left=6mm,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}}}
\documentclass{article}
\begin{document}
\section{Test Section}
   Welcome to \LaTeX.
\end{document}
\end{tcblisting}

\endgroup

\section{Second Section}

\blindtext

\end{document}

答案1

这会存储当前节计数器并在列表环境之后恢复它。在内部,节计数器会重置,以便输出列表。

\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%
\usepackage{xcolor}

\lstset{ % General setup for the package
    language={[LaTeX]TeX},
    basicstyle=\small\sffamily,
    numbers=left,
    numberstyle=\tiny,
    frame=tb,
    tabsize=4,
    columns=fixed,
    showstringspaces=false,
    showtabs=false,
    keepspaces,
    commentstyle=\color{red},
    keywordstyle=\color{blue}
}%



\tcbset{listing engine={listings}}


\usepackage[english]{babel}
\usepackage{blindtext}

\begin{document}

\section{First Section}

\blindtext

\newcounter{truesectioncounter}
\setcounter{truesectioncounter}{\number\value{section}}
\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}
\setcounter{section}{0}


\begin{tcblisting}{colback=red!5!white,colframe=red!25,left=6mm,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}}}
\documentclass{article}
\begin{document}
\section{Test Section}
   Welcome to \LaTeX.
\end{document}
\end{tcblisting}
\setcounter{section}{\number\value{truesectioncounter}}

\endgroup

\section{Second Section}

\blindtext

\end{document}

在此处输入图片描述

改良版

\PushCounterValues此版本在环境启动之前通过命令存储所有分段计数器(部分....子段落),然后tcblistings在环境启动之后恢复它们。

这样,任何内部的section 命令不会破坏文档计数器值。

\newtcblistingsenvironment如果使用自定义的话,这可以简化...

\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%
\usepackage{xcolor}

\lstset{ % General setup for the package
    language={[LaTeX]TeX},
    basicstyle=\small\sffamily,
    numbers=left,
    numberstyle=\tiny,
    frame=tb,
    tabsize=4,
    columns=fixed,
    showstringspaces=false,
    showtabs=false,
    keepspaces,
    commentstyle=\color{red},
    keywordstyle=\color{blue}
}%



\tcbset{listing engine={listings}}


\usepackage[english]{babel}
\usepackage{blindtext}

%%% Storage tool macros

\listgadd{\restorecounterlist}{} % Dummy list
\newcommand{\newbackupcounter}[1]{%
  \ifltxcounter{#1}{%
    \newcounter{backup#1}%
  }{}%
}%


\newcommand{\PushCounterValue}[1]{%
  \ifltxcounter{#1}{%
    \setcounter{backup#1}{\number\value{#1}}
    \setcounter{#1}{0}
  }{%
  }%
}%

\newcommand{\PushCounterValues}{%
\forlistloop{\PushCounterValue}{\restorecounterlist}%
}%


\newcommand{\PopCounterValue}[1]{%
  \ifltxcounter{#1}{%
    \setcounter{#1}{\number\value{backup#1}}%
    \setcounter{backup#1}{0}% For 'safety'
  }{}%
}%

\newcommand{\PopCounterValues}{%
\forlistloop{\PopCounterValue}{\restorecounterlist}%
}%


\AtBeginDocument{%
% Setup the counter lists and provide the backup counters
\forcsvlist{\listgadd{\restorecounterlist}}{part,chapter,section,subsection,subsubsection,paragraph,subparagraph}%
\forlistloop{\newbackupcounter}{\restorecounterlist}%
}%

\begin{document}

\section{First Section}

\subsection{Outer subsection Number one}

\subsection{Outer subsection Number two}


\blindtext

% Redefine the document environment within a group


\begingroup
\PushCounterValues% Push the counter values to the backup values
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}
\setcounter{section}{0}


\begin{tcblisting}{colback=red!5!white,colframe=red!25,left=6mm,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}}}
\documentclass{article}
\begin{document}
\section{Test Section}
   Welcome to \LaTeX.
\subsection{ A test subsection}%
\end{document}
\end{tcblisting}
\PopCounterValues% Restore the original counter values%

\endgroup

\section{Second Section}

\blindtext

\end{document}

在此处输入图片描述

相关内容