这是这个问题。使用我的 MWE 时,我得到的是错误的Listing numbers
、错误的toc
(包括列表中的章节和部分环境)和错误的bookmarks
pdf toc
。任何帮助都将不胜感激。谢谢
平均能量损失
\documentclass[oneside]{book}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%
\usepackage{xcolor}
\usepackage[bookmarks=true, bookmarksopenlevel=2]{hyperref}
%Definition of New Environments
\tcbset{
texexp/.style={colframe=green!85!black, colback=red!5!white,
coltitle=red!50!yellow!3!black, left=6mm,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}},
fonttitle=\small\textsc\bfseries, fontupper=\small, fontlower=\small},
example/.style 2 args={texexp,
title={Listing \thetcbcounter: #1},label={#2}},
}
\newtcblisting{texexp}[1]{texexp,#1}
\newtcblisting[auto counter,number within=section]{texexptitled}[3][]{%
example={#2}{#3},#1}
\newtcolorbox[use counter from=texexptitled]{texexptitledspec}[3][]{%
example={#2}{#3},#1}
\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}
\tableofcontents
\mainmatter
\chapter{First Chapter}
\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{chapter}{0}
\begin{texexptitled}{Example}{mylab}
\documentclass{book}
\begin{document}
\chapter{Chapter Test}
\section{Section 334}
Here, we use Example with a title line.
\end{document}
\end{texexptitled}
\PopCounterValues% Restore the original counter values%
\endgroup
\chapter{New Chapter}
\section{New Section}
\blindtext
\end{document}
输出
答案1
我建议将结构保存命令仅放入下部。这样,编号tcolorbox
就不会受到影响。
为了避免tocs
和bookmarks
问题,我建议停用\addtocontents
和,\addcontentsline
就像对一样\documentclass
。
protect structure
我的答案创建了一个包含所有这些内容的新选项。
\tcbset{protect structure/.style={
before lower={\begingroup%
\PushCounterValues% Push the counter values to the backup values
\renewenvironment{document}{}{}%
\renewcommand\documentclass[2][]{}%
\renewcommand\addtocontents[2]{}%
\renewcommand\addcontentsline[3]{}%
\renewcommand\chaptermark[1]{}%
\renewcommand\sectionmark[1]{}%
#1%
},
after lower={%
\PopCounterValues% Restore the original counter values%
\endgroup%
}},
protect structure/.default=}
它需要一个可以给出计数器设置的参数;这里:
protect structure={\setcounter{chapter}{0}}
现在,我们总共得到:
\documentclass[oneside]{book}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%
\usepackage{xcolor}
\usepackage[bookmarks=true, bookmarksopenlevel=2]{hyperref}
%Definition of New Environments
\tcbset{
texexp/.style={colframe=green!85!black, colback=red!5!white,
coltitle=red!50!yellow!3!black, left=6mm,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}},
fonttitle=\small\textsc\bfseries, fontupper=\small, fontlower=\small},
example/.style 2 args={texexp,
title={Listing \thetcbcounter: #1},label={#2}},
}
\newtcblisting{texexp}[1]{texexp,#1}
\newtcblisting[auto counter,number within=section]{texexptitled}[3][]{%
example={#2}{#3},#1}
\newtcolorbox[use counter from=texexptitled]{texexptitledspec}[3][]{%
example={#2}{#3},#1}
\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}%
}%
\tcbset{protect structure/.style={
before lower={\begingroup%
\PushCounterValues% Push the counter values to the backup values
\renewenvironment{document}{}{}%
\renewcommand\documentclass[2][]{}%
\renewcommand\addtocontents[2]{}%
\renewcommand\addcontentsline[3]{}%
\renewcommand\chaptermark[1]{}%
\renewcommand\sectionmark[1]{}%
#1%
},
after lower={%
\PopCounterValues% Restore the original counter values%
\endgroup%
}},
protect structure/.default=}
\begin{document}
\tableofcontents
\mainmatter
\chapter{First Chapter}
\section{First Section}
\subsection{Outer subsection Number one}
\subsection{Outer subsection Number two}
\blindtext
\begin{texexptitled}[protect structure={\setcounter{chapter}{0}}]{Example}{mylab}
\documentclass{book}
\begin{document}
\chapter{Chapter Test}
\section{Section 334}
Here, we use Example with a title line.
\end{document}
\end{texexptitled}
\chapter{New Chapter}
\section{New Section}
\blindtext
\end{document}
答案2
number within
定义中的选项必须texexptitled
设置为backupsection
或\backupchapter
(也许更好)
\documentclass[oneside]{book}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%
\usepackage{xcolor}
\usepackage[bookmarks=true, bookmarksopenlevel=2]{hyperref}
%Definition of New Environments
\tcbset{
texexp/.style={colframe=green!85!black, colback=red!5!white,
coltitle=red!50!yellow!3!black, left=6mm,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}},
fonttitle=\small\textsc\bfseries, fontupper=\small, fontlower=\small},
example/.style 2 args={texexp,
title={Listing \thetcbcounter: #1},label={#2}},
}
\newtcblisting{texexp}[1]{texexp,#1}
\newtcblisting[auto counter,number within=backupsection]{texexptitled}[3][]{%
example={#2}{#3},#1}
\newtcolorbox[use counter from=texexptitled]{texexptitledspec}[3][]{%
example={#2}{#3},#1}
\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}
\tableofcontents
\mainmatter
\chapter{First Chapter}
\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{chapter}{0}
\begin{texexptitled}{Example}{mylab}
\documentclass{book}
\begin{document}
\chapter{Chapter Test}
\section{Section 334}
Here, we use Example with a title line.
\end{document}
\end{texexptitled}
\PopCounterValues% Restore the original counter values%
\endgroup
\chapter{New Chapter}
\section{New Section}
\blindtext
\end{document}