我正在尝试定义一个应该在以下语法下工作的 tcblistings 环境......
\begin{code}{TITLE}{LANG}{CAPTION}{OPTS}
code here
\end{code}
除了语言之外的所有内容都应该是可选的。我想从左上角开始添加列表标题,并重新调整标题框的形状。以下代码是我得到的最接近的代码(灵感来自这和这)...
\documentclass{article}
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings, breakable, skins}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage[T1]{fontenc}
\usepackage[ttdefault=true]{AnonymousPro}
\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\newtcblisting[auto counter]{code}[4][]{
enhanced,
attach boxed title to top right={yshift=-\tcboxedtitleheight},
boxed title style={
size=small,colback=gray!50,
colframe=gray!50,
sharp corners=downhill,
arc=.5cm,
top=1mm,bottom=1mm,left=1mm,right=1mm
},
fonttitle=\color{black}\itshape\ttfamily,
colframe=gray!20,
top=\tcboxedtitleheight,
bottom=\smallskipamount,
sharp corners=downhill,
arc=.5cm,
overlay={
\begin{tcbclipinterior}
\fill[gray!25](frame.south west)
rectangle ([xshift=5.1mm]frame.north west);
\end{tcbclipinterior}},
listing remove caption=false,
listing only,
listing options={
numbers=left,
numberstyle=\tiny,
captionpos=t,
language=#3,
xleftmargin=0.6em,
basicstyle=\fontfamily{AnonymousPro}\selectfont,
keywordstyle=\bfseries\color{pblue},
stringstyle=\bfseries\itshape\color{green!40!black},
commentstyle=\bfseries\itshape\color{black!60},
showspaces=false,
showtabs=false,
breaklines=true,
showstringspaces=false,
tabsize=1,
caption=#4,
% emph={
% downto, for, String, TextView, Toast, Button, EditText, ImageView, Typeface, Intent, WebView, WebSettings, SwipeRefreshLayout, RelativeLayout, Animation, AlertDialog, SharedPreferences, Editor, ToggleButton, CardView, LinearLayout, gradient, shape,
% },
emphstyle={\bfseries\color{pblue}},
},
title=#2,
#1
}
\begin{document}
\begin{code}{Hello Java}{Java}{My caption}
String s = "Hello World";
Animation from_top = AnimationUtils.loadAnimation(this, R.anim.from_top);
imageView.setAnimation(from_top);
// change activity with fade animation
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
finish();
}
}, SPLASH_TIMEOUT);
\end{code}
\end{document}
其结果是...
是否有任何方法可以控制 tcolorbox 中 lstlistings 标题的位置和样式以实现所需的结果?或者,如何定义除列表标题之外的其他标题以实现相同的结果?
此外,是否可以修改一些东西,以便我可以指定像这样的环境......
\begin{code}[lang={LANG}, title={TITLE}, caption={CAPTION},...}
code here
\end{code}
?
答案1
我希望能够通过caption
,但它不与进行交互tcolorbox
。