I have an algorithm that I want to write in mathematical pseudo-code. However, some conditions and statements are very long, e.g. "if (a series of relations and constraints hold) do" or "X gets (a series with several elements)". These statements does not fit nicely into a single line. I don't want to break them into temporary variables just to meet the width limit.

What I would have done in a real program code, is something like:

if (
  long condition 1 and
  long condition 2 ...
) {...}


X <- {
  long element 1,
  long element 2...

How can I achieve this effect in some LaTeX algorithmic environment? I am currently using algorithm2e but it's not a must.

  • Is there a canonical way to denote linebreaks in a code? Maybe there is and you can continue the code indented on the next line.
    – Sigur
    Apr 16, 2015 at 10:24
  • For regular line-breaking inside algorithm2e (not inside conditionals), see Linebreak in package algorithm2e (partial duplicate).
    – Werner
    Apr 17, 2015 at 20:47

2 Answers 2


Regular line-breaks and associated indentation is discussed in Line-break in package algorithm2e. For conditional adjustments, perhaps the following might be of interest:

enter image description here

\SetKwIF{If}{ElseIf}{Else}{if~(\endgraf}{\endgraf)~then}{else if}{else}{end if}%

  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  \While{not at end of this document}{
    read current\;
        understand {\normalfont and} \\
        understand some more
      go to next section\;
      go back to the beginning of current section\;
  \caption{How to write algorithms}

I have adjusted the formatting of \eIf - only for the conditional part - to include (..). You'll have to do the same for other structures/conditionals.

  • Is it possible to have the if statement without the paranthesis
    – alper
    May 11, 2022 at 2:07
  • @alper: Sure... just remove the parentheses in the definition.
    – Werner
    May 11, 2022 at 5:29
  • I did but now there is new line after the if, is it also possible to prevent it // I have removed \endgraf but it does not help
    – alper
    May 11, 2022 at 10:38
  • @alper: You may have something in mind that I'm not aware of. Post a visual of what you're after so it's clearer.
    – Werner
    May 11, 2022 at 15:58
  • please see: tex.stackexchange.com/questions/643821/…
    – alper
    May 11, 2022 at 16:00

This is all much easier if you use the algoritmicx package, it has a lot more functionality and works better with other packages:


%Defines the \If command, ending in \EndIf
\algblockdefx[If]{If}{EndIf}[0]{\textbf{if} $($}{$)$}
%Defines the alternative ending \Then to the \If command, 
%which in turn ends in \EndThen
\algcblockdefx[Then]{If}{Then}{EndThen}{$)$ $\{$}{$\}$}
%Provides the alternative ending \Else to the \Then command,
%which in turn ends in \EndElse
\algcblockdefx[Else]{Then}{Else}{EndElse}{$\}$ \textbf{else} $\{$}{$\}$}


Here is an example:

\State Here We Begin
    \State this is true
    \State Or this is true
    \State Then we do that
    \State this is true
    \State Or this is true
    \State Then we do that
    \State Or do this



This gives the output:

Output example

The package also provides algpseudocode, which has a lot of useful blocks predefined.

You must log in to answer this question.

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