2

I am using algorithm2e to write algorithms. I'm doing things in such a way that, before the actual algorithm, I have a section for input and output arguments, and when relevant, references to other algorithms and equations that may be far away in the document. Like so:

This is rendering properly

I have introduced the "preamble" section of references with \SetKwInput{KwRef}{Referências}. So far so good.

But the way I'm doing it seems not to be the most clever way. I'm using \quad at the beginning of every item:

\begin{algorithm}
        \caption{My example}
        \KwIn{\\
            \quad$X$: something\\
            \quad$Y$: something else
        }
        \KwOut{\\
            \quad$R$: the result
        }
        \KwRef{\\
            \quad$f(\cdot)$ is the function introduced in Equation~\ref{eq:znorm}
        }
        $R \leftarrow X + Y$\;
    \end{algorithm}

What's the smart way of doing it?

1

You could instead perform the placement of \quad automatically via a tabular; this seems to fit in with your current usage:

enter image description here

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{algorithm2e}

\renewcommand{\KwIn}[1]{%
  \KwIna{\strut\\ \quad\begin{tabular}{@{} l @{}} #1 \end{tabular}}}
\renewcommand{\KwOut}[1]{%
  \KwOuta{\strut\\ \quad\begin{tabular}{@{} l @{}} #1 \end{tabular}}}
\newcommand{\KwRef}[1]{%
  \KwRefa{\strut\\ \quad\begin{tabular}{@{} l @{}} #1 \end{tabular}}}
\SetKwInput{KwIna}{Parâmetros de Entrada}
\SetKwInput{KwOuta}{Parâmetros de Saída}
\SetKwInput{KwRefa}{Referências}

\begin{document}

\begin{algorithm}
  \caption{My example}
  \KwIn{%
    $X$: something \\
    $Y$: something else
  }
  \KwOut{%
    $R$: the result
  }
  \KwRef{%
    $f(\cdot)$ is the function introduced in Equation~\ref{eq:znorm}
  }
  $R \leftarrow X + Y$\;
\end{algorithm}

\renewcommand{\theequation}{2.3}% Just for this example
\begin{equation}
  f(x) = ax^2 + bx + c \label{eq:znorm}
\end{equation}

\end{document}
  • It works, but... how? – giusti Jan 28 '17 at 0:34
  • 1
    The argument supplied to \KwIn (and friends) is placed inside a tabular. Using \\ to line-break the content translates well to the tabular environment. Each command inserts \\, followed by a \quad and the tabular. The tabular sets its content left aligned (without a prepended space, due to @{}). – Werner Jan 28 '17 at 7:17
  • Ah, so maybe what you actually meant is that I could automatically add a \quad and insert the lines into a tabular? I was thinking that somehow this command was inserting several \quads: one for each line of the tabular environment. – giusti Jan 28 '17 at 14:28

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.