最近几天我遇到了一种奇怪的行为,algorithm2e
它在 IF 块后产生了一个不需要的空格。
我在这里放了一个 MWE,它表明问题确实来自我的定制,而不是来自包algorithm2e
。
我不明白我做错了什么......
奇怪的输出
平均能量损失
\documentclass{article}
\usepackage[utf8x]{inputenc}
\usepackage[longend]{algorithm2e}
\makeatletter
\newcommand\@algoVerboseBlock[3]{%
\hspace{-0.8em} \KwSty{#1} \ifhmode \\ \fi #2 \ifhmode \\ \fi \hspace{-0.8em} \KwSty{#3}%
}
\newcommand\@algoDefVerboseIf[2]{%
\renewcommand\algocf@uIf[2]%
{\If@ifthen{##1}\If@noend{\@algoVerboseBlock{#1}{##2}{#2}}}
%
\renewcommand\algocf@If[2]%
{\If@ifthen{##1}\If@noend{\@algoVerboseBlock{#1}{##2}{#2}}}
%
}
\newcommand\@algoDefVerboseElse[2]{%
\renewcommand{\algocf@Else}[1]%
{\Else@else\If@endif{\@algoVerboseBlock{#1}{##1}{#2}}}
\renewcommand{\algocf@uElse}[1]%
{\Else@else\If@endif{\@algoVerboseBlock{#1}{##1}{#2}}}
}
\makeatother
\begin{document}
\section{Default}
\begin{algorithm}[H]
\uIf{1}{if...}%
\Else{else... \\ \If{1}{if...}}%
\end{algorithm}
\section{Customization}
\makeatletter
\SetKwIF{If}{ElseIf}{Else}{Si}{Alors}{Sinon Si}{Sinon}{}
\@algoDefVerboseIf{Début Si}{Fin Si}
\@algoDefVerboseElse{Début Sinon}{Fin Sinon}
\makeatother
\begin{algorithm}[H]
\uIf{1}{if...}%
\Else{else... \\ \If{1}{if...}}%
\end{algorithm}
\end{document}
答案1
这似乎是 algorithm2e 中的一个错误,它在没有任何自定义的情况下发生,应该报告给软件包作者;上一个版本中已经修复了一个类似的错误。
一种解决方法是在有问题的块之后写入\\[-\baselineskip]
。这是您的示例,下面我给出了显示错误的最小示例。
\documentclass{article}
\usepackage[utf8x]{inputenc}
\usepackage[longend]{algorithm2e}
\makeatletter
\newcommand\@algoVerboseBlock[3]{%
\hspace{-0.8em} \KwSty{#1} \ifhmode \\ \fi #2 \ifhmode \\ \fi \hspace{-0.8em} \KwSty{#3}%
}
\newcommand\@algoDefVerboseIf[2]{%
\renewcommand\algocf@uIf[2]%
{\If@ifthen{##1}\If@noend{\@algoVerboseBlock{#1}{##2}{#2}}}
%
\renewcommand\algocf@If[2]%
{\If@ifthen{##1}\If@noend{\@algoVerboseBlock{#1}{##2}{#2}}}
%
}
\newcommand\@algoDefVerboseElse[2]{%
\renewcommand{\algocf@Else}[1]%
{\Else@else\If@endif{\@algoVerboseBlock{#1}{##1}{#2}}}
\renewcommand{\algocf@uElse}[1]%
{\Else@else\If@endif{\@algoVerboseBlock{#1}{##1}{#2}}}
}
\makeatother
\begin{document}
\section{Customization}
\makeatletter
\SetKwIF{If}{ElseIf}{Else}{Si}{Alors}{Sinon Si}{Sinon}{}
\@algoDefVerboseIf{Début Si}{Fin Si}
\@algoDefVerboseElse{Début Sinon}{Fin Sinon}
\makeatother
\begin{algorithm}[H]
\uIf{1}{if...}%
\Else{else... \\ \If{1}{if...}\\[-\baselineskip]}%
\end{algorithm}
\end{document}
\documentclass{article}
\usepackage[utf8x]{inputenc}
\usepackage[longend]{algorithm2e}
\begin{document}
\thispagestyle{empty}
\begin{algorithm}[H]
\If{1}{if...}%
\Else{else... \\ \uIf{2}{if...}}%
\end{algorithm}
\end{document}