考虑以下代码:
% Preamble
\documentclass[letterpaper, 10pt]{article}
\usepackage{calc}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{multicol}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
% Lengths
\newlength{\xshift}
\newlength{\tcbboxrule}
\newlength{\tcbleft}
\newlength{\tcbright}
\newlength{\lstxleftmargin}
\newlength{\lstxrightmargin}
% Parameters (that can be changed by the user)
\setlength{\xshift}{1em}
\setlength{\tcbboxrule}{1mm}
% Variables (to be adjusted automatically from the previous ones)
\setlength{\tcbleft}{0pt} % <- TO BE MODIFIED
\setlength{\tcbright}{0pt} % <- TO BE MODIFIED
\setlength{\lstxleftmargin}{\xshift} % <- TO BE MODIFIED
\setlength{\lstxrightmargin}{0pt} % <- TO BE MODIFIED
% Other options
\definecolor{backgroundcolor}{RGB}{240, 240, 240}
\definecolor{framecolor}{RGB}{24, 24, 128}
\colorlet{keywordcolor}{blue}
\colorlet{commentcolor}{red}
\newlength{\tcbboxrulemask}
\setlength{\tcbboxrulemask}{\dimexpr 0.5\tcbboxrule + 0.5pt \relax}
% Environments
\newtcblisting{multilisting}[3][\normalsize]{
before upper = {\topskip0pt \maxdepth=0pt},
enhanced,
listing only,
breakable = unlimited,
top = 0em,
bottom = 0em,
arc = 0em,
outer arc = 0em,
boxsep = 0em,
titlerule = 0em,
colback = backgroundcolor,
colframe = framecolor,
boxrule = \tcbboxrule,
left = \tcbleft,
right = \tcbright,
listing options = {
language = C++,
showstringspaces = false,
literate = {~}{$\sim$}{1},
framesep = 0pt,
aboveskip = 0pt,
belowskip = 0pt,
xleftmargin = \lstxleftmargin,
xrightmargin = \lstxrightmargin,
basicstyle = \fontfamily{cmtt}\selectfont#1,
keywordstyle = \color{keywordcolor},
commentstyle = \color{commentcolor},
tabsize = 4,
numbers = none,
multicols = 2,
#2,
},
overlay = {
\draw[line width = \tcbboxrule, framecolor]
(interior.north) -- (interior.south);
\fill[backgroundcolor] (interior.north) rectangle
([xshift = \tcbboxrulemask] interior.south);
},
#3,
}
% Document
\begin{document}
\lipsum[1]
\begin{multilisting}[\normalsize]{}{}
double f(double x)
{
return 2 * x;
}
double g(double x)
{
return 3 * x;
}
\end{multilisting}
\lipsum[1]
\end{document}
它产生以下输出:
以下是一些测量结果:
在上面的代码中,fontsize
列表的可以调整,以及\xshift
最终\tcbboxrule
用户可以调整。
问题:如何更改标记的行,TO BE MODIFIED
以便多列列表正确“居中” tcolorbox
(意味着图上标记为的距离?x?
等于\xshift
)?