如何将 lstlisting 与 tcolorbox 结合使用?

如何将 lstlisting 与 tcolorbox 结合使用?

我想在 tcolorbox 中插入 lstlisting,它必须有一个标题,并且可以在需要时隐藏标题,而且它的右上角和左下角将是圆角,如下所示:

在此处输入图片描述

我知道如何使用 tcolorbox 和 lstlisting,但我不知道如何将它们一起使用。有人能帮我吗?任何不使用 tcolorbox 的替代解决方案也将不胜感激。

我的代码片段:

\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

你的tcolorbox风格可以设置如下

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

在此处输入图片描述

因此我们将其转换为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}

在此处输入图片描述

相关内容