我希望if-then-else
在输出块中包含一个带有以下 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}
有没有办法让if-then-else
块的行与第一个 if 对齐if
?
像这样:
谢谢!
答案1
如果你只想要你显示的内容,那么你可以调整\algomargin
:
\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}
第二种算法仅供比对参考。
答案2
您可以在算法中使用算法。
采用一种不正规的解决方法(无法弄清楚垂直跳跃的长度来自哪里):
\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}
输出: