14

I want to insert lstlisting inside a tcolorbox which must has a title and title can be hidden whenever I need, also it's top right and bottom left corner will be rounded something like this:

enter image description here

I know how to use tcolorbox and lstlisting but I don't know how to use them together. Could anyone please help me? Any alternate solution without tcolorbox will be also appreciated.

My code snippet :

\documentclass[10pt,a4paper,oneside]{book}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[left=2.00cm, right=2.00cm, top=2.00cm, bottom=2.00cm, showframe]{geometry}
\author{Ravi}
\title{How to wite code in \LaTeX}

%%%%%%%%%%%%%%% CODE STYLE %%%%%%%%%%%%%%%

\usepackage{blindtext}
\usepackage{xcolor}

\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}

\renewcommand{\rmdefault}{phv}
\usepackage[T1]{fontenc}
\usepackage[ttdefault=true]{AnonymousPro}

\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage{inconsolata}
\lstset {
    language=Java,%
    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,
    %breakatwhitespace=true,
    %numbers=left,%
    %numbersep=8pt,%
    %numberstyle=\tiny,%
    rulecolor=\color{white!60!black},%
    breaklines=true,%
    frame=single,%single
    %keepspaces=true,%
    xleftmargin=1.5mm,%\parindent
    xrightmargin=1.5mm,%
    framerule=1.2pt,%
    backgroundcolor=\color{gray!8},%
    %morecomment=[s][\bfseries\color{orange!40!red}]{@}{Override},
    tabsize=1,
}
\lstset{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}}%
}

\begin{document}
    %\maketitle
    \chapter{Hello \LaTeX}

    \begin{lstlisting}
        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{lstlisting}

\end{document}

1 Answer 1

12

Your tcolorbox style can be set as follows

\documentclass{article}
\usepackage[most]{tcolorbox}
\newtcolorbox{mycode}[2][]{%
    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,
    colframe=gray!20,
    top=\tcboxedtitleheight,
    bottom=\tcboxedtitleheight,
    sharp corners=downhill,
    arc=.5cm,
    title={#2},#1
}
\usepackage{lipsum}
\begin{document}
\begin{mycode}{This is some blahblah}
\lipsum[1]
\end{mycode}
\end{document}

enter image description here

So let's convert it to a listings tcolorbox:

\documentclass{article}
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings}
\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}
\begin{document}
\begin{tcblisting}{
    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=\tcboxedtitleheight,
    sharp corners=downhill,
    arc=.5cm,
    title=MainActivity.java,
    listing only,
    listing options={
        language=Java,
        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,
        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}}
    }
}
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{tcblisting}
\end{document}

enter image description here

2
  • Using this the text sometimes gets out of the box, how to fix this? Apr 25, 2019 at 10:01
  • @RedaDrissi Please ask a new question so that I can have a look
    – user156344
    Apr 25, 2019 at 13:36

You must log in to answer this question.

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