Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

3 Answers 3

up vote 2 down vote accepted

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}
share|improve this answer

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:.

share|improve this answer

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

share|improve this answer
    
This limits the Input and Output to a single line. However, tabularx would circumvent this limitation. –  Werner Aug 12 at 13:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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