将算法的输入和输出左对齐

将算法的输入和输出左对齐

我使用以下 KDD 2017 会议模板(sample-sigconf 文件):这里这是我用于算法的代码:

\documentclass[sigconf]{acmart}
\usepackage{booktabs,makecell,tabularx}

\renewcommand\theadfont{\small}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\usepackage{siunitx}
\usepackage{adjustbox}
\usepackage{array,booktabs}

\usepackage{graphicx}
\usepackage{epstopdf}

%\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{amsmath}


\begin{algorithm} [H]
        \caption{Dsfdgfgskj sdfgfkjsdl sdfggjlkj fgsdt}
        \label{alg:ALG1}
        \textbf{INPUT:} $x$ - decision tree\\
        \textbf{OUTPUT:} $abc$ is $x$ in Txx Bxxxxx Gxxxx
        \begin{algorithmic}
            \State  Function $UpdateNodes(x)$:

            \If {  $wrejkwe$ ($rw$) trwer tewwerl }
            %       \COMMENT { 
            \State {jklrjkljfgkljlkj  kjkldfj gfdsdf }
            \State  Set fdgsdsd
            \ForAll  {$j=1$ to $N (x)$}
            \State        Call fgsd(x)$  
            \State        Set sfgdfgd =sfdg + fgds  

            \EndFor 
            \EndIf

        \end{algorithmic}
    \end{algorithm}

但是,输入和输出都居中对齐。我该如何将其改为左对齐? 在此处输入图片描述

答案1

添加\begin{flushleft}可解决问题:

\begin{algorithm} [H]
        \caption{Dsfdgfgskj sdfgfkjsdl sdfggjlkj fgsdt}
        \label{alg:ALG1}
\begin{flushleft}
        \textbf{INPUT:} $x$ - decision tree\\
        \textbf{OUTPUT:} $abc$ is $x$ in Txx Bxxxxx Gxxxx
\end{flushleft}
        \begin{algorithmic}
            \State  Function $UpdateNodes(x)$:

            \If {  $wrejkwe$ ($rw$) trwer tewwerl }
            %       \COMMENT { 
            \State {jklrjkljfgkljlkj  kjkldfj gfdsdf }
            \State  Set fdgsdsd
            \ForAll  {$j=1$ to $N (x)$}
            \State        Call fgsd(x)$  
            \State        Set sfgdfgd =sfdg + fgds  

            \EndFor 
            \EndIf

        \end{algorithmic}
    \end{algorithm}

答案2

使用包的命令algpseudocode来定义输入、输出和函数标题。预定义命令\Require\Ensure用于描述输入和输出。如果您不喜欢关键字RequireEnsure,请重新定义它们:

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

您还可以定义新的语句\Input\Output

\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\algorithmicoutput{\textbf{Output:}}
\algnewcommand\Input{\item[\algorithmicinput]}%
\algnewcommand\Output{\item[\algorithmicoutput]}%

现在您可以使用\Input ...description of input并且同样\Output ...

对于函数标题,使用

 \Function {UpdateNodes}{$x$}
 ...
 \EndFunction

在此处输入图片描述

\documentclass[sigconf]{acmart}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\algrenewcommand\algorithmicrequire{\textbf{Input:}}
\algrenewcommand\algorithmicensure{\textbf{Output:}}
\begin{document}
    \begin{algorithm}
        \caption{Dsfdgfgskj sdfgfkjsdl sdfggjlkj fgsdt}
        \label{alg:ALG1}
        \begin{algorithmic}
        \Require $x$ - decision tree
        \Ensure $abc$ is $x$ in Txx Bxxxxx Gxxxx
        \Function {UpdateNodes}{$x$}
            \If {  $wrejkwe$ ($rw$) trwer tewwerl }
            %       \COMMENT { 
            \State {jklrjkljfgkljlkj  kjkldfj gfdsdf }
            \State  Set fdgsdsd
            \ForAll  {$j=1$ to $N (x)$}
            \State        Call $fgsd(x)$  
            \State        Set $sfgdfgd =sfdg + fgds $ 

            \EndFor 
            \EndIf
        \EndFunction
        \end{algorithmic}
    \end{algorithm}
\end{document}

相关内容