1
\usepackage{algorithm, algcompatible, algpseudocode}

    \begin{algorithm}\caption{Steps of computation in a full GN block}\label{alg:gn_block}
            \begin{algorithmic}[1]
            \Function{GraphNetwork}{$E, V, u$}
                \For{$k \in \{1 \dots N_e\}$}
                    \State $e_k' \gets \phi^e(e_k, E_{r_k}, E_{s_k}, u)$ \Comment{1. Compute updated edge attributes}
                \EndFor
                \For{$i \in \{1 \dots N_v\}$}
                    \State $E_i' \gets \{(e_k', r_k, s_k)\}_{r_k = i, k = 1:N_e}$
                    \State $\Bar{e}_i' \gets \rho^{e \to v}(E_i')$ \Comment{2. Aggregate edge attributes per node}
                    \State $E_i' \gets \phi^v(\Bar{e}_i', E_i, u)$ \Comment{3. Compute updated node attributes}
                \EndFor
                \State $E' \gets \{E_i'\}_{i = 1:N_v}$
                \State $E' \gets \{(e_k', r_k, s_k)\}_{k = 1:N_e}$
                \State $\Bar{e}' \gets \rho^{e \to u}(E')$ \Comment{4. Aggregate edge attributes globally}
                \State $\Bar{v}' \gets \rho^{v \to u}(E')$ \Comment{5. Aggregate node attributes globally}
                \State $u' \gets \phi^u(\Bar{e}', \Bar{v}', u)$ \Comment{6. Compute updated global attribute}
                \State \Return $(E', V', u')$
            \EndFunction
            \end{algorithmic}
        \end{algorithm}

This is the code I have.. I want the triangles to be exactly one below the other (like in the image), not that the comments are aligned to the right. goal image

2

2 Answers 2

3

You can use the \makebox[width][position]{text} command to create a box of a specified width, allowing you to control the alignment of the text within it.

\documentclass{article}
\usepackage[a4paper]{geometry}

\usepackage{algorithm, algcompatible, algpseudocode}
\usepackage{xcolor}% needed for the color 'blue'

\newcommand{\Bar}[1]{\bar{#1}}

% 0. this seems to be the package definition
% \renewcommand{\algorithmiccomment}[1]{\hfill\(\triangleright\) #1}

% 1. let's align all comments to the right within a 7cm box
% \renewcommand{\algorithmiccomment}[1]{\hfill\makebox[7cm][l]{\(\triangleright\) #1}}
% 2. let's use \fbox to visually check the box limits
% \renewcommand{\algorithmiccomment}[1]{\hfill\fbox{\makebox[7cm][l]{\(\triangleright\) #1}}}

% 3. Bonus tip: let's make the comments stand out with different font and color
\newcommand\commentFont[1]{\small\ttfamily\textcolor{blue}{#1}}
\renewcommand{\algorithmiccomment}[1]{\hfill\makebox[7cm][l]{\commentFont{// #1}}}

\begin{document}
\begin{algorithm}\caption{Steps of computation in a full GN block}\label{alg:gn_block}
    \begin{algorithmic}[1]
    \Function{GraphNetwork}{$E, V, u$}
        \For{$k \in \{1 \dots N_e\}$}
            \State $e_k' \gets \phi^e(e_k, E_{r_k}, E_{s_k}, u)$ \Comment{1. Compute updated edge attributes}
        \EndFor
        \For{$i \in \{1 \dots N_v\}$}
            \State $E_i' \gets \{(e_k', r_k, s_k)\}_{r_k = i, k = 1:N_e}$
            \State $\Bar{e}_i' \gets \rho^{e \to v}(E_i')$ \Comment{2. Aggregate edge attributes per node}
            \State $E_i' \gets \phi^v(\Bar{e}_i', E_i, u)$ \Comment{3. Compute updated node attributes}
        \EndFor
        \State $E' \gets \{E_i'\}_{i = 1:N_v}$
        \State $E' \gets \{(e_k', r_k, s_k)\}_{k = 1:N_e}$
        \State $\Bar{e}' \gets \rho^{e \to u}(E')$ \Comment{4. Aggregate edge attributes globally}
        \State $\Bar{v}' \gets \rho^{v \to u}(E')$ \Comment{5. Aggregate node attributes globally}
        \State $u' \gets \phi^u(\Bar{e}', \Bar{v}', u)$ \Comment{6. Compute updated global attribute}
        \State \Return $(E', V', u')$
    \EndFunction
    \end{algorithmic}
\end{algorithm}
\end{document}

output

2

You can set all the elements inside a box of equal width. This can be automated using eqparbox's \eqmakebox[<tag>][<align>]{<stuff>} that sets all <stuff> inside a box of maximum width across the same <tag>. An additional <align>ment can be specified.

The code below automatically defined the <tag> to be algorithm-dependent, so you can use it across different algorithms.

enter image description here

\documentclass{article}

\usepackage{algorithm,algpseudocode}
\usepackage{amsmath}
\usepackage{eqparbox}

\NewCommandCopy\OldComment\Comment
% Update \Comment to utilize \eqmakebox[<tag>][<align>]{<stuff>}
\RenewDocumentCommand{\Comment}{m}{\OldComment{\eqmakebox[alg-\thealgorithm][l]{#1}}}

\begin{document}

\begin{algorithm}
  \caption{Steps of computation in a full GN block}
  \begin{algorithmic}[1]
    \Function{GraphNetwork}{$E, V, u$}
      \For{$k \in \{1, \dots, N_e\}$}
        \State $e_k' \gets \phi^e(e_k, E_{r_k}, E_{s_k}, u)$
          \Comment{1. Compute updated edge attributes}
      \EndFor
      \For{$i \in \{1, \dots, N_v\}$}
        \State $E_i' \gets \{(e_k', r_k, s_k)\}_{r_k = i, k = 1:N_e}$
        \State $\Bar{e}_i' \gets \rho^{e \to v}(E_i')$
          \Comment{2. Aggregate edge attributes per node}
        \State $E_i' \gets \phi^v(\Bar{e}_i', E_i, u)$
          \Comment{3. Compute updated node attributes}
      \EndFor
      \State $E' \gets \{E_i'\}_{i = 1:N_v}$
      \State $E' \gets \{(e_k', r_k, s_k)\}_{k = 1:N_e}$
      \State $\Bar{e}' \gets \rho^{e \to u}(E')$
        \Comment{4. Aggregate edge attributes globally}
      \State $\Bar{v}' \gets \rho^{v \to u}(E')$
        \Comment{5. Aggregate node attributes globally}
      \State $u' \gets \phi^u(\Bar{e}', \Bar{v}', u)$
        \Comment{6. Compute updated global attribute}
      \State \Return $(E', V', u')$
    \EndFunction
  \end{algorithmic}
\end{algorithm}

\end{document}

Since \eqmakebox uses a \label-\ref-like approach to finding the widest element across the same <tag>, two compilations are needed with every change in the widest element (including the first compilation).

1
  • I was searching for this command but I didn't remember the name, thank you :) Commented Dec 13, 2024 at 1:39

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.