I want to create a custom enumerate environment specifically for algorithms:
The First \item should have the label Input:
The second \item should have the label Output:
From then on, have the standard (\arabic*) counter.
This is, what it should look like:
I got the counter offset to work, but I can't figure out how to check the counter for the two first items.
How can I do that?
I already tried all kinds of combinations of algo
, algoi
, thealgo
, \thealgo
and \value{...}
etc., all if which result in the following:
MWE:
\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}
\usepackage{ifthen}
\usepackage[shortlabels]{enumitem}
\newcommand{\step}{
\ifthenelse{ \equal{\value{algoi}}{-2} }
{ \item[Input: ]}
{ \ifthenelse { \equal{\value{algoi}}{-1} }
{\item[Output: ]}
{\item }
}
}
\newenvironment{algo}{\begin{enumerate}[label=(\arabic*), start = -1]}{\end{enumerate}}
\begin{document}
\begin{algo}
\step This is the input.
\step This is the output.
\step This is step 1.
\step This is step 2.
\end{algo}
\end{document}
This code will throw the error message You can't use `\relax' after \the. \step
twice for each \step
.
Best Answer
I'd avoid
\ifthenelse
. Here's a way that also allows to have automatic good alignment to the left margin.I define two new counter representations, namely
\steplabel
and\steplabelref
, the latter to avoid parentheses around the step number. Next the two representations are registered withenumitem
.If you want parentheses around the step numbers in cross-references, just remove the
ref=
key.