算法:编号和对齐

算法:编号和对齐

在算法上,我们如何使“输入”在第一行对齐并减少表格的缩进?

\documentclass[a4paper]{scrartcl}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{algorithm}
\usepackage{algpseudocode}

\algrenewcommand\algorithmicrequire{\textbf{Input:}}
\algrenewcommand\algorithmicensure{\textbf{Output:}}

\begin{document}
    
\begin{algorithm}
    \caption{Anonymous Key Generation}\label{alg:akg}
    \begin{algorithmic}[1]
        \Require \begin{tabular}{l l l}
            $\mathrm{AAA}$ & $=$ & $\mathrm{BBB}$\\
            $\mathrm{CCC}$ & $=$ & $\mathrm{DDD}$
        \end{tabular}
        
        \Ensure $\mathrm{EEE}$
        \State Compute
        \Statex \hspace{\algorithmicindent} 
        \begin{tabular}{l l l}
            $\mathrm{FFF}$ & $\mathrm{=}$ &  $\mathrm{GGG}$\\
            & $\mathrm{=}$ &  $\mathrm{HHH}$ \\
        \end{tabular}
        \State Return $III$
    \end{algorithmic}
\end{algorithm}

\end{document} 

在此处输入图片描述

答案1

如果必须对齐行,我建议使用修改后的版本aligned。请注意使用对齐。最后一行应使用top否。\\

\documentclass[a4paper]{scrartcl}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{algorithm}
\usepackage{algpseudocode}

\algrenewcommand\algorithmicrequire{\textbf{Input:}}
\algrenewcommand\algorithmicensure{\textbf{Output:}}

\newenvironment{ilaligned}{%
  $\openup-1\jot\begin{aligned}[t]%
}{\\[0.5ex]\end{aligned}$}

\begin{document}
    
\begin{algorithm}
    \caption{Anonymous Key Generation}\label{alg:akg}
    \begin{algorithmic}[1]
        \Require \begin{ilaligned}
            \mathrm{AAA} &= \mathrm{BBB}\\
            \mathrm{CCC} &= \mathrm{DDD}
        \end{ilaligned}
        
        \Ensure $\mathrm{EEE}$
        \State Compute
        \Statex \hspace{\algorithmicindent} 
          \begin{ilaligned}
            \mathrm{FFF} &= \mathrm{GGG}\\
                         &= \mathrm{HHH}
          \end{ilaligned}
        \State Return $III$
    \end{algorithmic}
\end{algorithm}

\end{document} 

在此处输入图片描述

相关内容