2

I am trying to add a title with counter to my Listings. This is what I have so far:

\usepackage{listings}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\newtcblisting[auto counter,number within=section]{cpp}[2][]{
  title={Listing \thetcbcounter: #2},
  colback=white,
  boxrule=0pt,
  arc=0pt,
  outer arc=0pt,
  top=0pt,
  bottom=0pt,
  colframe=white,
  listing only,
  left=15.5pt,
  enhanced,
  listing options={
    basicstyle=\footnotesize\ttfamily,
    keywordstyle=\color{blue}\ttfamily,
    stringstyle=\color{red}\ttfamily,
    commentstyle=\color{green}\ttfamily,
    breaklines=true,
    language=C++,
    showstringspaces=false,
    tabsize=2,
    numbers=left
  },
  overlay={
    \fill[gray!30] 
      ([xshift=-3pt]frame.south west)
      rectangle 
      ([xshift=11.5pt]frame.north west);
  }
}
\newtcblisting[auto counter,number within=section]{logs}[2][]{
  title={Listing \thetcbcounter: #2},
  colback=white,
  boxrule=0pt,
  arc=0pt,
  outer arc=0pt,
  top=0pt,
  bottom=0pt,
  colframe=white,
  listing only,
  left=15.5pt,
  enhanced,
  listing options={
     basicstyle=\scriptsize\ttfamily,
    breaklines=true,
    showstringspaces=false,
    tabsize=2,
    numbers=left
  },
  overlay={
    \fill[gray!30] 
      ([xshift=-3pt]frame.south west)
      rectangle 
      ([xshift=11.5pt]frame.north west);
  }
}

But the problem is that the title is colored white (I marked it that you can see it) and the gray bar on the left is going all the way up beside the title like that:

\begin{logs}{iwconfig Aufruf}
root:~> iwconfig eth0
eth0      IEEE 802.11-bgn  ESSID:off/any  
          Access Point: Not-Associated   Bit Rate=0 kb/s 
\end{logs}

enter image description here

The second problem is that I want the two different Listings handle the numbers in common, but each listing is using its own counter.

I tried to use this post here but did not get it working..

Tobi
  • 165
  • You should not request two completely different issues in one question. –  May 08 '16 at 11:35

1 Answers1

4

See the improved version at the bottom, using tikz styles.

coltitle=black works wonders (the default is coltitle=white) ;-), but white text colour on white background may be difficult to read (;-))

To use a common counter for both listings environmens, use use counter from=cpp, for example.

\documentclass{book}

\usepackage{listings}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\newtcblisting[auto counter,number within=section]{cpp}[2][]{
  title={Listing \thetcbcounter: #2},
  colback=white,
  boxrule=0pt,
  arc=0pt,
  outer arc=0pt,
  top=0pt,
  bottom=0pt,
  colframe=white,
  listing only,
  left=15.5pt,
  enhanced,
  coltitle=black,
  listing options={
    basicstyle=\footnotesize\ttfamily,
    keywordstyle=\color{blue}\ttfamily,
    stringstyle=\color{red}\ttfamily,
    commentstyle=\color{green}\ttfamily,
    breaklines=true,
    language=C++,
    showstringspaces=false,
    tabsize=2,
    numbers=left
  },
  overlay={
    \fill[gray!30] 
      ([xshift=-3pt]frame.south west)
      rectangle 
      ([xshift=11.5pt]frame.north west);
  }
}
\newtcblisting[use counter from=cpp,number within=section]{logs}[2][]{
  title={Listing \thetcbcounter: #2},
  colback=white,
  boxrule=0pt,
  arc=0pt,
  outer arc=0pt,
  top=0pt,
  bottom=0pt,
  colframe=white,
  listing only,
  left=15.5pt,
  enhanced,
  coltitle=black,
  listing options={
     basicstyle=\scriptsize\ttfamily,
    breaklines=true,
    showstringspaces=false,
    tabsize=2,
    numbers=left
  },
  overlay={
    \fill[gray!30] 
      ([xshift=-3pt]frame.south west)
      rectangle 
      ([xshift=11.5pt]frame.north west);
  }
}

\begin{document}
\chapter{Foo}

\section{Foo Foo}
\begin{cpp}{Hello World}
  #include <iostream>
  // Hello World - Example
  int main( int argc, char **argv )
  {
    std::cout << "Hello World\n";
    return 0;
  }
\end{cpp}
\begin{logs}{iwconfig Aufruf}
root:~> iwconfig eth0
eth0      IEEE 802.11-bgn  ESSID:off/any  
          Access Point: Not-Associated   Bit Rate=0 kb/s 
\end{logs}
\end{document}

enter image description here

An improved version with a common style (makes the code shorter!)

\documentclass{book}

\usepackage{listings}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}


\tcbset{meinlistingsstil/.style={  colback=white,
  boxrule=0pt,
  arc=0pt,
  outer arc=0pt,
  top=0pt,
  bottom=0pt,
  colframe=white,
  listing only,
  left=15.5pt,
  enhanced,
  coltitle=black,
  overlay={
    \fill[gray!30] 
      ([xshift=-3pt]frame.south west)
      rectangle 
      ([xshift=11.5pt]frame.north west);
  }}
}

\newtcblisting[auto counter,number within=section]{cpp}[2][]{
  title={Listing \thetcbcounter: #2},
  meinlistingsstil,
  listing options={
    basicstyle=\footnotesize\ttfamily,
    keywordstyle=\color{blue}\ttfamily,
    stringstyle=\color{red}\ttfamily,
    commentstyle=\color{green}\ttfamily,
    breaklines=true,
    language=C++,
    showstringspaces=false,
    tabsize=2,
    numbers=left
  },
  #1
}
\newtcblisting[use counter from=cpp,number within=section]{logs}[2][]{
  title={Listing \thetcbcounter: #2},
  meinlistingsstil,
  coltitle=black,
  listing options={
     basicstyle=\scriptsize\ttfamily,
    breaklines=true,
    showstringspaces=false,
    tabsize=2,
    numbers=left
  },
  #1
}

\begin{document}
\chapter{Foo}

\section{Foo Foo}
\begin{cpp}{Hello World}
  #include <iostream>
  // Hello World - Example
  int main( int argc, char **argv )
  {
    std::cout << "Hello World\n";
    return 0;
  }
\end{cpp}
\begin{logs}{iwconfig Aufruf}
root:~> iwconfig eth0
eth0      IEEE 802.11-bgn  ESSID:off/any  
          Access Point: Not-Associated   Bit Rate=0 kb/s 
\end{logs}
\end{document}
  • Thanks that s working, how can I remove the gray bar on the left next to the title? – Tobi May 08 '16 at 11:47
  • @Tobi: That's another question -- as I wrote in the comment to your question above: One issue per question, not a bunch of issues and changing the question later on! –  May 08 '16 at 11:47
  • @Tobi: A tiny hint: Look for the only portion of the code that has gray in it -- it should be easy to figure out, which is responsible for the gray bar! ;-) –  May 08 '16 at 11:55