0

I simply would like to have an enumerated list that contains code, but the result is glitchy:

enter image description here

I would ideally have the code-block indented and starting at the same horizontal level as the item label. Here is my code:

\documentclass[10pt]{article}
\usepackage[a4paper, total={18cm, 25cm}]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{fontspec}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{multicol}
\usepackage{xpatch}
\usepackage{realboxes}

\definecolor{mygray}{rgb}{0.9,0.9,0.9}

\lstset{
  language=c,
  breaklines=true,
  keepspaces=true,
  backgroundcolor=\color{mygray},
  frame=single,
  framerule=0pt,  
  framextopmargin=3ex,
  framexbottommargin=3ex,
  framexleftmargin=1em,
  xleftmargin={\dimexpr 1em+3pt},
  linewidth={\dimexpr \linewidth-3pt}
}

\begin{document}
\begin{multicols}{2}
\begin{enumerate}[label=(\roman*)]
\item\begin{lstlisting}
#include <stdio.h>

int main() {
   printf("Hello World!");
}
\end{lstlisting}

\item\begin{lstlisting}[language=lisp]
(defun make-hanoi (from-tower aux-tower to-tower)
(list from-tower aux-tower to-tower))

;; Select I'th tower of HANOI
(defun hanoi-tower (hanoi i)
(nth (1- i) hanoi))
\end{lstlisting}

\end{enumerate}
\end{multicols}
\end{document}
2

You need a few adjustments:

  1. issue something that makes \item to do its job, for instance \mbox{};
  2. add a negative vertical space to cover your tracks;
  3. fix the xleftmargin;
  4. fix the linewidth.
\documentclass[10pt]{article}
\usepackage[a4paper, total={18cm, 25cm}]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{fontspec}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{multicol}
\usepackage{xpatch}
\usepackage{realboxes}

\definecolor{mygray}{rgb}{0.9,0.9,0.9}

\lstnewenvironment{itemlisting}[1][]
 {%
  \mbox{}
  \vspace*{-\baselineskip}
  \lstset{
    xleftmargin=\leftmargin,
    linewidth=\linewidth,
    #1
  }%
 }
 {}

\lstset{
  language=c,
  breaklines=true,
  keepspaces=true,
  backgroundcolor=\color{mygray},
  frame=single,
  framerule=0pt,  
  framextopmargin=3ex,
  framexbottommargin=3ex,
  framexleftmargin=1em,
  xleftmargin=\dimexpr 1em+3pt\relax,
  linewidth=\dimexpr \linewidth-3pt\relax
}

\begin{document}

\begin{multicols}{2}
\begin{enumerate}[label=(\roman*)]
\item \begin{itemlisting}
#include <stdio.h>

int main() {
   printf("Hello World!");
}
\end{itemlisting}

\item\begin{itemlisting}[language=lisp]
(defun make-hanoi (from-tower aux-tower to-tower)
(list from-tower aux-tower to-tower))

;; Select I'th tower of HANOI
(defun hanoi-tower (hanoi i)
(nth (1- i) hanoi))
\end{itemlisting}

\end{enumerate}
\end{multicols}
\end{document}

I left the bad column break to show what can be the remaining issues: apparently, listings considers the space above the listing in the frame as a good page break point; I'm afraid you'll have to manually adjust the bad breaks.

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.