如何在 minted 和 tcolorbox 上绘制花括号?

如何在 minted 和 tcolorbox 上绘制花括号?

我有下面的代码来生成框内的铸造代码(来源:伊格纳西)。我之前的报告论文非常需要这种格式(框内的代码)。

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

上面的代码将生成:

现在我需要给出一个像图 1 或图 2 这样的标记。该标记将是一个从 X 行延伸到 Y 行的花括号。

因此我们可以定义:

  • 起跑线:x
  • 结束行:y
  • 号码:z
  • (如果可能的话)我们可以动态地改变标记,只需一行,我们可以使用行代替花括号,例如在图 2 中

图1:

图2:

我目前的报告非常需要这种格式。非常感谢您的帮助。谢谢。

答案1

您可以添加一些underlay允许在其上绘制某些内容的代码tcolorbox

我添加了一些宏\drawbrace\drawline它们适合您的盒子的尺寸。

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

为了举个例子,我们需要:

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

在此处输入图片描述

完整代码如下

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

如果你检查\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};
}

您会看到所有位置都是相对于 计算的interior.north west

对于易碎的盒子,此方法可能会/将会失败!

相关内容