我使用该algorithm2e
包来排版带有自定义关键字的算法以供输入和输出。
使用时\SetKwInput{KwInput}{Input}
定义关键字,会添加冒号,但输入和输出不对齐。
使用时\SetKwInOut{KwInput}{Input}
文本是对齐的。但是,冒号在右侧,因此会出现像“输入:”这样难看的空格,让人眼花缭乱。
我的问题是:如何才能获得输出(如下图所示)而无需\hspace
在每个algorithm
环境中进行摆弄?
以下是 MWE:
\documentclass[a4paper]{article}
\usepackage[ruled]{algorithm2e}
% Define keywords
\SetKwInput{KwInput}{Input}
\SetKwInput{KwOutput}{Output}
% This aligns the colons
%\SetKwInOut{KwInput}{Input}
%\SetKwInOut{KwOutput}{Output}
\begin{document}
\begin{algorithm}[h]
\KwInput{Network}
\KwOutput{Flow}
\begin{enumerate}
\item Start with $\ldots$
\end{enumerate}
\caption{Some algorithm.}
\end{algorithm}
\end{document}
答案1
\documentclass[a4paper]{article}
\usepackage[ruled]{algorithm2e}
\usepackage{etoolbox}
\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}{\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}{\wd\algocf@inoutbox}%
\fi%
\fi% the dimension of the box is now defined.
\algocf@newcommand{#1}[1]{%
\ifthenelse{\boolean{algocf@inoutnumbered}}{\relax}{\everypar={\relax}}%
% {\let\\\algocf@newinout\hangindent=\wd\algocf@inoutbox\hangafter=1\parbox[t]{\inoutsize}{\KwSty{#2}\algocf@typo\hfill:}~##1\par}%
{\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
}}%
\makeatother
% Define keywords
%\SetKwInput{KwInput}{Input}
%\SetKwInput{KwOutput}{Output}
% This aligns the colons
\SetKwInOut{KwInput}{Input}
\SetKwInOut{KwOutput}{Output}
\begin{document}
\begin{algorithm}[h]
\KwInput{Network}
\KwOutput{Flow}
\begin{enumerate}
\item Start with $\ldots$
\end{enumerate}
\caption{Some algorithm.}
\end{algorithm}
\end{document}
答案2
如果您坚持使用单行\KwInput
,则可以使用以下命令:
\documentclass[a4paper]{article}
\usepackage[ruled]{algorithm2e}
\usepackage{calc}
% Define keywords
\SetKwInput{KwInput}{Input}
\SetKwInput{KwOutput}{Output}
\let\oldKwInput\KwInput
\renewcommand{\KwInput}[1]{%
\makebox[\widthof{\KwOutput{}}][l]{\oldKwInput{}#1}%
}
\begin{document}
\begin{algorithm}[h]
\KwInput{Network}
\KwOutput{Flow}
\begin{enumerate}
\item Start with $\ldots$
\end{enumerate}
\caption{Some algorithm.}
\end{algorithm}
\end{document}
\KwInput
通过将 放置Input:
在与 宽度匹配的框内,重新定义上述代码Output:
。
答案3
这是一种不寻常的实现方法......使用tabular
。
\documentclass[a4paper]{article}
\usepackage[ruled]{algorithm2e}
% Define keywords
\SetKwInput{KwInput}{Input}
\SetKwInput{KwOutput}{Output}
% This aligns the colons
%\SetKwInOut{KwInput}{Input}
%\SetKwInOut{KwOutput}{Output}
\begin{document}
\begin{algorithm}[h]
\tabcolsep=0pt
\begin{tabular}{@{}ll}
\KwInput{}&Network\\
\KwOutput{}&Flow
\end{tabular}
\begin{enumerate}
\item Start with $\ldots$
\end{enumerate}
\caption{Some algorithm.}
\end{algorithm}
\end{document}