6

I have the code below to generate minted code inside box (credit:Ignasi). That format (code inside box) is heavely needed in my previous report paper.

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

\usepackage{listings}
\usepackage[newfloat,chapter]{minted}
\usepackage[labelfont=bf]{caption}

\newenvironment{code}{\captionsetup{type=listing}}{}
\SetupFloatingEnvironment{listing}{name=Tabel Kode}

\usepackage{tcolorbox}
\tcbuselibrary{listings,minted,skins,breakable}
\newtcblisting{ignasicblock}[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,
    fontsize=\footnotesize,
    numbersep=6mm,
    texcl=true,
    breaklines=true,
    autogobble=true},
  coltitle=black,
  #1
}

\definecolor{codebg}{rgb}{0.95,0.95,0.95}
\renewcommand\theFancyVerbLine{\footnotesize\arabic{FancyVerbLine}}

\begin{document}

\begin{code}
  \captionof{listing}{\emph{Pseudocode function} fizzbuzz}
  \label{pc:construct-ed}
\begin{ignasicblock}[title=fizzbuzz,minted language=text]
program fizzbuzz
  Do i = 1 to 100
    set print_number to true
    If i is divisible by 3
      print "Fizz"
      set print_number to false
    If i is divisible by 5
      print "Buzz"
      set print_number to false
    If print_number, print i
    print a newline
    end do
\end{ignasicblock}
\end{code}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% TeX-command-extra-options: "-shell-escape"
%%% End:

The code above will generate:

Now I need to give a mark like figure1 or figure2. The mark will be a curly braces that stretch from line X until line Y.

So we can define:

  • starting line:x
  • end line:y
  • number:z
  • (if possible) we can dynamically change the mark, for just one line, we can use line instead of curly braces e.g in figure2

Figure1:

Figure2:

I heavily need this format for my current report. Thanks a ton in advance for the help. Thank you.

1 Answer 1

4

You could add some underlay code which allows to draw something on the tcolorbox.

I added some macros \drawbrace and \drawline which are adapted to the dimensions of your box.

  • \drawbrace[options]{xshift}{first line}{last line}{Text}
  • \drawline[options]{xshift west}{xshift east}{line}{Text}

For your example, we need:

\drawbrace{7.5cm}{0}{4}{1}
\drawbrace{8cm}{4}{11}{2}
\drawline{4cm}{8.5cm}{12}{3}

enter image description here

The full code is

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

\usepackage{listings}
\usepackage[newfloat,chapter]{minted}
\usepackage[labelfont=bf]{caption}

\newenvironment{code}{\captionsetup{type=listing}}{}
\SetupFloatingEnvironment{listing}{name=Tabel Kode}

\usepackage{tcolorbox}
\tcbuselibrary{listings,minted,skins,breakable}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.pathreplacing}

\newtcblisting{ignasicblock}[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,
    fontsize=\footnotesize,
    numbersep=6mm,
    texcl=true,
    breaklines=true,
    autogobble=true},
  coltitle=black,
  #1
}

\newcommand{\drawbrace}[5][]{%
  \draw [decorate,decoration={brace,amplitude=5pt},blue!75!black,line width=1pt]
    ([xshift=#2,yshift=-3.3mm-#3*12pt]interior.north west)
    -- ([xshift=#2,yshift=-2.7mm-#4*12pt]interior.north west)
    node [align=center,right=10pt,midway,circle,draw=blue!50,fill=blue!5,text=black,
      font=\sffamily\small,#1] {#5};
}

\newcommand{\drawline}[5][]{%
  \draw [blue!75!black,line width=1pt]
    ([xshift=#2,yshift=-3mm+6pt-#4*12pt]interior.north west)
    -- ([xshift=#3,yshift=-3mm+6pt-#4*12pt]interior.north west)
    node [align=center,right=5pt,circle,draw=blue!50,fill=blue!5,text=black,
      font=\sffamily\small,#1] {#5};
}

\definecolor{codebg}{rgb}{0.95,0.95,0.95}
\renewcommand\theFancyVerbLine{\footnotesize\arabic{FancyVerbLine}}

\begin{document}

\begin{code}
  \captionof{listing}{\emph{Pseudocode function} fizzbuzz}
  \label{pc:construct-ed}
\begin{ignasicblock}[title=fizzbuzz,minted language=text,
  underlay={
    \drawbrace{7.5cm}{0}{4}{1}
    \drawbrace{8cm}{4}{11}{2}
    \drawline{4cm}{8.5cm}{12}{3}
  }]
program fizzbuzz
  Do i = 1 to 100
    set print_number to true
    If i is divisible by 3
      print "Fizz"
      set print_number to false
    If i is divisible by 5
      print "Buzz"
      set print_number to false
    If print_number, print i
    print a newline
    end do
\end{ignasicblock}
\end{code}

\end{document}

If you inspect \drawbrace

\newcommand{\drawbrace}[5][]{%
  \draw [decorate,decoration={brace,amplitude=5pt},blue!75!black,line width=1pt]
    ([xshift=#2,yshift=-3.3mm-#3*12pt]interior.north west)
    -- ([xshift=#2,yshift=-2.7mm-#4*12pt]interior.north west)
    node [align=center,right=10pt,midway,circle,draw=blue!50,fill=blue!5,text=black,
      font=\sffamily\small,#1] {#5};
}

you see that all positions are computed relative to interior.north west.

This method may/will fail for breakable boxes!

1
  • The great part is I can adjust the horizontal position (and the length of line) of the line/braces. I can't imagine this. Great thanks for the help. By tomorrow I can submit my report. Once again, thank you. May your kindness be replied.
    – azzamsa
    Commented May 9, 2019 at 11:54

You must log in to answer this question.

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