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:
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}
Best Answer
Your
tcolorbox
style can be set as followsSo let's convert it to a
listings
tcolorbox
: