
これは次の質問の続きですこの質問私の 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}