1

I want to create a new environment that wraps mdframed and lstlisting together as shown below, however that does not work for me as it does not compile. Any ideas what the issue is and how to define a new environment similar to the mine below?

\documentclass{llncs}
\pagestyle{plain}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[scaled]{beramono}
\usepackage{xcolor}

\usepackage{mdframed}
\definecolor{light-gray}{gray}{0.95} 

\usepackage{listings}
\lstdefinelanguage{Sage}[]{Python}
{morekeywords={False,sage,True},sensitive=true}
\lstset{
  frame=none,
  showtabs=False,
  showspaces=False,
  showstringspaces=False,
  commentstyle={\ttfamily\color{dgreencolor}},
  keywordstyle={\ttfamily\color{dbluecolor}\bfseries},
  stringstyle={\ttfamily\color{dgraycolor}\bfseries},
  language=Sage,
  basicstyle={\fontsize{10pt}{10pt}\ttfamily},
  aboveskip=0.3em,
  belowskip=0.1em,
  numbers=left,
  numberstyle=\footnotesize
}
\definecolor{dblackcolor}{rgb}{0.0,0.0,0.0}
\definecolor{dbluecolor}{rgb}{0.01,0.02,0.7}
\definecolor{dgreencolor}{rgb}{0.2,0.4,0.0}
\definecolor{dgraycolor}{rgb}{0.30,0.3,0.30}
\newcommand{\dblue}{\color{dbluecolor}\bf}
\newcommand{\dred}{\color{dredcolor}\bf}
\newcommand{\dblack}{\color{dblackcolor}\bf}

\newenvironment{code}
{\begin{mdframed}[
    backgroundcolor=light-gray,
    roundcorner=10pt,
    leftmargin=1,
    rightmargin=1,
    innerleftmargin=10,
    innertopmargin=5,
    innerbottommargin=5,
    outerlinewidth=1,
    linecolor=light-gray
]\begin{lstlisting}[numbers=none]}
{\end{lstlisting}\end{mdframed}}

\begin{document}

\section{Test}

A code example:

\begin{code}
sage: R.<x> = ZZ[]
sage: type(R.an_element())
<type 'sage.rings...Polynomial_integer_dense_flint'>
sage: R.<x,y> = ZZ[]
sage: type(R.an_element())
<type 'sage.rings...MPolynomial_libsingular'>
sage: R = PolynomialRing(ZZ, 'x', implementation='NTL')
sage: type(R.an_element())  # this is a comment
<type 'sage.rings...Polynomial_integer_dense_ntl'>
sage: def abc():
...       """
...       This should be a very long comment.
...       That should span multiple lines.
...       To illustrate what colour Sage comments look like.
...       To get a feel for the color when rendered using LaTeX.
...       """
...       return 2
\end{code}

\end{document}
3
  • 1
    I would use tcolorbox instead of mdframed. This has build in support for new lstlistings environments. Dec 26, 2022 at 13:43
  • @samcarter_is_at_topanswers.xyz If one can achieve exactly same shaped and colored box with tcolorbox that would work of course.
    – pixel
    Dec 26, 2022 at 13:47
  • 1
    Tcolorbox is very flexible - you can adjust it to whatever you want it to look like. Dec 26, 2022 at 13:59

1 Answer 1

1

I suggest to use the tcolorbox package instead of mdframes. Tcolorbox has build-in support for new listings environments:

\documentclass{llncs}
\pagestyle{plain}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[scaled]{beramono}
\usepackage{xcolor}

\usepackage[most]{tcolorbox}
\definecolor{light-gray}{gray}{0.95} 

\usepackage{listings}
\lstdefinelanguage{Sage}[]{Python}
{morekeywords={False,sage,True},sensitive=true}
\lstset{
  frame=none,
  showtabs=False,
  showspaces=False,
  showstringspaces=False,
  commentstyle={\ttfamily\color{dgreencolor}},
  keywordstyle={\ttfamily\color{dbluecolor}\bfseries},
  stringstyle={\ttfamily\color{dgraycolor}\bfseries},
  language=Sage,
  basicstyle={\fontsize{10pt}{10pt}\ttfamily},
  aboveskip=0.3em,
  belowskip=0.1em,
  numbers=left,
  numberstyle=\footnotesize
}
\definecolor{dblackcolor}{rgb}{0.0,0.0,0.0}
\definecolor{dbluecolor}{rgb}{0.01,0.02,0.7}
\definecolor{dgreencolor}{rgb}{0.2,0.4,0.0}
\definecolor{dgraycolor}{rgb}{0.30,0.3,0.30}
\newcommand{\dblue}{\color{dbluecolor}\bf}
\newcommand{\dred}{\color{dredcolor}\bf}
\newcommand{\dblack}{\color{dblackcolor}\bf}

\newtcblisting{code}{
  listing options={numbers=none},
  colback=light-gray,
  enhanced,
  frame hidden,
  sharp corners,
  listing only,
}

\begin{document}

\section{Test}

A code example:

\begin{code}
sage: R.<x> = ZZ[]
%sage: type(R.an_element())
%<type 'sage.rings...Polynomial_integer_dense_flint'>
%sage: R.<x,y> = ZZ[]
%sage: type(R.an_element())
%<type 'sage.rings...MPolynomial_libsingular'>
%sage: R = PolynomialRing(ZZ, 'x', implementation='NTL')
%sage: type(R.an_element())  # this is a comment
%<type 'sage.rings...Polynomial_integer_dense_ntl'>
%sage: def abc():
%...       """
%...       This should be a very long comment.
%...       That should span multiple lines.
%...       To illustrate what colour Sage comments look like.
%...       To get a feel for the color when rendered using LaTeX.
%...       """
%...       return 2
\end{code}

\end{document}

enter image description here

2
  • Btw is it possible to specify how round the corners to be? Like roundcorner=10pt in mdframed?
    – pixel
    Dec 26, 2022 at 14:30
  • 1
    @pixel arc=10pt instead of sharp corners Dec 26, 2022 at 14:32

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