
我目前正在使用以下 mwe
\documentclass{article}
\usepackage{algpseudocode}
\usepackage[plain]{algorithm}
\begin{document}
\begin{algorithm}[H]
\hrule
\vspace{0.5em}
\caption{Euclide's algorithm}\label{euclid}
\begin{algorithmic}[5]
\Procedure{Euclide}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \Return $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\hrule
\end{algorithm}
\end{document}
獲得純色和直紋樣式的混合(注意hrule
)。我喜歡用規則來分隔它所引起的文本,我喜歡用簡單的方式來尊重我的文檔中的標題風格。見圖。
有沒有簡單的方法來重新定義平原環境?我嘗試使用答案這個問題
\makeatletter
\let\oldalgorithmic\algorithmic
\def\algorithmic{\@ifnextchar[\algorithmic@i \algorithmic@ii}
\def\algorithmic@i[#1]{\hrule\vspace{0.5em}\oldalgorithmic[#1]\hrule}
\def\algorithmic@ii[#1]{\hrule\vspace{0.5em}\oldalgorithmic\hrule}
\makeatother
但這並不能如預期般運作,因為第二個 hrule 仍然位於演算法的頂部。我錯過了什麼嗎?
答案1
algorithm
使用float
包裹algorithm
使用s設定環境樣式\floatstyle
。以下是plain
和 的浮動樣式ruled
:
% The 'plain' float style
\newcommand\fs@plain{\def\@fs@cfont{\rmfamily}\let\@fs@capt\floatc@plain
\def\@fs@pre{}\def\@fs@post{}%
\def\@fs@mid{\vspace\abovecaptionskip\relax}%
\let\@fs@iftopcapt\iffalse}
% The 'ruled' float style
\newcommand\fs@ruled{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
\def\@fs@pre{\hrule height.8pt depth0pt \kern2pt}%
\def\@fs@post{\kern2pt\hrule\relax}%
\def\@fs@mid{\kern2pt\hrule\kern2pt}%
\let\@fs@iftopcapt\iftrue}
每個浮動樣式定義了用於建立浮動的多個元件。考慮ruled
浮動樣式:pre
- 和post
- 組件包圍\caption
,而mid
- 組件結束浮動。
以此為基礎,我們可以創建一個新的浮動樣式,假設plainruled
它具有兩者的風格:
\documentclass{article}
\usepackage{algpseudocode,algorithm}
\makeatletter
% The 'plainruled' float style
\newcommand\fs@plainruled{\def\@fs@cfont{\rmfamily}\let\@fs@capt\floatc@plain
\def\@fs@pre{\hrule height.8pt depth0pt \kern2pt}%
\def\@fs@post{}%
\def\@fs@mid{\kern2pt\hrule height.8pt depth0pt\relax\kern\abovecaptionskip}%
\let\@fs@iftopcapt\iffalse}
\makeatother
\floatstyle{plainruled}
\restylefloat{algorithm}
\begin{document}
\begin{algorithm}[H]
\caption{Euclide's algorithm}\label{euclid}
\begin{algorithmic}[5]
\Procedure{Euclide}{$a,b$}\Comment{The g.c.d.\ of~$a$ and~$b$}
\State $r \gets a \bmod b$
\While{$r \neq 0$}\Comment{We have the answer if~$r$ is~$0$}
\State $a \gets b$
\State $b \gets r$
\State $r \gets a \bmod b$
\EndWhile\label{euclidendwhile}
\State \Return $b$\Comment{The g.c.d.\ is~$b$}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}