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}

1 Answer 1

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .