1

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:

enter image description here

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:

enter image description here

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.

2 Answers 2

1

I'd avoid \ifthenelse. Here's a way that also allows to have automatic good alignment to the left margin.

\documentclass[a4paper,12pt]{article}
\usepackage{showframe}

\usepackage{amsmath}
\usepackage{enumitem}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\steplabel}{m}
 {
  \xoriun_steplabel_std:n { #1 }
 }
\NewExpandableDocumentCommand{\steplabelref}{m}
 {
  \xoriun_steplabel_ref:n { #1 }
 }

\cs_new:Nn \xoriun_steplabel_std:n
 {
  \__xoriun_steplabel:nnn { ( } { ) } { #1 }
 }
\cs_new:Nn \xoriun_steplabel_ref:n
 {
  \__xoriun_steplabel:nnn { } { } { #1 }
 }
\cs_new:Nn \__xoriun_steplabel:nnn
 {
  \int_case:nnF { \value{#3} }
   {
    {-1}{Input:}
    {0}{Output:}
   }
   {#1\arabic{#3}#2}
 }
\AddEnumerateCounter{\steplabel}{\xoriun_steplabel_std:n}{Output:}
\AddEnumerateCounter{\steplabelref}{\xoriun_steplabel_ref:n}{Output:}
\ExplSyntaxOff

\let\step\item

\newenvironment{algo}
 {\begin{enumerate}[label=\steplabel*,ref=\steplabelref*,start=-1,leftmargin=*]}
 {\end{enumerate}}

\begin{document}
    \begin{algo}
        \step This is the input.
        \step This is the output.
        \step This is step 1.\label{test}
        \step This is step 2.
    \end{algo}

\ref{test}
\end{document}

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 with enumitem.

enter image description here

If you want parentheses around the step numbers in cross-references, just remove the ref= key.

1
  • thank you, this works really nicely. I don't care so much about the \ifthenelse, but the alignment is really nice.
    – Xoriun
    Nov 19, 2021 at 10:58
1

There is no counter algoi, in enumerate it is called enumi:

\documentclass[a4paper,12pt]{article}

\usepackage{amsmath}
\usepackage{ifthen}
\usepackage[shortlabels]{enumitem}


\newcommand{\step}{
    \ifthenelse{ \equal{\value{enumi}}{-2} }
        { \item[Input: ]\stepcounter{enumi}}
        { \ifthenelse { \equal{\value{enumi}}{-1} }
            {\item[Output: ]\stepcounter{enumi}}
            {\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}

enter image description here

3
  • thank you. Btw, why is the \stepcounter necessary? Bc when using \item[...] it would increment, or did I get this wrong?
    – Xoriun
    Nov 18, 2021 at 14:57
  • Try it out ... then you will see if it increments or not. Nov 18, 2021 at 14:59
  • First I didn't see that \stepcounter was added so I know it doesn't work, but I wanted to know why it doesn't work. If there is a simple explanation for it or is just low level TeX weirdness?
    – Xoriun
    Nov 18, 2021 at 15:05

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.