Consider the following code taken from here to produce a fancy listing :
% PACKAGES
\documentclass[11pt,a4paper]{book}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{lmodern}
\usepackage{multicol}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{tcolorbox}
\tcbuselibrary{skins,listings}
\definecolor{nicedarkgreen}{RGB}{0,127,0}
\definecolor{nicedarkblue}{RGB}{0, 0, 102}
% LISTINGS
\newtcblisting{cpplisting}[1][]{%
enhanced,
arc = 0pt,
outer arc = 0pt,
colback = blue!5,
colframe = nicedarkblue,
listing only,
fonttitle = \bfseries,
listing options = {%
basicstyle=\tiny\ttfamily,
keywordstyle=\color{blue},
language = C++,
commentstyle=\color{nicedarkgreen},
tabsize=2,
morekeywords={constexpr},
multicols = 2,
numbers = left,
xleftmargin = 0.5em,
showstringspaces = false,
},
overlay = {%
\fill[gray!30]
(interior.north west)
rectangle
([xshift = 1.25em]interior.south west);
\fill[gray!30]
([xshift = -1em]interior.north)
rectangle
([xshift = 0.25em]interior.south);
\draw[ultra thick, nicedarkblue]
([xshift = -1em]interior.north) --
([xshift = -1em]interior.south);
}
/utils/exec = {%
\def\thelstnumber{%
\texttt{\csname two@digits\endcsname{\the\value{lstnumber}}}}},
title = {\centering\ttfamily #1}
}
% TEST
\begin{document}
\lipsum[1]
\begin{cpplisting}[An example of C++ file]
// Include
#include <iostream>
// Example
int main()
{
std::cout<<"Hello World"<<std::endl;
std::cout<<"This is just an example"<<std::endl;
std::cout<<"With two columns"<<std::endl;
return 0;
}
\end{cpplisting}
\end{document}

With :
- alpha : one-column text width
- beta : first column width
- gamma : second column width
- mu : number column width
- nu : distance between numbers and text
- sigma : mu+nu
- phi : distance between title and first line
- psi : distance after last line
It looks great ... but the alignment is not and it causing me headaches... Instead of the current result, I want :
- beta = gamma : same total column width (so the blue line in the middle will correspond to the middle of the page)
- mu : to be adjusted to contains 3 digits (and when a number is three digit-long I want it to be centered on mu)
- phi = psi : same top and bottom margin
To know exactly the logic, it would be great if all of that would be achievable programmatically using :
\newlength{...}
\setlength{...}{...}
or something similar.
How to achieve this ?
