1

I would like to include code between two paragraphs, just like you can include equations. I use the lstlisting environment from the mcode package. I do not understand why it adds a vertical space before the code. How can I change it, so the spaces around the lstlisting are the same as around the equation*?

MWE

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{mcode}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam scelerisque nulla libero, ullamcorper sodales nulla pulvinar sit amet. Proin diam magna, elementum vel lacinia sed, viverra vel nibh. Nam in euismod orci. Maecenas sapien massa, varius vel tempor non, viverra id risus. Maecenas maximus ligula et erat maximus venenatis. Nam nec fringilla magna. Sed facilisis porta nisi sit amet aliquam. Nunc a ullamcorper libero. Aenean malesuada venenatis purus, at dictum augue elementum vel. Curabitur in cursus tellus. Nullam sit amet ullamcorper mi. Aenean id tincidunt odio, sed semper dolor. Nullam et diam malesuada, ultricies tellus eu, blandit nibh. Integer posuere ex neque, ut cursus mauris condimentum in. Suspendisse non ipsum ac nunc venenatis accumsan quis vel diam.

\begin{lstlisting}
x = y+2;
\end{lstlisting}

Nunc aliquet mi eros, id venenatis lorem iaculis quis. Donec pharetra vestibulum ex quis consequat. Fusce vestibulum vel mauris eu posuere. Vestibulum viverra erat at feugiat vestibulum. Sed ac est sed est mattis dignissim. Donec faucibus arcu a mollis consectetur. Suspendisse ac arcu cursus, pulvinar dolor vel, fringilla sem.

\begin{equation*}e^{i\theta}=cos\theta+i\sin\theta\end{equation*}

Vestibulum eu fringilla lacus. Etiam ac augue ac risus bibendum scelerisque. In vel consequat dolor, eget feugiat magna. Nunc eget lorem dignissim, accumsan est quis, fringilla leo. Integer tristique orci et quam eleifend egestas. Pellentesque metus metus, luctus nec tellus vel, efficitur consequat urna. Donec pellentesque mi non nisi accumsan, sed consequat sapien aliquam. Praesent eros mauris, suscipit sit amet tristique sit amet, fringilla ut mi. Pellentesque eu volutpat diam, et iaculis sapien. Nulla ultricies erat augue, eget pharetra tortor porttitor sit amet. Nulla molestie nisi ipsum, id feugiat libero semper a. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sed mauris arcu. Morbi consectetur odio sit amet arcu sodales auctor. Fusce sem purus, cursus quis nulla sit amet, commodo condimentum elit. Vivamus ornare quam lorem, sed scelerisque tellus elementum in. 

\end{document}

Result

The excessive vertical space is indicated with a blue arrow. enter image description here

2

This "problem" is due to a particular setting in mcode.sty, defining

aboveskip={1.5\baselineskip}

for lstlisting environements. So it seems like a deliberate choice.

If you have/want to use this package (I'd rather recommend loading the listings package yourself and setting those parameters you really need), add the line

\lstset{aboveskip=\medskipamount}

somewhere before the lstlisting environment or supply this value as an option to a particular enviroment as

\begin{lstlisting}[aboveskip=\medskipamount]

Starting your code with the lines

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{mcode}
\lstset{aboveskip=\medskipamount}
\begin{document}

results in the following output.

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.