2

I am trying to apply solution in Include a line break in algorithmic while maintaining indentation for the lines starting with if. But then does not carried out to breaked line.

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\makeatletter
\let\OldStatex\Statex
\renewcommand{\Statex}[1][3]{%
  \setlength\@tempdima{\algorithmicindent}%
  \OldStatex\hskip\dimexpr#1\@tempdima\relax}
\makeatother
\begin{document}
\section{Struttura dati}
\begin{algorithm}
  \caption{Leggi file .gpx}\label{getgpx}
  \begin{algorithmic}[1]
      \If{x=10 \textbf{and}}
      \Statex y=10
    \State speed~$\gets$~computeSpeed(
    \Statex gpx.track(i).segment(j).delta\_s(q),
    \Statex[2] gpx.track(i).segment(j).delta\_t(q));
    \EndIf
  \end{algorithmic}
\end{algorithm}
\end{document}

output:

enter image description here

Wanted output:

1: if x=10 and 
       y=10  then
2:     speed ← computeSpeed(...
alper
  • 1,389

1 Answers1

1

The following defines a new if called \multilineIf that allows you to specify a stacked set of conditions using a tabular-like format (conditions are separated by \\). The closing then is automatically added to the end of the conditions.

enter image description here

\documentclass{article}

\usepackage{algorithm,algpseudocode}

\makeatletter \let\OldStatex\Statex \renewcommand{\Statex}[1][3]{% \setlength@tempdima{\algorithmicindent}% \OldStatex\hskip\dimexpr#1@tempdima\relax} \makeatother

\newcommand{\stackedconditions}[1]{% \begin{tabular}[t]{@{} l @{}} #1 \end{tabular}% } \algdef{SE}[IF]{multilineIf}{EndIf}[1]{\algorithmicif\ \stackedconditions{#1\ \algorithmicthen}}{\algorithmicend\ \algorithmicif}%

\begin{document}

\section{Struttura dati}

\begin{algorithm} \caption{Leggi file .gpx}\label{getgpx} \begin{algorithmic}[1] \multilineIf{% $x = 10$ \textbf{and} \ $y = 10$} \State speed~$\gets$~computeSpeed( \Statex gpx.track(i).segment(j).delta_s(q), \Statex[2] gpx.track(i).segment(j).delta_t(q)); \EndIf \end{algorithmic} \end{algorithm}

\end{document}

The conditions are left-aligned by default, but you could change this to suit your needs.

The definition of \multilineIf via \algdef was copied-and-changed from the original \If definition with algpseudocode.sty.

Werner
  • 603,163