9

Is it possible to make a rounded corner for the tex code listing? Given:

\documentclass[10pt]{article}
\usepackage[usenames,dvipsnames]{xcolor} %used for font color
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}

\begin{document}

\definecolor{lbcolor}{rgb}{0.1,0.1,0.1}

\lstdefinestyle{mystyle}{
     basicstyle=\ttfamily\color{White},
     language=Java,
     tabsize=1,
     keywordstyle=\color{Red}\bf,
     showstringspaces=false,
     morekeywords={public, class}
 }

\newtcblisting{mylisting}{
      arc=0pt,
      top=0mm,
      bottom=0mm,
      left=0mm,
      right=0mm,
      boxrule=0pt,
      colback=black,
      listing only,
      listing options={style=mystyle},
      hbox
}

\begin{mylisting}
public class MyObject{

    public MyObject(){
        System.out.println("MyObject");
    }
}
\end{mylisting}

\end{document} 

I would like to make a rounded corner. This code will be included in a presentation, and although only a cosmetics issue, it looks much appealing when you have nice rounded corners and shadows. Is it possible to do in in Latex?

  • Didn't know it was that easy. Would you post as an answer Harish? – user3232 Feb 23 '14 at 22:24
  • user3232: I have added some shadow for you. See the updated answer. – user11232 Feb 23 '14 at 23:42
10

The option arc is provided for this purpose. You have set it to 0pt. Change its value to say some thing like 5mm (say). Further, you can add shadows with enhanced option and skins library of tcolorbox. The syntax would be

shadow={<xshift>}{<yshift>}{<offset>}{<options>}

Full code:

\documentclass[10pt]{article}
\usepackage[usenames,dvipsnames]{xcolor} %used for font color
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings,skins}        %%% skins needed for shadow

\begin{document}

\definecolor{lbcolor}{rgb}{0.1,0.1,0.1}

\lstdefinestyle{mystyle}{
     basicstyle=\ttfamily\color{White},
     language=Java,
     tabsize=1,
     keywordstyle=\color{Red}\bf,
     showstringspaces=false,
     morekeywords={public, class}
 }

\newtcblisting{mylisting}{
      enhanced,                             %%% needed for shadow
      arc=5mm,
      top=0mm,
      bottom=0mm,
      left=0mm,
      right=0mm,
      boxrule=0pt,
      colback=black,
      shadow={5mm}{-3mm}{0mm}{fill=black!50!white,
                      opacity=0.5},             %%% here for shadow  and adjust as you like
      listing only,
      listing options={style=mystyle},
      hbox
}

\begin{mylisting}
public class MyObject{

    public MyObject(){
        System.out.println("MyObject");
    }
}
\end{mylisting}

\end{document}

enter image description here

10
\documentclass{book}
\usepackage{listings}    
\lstset{frameshape={RYR}{Y}{Y}{RYR}}   
\begin{document}

\begin{lstlisting}[language=Python,breaklines,basicstyle=\footnotesize\ttfamily]{P04-Tk.py}
from tkinter import *                     # import Tkinter module

root = Tk()                               # create Tk root widget

root.mainloop()                           # enter Tkinter event loop
\end{lstlisting}

\end{document}

enter image description here

And for a double line: \lstset{frameshape={ryrRYR}{yY}{yY}{ryrRYR}}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.