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*?



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.

x = y+2;

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.


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. 



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

1 Answer 1


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


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


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


Starting your code with the lines


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.