我有以下代码,
\documentclass[]{memoir}
\usepackage[utf8x]{inputenc}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{algorithmic}
\newcommand{\algorithmname}{Algorithm}
\newcommand{\listalgorithmname}{List of Algorithms}
\newlistof{listofalgorithms}{loa}{\listalgorithmname}
\newfloat{algorithm}{loa}{\algorithmname}
\newfixedcaption{\falgcaption}{algorithm}
\newlistentry{algorithm}{loa}{0}
\usepackage{float}
\floatstyle{ruled}
\restylefloat{algorithm}
\begin{document}
\tableofcontents
\newpage
\listoffigures
\listoftables
\listofalgorithms
\newpage
\chapter{Example Chapter}
\section{Example Section}
\begin{algorithm}[htbp]
\caption{An example algorithm}
\label{alg1}
\begin{algorithmic}
\REQUIRE Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus.
\ENSURE Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus.
\STATE Statement 1: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus. \COMMENT{This is a comment}
\REPEAT
\STATE Statement 2: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus.
\STATE Statement 3: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus.
\FOR{$i=0$ to $k$}
\STATE Statement 4: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus.
\STATE Statement 5: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus.
\ENDFOR
\UNTIL{Lorem ipsum dolor sit amet}
\STATE Statement 6: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor cursus, felis lectus.
\end{algorithmic}
\end{algorithm}
\end{document}
排版如下,
由于我的伪代码语句相对较长,我希望该\STATE
部分(语句 1......语句 6)与各部分一样具有轻微的悬挂缩进\REQUIRE
。
答案1
这与类别无关memoir
,仅与algormithmic
包有关。
控制打印命令的内部命令\STATE
是\ALC@it
。此命令在每个algorithmic
环境中都是本地定义的。\STATE
只是\item
列表中的 ,但进行了一些调整。要对此命令进行的更改是\parshape
在调用 之后立即添加\item
。在这种情况下,适当的\parshape
是
\parshape 2 \dimexpr\ALC@tlm+\labelwidth\relax
\dimexpr\textwidth-\ALC@tlm-\labelwidth\relax
\dimexpr\ALC@tlm+\labelwidth+\continueindent\relax
\dimexpr\textwidth-\ALC@tlm-\labelwidth-\continueindent\relax
\ALC@tlm
本质上保存了当前的左缩进,但需要通过 进行更正\labelwidth
。该命令\parshape n a_1 b_1 a_2 b_2 ...
意味着第一行将具有左边距a_1
和长度b_1
等。n
th 规范适用于段落中剩余的每一行。因此,要使右边距齐平,b_k
必须是。我在下面的代码中\textwidth - a_k
引入了一个新的长度;它最初设置为,但您可能希望选择不同的值。\continueindent
\algorithmicindent
理想情况下,人们只想修补algorithmic
环境的定义,但不幸的是etoolbox
报告称它无法做到这一点。因此,下面的代码从 复制了 (长) 定义algorithmic.sty
,并仅修改了 的定义\ALC@it
,请参见 后的几行%% Added three lines here %%
:
\documentclass[]{memoir}
\usepackage[utf8x]{inputenc}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{algorithmic}
\newcommand{\algorithmname}{Algorithm}
\newcommand{\listalgorithmname}{List of Algorithms}
\newlistof{listofalgorithms}{loa}{\listalgorithmname}
\newfloat{algorithm}{loa}{\algorithmname}
\newfixedcaption{\falgcaption}{algorithm}
\newlistentry{algorithm}{loa}{0}
\usepackage{float}
\floatstyle{ruled}
\restylefloat{algorithm}
\makeatletter
\newlength{\continueindent}
\setlength{\continueindent}{\algorithmicindent}
\renewenvironment{algorithmic}[1][0]{
\setcounter{ALC@depth}{\@listdepth}%
\let\@listdepth\c@ALC@depth%
\let\@item\ALC@item%
\newcommand{\ALC@lno}{%
\ifthenelse{\equal{\arabic{ALC@rem}}{0}}
{{\ALC@linenosize \arabic{ALC@line}\ALC@linenodelimiter}}{}%
}
\let\@listii\@listi
\let\@listiii\@listi
\let\@listiv\@listi
\let\@listv\@listi
\let\@listvi\@listi
\let\@listvii\@listi
\newenvironment{ALC@g}{
\begin{list}{\ALC@lno}{ \itemsep\z@ \itemindent\z@
\listparindent\z@ \rightmargin\z@
\topsep\z@ \partopsep\z@ \parskip\z@\parsep\z@
\leftmargin \algorithmicindent%1em
\addtolength{\ALC@tlm}{\leftmargin}
}
}
{\end{list}}
\newcommand{\ALC@it}{%
\stepcounter{ALC@rem}%
\ifthenelse{\equal{\arabic{ALC@rem}}{#1}}{\setcounter{ALC@rem}{0}}{}%
\stepcounter{ALC@line}%
\refstepcounter{ALC@unique}%
\item\def\@currentlabel{\theALC@line}%
%% Added four lines here %%
\parshape 2 \dimexpr\ALC@tlm+\labelwidth\relax
\dimexpr\textwidth-\ALC@tlm-\labelwidth\relax
\dimexpr\ALC@tlm+\labelwidth+\continueindent\relax
\dimexpr\textwidth-\ALC@tlm-\labelwidth-\continueindent\relax
%% End of added material
}
\newcommand{\ALC@com}[1]{\ifthenelse{\equal{##1}{default}}%
{}{\ \algorithmiccomment{##1}}}
\newcommand{\REQUIRE}{\item[\algorithmicrequire]}
\newcommand{\ENSURE}{\item[\algorithmicensure]}
\newcommand{\PRINT}{\ALC@it\algorithmicprint{} \ }
\newcommand{\RETURN}{\ALC@it\algorithmicreturn{} \ }
\newcommand{\TRUE}{\algorithmictrue{}}
\newcommand{\FALSE}{\algorithmicfalse{}}
\newcommand{\AND}{\algorithmicand{} }
\newcommand{\OR}{\algorithmicor{} }
\newcommand{\XOR}{\algorithmicxor{} }
\newcommand{\NOT}{\algorithmicnot{} }
\newcommand{\TO}{\algorithmicto{} }
\newcommand{\STATE}{\ALC@it}
\newcommand{\STMT}{\ALC@it}
\newcommand{\COMMENT}[1]{\algorithmiccomment{##1}}
\newenvironment{ALC@inputs}{\begin{ALC@g}}{\end{ALC@g}}
\newenvironment{ALC@outputs}{\begin{ALC@g}}{\end{ALC@g}}
\newenvironment{ALC@globals}{\begin{ALC@g}}{\end{ALC@g}}
\newenvironment{ALC@body}{\begin{ALC@g}}{\end{ALC@g}}
\newenvironment{ALC@if}{\begin{ALC@g}}{\end{ALC@g}}
\newenvironment{ALC@for}{\begin{ALC@g}}{\end{ALC@g}}
\newenvironment{ALC@whl}{\begin{ALC@g}}{\end{ALC@g}}
\newenvironment{ALC@loop}{\begin{ALC@g}}{\end{ALC@g}}
\newenvironment{ALC@rpt}{\begin{ALC@g}}{\end{ALC@g}}
\renewcommand{\\}{\@centercr}
\newcommand{\INPUTS}[1][default]{\ALC@it\algorithmicinputs\ \ALC@com{##1}\begin{ALC@inputs}}
\newcommand{\ENDINPUTS}{\end{ALC@inputs}}
\newcommand{\OUTPUTS}[1][default]{\ALC@it\algorithmicoutputs\ \ALC@com{##1}\begin{ALC@outputs}}
\newcommand{\ENDOUTPUTS}{\end{ALC@outputs}}
\newcommand{\GLOBALS}{\ALC@it\algorithmicglobals\ }
\newcommand{\BODY}[1][default]{\ALC@it\algorithmicbody\ \ALC@com{##1}\begin{ALC@body}}
\newcommand{\ENDBODY}{\end{ALC@body}}
\newcommand{\IF}[2][default]{\ALC@it\algorithmicif\ ##2\ \algorithmicthen%
\ALC@com{##1}\begin{ALC@if}}
\newcommand{\ELSE}[1][default]{\end{ALC@if}\ALC@it\algorithmicelse%
\ALC@com{##1}\begin{ALC@if}}
\newcommand{\ELSIF}[2][default]%
{\end{ALC@if}\ALC@it\algorithmicelsif\ ##2\ \algorithmicthen%
\ALC@com{##1}\begin{ALC@if}}
\newcommand{\FOR}[2][default]{\ALC@it\algorithmicfor\ ##2\ \algorithmicdo%
\ALC@com{##1}\begin{ALC@for}}
\newcommand{\FORALL}[2][default]{\ALC@it\algorithmicforall\ ##2\ %
\algorithmicdo%
\ALC@com{##1}\begin{ALC@for}}
\newcommand{\WHILE}[2][default]{\ALC@it\algorithmicwhile\ ##2\ %
\algorithmicdo%
\ALC@com{##1}\begin{ALC@whl}}
\newcommand{\LOOP}[1][default]{\ALC@it\algorithmicloop%
\ALC@com{##1}\begin{ALC@loop}}
\newcommand{\REPEAT}[1][default]{\ALC@it\algorithmicrepeat%
\ALC@com{##1}\begin{ALC@rpt}}
\newcommand{\UNTIL}[1]{\end{ALC@rpt}\ALC@it\algorithmicuntil\ ##1}
\ifthenelse{\boolean{ALC@noend}}{
\newcommand{\ENDIF}{\end{ALC@if}}
\newcommand{\ENDFOR}{\end{ALC@for}}
\newcommand{\ENDWHILE}{\end{ALC@whl}}
\newcommand{\ENDLOOP}{\end{ALC@loop}}
}{
\newcommand{\ENDIF}{\end{ALC@if}\ALC@it\algorithmicendif}
\newcommand{\ENDFOR}{\end{ALC@for}\ALC@it\algorithmicendfor}
\newcommand{\ENDWHILE}{\end{ALC@whl}\ALC@it\algorithmicendwhile}
\newcommand{\ENDLOOP}{\end{ALC@loop}\ALC@it\algorithmicendloop}
}
\renewcommand{\@toodeep}{}
\begin{list}{\ALC@lno}{\setcounter{ALC@rem}{0}\setcounter{ALC@line}{0}%
\itemsep\z@ \itemindent\z@ \listparindent\z@%
\partopsep\z@ \parskip\z@ \parsep\z@%
\labelsep 0.5em \topsep 0.2em%
\ifthenelse{\equal{#1}{0}}
{\labelwidth 0.5em }
{\labelwidth 1.2em }
\leftmargin\labelwidth \addtolength{\leftmargin}{\labelsep}
\ALC@tlm\labelsep
}
}
{\end{list}}
\makeatother
\begin{document}
\chapter{Example Chapter}
\section{Example Section}
\begin{algorithm}[htbp]
\caption{An example algorithm}
\label{alg1}
\begin{algorithmic}
\REQUIRE Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor
cursus, felis lectus.
\ENSURE Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor
cursus, felis lectus.
\STATE Statement 1: Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor
cursus, felis lectus. \COMMENT{This is a comment}
\REPEAT
\STATE Statement 2: Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor
cursus, felis lectus.
\STATE Statement 3: Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat
quis tempor cursus, felis lectus.
\FOR{$i=0$ to $k$}
\STATE Statement 4: Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Integer lobortis luctus tincidunt. Ut commodo, erat
quis tempor cursus, felis lectus.
\STATE Statement 5: Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor
cursus, felis lectus.
\ENDFOR
\UNTIL{Lorem ipsum dolor sit amet}
\STATE Statement 6: Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Integer lobortis luctus tincidunt. Ut commodo, erat quis tempor
cursus, felis lectus.
\end{algorithmic}
\end{algorithm}
\end{document}