TransWikia.com

Automatically adjust tcolobox to size of minted line numbers

TeX - LaTeX Asked by Irgendwhy on May 31, 2021

I want to create several minted listings inside tcolorboxes.
Similarly to https://tex.stackexchange.com/a/396731, I would like to adjust the size of the tcolorbox to contain the line numbers.
However, the linked solution uses a fixed offset of 4mm, which is too short for line numbers with 3 or more digits; this can easily happen when I want to print parts of some existing code using tcbinputlisting.
Setting the offset to be big enough for 4 digit line numbers when all line numbers in a listing only use one or two digits also doesn’t look too pretty to me.
Ideally, I would like this offset to be automatically adjusted based on the length of the longest line number (which should always be the last line number).

Is this possible to do or do I have to use different macros depending on the number of digits in line numbers?

One Answer

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{}{%
    globalprotect@namedef{verbatim@numwidth@thec@verbatim@cnt}{%
      ifnumc@FancyVerbLine>999
        7.5mm%
      elseifnumc@FancyVerbLine>99
        6mm%
      else
        4mm%
      fifi
    }%
  }%
}

defminted@auto@numwidth#1{%
  ifcsname verbatim@numwidth@thenumexprc@verbatim@cnt#1relaxendcsname
    csname verbatim@numwidth@thenumexprc@verbatim@cnt#1relaxendcsname
  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=dimexprminted@auto@numwidth{}relax]frame.north west);
        end{tcbclipinterior}
    },
    % in "left", c@verbatim@cnt is not stepped yet, hence the argument "+1"
    left=dimexprminted@auto@numwidth{+1}relax,
    #1   
}
makeatother

%usepackage{unravel}
providecommandunravel[1]{#1}
providecommandunravelsetup[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}

enter image description here

Correct answer by muzimuzhi Z on May 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP