编辑:我现在已经“解决”了我的主要问题。虽然有点丑陋/粗制滥造,但至少结果看起来还不错。通过这次编辑,我将用现在的代码替换现在已过时的旧代码。但欢迎提出改进建议。
目标:在右侧突出显示区域和相应注释的伪代码。现在的样子: 问题:
颜色框的水平宽度。我希望它们一直向右移动,并包含右侧的注释。(它们应该都在同一位置结束。)通过复制我自己的 boxit 命令“解决”。丑陋且不可移植。颜色在文本前面,如果放在后台,可读性会更好。已解决
代码如下:
\documentclass[a4paper,english]{article}
\usepackage[margin=3cm]{geometry}
% colors
\usepackage{xcolor, amsmath}
\definecolor{amaranth}{rgb}{0.9, 0.17, 0.31}
\colorlet{green}{green!20}
\colorlet{yellow}{yellow!60}
\colorlet{red}{red!30}
% pseudocode formatting
\usepackage{setspace}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\renewcommand{\KwSty}[1]{\textnormal{\textcolor{amaranth!90!black}{\bfseries #1}}\unskip}
\renewcommand{\ArgSty}[1]{\textnormal{\ttfamily #1}\unskip}
\SetKwComment{Comment}{\color{green!80!black!190}// }{}
\renewcommand{\CommentSty}[1]{\textnormal{\ttfamily\color{green!80!black!190}#1}\unskip}
\newcommand{\var}{\texttt}
\newcommand{\FuncCall}[2]{\texttt{\bfseries #1(#2)}}
\SetKwProg{Function}{function}{}{}
\SetKw{Continue}{continue}
\renewcommand{\ProgSty}[1]{\texttt{\bfseries #1}}
\DontPrintSemicolon
\SetAlFnt{\small}
\SetAlgorithmName{Pseudocode}{algorithmautorefname}
% pseudocode highlighting
\usepackage{tikz}
\usetikzlibrary{fit,calc}
% two slightly different boxit commands, to ensure the inner boxes end at the same spot
\newcommand{\boxit}[2]{
\tikz[remember picture,overlay] \node (A) {};\ignorespaces
\tikz[remember picture,overlay]{\node[yshift=3pt,fill=#1,opacity=.25,fit={($(A)+(0,0.15\baselineskip)$)($(A)+(.9\linewidth,-{#2}\baselineskip - 0.25\baselineskip)$)}] {};}\ignorespaces
}
\newcommand{\boxitt}[2]{
\tikz[remember picture,overlay] \node (A) {};\ignorespaces
\tikz[remember picture,overlay]{\node[yshift=3pt,fill=#1,opacity=.25,fit={($(A)+(0,0.15\baselineskip)$)($(A)+(.858\linewidth,-{#2}\baselineskip - 0.25\baselineskip)$)}] {};}\ignorespaces
}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{find\_best\_split}
\Function{find\_best\_split(X, gradients, curr\_depth)}{
\tcp{determine node privacy budget}
\boxit{green}{6.5}
\eIf (\Comment{params.use\_decay}) {use\_decay} {
\boxitt{yellow}{3}
\eIf (\Comment{curr\_depth == 0}) {curr\_depth == 0} {
$\var{node\_budget} = \frac{\var{tree\_budget}}{2}$\;
}{
$\var{node\_budget} = \frac{\var{tree\_budget}}{4}$\;
}
}{
$\var{node\_budget} = \frac{\var{tree\_budget}}{6}$\;
}
\tcp{iterate over all possible splits} \boxit{green}{5}
\For(\Comment{number of cols in X}){feature\_index : features} {
\boxitt{red}{3.5}
\For(\Comment{number of rows in X}){feature\_value : X} {
\If {use\_dp} {
\Continue
}
$\var{gain} = \texttt{\emph{compute\_gain(X, gradients)}}$\;
}
}
$\texttt{TreeNode *node = new TreeNode()}$\;
\Return{\var{node}}\;
}
\end{algorithm}
\end{document}