6

I use the algorithm2e package to typeset algorithms with custom keywords for input and output.

When using \SetKwInput{KwInput}{Input} to define the keywords, colons are added but input and output are not aligned.

enter image description here

When using \SetKwInOut{KwInput}{Input} the text is aligned. However, the colon is at the right, so there will be ugly space like "Input :" that creates eye cancer.

enter image description here

My question is: How can I get the output (shown in the image below) without fiddling with \hspace in every algorithm environment?

The result I would like to achieve.

Here is a MWE:

\documentclass[a4paper]{article}
\usepackage[ruled]{algorithm2e}

% Define keywords
\SetKwInput{KwInput}{Input}
\SetKwInput{KwOutput}{Output}

% This aligns the colons
%\SetKwInOut{KwInput}{Input}
%\SetKwInOut{KwOutput}{Output}

\begin{document}

    \begin{algorithm}[h]
        \KwInput{Network}

        \KwOutput{Flow}

        \begin{enumerate}
            \item Start with $\ldots$
        \end{enumerate}
        \caption{Some algorithm.}
    \end{algorithm}
\end{document}

3 Answers 3

2

enter image description here

\documentclass[a4paper]{article}
\usepackage[ruled]{algorithm2e}
\usepackage{etoolbox}

\makeatletter
\renewcommand{\SetKwInOut}[2]{%
  \sbox\algocf@inoutbox{\KwSty{#2}\algocf@typo:}%
  \expandafter\ifx\csname InOutSizeDefined\endcsname\relax% if first time used
    \newcommand\InOutSizeDefined{}\setlength{\inoutsize}{\wd\algocf@inoutbox}%
    \sbox\algocf@inoutbox{\parbox[t]{\inoutsize}{\KwSty{#2}\algocf@typo:\hfill}~}\setlength{\inoutindent}{\wd\algocf@inoutbox}%
  \else% else keep the larger dimension
    \ifdim\wd\algocf@inoutbox>\inoutsize%
    \setlength{\inoutsize}{\wd\algocf@inoutbox}%
    \sbox\algocf@inoutbox{\parbox[t]{\inoutsize}{\KwSty{#2}\algocf@typo:\hfill}~}\setlength{\inoutindent}{\wd\algocf@inoutbox}%
    \fi%
  \fi% the dimension of the box is now defined.
  \algocf@newcommand{#1}[1]{%
    \ifthenelse{\boolean{algocf@inoutnumbered}}{\relax}{\everypar={\relax}}%
%     {\let\\\algocf@newinout\hangindent=\wd\algocf@inoutbox\hangafter=1\parbox[t]{\inoutsize}{\KwSty{#2}\algocf@typo\hfill:}~##1\par}%
    {\let\\\algocf@newinout\hangindent=\inoutindent\hangafter=1\parbox[t]{\inoutsize}{\KwSty{#2}\algocf@typo:\hfill}~##1\par}%
    \algocf@linesnumbered% reset the numbering of the lines
  }}%
\makeatother

% Define keywords
%\SetKwInput{KwInput}{Input}
%\SetKwInput{KwOutput}{Output}

% This aligns the colons
\SetKwInOut{KwInput}{Input}
\SetKwInOut{KwOutput}{Output}

\begin{document}

    \begin{algorithm}[h]
        \KwInput{Network}

        \KwOutput{Flow}

        \begin{enumerate}
            \item Start with $\ldots$
        \end{enumerate}
        \caption{Some algorithm.}
    \end{algorithm}
\end{document}
2

If you're sticking to a single-line \KwInput, you can use the following:

enter image description here

\documentclass[a4paper]{article}
\usepackage[ruled]{algorithm2e}
\usepackage{calc}

% Define keywords
\SetKwInput{KwInput}{Input}
\SetKwInput{KwOutput}{Output}
\let\oldKwInput\KwInput
\renewcommand{\KwInput}[1]{%
  \makebox[\widthof{\KwOutput{}}][l]{\oldKwInput{}#1}%
}

\begin{document}

\begin{algorithm}[h]
  \KwInput{Network}

  \KwOutput{Flow}

  \begin{enumerate}
    \item Start with $\ldots$
  \end{enumerate}
  \caption{Some algorithm.}
\end{algorithm}
\end{document}

The above code redefined \KwInput by placing Input: inside a box of width matching that of Output:.

1

Here's an unusual way to achieve it... using tabular.

\documentclass[a4paper]{article}
\usepackage[ruled]{algorithm2e}

% Define keywords
\SetKwInput{KwInput}{Input}
\SetKwInput{KwOutput}{Output}

% This aligns the colons
%\SetKwInOut{KwInput}{Input}
%\SetKwInOut{KwOutput}{Output}

\begin{document}

    \begin{algorithm}[h]
    \tabcolsep=0pt
    \begin{tabular}{@{}ll}
        \KwInput{}&Network\\
        \KwOutput{}&Flow
    \end{tabular}
        \begin{enumerate}
            \item Start with $\ldots$
        \end{enumerate}
        \caption{Some algorithm.}
    \end{algorithm}

\end{document} 

enter image description here

1
  • This limits the Input and Output to a single line. However, tabularx would circumvent this limitation.
    – Werner
    Aug 12, 2014 at 13:54

You must log in to answer this question.

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