我如何修改我的 tcblisting 以并排显示输入代码和预期输出?

我如何修改我的 tcblisting 以并排显示输入代码和预期输出?

我有一个代码清单,tcblisting定义如下。

\lstdefinelanguage{MyScheme}{
  basewidth=0.5em,
  morekeywords=[1]{define, cond, lambda, else},
  alsodigit=!\$\%&*+-./:<=>?@^_~,
  sensitive=true,
  escapeinside={(*;}{;*)},
  basicstyle=\scriptsize\ttfamily,
  keywordstyle=\bf\ttfamily\color[rgb]{0,.3,.7},
  commentstyle=\color[rgb]{0.133,0.545,0.133},
  stringstyle={\color[rgb]{0.75,0.49,0.07}},
  upquote=true,
  breaklines=true,
  breakatwhitespace=true,
  literate=*{`}{{`}}{1},
  showstringspaces=false,
}
\newtcblisting[auto counter, number within=chapter]{MySchemeListing}[2][]{%
    enhanced,
    frame hidden, 
    breakable,
    borderline north = {0.5pt}{0pt}{black},
    borderline south = {0.5pt}{0pt}{black},
    opacityfill=0,
    listing only,
    top=-5pt,bottom=-5pt,left=0pt,right=0pt,
    listing options={
        basewidth=0.5em,
        commentstyle=\color{orange},
        keywordstyle=\color{blue},
        stringstyle=\color{red},
        morekeywords={NULL},
        language=MyScheme,
        basicstyle=\small\ttfamily,
        breaklines=true,
        showstringspaces=false,
        escapeinside={(*;}{;*)},
        literate={~}{{\fontfamily{ptm}\selectfont \textasciitilde}}1{'}{\textquotesingle}1{`}{\textasciigrave}1
    },
    title={\textcolor{black}{Listing \thetcbcounter\ifstrempty{#2}{}{---#2}\ifstrempty{#1}{}{~(\texttt{#1})}}},
    attach boxed title to top center={xshift=-3.55mm,yshift=-0.4mm},
    boxed title style={frame hidden,colback=black!0!white},
    minted language=scheme,
    minted options={autogobble},
}

我希望能够显示输入代码输出结果并排显示。现在,我使用一个单独的 tcb 环境,将输出放在代码下方(未显示,因为它确实不相关)。理想情况下,我只需添加一些内容即可,MySchemeListing这样我就不需要浏览和更新所有列表。我尝试添加逐字小页面环境,但问题是我用于方案列表的标题不会覆盖输出。此外,我希望输出像代码一样从列表顶部开始(而不是垂直向下移动)。

\documentclass{article}
\usepackage{listings}
\usepackage{etoolbox}
\usepackage[minted,skins]{tcolorbox}
\tcbuselibrary{listings, skins, breakable}

%%% Two listings from above...

\begin{document}

\begin{minipage}{0.6\textwidth}
\begin{MySchemeListing}[main.rkt]{Factorial}
(define fact
  (lambda (n)
    (cond
      [(zero? n) 1]
      [else (* n (fact (sub1 n)))])))
\end{MySchemeListing}
\end{minipage}
\begin{minipage}{0.25\textwidth}
\begin{verbatim}
>>> 5
\end{verbatim}
\end{minipage}

\end{document}

这是当前输出: 在此处输入图片描述

与我所寻找的(刚刚在照片编辑器中编辑)相比,也许有一条虚线将输入与输出分开: 在此处输入图片描述

请注意,当前存在的 5 仅仅是可能存在的其他内容的占位符。

答案1

我决定使用tcolorbox并通过来止损lstlisting


\documentclass{article}
\usepackage{listings}
\usepackage{etoolbox}
\usepackage[minted,skins]{tcolorbox}
\tcbuselibrary{listings, skins, breakable}

\lstdefinelanguage{MyScheme}{
  basewidth=0.5em,
  morekeywords=[1]{define, cond, lambda, else},
  alsodigit=!\$\%&*+-./:<=>?@^_~,
  sensitive=true,
  escapeinside={(*;}{;*)},
  basicstyle=\small\ttfamily,
  keywordstyle=\bf\ttfamily\color[rgb]{0,.3,.7},
  commentstyle=\color[rgb]{0.133,0.545,0.133},
  stringstyle={\color[rgb]{0.75,0.49,0.07}},
  upquote=true,
  breaklines=true,
  breakatwhitespace=true,
  literate=*{`}{{`}}{1},
  showstringspaces=false,
}

\newtcolorbox[list inside=mybox,auto counter,number within=section]{MyBox}[2][]{
colback=gray!5!white,
colframe=gray!75!black,
sidebyside,
sidebyside align=top,
boxsep=1pt,
left=5pt,
right=5pt,
top=2pt,
bottom=0pt,
lefthand width={0.60\textwidth},
title={Listing \thetcbcounter\;[\texttt{#1}]: #2}}

\begin{document}

\begin{MyBox}[fact.scm]{Factorial Program}
\begin{lstlisting}[language=MyScheme]
;; ! : Number -> Number
;; Computes the factorial of a number.
(define !
 (lambda (n)
  (cond
   [(zero? n) 1]
   [else (* n (! (sub1 n)))])))
\end{lstlisting}
\tcblower
\begin{lstlisting}[language=MyScheme]
(*;\textbf{>{>}>};*) (! 5)
120
\end{lstlisting}
\end{MyBox}

\end{document}

相关内容