7

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.

\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

enter image description here

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!

  • 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.