3

I have some style references to use style in python highlighted code: enter image description here

or

enter image description here


Can you achieve this type of style with minted or listings? By the way, the language is Python.

please give an example of how it is used

royer
  • 361
  • 1
  • 14
  • 2
    This should be possible using both packages and some options like escapeinside. (The first one seems to be more minted-like, the second one more lstlisting-like.) What have you tried so far? – epR8GaYuh Jun 27 '19 at 12:15
  • Just for clarity, you are asking about how to add these circled numbers to your listings? – siracusa Jun 27 '19 at 14:47
  • @siracusa Yes, My question is as achievement this design with the package minted? but I would like you to make an example – royer Jun 27 '19 at 16:46
  • 1
    You received an answer, but we generally like to see some sort of effort on the part of those asking questions (much more than just posting two pictures). It would have been preferable to also include the TeX code you used to create the output you showed us. – Teepeemm Jun 27 '19 at 23:44

1 Answers1

3

A solution sketch using the listings package

As suggested in the comments, with the listings package you can use the escapeinside option to escape from the verbatim typesetting context to call a macro that outputs a circled number.

The \circled macro is inspired by this answer but with some additions to make the circles have a fixed size. Also the background and text color can be passed as a parameter.

For the circled notes after certain lines in the listing, escapeinside = {<@}{@>} was set to escape the listing. Within those delimiters we then can use the \lstnote{...} macro to add a fixed amount of spacing plus the number in a circle.

For the circled line numbers another macro \circledstyle was defined. We make listings typeset line numbers using this style by adding the numberstyle = \circledstyle option.

The full example document:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{tikz}

\newcommand\circled[3]{%
    \tikz[baseline=(char.base)]{
        \node[shape=circle, fill=#1, inner sep=0pt, text width=8pt, align=center]
            (char) {\textcolor{#2}{\sffamily\bfseries\scriptsize #3}};
    }%
}

\newcommand\lstnote[1]{%
    \kern 1.5em%
    \circled{purple}{pink!50!white}{#1}%
}

\newcommand\circledstyle[1]{%
    \circled{gray}{white}{#1}%
}

\lstset{
    basicstyle = \ttfamily,
    language = [LaTeX]TeX,
    texcsstyle = *\color{blue},
    escapeinside = {<@}{@>},
    backgroundcolor = \color{lightgray!30!white},
    framexleftmargin = 3em,
    framerule = 0pt,
    frame = tb,
    numbers = left,
    numberstyle = \circledstyle
}

\begin{document}
\begin{lstlisting}
\documentclass{article} <@ \lstnote{1} @>
\usepackage{xcolor}  <@ \lstnote{2} @>

\begin{document}
\textcolor{blue}{Hello world!} <@ \lstnote{3} @>

New paragraph.

And another one.
\end{document}
\end{lstlisting}
\end{document}

enter image description here

siracusa
  • 13,411