5

I start to draw an algorithm using the algorithm package. And, I meet the problem that I can't well indent the parbox block which includes the loop, and it seems the parbox still effect the later parts:

My code is:

\documentclass[twoside, onecolumn, 11pt, a4paper]{article}
\usepackage[english,francais]{babel}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\algnewcommand{\Inputs}[1]{%
\State \textbf{Inputs:}
\Statex \hspace*{\algorithmicindent}\parbox[t]{.9\linewidth}{\raggedright #1}
}
\algnewcommand{\Outputs}[1]{%
\State \textbf{Outputs:}
\Statex \hspace*{\algorithmicindent}\parbox[t]{.9\linewidth}{\raggedright #1}
}
\algnewcommand{\Compute}[1]{%
\State \textbf{Compute:}
\hspace*{\algorithmicindent}\parbox[t]{.9\linewidth}{\raggedright #1}
}

\begin{document}
\begin{algorithm}
  \begin{algorithmic}
    \Inputs{$X$}
    \Compute{
      \For{i = 0 to I}
        \State do sth in the loop
      \EndFor
    }
    \Outputs{$Y$}
  \end{algorithmic}
\end{algorithm}
\end{document}

and I got this: enter image description here

I got a strange large indent of the content of Compute, and the Output is also with a indent seems effected by the parbox. How chould I well arrange this algorithm?

1
  • Welcome to TeX.SX!! Can you please complete your minimal working example so that it compiles.
    – user30471
    Commented Oct 24, 2016 at 12:48

1 Answer 1

4

I don't think you want to use parboxes at all, as it is not the way algorithmicx intends for you to do this. Indeed, algorithmicx lets you define new blocks for precisely this use. The parbox implementation (if you can get it to work) will have other unintended consequences as well, like indenting line numbers.

Instead, use algorithmicx's block definition syntax. It does take a bit of slogging through the manual to understand. I think it's simplest just to use the meta-block-definer \algdef rather than trying to figure out which of the derived commands matches your desired use case (there are a lot of them!).

\documentclass[twoside, onecolumn, 11pt, a4paper]{article}
\usepackage[english,francais]{babel}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\algdef{SnEL}{Inputs}{EndInputs}{1}{\textbf{Inputs:} \State}
\algdef{SnEL}{Outputs}{EndOutputs}{1}{\textbf{Outputs:} \State}
\algdef{SnEl}{Compute}{EndCompute}{\textbf{Compute:}}

\begin{document}
\begin{algorithm}
  \begin{algorithmic}
    \Inputs $X$
    \Compute
      \For{$i = 0$ to $I$}
        \State do $s$th in the loop
      \EndFor
    \EndCompute
    \Outputs $Y$
  \end{algorithmic}
\end{algorithm}
\end{document}

You might also consider redefining \For like on page 11 of the manual to get nicer formatting.

enter image description here

3
  • I added a picture of the nice result. The package warns algdef: 'N' obsoloted, use 'nE' (sic); can you do something about it?
    – egreg
    Commented Oct 24, 2016 at 15:54
  • Thanks a lot, it really helps! As I am still not familiar with this package.
    – Fay
    Commented Oct 24, 2016 at 16:04
  • Okay, I've changed the N flags to nE, and that seems to get rid of the warning. I don't see nE documented though, so I don't know what's actually changed.
    – Emma
    Commented Oct 24, 2016 at 16:18

You must log in to answer this question.

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