[Tex/LaTex] Indentation in the algorithm package after \Ensure, \Require

algorithmicxindentation

I have this piece of code:

\documentclass[11pt]{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{varwidth}

\begin{document}

   \begin{algorithmic}
      \Require   \begin{varwidth}[t]{\linewidth}
                  A \par
                  AA  
               \end{varwidth}
      \Ensure      \begin{varwidth}[t]{\linewidth}
                  B \par
                  BB
               \end{varwidth}
   \end{algorithmic}

\end{document}

which yields

enter image description here

I would like the text which follows after \Ensure to be indented in such a way that it is placed right under the text which follows after \Require. How can this be done?

Best Answer

You can modify the definition of \Ensure to build a box as wide as Require:; you can do it globally or locally:

Global

\documentclass[11pt]{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{varwidth}
\usepackage{calc} % for \widthof
\algrenewcommand\algorithmicensure{%
  \makebox[\widthof{\textbf{Require:}}][l]{\textbf{Ensure:}}}

\begin{document}

   \begin{algorithmic}
      \Require   \begin{varwidth}[t]{\linewidth}
                  A \par
                  AA\strut
               \end{varwidth}
      \Ensure      \begin{varwidth}[t]{\linewidth}
                  B \par
                  BB\strut
               \end{varwidth}
   \end{algorithmic}

\end{document}

Local

\documentclass[11pt]{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{varwidth}
\usepackage{calc} % for \widthof

\begin{document}

   \begin{algorithmic}
   \algrenewcommand\algorithmicensure{%
     \makebox[\widthof{\textbf{Require:}}][l]{\textbf{Ensure:}}}
      \Require   \begin{varwidth}[t]{\linewidth}
                  A \par
                  AA\strut
               \end{varwidth}
      \Ensure      \begin{varwidth}[t]{\linewidth}
                  B \par
                  BB\strut
               \end{varwidth}
   \end{algorithmic}

\end{document}

Notice also the \strut at the end of varwidth to get an even vertical spacing.

enter image description here