I am trying to use algorithm2e for my pseudocode. I am having a weird behavior with the comments.

1. I want them to appear right after the code, on the right, while there seems to be a random (quite big) space that I don't want.

2. sometimes I tried to have comments on a line, which means that they don't come right after a piece of pseudocode, but I start directly with the comment itself. This was giving me error, any way to achieve this?

\begin{algorithm}[H]
\KwData{Number of samples per pixel N, image and camera properties}
\KwResult{Path Traced image $I_i$, and Gradient Domain Path Traced $\Delta_{i,j}$}
\For{each pixel \emph{i} of the image }{
\For{each sample per pixel}{
$I_i = I_i + h(x-x_i)f(\bar{x})/p(\bar{x})$\tcp*{\textbf{add contribution of base path to the primal image}}
$I_i = I_i/N;$\\
\For{all neighbour pixels $j \in \Phi_i of i$ }{
$\bar{y} := T_{ij}(\bar{x})$\tcp*{\textbf{calculate offset path with shift mapping}}
$\Delta_{i,j} := \Delta_{i,j} + w_{i,j}(\bar{x})h(x-x_i)(f(\bar{x}) - f(\bar{y})|T_{ij}|);$\\
$\Delta_{i,j} := Delta_{i,j} /N$  \tcp*{\textbf{for all \emph{j}}}
}
}
}
\caption{How to compute GDPT}
\end{algorithm}


There is an optional argument you can supply to \tcp that updates the way it displays in terms of the alignment. For more details, see section 10.3 comments (p 32) of the algorithm2e documentation.

Below I've used [h] and [l] to have a left-aligned comment without/with a line-end. \documentclass{article}

\usepackage{algorithm2e,mathtools}
\newcommand{\ce}{\coloneqq}

\begin{document}

\begin{algorithm}[H]
\KwData{Number of samples per pixel $N$, image and camera properties}
\KwResult{Path Traced image $I_i$, and Gradient Domain Path Traced $\Delta_{i,j}$}
\For{each pixel\/ $i$ of the image }{
\For{each sample per pixel}{
\tcp*[h]{\textbf{add contribution of base path to the primal image}}
$I_i = I_i + h(x-x_i)f(\bar{x})/p(\bar{x})$\;
$I_i = I_i/N$\;
\For{all neighbour pixels\/ $j \in \Phi_i$ of\/ $i$}{
\tcp*[h]{\textbf{calculate offset path with shift mapping}}
$\bar{y} \ce T_{ij}(\bar{x})$\;
$\Delta_{i,j} \ce \Delta_{i,j} + w_{i,j}(\bar{x})h(x-x_i)(f(\bar{x}) - f(\bar{y})|T_{ij}|)$\;
$\Delta_{i,j} \ce Delta_{i,j} /N$\tcp*[l]{\textbf{for all $j$}}
}
}
}
\caption{How to compute GDPT}
\end{algorithm}

\end{document}

• thank you very much! it's exactly what I was looking for! Jun 16, 2016 at 8:10