![如何删除 algorithm2e 中的多余空间](https://linux22.com/image/378772/%E5%A6%82%E4%BD%95%E5%88%A0%E9%99%A4%20algorithm2e%20%E4%B8%AD%E7%9A%84%E5%A4%9A%E4%BD%99%E7%A9%BA%E9%97%B4.png)
我正在使用algorithm2e
包来编写算法。我需要定义描述为多行的函数。仅在第一个函数中,algorithm2e
在第二行、第三行等行中添加额外空格。但是从第二个函数开始,它并没有添加我想要的额外空间。我需要删除第一个函数行中添加的额外空间(第一行之后的所有行)。脚本在这里。我使用 ACM 会议模板,我需要将算法分为两列,因为它很长(不像这个小样本)。
脚本如下:
\documentclass[sigconf]{acmart}
\pagestyle{empty}
\usepackage{amsmath}
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{multicol}
\begin{document}
\title{Title Here}
\begin{abstract}
Abstract here.
\end{abstract}
\maketitle
\section{Algorithm}
\begin{algorithm*}
\begin{multicols}{2}
\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}\SetKwInOut{Functions}{Functions}
\SetKwFunction{Query}{Query}\SetKwFunction{Calc}{Calc}
\Functions
{
\Query{$a,b$}: Queries a and b and returns the answer as c. This function name is Query.\;
\Calc{$a,b$}: Calculates a and b and returns the answer as c. This function name is Calc.\;
}
\caption{The algorithm}
\label{alg:query-verify}
\end{multicols}
\end{algorithm*}
\end{document}
答案1
此解决方案改编自https://tex.stackexchange.com/a/335862/90297:
基本上更新 \SetKWInOut 命令以便缩进大小为 0em。
\documentclass[sigconf]{acmart}
\pagestyle{empty}
\usepackage{amsmath}
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{multicol}
\makeatletter
\renewcommand{\SetKwInOut}[2]{%
\sbox\algocf@inoutbox{\KwSty{#2\algocf@typo:}}%
\expandafter\ifx\csname InOutSizeDefined\endcsname\relax% if first time used
\newcommand\InOutSizeDefined{}\setlength{\inoutsize}{\wd\algocf@inoutbox}%
\sbox\algocf@inoutbox{\parbox[t]{\inoutsize}{\KwSty{#2\algocf@typo\hfill:}}~}\setlength{\inoutindent}{0em}%{\wd\algocf@inoutbox}% <------------
\else% else keep the larger dimension
\ifdim\wd\algocf@inoutbox>\inoutsize%
\setlength{\inoutsize}{\wd\algocf@inoutbox}%
\sbox\algocf@inoutbox{\parbox[t]{\inoutsize}{\KwSty{#2\algocf@typo\hfill:}}~}\setlength{\inoutindent}{0em}%{\wd\algocf@inoutbox}% <------------------
\fi%
\fi% the dimension of the box is now defined.
\algocf@newcommand{#1}[1]{%
\ifthenelse{\boolean{algocf@hanginginout}}{\relax}{\algocf@seteveryparhanging{\relax}}%
\ifthenelse{\boolean{algocf@inoutnumbered}}{\relax}{\algocf@seteveryparnl{\relax}}%
{\let\\\algocf@newinout\hangindent=\inoutindent\hangafter=1\parbox[t]{\inoutsize}{\KwSty{#2\algocf@typo\hfill:}}~##1\par}
\algocf@linesnumbered% reset the numbering of the lines
\ifthenelse{\boolean{algocf@hanginginout}}{\relax}{\algocf@reseteveryparhanging}%
}}%
\makeatother
\begin{document}
\title{Title Here}
\begin{abstract}
Abstract here.
\end{abstract}
\maketitle
\section{Algorithm}
\begin{algorithm*}
\begin{multicols}{2}
\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}\SetKwInOut{Functions}{Functions}
\SetKwFunction{Query}{Query}\SetKwFunction{Calc}{Calc}
\Functions
{
\Query{$a,b$}: Queries a and b and returns the answer as c. This function name is Query.\;
\Calc{$a,b$}: Calculates a and b and returns the answer as c. This function name is Calc.\;
}
\caption{The algorithm}
\label{alg:query-verify}
\end{multicols}
\end{algorithm*}
\end{document}