使用 caption 包设置列表标题的样式

使用 caption 包设置列表标题的样式

我想以特定的方式设置标题样式,但似乎无法找到正确的设置。

首先,我想在使用时将标签从“列表”更改为“算法”\captionof{lstlisting}{foo}

其次,我想将“算法 1”部分设为粗体,其余标签部分则不设为粗体

第三,我希望在标题上方有一行。

这是与我想要实现的目标类似的图像,以下是我的代码。

在此处输入图片描述

这就是我所拥有的:

在此处输入图片描述

\documentclass{article}

\usepackage{listings}
\usepackage{caption}
\usepackage{geometry}
\usepackage{DejaVuSans}
\usepackage{DejaVuSansMono}
\renewcommand*\familydefault{\ttdefault}

\geometry{left=1.0in,right=1.0in,top=1.0in,bottom=1.0in }

\lstset{
  frame=top,frame=bottom,
  basicstyle=\small\normalfont\sffamily,    % the size of the fonts that are used for the code
  stepnumber=1,                           % the step between two line-numbers. If it is 1 each line will be numbered
  numbersep=10pt,                         % how far the line-numbers are from the code
  tabsize=2,                              % tab size in blank spaces
  extendedchars=true,                     %
  breaklines=true,                        % sets automatic line breaking
  captionpos=t,                           % sets the caption-position to top
  mathescape=true,
  stringstyle=\color{white}\ttfamily, % Farbe der String
  showspaces=false,           % Leerzeichen anzeigen ?
  showtabs=false,             % Tabs anzeigen ?
  xleftmargin=17pt,
  framexleftmargin=17pt,
  framexrightmargin=17pt,
  framexbottommargin=5pt,
  framextopmargin=5pt,
  showstringspaces=false      % Leerzeichen in Strings anzeigen ?
 }

\DeclareCaptionFormat{listing}{#1#2#3}
\captionsetup[lstlisting]{format=listing,singlelinecheck=false, margin=0pt, font={sf}}

\DeclareMathVersion{sans}
\SetSymbolFont{operators}{sans}{OT1}{cmbr}{m}{n}
\SetSymbolFont{letters}  {sans}{OML}{cmbrm}{m}{it}
\SetSymbolFont{symbols}  {sans}{OMS}{cmbrs}{m}{n}

\lstnewenvironment{sflisting}[1][]
  {\lstset{#1}\mathversion{sans}}{}


\begin{document}

\begin{sflisting}[caption=Spanning tree broadcast algorithm.]
Initially $\langle M\rangle$ is in transit from $p_{r}$ to all its children in the spanning tree.

Code for $p_{r}$:
1:  upon receiving no message:                                          // first computation event by $p_{r}$
2:      terminate

Code for $p_{i},\ 0\leq i\leq n-1,\ i\neq r$:
3:  upon receiving $\langle M\rangle$ from parent:
4:      send $\langle M\rangle$ to all children
5:      terminate
\end{sflisting}
\end{document}

答案1

你可以使用以下方式生成顶级规则

\DeclareCaptionFormat{listing}{\rule{\dimexpr\textwidth+17pt\relax}{0.4pt}\vskip1pt#1#2#3}

附加内容17pt来自您所使用的设置xleftmargin

对于格式的其他部分,您可以添加

labelsep=space,labelfont=bf

您已经声明的选项\captionsetup

通过重新定义,可以将名称从“列表”更改为“算法” \lstlistingname

\renewcommand\lstlistingname{Algorithm}

完整示例:

\documentclass{article}
\usepackage{listings}
\usepackage{caption}
\usepackage{geometry}
\usepackage{DejaVuSans}
\usepackage{DejaVuSansMono}
\renewcommand*\familydefault{\ttdefault}

\geometry{left=1.0in,right=1.0in,top=1.0in,bottom=1.0in }

\lstset{
  frame=top,frame=bottom,
  basicstyle=\small\normalfont\sffamily,    % the size of the fonts that are used for the code
  stepnumber=1,                           % the step between two line-numbers. If it is 1 each line will be numbered
  numbersep=10pt,                         % how far the line-numbers are from the code
  tabsize=2,                              % tab size in blank spaces
  extendedchars=true,                     %
  breaklines=true,                        % sets automatic line breaking
  captionpos=t,                           % sets the caption-position to top
  mathescape=true,
  stringstyle=\color{white}\ttfamily, % Farbe der String
  showspaces=false,           % Leerzeichen anzeigen ?
  showtabs=false,             % Tabs anzeigen ?
  xleftmargin=17pt,
  framexleftmargin=17pt,
  framexrightmargin=17pt,
  framexbottommargin=5pt,
  framextopmargin=5pt,
  showstringspaces=false      % Leerzeichen in Strings anzeigen ?
 }

\DeclareCaptionFormat{listing}{\rule{\dimexpr\textwidth+17pt\relax}{0.4pt}\par\vskip1pt#1#2#3}
\captionsetup[lstlisting]{format=listing,singlelinecheck=false, margin=0pt, font={sf},labelsep=space,labelfont=bf}

\renewcommand\lstlistingname{Algorithm}

\DeclareMathVersion{sans}
\SetSymbolFont{operators}{sans}{OT1}{cmbr}{m}{n}
\SetSymbolFont{letters}  {sans}{OML}{cmbrm}{m}{it}
\SetSymbolFont{symbols}  {sans}{OMS}{cmbrs}{m}{n}

\lstnewenvironment{sflisting}[1][]
  {\lstset{#1}\mathversion{sans}}{}


\begin{document}

\begin{sflisting}[caption=Spanning tree broadcast algorithm.]
Initially $\langle M\rangle$ is in transit from $p_{r}$ to all its children in the spanning tree.

Code for $p_{r}$:
1:  upon receiving no message:                                          // first computation event by $p_{r}$
2:      terminate

Code for $p_{i},\ 0\leq i\leq n-1,\ i\neq r$:
3:  upon receiving $\langle M\rangle$ from parent:
4:      send $\langle M\rangle$ to all children
5:      terminate
\end{sflisting}

\end{document}

在此处输入图片描述

相关内容