3

I want to get listings using tcolorbox package but without counting Chapter and Section environments. See in MWE, I used section environment withing listings which effects the counter for following sections. Would highly appreciate if someone help me get listings using tcolorbox package but without counting Chapter and Section environments. Thanks


Output


enter image description here


MWE


\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%
\usepackage{xcolor}

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

\begin{document}

\section{First Section}

\blindtext

% Redefine the document environment within a group
\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\begin{tcblisting}{colback=red!5!white,colframe=red!25,left=6mm,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}}}
\documentclass{article}
\begin{document}
\section{Test Section}
   Welcome to \LaTeX.
\end{document}
\end{tcblisting}

\endgroup

\section{Second Section}

\blindtext

\end{document}
  • Shall only section in the listings be unnumbered or should the counting be according to listed internal document? – user31729 Aug 18 '14 at 9:57
  • I want chapter and section environments in the listings should be numbered. – MYaseen208 Aug 18 '14 at 10:05
3

This stores the current section counter and restores it after the listing environment. Internally, the section counter is reset for the listings output purposes.

\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%
\usepackage{xcolor}

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

\begin{document}

\section{First Section}

\blindtext

\newcounter{truesectioncounter}
\setcounter{truesectioncounter}{\number\value{section}}
\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}
\setcounter{section}{0}


\begin{tcblisting}{colback=red!5!white,colframe=red!25,left=6mm,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}}}
\documentclass{article}
\begin{document}
\section{Test Section}
   Welcome to \LaTeX.
\end{document}
\end{tcblisting}
\setcounter{section}{\number\value{truesectioncounter}}

\endgroup

\section{Second Section}

\blindtext

\end{document}

enter image description here

Improved Version

This version stores all sectioning counters (parts....subparagraph) by the command \PushCounterValues just before the tcblistings environment starts and restore them afterwards.

This way, any internal section command will not corrupted the documents counter values.

This could be simplified, if a self defined \newtcblistingsenvironment would be used...

\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%
\usepackage{xcolor}

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

\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{section}{0}


\begin{tcblisting}{colback=red!5!white,colframe=red!25,left=6mm,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}}}
\documentclass{article}
\begin{document}
\section{Test Section}
   Welcome to \LaTeX.
\subsection{ A test subsection}%
\end{document}
\end{tcblisting}
\PopCounterValues% Restore the original counter values%

\endgroup

\section{Second Section}

\blindtext

\end{document}

enter image description here

  • 1
    @MYaseen208: Ok, that's nice -- I will add a solution, which stores all section counter values (chapters,sections,subsections,etc...) – user31729 Aug 18 '14 at 10:22
  • This works fine but does not work properly if numbered listings are used. The numbered listings will have wrong counters. – MYaseen208 Aug 19 '14 at 15:23
  • @MYaseen208: Your original listings didn't have numbers (You mean the tcblisting is numbered)?), so I could not know, that you want to use them. I'll take a look later on – user31729 Aug 20 '14 at 6:15
  • I have a follow up question. – MYaseen208 Aug 20 '14 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.