我尝试修改我的算法标题以使其填充灰色。
然而,还留有一些空白。
有人知道问题是什么吗?
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{algorithm}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{algpseudocode}
\DeclareCaptionFormat{algo}{\colorbox{lightgray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[algorithm]{format=algo}
\begin{document}
\begin{algorithm}
\caption{Dummy Caption}
\footnotesize
\begin{algorithmic}
\Require $a$
\Ensure $b$
\State $b \gets a$
\end{algorithmic}
\end{algorithm}
\end{document}
提前致谢!
答案1
两个问题:
顶部有较大的空间是因为您的盒子太宽,无法排成一行。如果您使用
\parbox
,\textwidth
则没有空间容纳\fboxsep
彩色盒子添加的另外 2 个。一旦修复,上下就只有一个很小的间隙。要消除这个间隙,您可以使用以下快速技巧:
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{algorithm}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{algpseudocode}
\DeclareCaptionFormat{algo}{\colorbox{lightgray}{\parbox{\dimexpr\textwidth-2\fboxsep}{#1#2#3}}}
\captionsetup[algorithm]{format=algo}
\makeatletter
\renewcommand\fs@ruled{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
\def\@fs@pre{\hrule height.8pt depth0pt \kern2pt \vskip-2pt}%
\def\@fs@post{\kern2pt\hrule\relax}%
\def\@fs@mid{\vskip-2pt \kern2pt\hrule\kern2pt}%
\let\@fs@iftopcapt\iftrue}
\makeatother
\begin{document}
\begin{algorithm}
\caption{Dummy Caption}
\footnotesize
\begin{algorithmic}
\Require $a$
\Ensure $b$
\State $b \gets a$
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\caption{Dummy Caption Dummy Caption Dummy Caption Dummy Caption Dummy Caption Dummy Caption Dummy Caption Dummy Caption Dummy Caption Dummy Caption Dummy Caption Dummy Caption Dummy Caption Dummy Caption }
\footnotesize
\begin{algorithmic}
\Require $a$
\Ensure $b$
\State $b \gets a$
\end{algorithmic}
\end{algorithm}
\end{document}