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
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.
\documentclass[11pt]{amsart}
\usepackage[framemethod=tikz]{mdframed} %%to make a framed environment
\usepackage{listings}
\usepackage{minted}
\usemintedstyle{xcode}
\definecolor{light-gray}{rgb}{0.789035,0.789035,0.789035}
\setminted{bgcolor=light-gray}
\lstset{frame=b,
language=Python,
aboveskip=-2mm,
belowskip=3mm,
columns=flexible,
basicstyle={\small\ttfamily}
}
\newminted{py}{linenos=true,
autogobble,
python3=true,
obeytabs=true,
tabsize=3,
frame=none,
framesep=10pt}
\begin{document}
\begin{pycode}
print("Hello world!")
\end{pycode}
\begin{lstlisting}
Hello world!
\end{lstlisting}
\end{document}
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
\newenvironment{doubleproof}{
\newcommand{\nextpart}{\end{proof} \begin{proof}}
\begin{proof}}{\end{proof}}
so that
\begin{doubleproof}
First proof
\nextpart
Second proof
\end{doubleproof}
produces
However, when I try defining
\newenvironment{cell}{
\newcommand{\nextpart}{\end{pycode}\begin{lstlisting}}
\begin{pycode}}{\end{lstlistings}}
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!
minted
-only solution, i.e. that you could produce the 'output' also withminted
. You may want to look intotcolorbox
and its verbatim boxes, they offer a nice interface for 'divided' boxes for presentation of input/output or code/result.\newenvironment{cell}
does not work because the environments involved areverbatim
-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.\nextpart
in there.