10

Edit: I "solved" my main issues now. It's a bit ugly/hacky, but at least the result looks ok. With this edit I'm replacing the now obsolete old code with what I have now. But suggestions for improvement are always welcome.

Goal: Pseudocode with highlighted regions and corresponding annotations on the right hand side. How it looks now: enter image description here Questions:

  • The horizontal width of the colorboxes. I would want them to go all the way to the right and include the right hand side comments. (They should all end at the same spot.) "Solved" by duplicating my own boxit command. Ugly and not portable.
  • The color is in front of the text, it would be more readable if it was in the background. Solved

The code for this is here:

\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}
4
  • 1
    I suggest to highlight somehow the sentence "As you can see ... some of those", so it's easier for this community to recognize your question at a glance ;-) Which in fact are more than one ... // Thanks Commented Aug 27, 2021 at 10:19
  • 1
    Have you reconized the "Related" links on the right, like this one? tex.stackexchange.com/questions/204592/… Commented Aug 27, 2021 at 10:20
  • 1
    @MS-SPO Thanks, that's a good suggestion, done! I have not seen the Related section, I'll check it out. :-) Commented Aug 27, 2021 at 11:13
  • Maybe the way in one simple way can be a good alternative. Commented May 10, 2024 at 0:57

1 Answer 1

1

Besides changing the approach as @magpie suggested refactoring is something you can always apply.

However, the only duplicate I can find for generalization are your two \newcommands, which are almost identical:

% two slightly different boxit commands, to ensure the inner boxes end at the same spot
% refactored
\newcommand{\boxit}[2]{\mybox{#1}{#2}{.9}}
\newcommand{\boxitt}[2]{\mybox{#1}{#2}{.858}}

\newcommand\mybox[3]{%
    \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)+(#3\linewidth,-{#2}\baselineskip - 0.25\baselineskip)$)}] {};}\ignorespaces}

This is a good example, how refactoring can make code more legible. For refactoring in a Latex context see e.g. here or here.


\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
% refactored
\newcommand{\boxit}[2]{\mybox{#1}{#2}{.9}}
\newcommand{\boxitt}[2]{\mybox{#1}{#2}{.858}}

\newcommand\mybox[3]{%
    \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)+(#3\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}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.