0

I am trying to define a tcblistings environment that should work under the following syntax...

\begin{code}{TITLE}{LANG}{CAPTION}{OPTS}
code here
\end{code}

Everything but the language should be optional. I want to add a listings caption starting from the top left and to reshape around the title box. The following code is the closest I got (inspired by this and this)...

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

Which results in...

tcblistings

Is there any way to control the position and style of the lstlistings caption inside the tcolorbox to achieve the desired result? Alternatively, how can I define another caption apart from the listing's one to achieve the same result?

Additionally, is it possible to modify things so that I can specify the environment like...

\begin{code}[lang={LANG}, title={TITLE}, caption={CAPTION},...}
code here
\end{code}

?

1 Answer 1

0

I hoped to get by with caption, but it doesn't interact with tcolorbox. 😩

Do it the hard way.

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

\makeatletter
\AtBeginEnvironment{code}{%
  \long\def\@makecaption#1#2{%
    \vskip\abovecaptionskip
    #1: #2\par
    \vskip\belowcaptionskip
  }%
}
\makeatother

\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,
        abovecaptionskip=0pt,
        belowcaptionskip=\medskipamount,
        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}

enter image description here

3
  • Thank you for your response. That looks like progress. Do you think it'd be possible to get the caption above the tcolorbox, and have it say, in bold, "Code Snippet i" (where i is the number code snippet in the document)?
    – user41177
    Commented Sep 2, 2023 at 21:03
  • @user41177 That's a completely different business.
    – egreg
    Commented Sep 2, 2023 at 21:15
  • In that case, would it be easier to try and turn what is currently the title into an overlay (or something else), and make the caption the title?
    – user41177
    Commented Sep 2, 2023 at 21:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .