7

I'm using the listings package as below:

\usepackage{listings}

\lstnewenvironment{pythoncode}[1][]
{\lstset{language=Python,
    basicstyle=\ttfamily\footnotesize,
    frame=shadowbox,
    frameround=tttt,
    rulecolor=\color{black},
    rulesepcolor=\color{gray},
    #1}
}{}

I wanted to get a rounded corners together with a drop-shadow (shadowbox). Is it possible? While the frame border itself had rounded corners, the shadow had square corners, which is kinda ugly.

2
  • Do you have to use listings to create the box? Could you place the listing inside a tikz node? Then you could use something like this: texample.net/tikz/examples/boxes-with-text-and-math – Jan Hlavacek Jan 15 '11 at 1:04
  • 1
    Well... I don't need. I just didn't know any other way. Maybe you could submit an answer (instead of a comment) containing a sample code? ;) It should work with pdflatex and beamer package. – Denilson Sá Maia Jan 16 '11 at 20:53
6

Next code uses tcolorbox to format the shadowed box where code is inserted through listing package. Size, color and format of shadows can be adapted to ones taste.

\documentclass{article}

\usepackage[most]{tcolorbox}

\newtcblisting{pythoncode}[2][]{%
    enhanced, title=#2, colframe=blue!50!black, 
    colback=blue!10!white, 
    fonttitle=\ttfamily, coltitle=white,
    attach boxed title to top left = {xshift=5mm,yshift=-2mm} ,
    boxed title style={size=small, colback=blue!75!black},
    width=.5\linewidth,
    listing only,listing options={language=Python, basicstyle=\ttfamily\footnotesize},#1}

\begin{document}

\begin{pythoncode}[drop shadow]{hello.py}
#!/usr/bin/env python
def main():
    print "Hello, World!"
if __name__ == '__main__':
    main()
\end{pythoncode}

\begin{pythoncode}[drop fuzzy shadow]{hello.py}
#!/usr/bin/env python
def main():
    print "Hello, World!"
if __name__ == '__main__':
    main()
\end{pythoncode}

\begin{pythoncode}[drop lifted shadow]{hello.py}
#!/usr/bin/env python
def main():
    print "Hello, World!"
if __name__ == '__main__':
    main()
\end{pythoncode}
\end{document}

enter image description here

4

do you mean something like this?

\documentclass{beamer}

\usepackage{listings}

\begin{document}

\begin{frame}[fragile]{demo}
\begin{beamerboxesrounded}[shadow]{A listing}
\begin{lstlisting}
foo
bar
baz 
\end{lstlisting}
\end{beamerboxesrounded}

\end{frame}

\end{document}
0
2

This is an example using TikZ. Also working with beamer

\documentclass{article}
 \usepackage{tikz}
\usepackage{amsmath}

\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
  decorations.pathreplacing,decorations.pathmorphing,shapes,%
  matrix,shapes.symbols,plotmarks,decorations.markings,shadows}

\begin{document}
 \begin{tikzpicture}[x=1cm,y=1cm]
  \node[drop shadow,fill=white,draw,rounded corners] 
   {$\sum\limits_{i=1}^N (x_i-\mu_i)^2$};
 \end{tikzpicture}
\end{document}

Would have posted a picture, but I dont have enough rep. to do so, yet.

Using a listing could look like this:

\documentclass{article}                                                                              
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{listings}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
  decorations.pathreplacing,decorations.pathmorphing,shapes,%
  matrix,shapes.symbols,plotmarks,decorations.markings,shadows}

%%%%%%%%%%%%%%%%%%%%%
% Code Listing
%%%%%%%%%%%%%%%%%%%%%
\lstnewenvironment{CODEENV}{
   \lstset{
     language=Matlab,
     numbers=left,
     numberstyle=\tiny,
     stepnumber=1,
     numbersep=1em,
     tabsize=1,
   }
  }{}

%%%%%%%%%%%%%%%%%%%%%
% Document
%%%%%%%%%%%%%%%%%%%%%
\begin{document}
 \begin{tikzpicture}[x=1cm,y=1cm]
  \node[drop shadow,fill=white,draw,rounded corners,inner sep=1em] {
  \begin{CODEENV}
N = 128;
sigma_r = 1;

% Leistungsdichte nach Clark
nu_max      = 1;
nu          = (-nu_max):(2*nu_max/(N-1)):(nu_max);

% Bessel & Clark
xlambda = (0:2/(N-1):2);
rho_R = sigma_r^2*besselj(0, 2*pi*xlambda);

figure;
subplot(211);
plot(xlambda,rho_R/max(rho_R));
subplot(212);
plot(nu, abs(fft(rho_R)).^2);
 \end{CODEENV}
};
 \end{tikzpicture}
\end{document}

Note, that if you have long code snippets, you may need to increase the 'inner sep' parameter of the node, as the line numbering is done outside the listing box (as it seems).

2
  • 1
    The tricky part is not creating the shadow box, but making it work together with listings. – Juan A. Navarro Jan 24 '11 at 13:51
  • You are right. It was a little tricky – Fabian Schuh Jan 24 '11 at 17:17

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.