带有列表的 IPython Notebook 输入和输出单元

带有列表的 IPython Notebook 输入和输出单元

我想包含看起来像 Jupyter IPython 单元格的输入和输出单元格。“带有列表的 IPython Notebook 单元格”位于带有列表的 IPython Notebook 单元格演示如何执行单个输入单元格,但我想同时显示多行输入输出行,例如,像这样,其中 Out[ ] 不会推进计数器,但 In[ ] 会: ,

答案1

我也是从您提供的链接开始的,并想出了这个解决方案。基本上,您定义了三个列表环境(一个用于输入,一个用于打印文本,一个用于输出)。我承认这个解决方案不是超级优雅(重复定义选项),但它运行良好,为您提供自动编号,甚至支持标签/引用。

\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{listings}

\definecolor{white}{rgb}{1,1,1}
\definecolor{mygreen}{rgb}{0,0.4,0}
\definecolor{light_gray}{rgb}{0.97,0.97,0.97}
\definecolor{mykey}{rgb}{0.117,0.403,0.713}

\tcbuselibrary{listings}
\newlength\inwd
\setlength\inwd{1.3cm}

\newcounter{ipythcntr}
\renewcommand{\theipythcntr}{\texttt{[\arabic{ipythcntr}]}}

\newtcblisting{pyin}[1][]{%
  sharp corners,
  enlarge left by=\inwd,
  width=\linewidth-\inwd,
  enhanced,
  boxrule=0pt,
  colback=light_gray,
  listing only,
  top=0pt,
  bottom=0pt,
  overlay={
    \node[
      anchor=north east,
      text width=\inwd,
      font=\footnotesize\ttfamily\color{mykey},
      inner ysep=2mm,
      inner xsep=0pt,
      outer sep=0pt
      ] 
      at (frame.north west)
      {\refstepcounter{ipythcntr}\label{#1}In \theipythcntr:};
  }
  listing engine=listing,
  listing options={
    aboveskip=1pt,
    belowskip=1pt,
    basicstyle=\footnotesize\ttfamily,
    language=Python,
    keywordstyle=\color{mykey},
    showstringspaces=false,
    stringstyle=\color{mygreen}
  },
}
\newtcblisting{pyprint}{
  sharp corners,
  enlarge left by=\inwd,
  width=\linewidth-\inwd,
  enhanced,
  boxrule=0pt,
  colback=white,
  listing only,
  top=0pt,
  bottom=0pt,
  overlay={
    \node[
      anchor=north east,
      text width=\inwd,
      font=\footnotesize\ttfamily\color{mykey},
      inner ysep=2mm,
      inner xsep=0pt,
      outer sep=0pt
      ] 
      at (frame.north west)
      {};
  }
  listing engine=listing,
  listing options={
      aboveskip=1pt,
      belowskip=1pt,
      basicstyle=\footnotesize\ttfamily,
      language=Python,
      keywordstyle=\color{mykey},
      showstringspaces=false,
      stringstyle=\color{mygreen}
    },
}
\newtcblisting{pyout}[1][\theipythcntr]{
  sharp corners,
  enlarge left by=\inwd,
  width=\linewidth-\inwd,
  enhanced,
  boxrule=0pt,
  colback=white,
  listing only,
  top=0pt,
  bottom=0pt,
  overlay={
    \node[
      anchor=north east,
      text width=\inwd,
      font=\footnotesize\ttfamily\color{mykey},
      inner ysep=2mm,
      inner xsep=0pt,
      outer sep=0pt
      ] 
      at (frame.north west)
      {\setcounter{ipythcntr}{\value{ipythcntr}}Out#1:};
  }
  listing engine=listing,
  listing options={
      aboveskip=1pt,
      belowskip=1pt,
      basicstyle=\footnotesize\ttfamily,
      language=Python,
      keywordstyle=\color{mykey},
      showstringspaces=false,
      stringstyle=\color{mygreen}
    },
}


\begin{document}

\begin{pyin}
print("Hello world")
\end{pyin}

\begin{pyprint}
Hello world
\end{pyprint}

And here we also have a return value in the last line of the input cell.
\begin{pyin}[labelOfTheSecondInput]
def twicify(arg):
    print("Received:", arg, "- Will double now...")
    return 2 * arg
twicify(1)
\end{pyin}

\begin{pyprint}
Received: 1 - Will double now...
\end{pyprint}

\begin{pyout}
2
\end{pyout}

You can also reference the labeled input \ref{labelOfTheSecondInput}.
\begin{pyin}
"and the counter will automatically do the right thing :)"
\end{pyin}
\begin{pyout}
'and the counter will automatically do the right thing :)'
\end{pyout}

\end{document}

输出

答案2

我使用了包moredelims的选项lstlisting来获得类似的输出,但没有框。如果您需要记录 IPython shell,这可能会很有用,因为您不需要那么多\begin{}and\end{}语句,而只需从 IPython shell 复制,并用冒号替换方括号内的数字,即在粘贴到文档后In[9]更改为。In[:]

这是 MWE


\documentclass[a4paper, 11pt, onecolumn, openany, oneside]{article}
\usepackage[svgnames]{xcolor}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\definecolor{ared}{rgb}{.647,.129,.149}

\definecolor{Dark}{gray}{.2}
\definecolor{MedDark}{gray}{.4}
\definecolor{Medium}{gray}{.6}
\definecolor{Light}{gray}{.8}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codered}{rgb}{0.6,0.1,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\definecolor{lightgray}{gray}{0.95}
\definecolor{codeblue}{rgb}{0.117,0.403,0.713}

% IPython
\newcounter{ipythcntr}
\renewcommand{\theipythcntr}{\texttt{[\arabic{ipythcntr}]}}
\newcommand{\ipin}[1][]{
    \stepcounter{ipythcntr}
    \hspace{-10pt}
    \color{codeblue}In  \theipythcntr}
\newcommand{\ipout}[1][\theipythcntr]{
    \hspace{-10pt}
    \color{codered}Out \theipythcntr}
\lstset{
    language=Python, 
    moredelim=[is][\ipin]{In[}{]},
    moredelim=[is][\ipout]{Out[}{]},
    backgroundcolor=\color{lightgray},
    commentstyle=\color{codegreen},
    xleftmargin = 8pt,
    basicstyle=\footnotesize\ttfamily,
    keywordstyle=\color{codeblue},
    showstringspaces=false,
}
\begin{document}

\begin{lstlisting}[caption=MWE]
# Add two numbers
In[:] 9 + 3
Out[:] 12
# Multiline output
In[:] for i in range(4):
      print(i**2-1)
    -1
    0
    3
    8
\end{lstlisting}
\end{document}

平均能量损失

相关内容