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!

  • 3
    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, 2018 at 7:46
  • 4
    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, 2018 at 7:48
  • 3
    Your best bet is to use a tcolorbox based solution. Do you really want to have verbatim material in the output?
    – TeXnician
    Dec 14, 2018 at 14:28
  • Maybe one could add active/command chars in the verbatim environments to put \nextpart in there.
    – nox
    Dec 15, 2018 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, 2018 at 9:10

1 Answer 1


Something similar to the desired results can be obtained with a listing and comment kind of listing from tcolorbox.

For non LaTeX language listing, the output is not automatic and the comment option can be used to enter the supposed result.

\usepackage[most, minted]{tcolorbox}

    listing and comment,
    listing engine=minted,
    minted style=xcode,
    minted language=Python,
    minted options={linenos, numbersep=3mm},
    sharp corners,
    toprule=0pt, leftrule=0pt, rightrule=0pt,
    colback=black!30, colframe=red!75!black,
    bicolor, colbacklower=white, colframe=black,


\begin{pythonexample}{Hello World!}
print("Hello World!")


enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .