2

I have a weird behaviour when I try to include some lstlistings inside a mdframed box. Most of the time, it works as expected, but under some conditions, I get incorrect output, or even a latex error (a missing \item, in this case)

I included a MWE below, with some stuff commented out for you to play with.

observations/questions:

  • compilation goes fine if I remove the mdframed completely. But then my "paragraph 1" heading disappears. I can bring it back by adding e.g. an extra ~ afther the \paragraph{}. any clue why ?
  • with mdframed included, compilation goes fine and the output looks correct if I remove the first \paragraph{} command. But with both, I get a Something's wrong--perhaps a missing \item. any clue why ?
  • a workaround which seems to work for me is to enclose the lstlisting in a minipage. but how robust is this approach ?

my main question is: how do I troubleshoot this kind of problem, and/or who do I report it to ? is it a bug in mdframed, in listings, or in article ?

MWE:

\documentclass{article}

\usepackage{mdframed}
\usepackage{listings}

\begin{document}

\begin{mdframed}
  \paragraph{Paragraph 1}
  % \begin{minipage}{1.0\linewidth}
\begin{lstlisting}
dummy verbatim text
\end{lstlisting}
  % \end{minipage}

  \paragraph{Paragraph 2} 
  \begin{itemize}
  \item first item
  \item second item
  \end{itemize}

\end{mdframed}

\end{document}
2

Trying to start a listing just after a sectional title is always a problem. In particular, if you try

\paragraph{Paragraph A}
\begin{lstlisting}
dummy verbatim text
\end{lstlisting}

at the top level (not inside mdframed), you'll notice that the heading disappears.

I don't think you want the listing to be next to the title: for instance,

\paragraph{Paragraph 1}
\begin{minipage}{1.0\linewidth}
\begin{lstlisting}
dummy verbatim text
dummy verbatim text
dummy verbatim text
dummy verbatim text
\end{lstlisting}

would produce

enter image description here

which, most likely, is not what you want.

Solution: instead of mdframed use tcolorbox (I'll leave to you how to customize the enclosing box), that also cures several other glitches in mdframed and use \mbox{} after the sectional title, if a listing immediately follows.

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{listings}

\lstset{
  basicstyle=\ttfamily,
  columns=fullflexible,
}

\begin{document}

\paragraph{Paragraph A}\mbox{}
\begin{lstlisting}
dummy verbatim text
dummy verbatim text
dummy verbatim text
dummy verbatim text
\end{lstlisting}

\paragraph{Paragraph B}
\begin{itemize}
\item first item
\item second item
\end{itemize}

\begin{tcolorbox}
\paragraph{Paragraph 1}\mbox{}
\begin{lstlisting}
dummy verbatim text
dummy verbatim text
dummy verbatim text
dummy verbatim text
\end{lstlisting}

\paragraph{Paragraph 2}
\begin{itemize}
\item first item
\item second item
\end{itemize}

\end{tcolorbox}

\end{document}

enter image description here

1
  • thanks for all the useful advice. Indeed, tcolorbox is the way to go (and its documentation is wonderful). – Gyom Mar 3 '16 at 19:18
3

I would use tcolorbox instead of mdframed -- much more robust. And you will have to add a \leavevmode to avoid that your \paragraph disappear:

\documentclass{article}

\usepackage[many]{tcolorbox}
\usepackage{listings}

\begin{document}

\begin{tcolorbox}
  \paragraph{Paragraph 1}\leavevmode
\begin{lstlisting}
dummy verbatim text
\end{lstlisting}


  \paragraph{Paragraph 2}
  \begin{itemize}
  \item first item
  \item second item
  \end{itemize}

\end{tcolorbox}

\end{document}
2

The verbatim-like environment are notorious in causing a lot of headaches, and getting around them can be quite difficult. Fortunately, the fancyvrb provides an alternative to the default verbatin-like environments that works in more places.

In your particular case, you can set tell the listings package to use fancyvrb in order to collect the code:

\documentclass{article}

\usepackage{mdframed}
\usepackage{fancyvrb}
\usepackage{listings}
\lstset{fancyvrb}

\begin{document}
\begin{mdframed}
  \paragraph{Listings} \leavevmode
  \begin{lstlisting}
    #include <iostream>

    int main()
    {
      std::cout << "Hello World!";
    }
  \end{lstlisting}
\end{mdframed}
\end{document}

listings

Another package to produce listings is minted which also uses fancyvrb in order to collect the code:

\documentclass{article}

\usepackage{mdframed}
\usepackage{minted}

\begin{document}
\begin{mdframed}
  \paragraph{Minted}
  \begin{minted}[autogobble]{c++}
    #include <iostream>

    int main()
    {
      std::cout << "Hello World!";
    }
  \end{minted}
\end{mdframed}
\end{document}

minted

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.