
Это дополнительный вопросэтот вопрос. С моим 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}