7

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?

2 Answers 2

8

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

2
  • Thank you for this solution. Is there also a solution local to an algorithm definition (\begin{algorithmic} ... solution ... \end{algorithmic})? My problem is that I have another definition in which 'Ensure' and 'Variables' appear, and in this case 'Ensure' has to be adjusted to the length of 'Variables'.
    – efie
    May 7, 2013 at 19:17
  • 1
    @efie You can put the \algrenewcommand inside the environment where you want it to hold. I've shown the two possibilities.
    – egreg
    May 7, 2013 at 19:27
1
\documentclass[11pt]{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{varwidth}

\begin{document}

\newlength{\reqwidth}
\settowidth{\reqwidth}{\textbf{Require:}}

\algrenewcommand{\algorithmicensure}{\makebox[\reqwidth][l]{\textbf{Ensure:}}}

   \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}

enter image description here

You must log in to answer this question.

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