3

I'm writing an algorithm box using algorithm2e and want to align the input arguments in a grid. I got close to the desired layout using two tabular environments, each inside of a minipage.

However, I would like both the word "Input:" and the two tables to align at the top instead of being vertically centered. Moreover, I would like to reduce the column and row spacing but putting \setlength{\tabcolsep}{.5em}% and \renewcommand{\arraystretch}{1.5}% inside the table does not have any effect. How can this be done?

Algorithm input grid

\usepackage[linesnumbered,ruled,noend]{algorithm2e}
\begin{algorithm}[h!]
\SetEndCharOfAlgoLine{}
\SetKwInOut{Input}{Input}
\Input{
\begin{minipage}{0.4\textwidth}
\begin{tabular}{ll}
$A$ & Description \\
$B$ & Description \\
$C$ & Description \\
$D$ & Description \\
\end{tabular}
\end{minipage}\hfill
\begin{minipage}{0.4\textwidth}
\begin{tabular}{ll}
$E$ & Description \\
$F$ & Description \\
$G$ & Description \\
\end{tabular}
\end{minipage}\hfill
}
\BlankLine
Initialize variables.
\For{all steps}{
  \Comment{Do something.}
}
\Return{The result.}
\BlankLine
\caption{Algorithm caption.}
\end{algorithm}
4

You don't need to use a minipage for this. Just assign a [t]op anchor for the tabulars:

enter image description here

\documentclass{article}

\usepackage[linesnumbered,ruled,noend]{algorithm2e}

\SetEndCharOfAlgoLine{}
\SetKwInOut{Input}{Input}

\begin{document}

\begin{algorithm}
  \Input{%
    \begin{tabular}[t]{ll}
      $A$ & Description \\
      $B$ & Description \\
      $C$ & Description \\
      $D$ & Description
    \end{tabular} \qquad
    \begin{tabular}[t]{ll}
      $E$ & Description \\
      $F$ & Description \\
      $G$ & Description
    \end{tabular}
  }
  \BlankLine
  Initialize variables\;
  \For{all steps}{
    Do something\;
  }
  \caption{Algorithm caption.}
\end{algorithm}

\end{document}

If you want the item and description to be closer to one another, adjust the column spacing:

\begin{tabular}[t]{l @{\hspace{.5em}} l}
  % ...
\end{tabular}

The @{...} column specification is used to adjust the inter-column spacing between two consecutive columns.

  • Thanks, this was very fast and a nice solution. I also added comment signs (\end{tabular}%) to avoid extra spaces. – danijar Jan 21 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.