仅在内容旁边列出覆盖内容,而不在标题旁边列出覆盖内容

仅在内容旁边列出覆盖内容,而不在标题旁边列出覆盖内容

我得到的这个列表左侧有一个灰色的条,里面有行号。我不想让它显示在标题旁边。

这是我的代码:

\usepackage{listings}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\newtcblisting[auto counter,number within=section]{cpp}[2][]{
  title={Listing \thetcbcounter: #2},
  coltitle=black,
  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);
  }
}

它看起来像这样

在此处输入图片描述

答案1

xshift您可以使用和yshift或使用calctikzlibrary 通过在角标识符中添加坐标元组(此处frame.north west和)来调整框覆盖的角frame.south west

梅威瑟:

\documentclass{article}
\usepackage{listings}
\usepackage[many]{tcolorbox}
\usetikzlibrary{calc}
\tcbuselibrary{listings}
\newtcblisting[auto counter,number within=section]{cpp}[2][]{
  title={Listing \thetcbcounter: #2},
  coltitle=black,
  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,yshift=8pt]frame.south west)
      rectangle 
      ($(frame.north west)+(0.5,-0.75)$);
  }
}
\begin{document}
\begin{cpp}{iwconfig Aufruf}
root:~> iwconfig eth0
eth0      IEEE 802.11-bgn  ESSID:off/any  
          Access Point: Not-Associated   Bit Rate=0 kb/s 
\end{cpp}
\end{document}

结果:

在此处输入图片描述

顺便问一下:当你关闭所有边框等时,为什么要使用 tcolorbox?一个简单的 lstlisting 可能就足够了。

相关内容