2

I have to put the source code with this kind of format inside my report. I've read other questions. Most of them are just putting listing or minted inside tabular environment. But not the source code with it's line side by side.

Maybe the possible method is changing the way minted represent it's number and the box around it. If this is not possible. Maybe put listing inside table. But I have no idea to achieve it. The desired result looks like this:

img

I tried some MWE but it's far from perfect. I have to add lines manually and the result is so bad.

\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:

Really need your help folks, to get this reports done.

Thanks a ton in advance.

3

You could try with 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}

enter image description here

  • You saved my day. May your kindness be rewarded. I've other issue regarding this report format. I'll open other question if I'm stuck. But this is one major issue of them. and this is solved. Thanks a ton. – azzamsa Nov 13 '18 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.