1

I have an algorithm and I would like to insert space between its steps.

Here is my try:

   \documentclass[12pt,a4paper,openright]{report}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{verbatim}
\usepackage{longtable}
\usepackage{graphicx}
\usepackage[square]{natbib}
    \usepackage[utf8]{inputenc}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\algrenewcommand\algorithmicrequire{\textbf{Input:}}
\algrenewcommand\algorithmicensure{\textbf{Output:}}
\newcommand{\sfunction}[1]{\textsf{\textsc{#1}}}
\algrenewcommand\algorithmicforall{\textbf{foreach}}
\algrenewcommand\algorithmicindent{.8em}
\begin{document}
       \begin{algorithm}
     \caption{my algorithm}
            \label{alg:ALG1}
    \begin{algorithmic}[1]

    \State $\text{For each line} of my algorithm I would like to insert a space between the steps but I cannot do that. The text goes out the box.$
    \State $i \gets \textit{patlen}$
    \If {$i > \textit{stringlen}$} \Return false
    \EndIf
    \State $j \gets \textit{patlen}$
    \If {$\textit{string}(i) = \textit{path}(j)$}
    \State $j \gets j-1$.
    \State $i \gets i-1$.
    \State \textbf{goto} \emph{loop}.
    \State \textbf{close};
    \EndIf
    \State $i \gets i+\max(\textit{delta}_1(\textit{string}(i)),\textit{delta}_2(j))$.
    \State \textbf{goto} \emph{top}.
    \end{algorithmic}
    \end{algorithm}
 \end{document}
  • Can you provide preamble part, i.e., from \documentclass to \begin{document} – MadyYuvi Feb 7 at 7:10
  • @MadyYuvi Ok. I will in a minute. – Maryam Feb 7 at 7:11
  • @MadyYuvi I have added them. – Maryam Feb 7 at 7:13
  • 3
    @Maryam: Instead of using \State $\text{your text}$ for a \Statement of code, just use \State your text, without the $\text{...}$. – Werner Feb 7 at 7:20
  • 1
    Glad to hear that Werner's suggestion works for you... – MadyYuvi Feb 7 at 8:36
2

In order to have line wrapping "enabled" you need to set your text as regular text, not in math mode wrapped inside \text:

enter image description here

\documentclass{article}

\usepackage{algorithm,algpseudocode}

\begin{document}

\begin{algorithm}
  \caption{An algorithm}
  \begin{algorithmic}[1]
    \State For each line of my algorithm I would like to insert a space 
      between the steps but I cannot do that. The text stays inside the box.
    \State Something else altogether.
  \end{algorithmic}
\end{algorithm}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.