如何在\algomargin
使用 algorithm2e 时使用负长度?我得到的不愉快的效果是垂直线和标题没有相应移动,例如:
请注意,此图像在左侧被standalone
文档类错误地剪切(请改用article
类)。
文本“算法 0.2”延伸到左边距(这是想要的),但标题和垂直线没有。我对任何允许我在左边距和右边距稍微扩展算法环境的解决方案都很满意,不一定非要使用\algomargin
。
\documentclass[varwidth]{standalone}
%\documentclass{article}
\usepackage[ruled,vlined,algosection]{algorithm2e}
\begin{document}
\begin{algorithm} normal computation \caption{normal algorithm} \end{algorithm}
\setlength{\algomargin}{-2em}
\begin{algorithm} long computation \caption{long algorithm} \end{algorithm}
\setlength{\algomargin}{2em}
\begin{algorithm} short computation \caption{short algorithm}
\end{algorithm}
\end{document}
答案1
这是一个可能的解决方案:
\documentclass{article}
\usepackage[ruled,vlined,algosection]{algorithm2e}
\newlength\marincrease
\makeatletter
\newenvironment{Walgo}[2][htbp]
{\renewcommand{\@algocf@start}{%
\setlength\marincrease{#2}
\@algoskip%
\begin{lrbox}{\algocf@algobox}%
\begin{minipage}{\dimexpr\textwidth+2\marincrease\relax}
\setlength{\algowidth}{\hsize}%
\vbox\bgroup% save all the algo in a box
\hbox to\algowidth\bgroup\hbox to \algomargin{\hfill}\vtop\bgroup%
\ifthenelse{\boolean{algocf@slide}}{\parskip 0.5ex\color{black}}{}%
% initialization
\addtolength{\hsize}{-1.5\algomargin}%
\let\@mathsemicolon=\;\def\;{\ifmmode\@mathsemicolon\else\@endalgoln\fi}%
\raggedright\AlFnt{}%
\ifthenelse{\boolean{algocf@slide}}{\IncMargin{\skipalgocfslide}}{}%
\@algoinsideskip%
% \let\@emathdisplay=\]\def\]{\algocf@endline\@emathdisplay\nl}%
}%
\renewcommand{\@algocf@finish}{%
\@algoinsideskip%
\egroup%end of vtop which contain all the text
\hfill\egroup%end of hbox wich contains [margin][vtop]
\ifthenelse{\boolean{algocf@slide}}{\DecMargin{\skipalgocfslide}}{}%
%
\egroup%end of main vbox
\end{minipage}
\end{lrbox}%
\makebox[\linewidth][c]{\algocf@makethealgo}% print the algo
\@algoskip%
% restore dimension and macros
\setlength{\hsize}{\algowidth}%
\lineskip\normallineskip\setlength{\skiptotal}{\@defaultskiptotal}%
\let\;=\@mathsemicolon%
\let\]=\@emathdisplay%
}%
\begin{algorithm}[#1]}
{\end{algorithm}}
\makeatother
\begin{document}
\section{Test Section}
\begin{Walgo}[ht]{20pt}
\While{not at end of this document}
{
read current\;
\eIf{understand}
{current section becomes this one\;}
{go back to the beginning of current section and add osme more text to see the change in the margins\;}
}
\caption{How to write algorithms and some more text to see the effect of the change in the margins}
\end{Walgo}
\begin{algorithm}[ht]
\While{not at end of this document}
{
read current\;
\eIf{understand}
{current section becomes this one\;}
{go back to the beginning of current section and add osme more text to see the change in the margins\;}
}
\caption{How to write algorithms and some more text to see the effect of the change in the margins}
\end{algorithm}
\begin{Walgo}[!ht]{2cm}
\While{not at end of this document}
{
read current\;
\eIf{understand}
{current section becomes this one\;}
{go back to the beginning of current section and add osme more text to see the change in the margins\;}
}
\caption{How to write algorithms and some more text to see the effect of the change in the margins}
\end{Walgo}
\end{document}
该algorithm2e
包将算法存储在里面lrbox
\algocf@makethealgo
,然后对其进行排版;我定义了一个Walgo
使用minipage
里面的新环境lrbox
;的宽度minipage
为\textwidth+2\marginincrease
;然后,\algocf@makethealgo
将放置在里面居中\makebox
以对其进行排版。
环境Walgo
有一个可选参数(位置说明符)和一个强制参数(用于提供\marginincrease
增加边距的长度)。