列表标题和计数器

列表标题和计数器

我正在尝试为我的列表添加一个带计数器的标题。这是我目前所拥有的:

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

但问题是标题是白色的(我将其标记为您可以看到它)并且左侧的灰色条一直延伸到标题旁边,如下所示:

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

在此处输入图片描述

第二个问题是,我希望两个不同的列表处理共同的数字,但每个列表都使用自己的计数器。

我尝试使用这篇文章但没有让它工作..

答案1

请参阅底部使用tikz样式的改进版本。

coltitle=black效果很好(默认是coltitle=white);-),但背景white上的文字颜色white可能难以阅读(;-))

例如,要对两个listings环境使用一个通用的计数器,请使用 。use counter from=cpp

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

在此处输入图片描述

具有通用风格的改进版本(使代码更短!)

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

相关内容