我正在写一系列讲义,由于我正在处理多个源文件,因此我想将当前文件名附加在列表环境的右上角。以下是我想要的示例:
我曾尝试查看 tcolorbox 包,但它看起来有点难以深入研究。
梅威瑟:
\begin{lstlisting}[language=MyC]
#include <stdio.h>
int
main(void) {
printf("Hello World!");
return 0;
}
\end{lstlisting}
附带lstdefinelanguage
:
\lstdefinelanguage{MyC}{%
numbers=left,
xleftmargin=5mm,
framexleftmargin=5mm,
commentstyle=\color{orange},
keywordstyle=\color{blue},
stringstyle=\color{red},
morekeywords=[1]{NULL},
language=C,
basicstyle=\scriptsize\ttfamily,
breaklines=true,
showstringspaces=false,
frame=single,
escapeinside={(*;}{;*)}
}
答案1
使用该包tcolorbox
,您可以执行以下操作:
\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{listings, skins}
\newtcblisting{mylisting}[1]{
enhanced,
colback=white,
colframe=black,
sharp corners,
boxrule=0.4pt,
left=0pt,
right=0pt,
top=-6pt,
bottom=-6pt,
oversize,
listing only,
listing options={
numbers=left,
xleftmargin=5mm,
commentstyle=\color{orange},
keywordstyle=\color{blue},
stringstyle=\color{red},
morekeywords={NULL},
language=C,
basicstyle=\scriptsize\ttfamily,
breaklines=true,
showstringspaces=false,
escapeinside={(*;}{;*)}
},
overlay={\begin{tcbclipinterior}
\node[draw, anchor={north east}] at (frame.north east) {#1};
\end{tcbclipinterior}}
}
\begin{document}
\begin{mylisting}{main.c}
#include <stdio.h>
int
main(void) {
printf("Hello World!");
return 0;
}
\end{mylisting}
\end{document}