2

I am writing an algorithm, where the Input has two lines. I first tried with itemize environment, later substitued with algorithmic for uniform look-and-feel. The problem is, the counter in the actual algorithm now starts with 3. To solve, I put it within another algorithmic (those lines are marked with % Comment out this line). This solves the numbering issues, but adds the issue of indentation (now it looks likes those are the lines under Output).

I have this minimal working example:

\documentclass{article}
\usepackage{algorithm}
%\usepackage{algorithmic}
\PassOptionsToPackage{noend}{algpseudocode}
\usepackage{algpseudocode}%

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\algorithmicoutput{\textbf{Output:}}
\algnewcommand\Input{\item[\algorithmicinput]}%
\algnewcommand\Output{\item[\algorithmicoutput]}%


\begin{document}

    \begin{algorithm}
        \caption{Caption}\label{label}
        \begin{algorithmic}[1]
            \Input 
            {\begin{algorithmic}[1]
                    \item Something in line 1
                    \item Something in line 2
                \end{algorithmic}
            }
            \Output {Suppose something is in here}
            \begin{algorithmic}[1] % Comment out this line 
                \State Here goes the actual algorithm
                \State Now the next step
                \State This step, and we are done
                \State \Return {Here you go}
            \end{algorithmic} % Comment out this line 
        \end{algorithmic}
    \end{algorithm}
    
\end{document}

Questions (all about indentation):

  1. Notice the lines within Input do not have equal indentation (line 2 is shifted to the left a bit more). How can I fix that?
  2. How can I reduce the indentation the steps in the actual algorithm given the current version?
  3. (Alternate to 2, getting an answer to at least one would suffice). The indentation in question 2 goes away if the marked lines are commented out. In this case, how can I reset the algorithm number back to 1?
2
  • Is the following closer to the expected output or do you want to keep the two "Input" lines numbered? i.sstatic.net/a5LXA.png
    – leandriis
    Commented May 30, 2021 at 9:17
  • @leandriis That will work. Line numbering inside Input is not essential (but desirable).
    – hola
    Commented May 30, 2021 at 9:31

1 Answer 1

2

Here are two suggestions. Probably one of them suits your needs:

enter image description here

\documentclass{article}
\usepackage{algorithm}
%\usepackage{algorithmic}
\PassOptionsToPackage{noend}{algpseudocode}
\usepackage{algpseudocode}%

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\algorithmicoutput{\textbf{Output:}}
\algnewcommand\Input{\item[\algorithmicinput]}%
\algnewcommand\Output{\item[\algorithmicoutput]}%


\begin{document}

    \begin{algorithm}
        \caption{Caption}\label{label}
        \begin{algorithmic}[1]
            \Input Something in line 1
                     
                   Something in line 2 
            \Output {Suppose something is in here}
                \State Here goes the actual algorithm
                \State Now the next step
                \State This step, and we are done
                \State \Return {Here you go} 
        \end{algorithmic}
    \end{algorithm}

    \begin{algorithm}
        \caption{Caption}\label{label}
        \begin{algorithmic}[1]
            \Input 
            \item Something in line 1 
            \item  Something in line 2 \setcounter{ALG@line}{0}
            \Output {Suppose something is in here}
                \State Here goes the actual algorithm
                \State Now the next step
                \State This step, and we are done
                \State \Return {Here you go} 
        \end{algorithmic}
    \end{algorithm}

    
\end{document}

You must log in to answer this question.

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