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

4
  • 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
    Commented Jun 27, 2019 at 12:15
  • Just for clarity, you are asking about how to add these circled numbers to your listings?
    – siracusa
    Commented Jun 27, 2019 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
    Commented Jun 27, 2019 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
    Commented Jun 27, 2019 at 23:44

1 Answer 1

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

3
  • 1
    Perfect your code and explanations! :-)
    – Sebastiano
    Commented Jun 27, 2019 at 21:47
  • Oh great, can you configure it from an external file of the "code.tex" code?
    – royer
    Commented Jun 28, 2019 at 1:57
  • @royer Sure, you can put the definitons into another file and \input it or even create a new package for them
    – siracusa
    Commented Jun 28, 2019 at 12:35

You must log in to answer this question.

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