我想在 lstlistoflistings 中的每个 lstlistingname 后面添加一个冒号“:”。
目前它向我显示“代码 1 StatusCalculator 初始化.....”但我希望在 lstlistingname 后面有一个冒号“代码 1:StatusCalculator 初始化...”
我无法运行它。有人能帮助我吗?
\documentclass[12pt, a4paper, oneside]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage[ngerman]{babel}
\usepackage[hidelinks=true]{hyperref}
\usepackage[final]{pdfpages}
\definecolor{grey}{gray}{0.9}
\usepackage{listings}
\renewcommand\lstlistingname{Code}
\lstloadlanguages{Ruby}
\lstset{%
basicstyle=\small\ttfamily\color{black},
commentstyle = \ttfamily\color{red},
keywordstyle=\ttfamily\color{blue},
stringstyle=\color{orange},
backgroundcolor=\color{grey},
columns=fullflexible}
\usepackage{color}
\usepackage{xcolor}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\let\oldlstlistoflistings\lstlistoflistings
\renewcommand{\lstlistoflistings}{%
\begingroup%
\let\oldnumberline\numberline%
\renewcommand{\numberline}{\lstlistingname~\oldnumberline}%
\oldlstlistoflistings%
\endgroup}
\usepackage{geometry}
\geometry{a4paper, top=25mm, left=25mm, right=25mm, bottom=20mm, headsep=10mm, footskip=12mm}
\begin{document}
\renewcommand\lstlistlistingname{Codelisting}
\addcontentsline{toc}{section}{Codelisting}
\lstlistoflistings
\newpage
\begin{lstlisting}[language=Ruby, caption=StatusCalculator Initialize, label=initialize]
def initialize(project_id)
@project = Project.find(project_id)
end
\end{lstlisting}
\end{document}
答案1
为了达到你想要的,诀窍
\let\oldlstlistoflistings\lstlistoflistings
\renewcommand{\lstlistoflistings}{%
\begingroup%
\let\oldnumberline\numberline%
\renewcommand{\numberline}{\lstlistingname~\oldnumberline}%
\oldlstlistoflistings%
\endgroup}
是不足够的...
您必须加载该包tocloft
并使用它。
首先,您必须将列表注册到tocloft
,如本说明中所述回答代码中的 egreg
\makeatletter
\begingroup\let\newcounter\@gobble\let\setcounter\@gobbletwo
\globaldefs\@ne \let\c@loldepth\@ne
\newlistof{listings}{lol}{\lstlistlistingname}
\endgroup
\let\l@lstlisting\l@listings
\makeatother
然后,使用一些tocloft
命令:
\setlength{\cftlistingsindent}{1.5em}
\renewcommand*{\cftlistingspresnum}{\lstlistingname~}
\settowidth{\cftlistingsnumwidth}{\cftlistingspresnum}
\addtolength{\cftlistingsnumwidth}{2.3em}
\renewcommand{\cftlistingsaftersnum}{:}
最后,删除前面提到的代码,也就是修改你的 MWE 为
\documentclass[12pt, a4paper, oneside]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{tocloft}
\usepackage[ngerman]{babel}
\usepackage[hidelinks=true]{hyperref}
\usepackage[final]{pdfpages}
\definecolor{grey}{gray}{0.9}
\usepackage{listings}
\renewcommand\lstlistingname{Code}
\lstloadlanguages{Ruby}
\lstset{%
basicstyle=\small\ttfamily\color{black},
commentstyle = \ttfamily\color{red},
keywordstyle=\ttfamily\color{blue},
stringstyle=\color{orange},
backgroundcolor=\color{grey},
columns=fullflexible}
\usepackage{color}
\usepackage{xcolor}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\makeatletter
\begingroup\let\newcounter\@gobble\let\setcounter\@gobbletwo
\globaldefs\@ne \let\c@loldepth\@ne
\newlistof{listings}{lol}{\lstlistlistingname}
\endgroup
\let\l@lstlisting\l@listings
\makeatother
\setlength{\cftlistingsindent}{1.5em}
\renewcommand*{\cftlistingspresnum}{\lstlistingname~}
\settowidth{\cftlistingsnumwidth}{\cftlistingspresnum}
\addtolength{\cftlistingsnumwidth}{2.3em}
\renewcommand{\cftlistingsaftersnum}{:}
\usepackage{geometry}
\geometry{a4paper, top=25mm, left=25mm, right=25mm, bottom=20mm, headsep=10mm, footskip=12mm}
\begin{document}
\renewcommand\lstlistlistingname{Codelisting}
\addcontentsline{toc}{section}{Codelisting}
\lstlistoflistings
\newpage
\begin{lstlisting}[language=Ruby, caption=StatusCalculator Initialize, label=initialize]
def initialize(project_id)
@project = Project.find(project_id)
end
\end{lstlisting}
\end{document}
你会得到