如何在表格中将源代码与其编号放在一起

如何在表格中将源代码与其编号放在一起

我必须将这种格式的源代码放在我的报告中。我读过其他问题。他们中的大多数只是将源代码listing放在minted表格环境中。但没有将源代码与其行并排。

也许可能的方法是改变 minted 表示其数字的方式以及围绕它的方框。如果这不可能的话。也许可以把列表放在表格里。但我不知道如何实现它。期望的结果如下所示:

图片

我尝试了一些 MWE,但它远非完美。我必须手动添加线条,结果很糟糕。

\documentclass{report}
\usepackage[top=3cm,bottom=3cm,left=4cm,right=3cm]{geometry}

\usepackage{longtable}
\usepackage{listings}

\lstset{
  language=Python,
  basicstyle=\small,
  breaklines=true
}

\begin{document}

\begin{longtable}[c]{|p{1cm}|p{7cm}|}
  \caption{Unit testing example} \label{tab:unit-testing-example}\\
  \hline
  \textbf{No} & \textbf{Source Code} \\
  \hline
  \endhead % <-- on every page
  1 \newline 2 \newline 3 &
\begin{lstlisting}
def hallo(nama):
    if nama == "Budi":
    return "Hai " + nama
    else:
    return "Nama Kosong"

def test_case_1():
    assert hallo("Budi") == "Hai Budi"

def test_case_2():
    assert hallo("Ani") == "Nama Kosong"

\end{lstlisting}
  \\\hline
\end{longtable}

\end{document}


%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:

真的需要你们的帮助来完成这份报告。

提前致谢。

答案1

您可以尝试tcolorbox

\documentclass[12pt,a4paper,oneside]{report}

\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[chapter]{minted}
\usepackage{tcolorbox}              

\tcbuselibrary{listings,minted,skins,breakable}

%\lstset{
%   language=Python,
%   basicstyle=\small,
%   breaklines=true
%}

\newtcblisting{code}[1][]{%
    breakable,
    colback=codebg,
    colframe=black!40,
    enhanced,
    listing engine=minted,
    listing only,
    left=10mm,
    title=Source Code,
    halign title=center,
    overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
      rectangle ([xshift=8mm]frame.north west);\end{tcbclipinterior},
        \node[right] at (title.west) {No};},
    minted style=colorful,
    minted language=Python,
    minted options={%
        linenos=true,
        numbersep=6mm,
        texcl=true,
        breaklines=true,
        autogobble=true},
    coltitle=black,
    #1
  }

\newtcblisting{anothercode}[1][]{%
    breakable,
    colback=white,
    colframe=black,
    colbacktitle=white,
    sharp corners,
    enhanced,
    listing engine=minted,
    listing only,
    left=10mm,
    title=Source Code,
    halign title=center,
    overlay={\draw[line width=.5mm] ([xshift=8mm]frame.south west)
      -- ([xshift=8mm]frame.north west);
        \node[right] at (title.west) {No};},
    minted style=colorful,
    minted language=Python,
    minted options={%
        linenos=true,
        numbersep=6mm,
        texcl=true,
        breaklines=true,
        autogobble=true},
    coltitle=black,
    #1
  }

\definecolor{codebg}{rgb}{0.95,0.95,0.95}


\begin{document}

\begin{code}
def hallo(nama):
    if nama == "Budi":
    return "Hai " + nama
    else:
    return "Nama Kosong"

def test_case_1():
    assert hallo("Budi") == "Hai Budi"

def test_case_2():
    assert hallo("Ani") == "Nama Kosong"
\end{code}

\begin{anothercode}
def hallo(nama):
    if nama == "Budi":
    return "Hai " + nama
    else:
    return "Nama Kosong"

def test_case_1():
    assert hallo("Budi") == "Hai Budi"

def test_case_2():
    assert hallo("Ani") == "Nama Kosong"
\end{anothercode}

\end{document}

在此处输入图片描述

相关内容