3

I would like to have an if-then-else statement in the output block, with this MWE:

\documentclass{article}
\usepackage[ruled]{algorithm2e}
\usepackage{amsmath}

\begin{document}
  \begin{algorithm}[H]
    \SetKwInput{Output}{Output}
    \Output{
      \eIf{QP is feasible}{ 
        $x^*$: primal solution of problem (1)\newline
        $\lambda^*$: dual solution solving (2).
      }{
        infeasibility status.
      }
    }
    \caption{QP solver}
  \end{algorithm}

\end{document}

i get the following result: enter image description here

Is there a way of getting the lines of the if-then-else block aligned with the first if if?

Like this:

enter image description here

Thanks!

2 Answers 2

1

If you only want what you show, then you can adjust \algomargin:

enter image description here

\documentclass{article}

\usepackage[ruled]{algorithm2e}
\DontPrintSemicolon

\begin{document}

% Update \algomargin
\settowidth{\algomargin}{\textbf{Output:} }
\addtolength{\algomargin}{\parindent}
\begin{algorithm}[H]
  \makebox[0pt][r]{\textbf{Output:} }%
    \eIf{QP is feasible}{
      $x^*$: primal solution of problem (1)\;
      $\lambda^*$: dual solution solving (2)\;
    }{
      infeasibility status\;
    }
  \caption{QP solver}
\end{algorithm}
\setlength{\algomargin}{\parindent}% Restore \algomargin

\begin{algorithm}[H]
  \SetKwInOut{Output}{Output}
  \Output{\eIf{QP is feasible}{
      $x^*$: primal solution of problem (1)\;
      $\lambda^*$: dual solution solving (2)\;
    }{
      infeasibility status\;
    }}
  \caption{QP solver}
\end{algorithm}

\end{document}

The second algorithm is merely for reference of the alignment.

1

You can use an algorithm inside an algorithm.

With a dirty workaround (can't figure out what lenghts the vertical skip comes from):

\documentclass[10pt]{article}
\usepackage[ruled]{algorithm2e}
\usepackage{amsmath}
\begin{document}
    \begin{algorithm}[H]
        \SetKwInput{Output}{Output}
        \Output{%
            \RestyleAlgo{plain}
            \setlength{\algomargin}{1ex}
            \begin{algorithm}[H]
                \vspace{-14.4pt}
                \eIf{QP is feasible}{ 
                    $x^*$: primal solution of problem (1)\newline
                    $\lambda^*$: dual solution solving (2).
                   }{
                    infeasibility status.
                    }
            \end{algorithm}
        }
    \caption{QP solver}
    \end{algorithm}
\end{document}

Output:

enter image description here

You must log in to answer this question.

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