Contadores para entornos de Capítulo y Sección cuando se usa tcolorbox para listados

Contadores para entornos de Capítulo y Sección cuando se usa tcolorbox para listados

Esta es una pregunta de seguimiento deesta pregunta. Con mi MWE me equivoco Listing numbers, me equivoco toc(incluidos los entornos de capítulos y secciones de los listados) y me equivoco bookmarksen pdf toc. Cualquier ayuda será muy apreciada. Gracias


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}

Producción


ingrese la descripción de la imagen aquí

ingrese la descripción de la imagen aquí

Respuesta1

Propongo poner los comandos para guardar la estructura solo en la parte inferior. De este modo, la numeración de los mismos tcolorboxno se ve afectada.

Para evitar problemas tocsy bookmarks, sugiero desactivar \addtocontentsy \addcontentslinecomo se hizo con \documentclass.

Mi respuesta crea una nueva opción protect structureque contiene todo esto.

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

Se necesita un parámetro donde se pueden dar configuraciones de contador; aquí:

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

Ahora todos juntos obtenemos:

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

Respuesta2

La number withinopción en la definición de texexptitleddebe establecerse en backupsectiono \backupchapter(quizás mejor)

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

información relacionada