编写算法输入部分的最佳方法

编写算法输入部分的最佳方法

我在对齐参数部分时遇到了问题。到目前为止,我得到了这样的结果:

\documentclass[a4paper,parskip=half*,twoside,numbers=noenddot]{scrbook}\usepackage{textcomp}
\usepackage{fancyhdr,graphicx,amsmath,amssymb}
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}

\begin{document}
\begin{algorithm}[h]
\SetKwInOut{getDerivable}{getFirstInfs($O$, {\normalfont \textit{I}})}
\getDerivable{compute an inference set from \textit{I} used to derive conclusions from $O$ using \textit{I} \\}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\Input{An ontology $O$ and an inference set \textit{I}}
\Output{\textit{J} $\subseteq$ \textit{I} such that $\forall$$\beta$ $O$ $\vdash_I$ $\beta$ $\Leftrightarrow$ $O$ $\vdash_J$ $\beta$        }


D $\gets$ \{$O$\} \;
\textit{J} $\gets$ \{$\emptyset$\} \;
\While{true}{
            \ForEach {${\gamma}$ $\in$ \textit{I} }{
                        \If{\normalfont{getPremises($\gamma$)} $\subseteq$ \textit{D}}{
                            \textit{C} $\gets$ getConclusion($\gamma$) \;
                            \If{\textit{C} $\notin$ \textit{D}}{
                            \textit{D} $\gets$ \textit{D} $\cup$ \{\textit{C}\} \;     
                            \textit{J} $\gets$ \textit{J} $\cup$ \{$\gamma$\} \;

                            \textbf{break} \;
                            }
                        }
}       

\textbf{break}\;            
\EndWhile}
\textbf{return} J\;
\caption{Compute inferences for derivable conclusions}
\label{alg:getDerivable}
\end{algorithm}
\end{document}

在此处输入图片描述

我想避免输入和输出的空间。下面是所需结果的示例 在此处输入图片描述

我希望有一个人可以帮助我!!

答案1

只需替换\SetKwInOut\SetKwInput

根据algorithm2e手册第 11.1 节,

\SetKwInOut{Kw}{input}作用与 相同\SetKwInput{Kw}{input}。但 ':' 的位置是固定的,由该宏定义的最长关键字设置。

\documentclass[a4paper,parskip=half*,twoside,numbers=noenddot]{scrbook}\usepackage{textcomp}
\usepackage{fancyhdr,graphicx,amsmath,amssymb}
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}

\begin{document}
\begin{algorithm}[h]
    \SetKwInput{getDerivable}{getFirstInfs($O$, {\normalfont \textit{I}})}
    \getDerivable{compute an inference set from \textit{I} used to derive conclusions from $O$ using \textit{I} \\}
    \SetKwInput{Input}{Input}
    \SetKwInput{Output}{Output}
    \Input{An ontology $O$ and an inference set \textit{I}}
    \Output{\textit{J} $\subseteq$ \textit{I} such that $\forall$$\beta$ $O$ $\vdash_I$ $\beta$ $\Leftrightarrow$ $O$ $\vdash_J$ $\beta$        }


    D $\gets$ \{$O$\} \;
    \textit{J} $\gets$ \{$\emptyset$\} \;
    \While{true}{
        \ForEach {${\gamma}$ $\in$ \textit{I} }{
            \If{\normalfont{getPremises($\gamma$)} $\subseteq$ \textit{D}}{
                \textit{C} $\gets$ getConclusion($\gamma$) \;
                \If{\textit{C} $\notin$ \textit{D}}{
                    \textit{D} $\gets$ \textit{D} $\cup$ \{\textit{C}\} \;
                    \textit{J} $\gets$ \textit{J} $\cup$ \{$\gamma$\} \;

                    \textbf{break} \;
                }
            }
        }
        \textbf{break}\;
        %\EndWhile
        }
    \textbf{return} J\;
    \caption{Compute inferences for derivable conclusions}
    \label{alg:getDerivable}
\end{algorithm}
\end{document}

在此处输入图片描述

:或者,如果您想实现输入和输出后的对齐,请\ResetInOut{Output}在第一个之后使用\SetKwInOut将默认值设置InOut为获得的默认值Output(因为输出比输入长)。

\documentclass[a4paper,parskip=half*,twoside,numbers=noenddot]{scrbook}\usepackage{textcomp}
\usepackage{fancyhdr,graphicx,amsmath,amssymb}
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}

\begin{document}
\begin{algorithm}[h]
    \SetKwInOut{getDerivable}{getFirstInfs($O$, {\normalfont \textit{I}})}
    \getDerivable{compute an inference set from \textit{I} used to derive conclusions from $O$ using \textit{I} \\}
    \ResetInOut{Output}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{An ontology $O$ and an inference set \textit{I}}
    \Output{\textit{J} $\subseteq$ \textit{I} such that $\forall$$\beta$ $O$ $\vdash_I$ $\beta$ $\Leftrightarrow$ $O$ $\vdash_J$ $\beta$        }


    D $\gets$ \{$O$\} \;
    \textit{J} $\gets$ \{$\emptyset$\} \;
    \While{true}{
        \ForEach {${\gamma}$ $\in$ \textit{I} }{
            \If{\normalfont{getPremises($\gamma$)} $\subseteq$ \textit{D}}{
                \textit{C} $\gets$ getConclusion($\gamma$) \;
                \If{\textit{C} $\notin$ \textit{D}}{
                    \textit{D} $\gets$ \textit{D} $\cup$ \{\textit{C}\} \;
                    \textit{J} $\gets$ \textit{J} $\cup$ \{$\gamma$\} \;

                    \textbf{break} \;
                }
            }
        }
        \textbf{break}\;
        %\EndWhile
        }
    \textbf{return} J\;
    \caption{Compute inferences for derivable conclusions}
    \label{alg:getDerivable}
\end{algorithm}
\end{document}

在此处输入图片描述

相关内容