3

I came across a scientific article that features beautifully formatted code, as shown in the image below. I'm interested in using a similar style for my own article, but I'm not sure how to achieve this.

Formatted code

The code appears well-organized and visually distinct. A notable feature is that it shows the programming language and indicates whether the code compiles correctly. I've already looked into the minted package in LaTeX, but I'm not entirely sure if it's the best option or if there are better alternatives.

1
  • 3
    One could probably use a tcblisting from the tcolorbox package and add the language as title, overlay or similar. Commented Jun 19 at 7:49

1 Answer 1

3

I figured out the solution using the tcolorbox package.

\documentclass{article}
\usepackage{minted}
\usepackage{tcolorbox}
\tcbuselibrary{minted}
\tcbuselibrary{skins}

\usepackage{pifont}
\newcommand{\cmark}{\ding{51}}
\newcommand{\xmark}{\ding{55}}

\renewcommand{\theFancyVerbLine}{\sffamily
    \textcolor{lightgray}{\scriptsize
        \oldstylenums{\arabic{FancyVerbLine}}}}

\newtcblisting{custom-minted}[3][]{%
    listing engine=minted,
    minted options={
        linenos,
        breaklines,
        fontsize=\footnotesize,
    },
    minted language=#2,
    listing only,
    colback=white,
    colframe=lightgray,
    enhanced,
    boxrule=0.5pt,
    sharp corners,
    left=2em,
    overlay={\node[anchor=south east, font=\small, outer sep=0pt,xshift=-3em,yshift=-0.5em,fill=white,inner xsep=0.75em] at (frame.south east) {#3};},
    #1
}

\begin{document}

\begin{custom-minted}{rust}{Rust \cmark}
let number = Complex(2, 1);
let a = &number;
let b = &number;
\end{custom-minted}

\begin{custom-minted}{rust}{Rust \xmark}
let mut number = Complex(1, 2);
let a = &mut number;
let b = &mut number;  // Error: cannot borrow `number` as mutable
                      // more than once at a time
\end{custom-minted}

\end{document}

Outputs the following

The result of the code

You must log in to answer this question.

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