목록에 tcolorbox를 사용할 때 장 및 섹션 환경에 대한 카운터

목록에 tcolorbox를 사용할 때 장 및 섹션 환경에 대한 카운터

의 후속 질문입니다.이 질문. 내 MWE를 사용하면 잘못되고 Listing numbers잘못 되었습니다 toc(목록의 장 및 섹션 환경 포함). bookmarkspdf에서는 잘못되었습니다 toc. 어떤 도움이라도 주시면 감사하겠습니다. 감사해요


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}

산출


여기에 이미지 설명을 입력하세요

여기에 이미지 설명을 입력하세요

답변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}

관련 정보