Zähler für Kapitel- und Abschnittsumgebungen bei Verwendung von tcolorbox für Auflistungen

Zähler für Kapitel- und Abschnittsumgebungen bei Verwendung von tcolorbox für Auflistungen

Dies ist eine Folgefrage vondiese Frage. Mit meinem MWE bekomme ich falsch Listing numbers, falsch toc(einschließlich Kapitel- und Abschnittsumgebungen aus Auflistungen) und falsch bookmarksim PDF toc. Jede Hilfe wird sehr geschätzt. Danke


MWE


\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}

Ausgabe


Bildbeschreibung hier eingeben

Bildbeschreibung hier eingeben

Antwort1

Ich schlage vor, die Befehle zum Speichern der Struktur nur in den unteren Teil zu setzen. Die Nummerierung tcolorboxwird dadurch nicht beeinflusst.

Um die tocsund bookmarksProbleme zu vermeiden, schlage ich vor, \addtocontentsund zu deaktivieren, \addcontentslinewie es für getan wurde \documentclass.

Meine Antwort erstellt eine neue Option protect structure, die all diese Dinge enthält.

\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=}

Es wird ein Parameter benötigt, in dem Zählereinstellungen angegeben werden können; hier:

protect structure={\setcounter{chapter}{0}}

Alles in allem erhalten wir nun:

\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}

Antwort2

Die number withinOption in der Definition von muss auf oder (vielleicht besser) texexptitledgesetzt werden .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}

verwandte Informationen