1

How do I made a lstlistings look like following?

wanted

The best I could get was:

tried

The code for the second image is:

\begin{lstlisting}[caption=id(in), frame=single]
out <- in;
return out;
\end{lstlisting}

The code for the first image is:

% \usepackage[boxruled,linesnumbered]{algorithm2e}
\begin{algorithm}
$out \gets in$\;
\Return{$out$}\;
\caption{id(in)}
\end{algorithm}

1 Answers1

3

May I suggest you to use tcolorbox and its wonderful interaction with listings. A little example (adjust the settings according to your needs):

\documentclass{article}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}

\newtcblisting[auto counter]{mytcblisting}[2][]{
  enhanced,
  breakable,
  arc=0pt,
  outer arc=0pt,
  listing only,
  colback=white,
  colframe=black,
  fonttitle=\normalfont,
  colbacktitle=white,
  coltitle=black,
  before skip=6pt,
  after skip=6pt,
  listing options={
    columns=fullflexible,
    basicstyle=\ttfamily\small,
    numbers=left,
    numbersep=10pt,
    xleftmargin=15pt
  },
  title={{\bfseries Algorithm~\thetcbcounter:} #2},
  #1
}

\begin{document}

\begin{mytcblisting}{Some example algorithm}
A test listing
and
some more
code
\end{mytcblisting}

\end{document} 

The result:

enter image description here

Gonzalo Medina
  • 505,128
  • I had to comment out the line before skip and after skip to make this work because of this error: I do not know the key '/tcb/before skip', to which you passed '6pt', and I am going to ignore it. Perhaps you misspelled it. – Bilal Syed Hussain Aug 18 '15 at 23:51
  • 1
    @BilalHussain The error indicates that you are using an outdated version of tcolorbox. Please update tcolorbox. – Gonzalo Medina Aug 18 '15 at 23:53