Try this not-elegant-enough attempt.
Some key ideas:
- Assign a unique number to each
Verbatim
environment by using new counter verbatim@cnt
.
- Get the ending code line number per
Verbatim
env (stored in counter FancyVerbLine
), convert it to a dimension, and make it linked with the unique env number.
- Write this relation to aux file, which will be read at the second compilation.
- In each customized
tcolorbox
env, use current Verbatim
env number to retrieve the corresponding dimension, then use it to set both left padding /tcb/left
and the offset of the background color of line numbers drawn in `/tcb/overlay.
\documentclass[11pt,letterpaper]{article}
\usepackage[most, minted]{tcolorbox}
\newcounter{verbatim@cnt}
\makeatletter
\AtEndEnvironment{Verbatim}{%
\stepcounter{verbatim@cnt}%
\protected@write\@auxout{}{%
\global\protect\@namedef{verbatim@numwidth@\the\c@verbatim@cnt}{%
\ifnum\c@FancyVerbLine>999
7.5mm%
\else\ifnum\c@FancyVerbLine>99
6mm%
\else
4mm%
\fi\fi
}%
}%
}
\def\minted@auto@numwidth#1{%
\ifcsname verbatim@numwidth@\the\numexpr\c@verbatim@cnt#1\relax\endcsname
\csname verbatim@numwidth@\the\numexpr\c@verbatim@cnt#1\relax\endcsname
\else
4mm%
\fi
}
\makeatother
\makeatletter
\newtcblisting{myminted}[2][]{%
listing engine=minted,
minted language=#2,
listing only,
breakable,
enhanced,
minted options = {
linenos,
breaklines=true,
breakbefore=.,
fontsize=\footnotesize,
numbersep=2mm
},
overlay={%
\begin{tcbclipinterior}
\fill[gray!25] (frame.south west) rectangle ([xshift=\dimexpr\minted@auto@numwidth{}\relax]frame.north west);
\end{tcbclipinterior}
},
% in "left", \c@verbatim@cnt is not stepped yet, hence the argument "+1"
left=\dimexpr\minted@auto@numwidth{+1}\relax,
#1
}
\makeatother
%\usepackage{unravel}
\providecommand\unravel[1]{#1}
\providecommand\unravelsetup[1]{}
\unravelsetup{max-action=1000, max-input=1000, max-output=100}
\begin{document}
\begin{myminted}[minted options app={firstnumber=8}]{c}
#include <stdio.h>
void main ()
{
printf("hello world");
}
\end{myminted}
\begin{myminted}[minted options app={firstnumber=98}]{c}
#include <stdio.h>
void main ()
{
printf("hello world");
}
\end{myminted}
\begin{myminted}[minted options app={firstnumber=998}]{c}
#include <stdio.h>
void main ()
{
printf("hello world");
}
\end{myminted}
\end{document}