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!

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

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.

\documentclass{article}
\usepackage[most, minted]{tcolorbox}

\DeclareTCBListing{pythonexample}{m}{%
    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,
    fontlower=\ttfamily,
    comment={#1},
}

\begin{document}

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

\end{document}

enter image description here

You must log in to answer this question.

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