I am writing some programming instructions for students, and I am trying to format the code so that it looks like the cell input/output structure of a Jupyter or Google Colab notebook. So I want it to look something like

enter image description here

So far I've been using the minted package to get the code because I like its automatic syntax highlighting and the listings package because it looks like the output of a notebook.


\usepackage[framemethod=tikz]{mdframed} %%to make a framed environment







print("Hello world!")
Hello world!


This looks good for the most part, except when LaTeX does weird things with spreading the vertical spacing out on a page, it separates the minted environment from the listings environment vertically. I would like to combine these into one environment to force them to stay together.

I know that I can combine environments together like

    \newcommand{\nextpart}{\end{proof} \begin{proof}}

so that

First proof
Second proof


enter image description here

However, when I try defining


and using it in a similar way, I get an error. Is there a way to get this to work, or another solution to formatting my output like this? Thanks in advance!

  • 2
    I'm quite confident that you could switch to a minted-only solution, i.e. that you could produce the 'output' also with minted. You may want to look into tcolorbox and its verbatim boxes, they offer a nice interface for 'divided' boxes for presentation of input/output or code/result. – moewe Dec 14 '18 at 7:46
  • 3
    I assume that the suggested \newenvironment{cell} does not work because the environments involved are verbatim-like. verbatim commands and environments have some restrictions in how they can be used in other commands and also in how you can hide their beginning and end code. – moewe Dec 14 '18 at 7:48
  • 2
    Your best bet is to use a tcolorbox based solution. Do you really want to have verbatim material in the output? – TeXnician Dec 14 '18 at 14:28
  • Maybe one could add active/command chars in the verbatim environments to put \nextpart in there. – nox Dec 15 '18 at 1:28
  • These newenvironments don't make much sense to me in this order. Especially since there seem to be too many closing parentheses in both of them. – thymaro Dec 15 '18 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.