2

I have a long "Ensure-block" in my algorithm. How can I force the second line to be exactly under the first "Text"?

\documentclass{article}

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}

\begin{document}

\begin{algorithm}[t]
\begin{algorithmic}[1]
\Ensure{Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text}
\State bla
\end{algorithmic}
\end{algorithm}

\end{document}

enter image description here

1 Answer 1

2

Here is a way to achieve what you're after:

enter image description here

\documentclass{article}

\usepackage{algorithm,lipsum}
\usepackage[noend]{algpseudocode}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}

\makeatletter
\newcommand{\setensurelength}{%
  \settowidth{\@tempdima}{\algorithmicensure}%
  \setlength{\@tempdima}{\dimexpr\linewidth-\@tempdima-1em+\@totalleftmargin}}
\newcommand{\setrequirelength}{%
  \settowidth{\@tempdima}{\algorithmicrequire}%
  \setlength{\@tempdima}{\dimexpr\linewidth-\@tempdima-1em+\@totalleftmargin}}
\newcommand{\ensurebox}[1]{%
  \setensurelength%
  \parbox[t]{\@tempdima}{\strut #1\strut}}
\newcommand{\requirebox}[1]{%
  \setrequirelength%
  \parbox[t]{\@tempdima}{\strut #1\strut}}
\makeatother
\begin{document}

\makeatletter
\begin{algorithm}[t]
  \begin{algorithmic}[1]
    \Ensure \ensurebox{\lipsum*[2]}
    \Require \requirebox{\lipsum*[2]}
    \State bla
  \end{algorithmic}
\end{algorithm}
\makeatother

\end{document}

The idea is to set the ensure/require blocks inside a \parbox of fixed width. The fixed width is determined from the list - yes, an algorithmic environment is actually a list - by removing certain components' lengths. The 1em is meant to simulate a (large) horizontal space, enough to shorten the paragraph block to fit within the margins.

You must log in to answer this question.

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