如何更改列表标题字体和列表条目间距?

如何更改列表标题字体和列表条目间距?

我已经在目录、表格列表和图片列表中设置了一些格式,我想将它们继续显示在列表列表中(使用tocloft包)。但是,我找不到执行此操作的命令。请考虑以下示例:

\documentclass[12pt,letterpaper,twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\geometry{letterpaper,left=1.25in,top=1in,right=1in,bottom=1in,nohead}
\usepackage{listings}
\renewcommand{\lstlistingname}{Code} %change 'listing' to 'code'
%
%
\begin{document}
%
%Adjusted the font of the list of listings title
\renewcommand{\lstlistlistingname}{\centering\bfseries\normalsize{LIST OF CODE}}
%
\lstlistoflistings
\vspace{1in}
%
%test listings
\begin{lstlisting}[caption=exlist]
test code
\end{lstlisting}
\begin{lstlisting}[caption=exlist2]
test code 2
\end{lstlisting}
%
\end{document}

结果如下: 编译结果

我想做几件事。

1) 我想控制从顶部边距到 LIST OF CODE 标题的距离,类似于\setlength{\cftbeforetoctitleskip}{1in}tocloft有没有办法用 listings 包做到这一点?更新:这个距离由 控制tocloft。因此,使用\setlength{\cftbeforetoctitleskip}{\spaceBeforeChapterTitles}

2) 我想控制 LIST OF CODE 标题和第一行之间的 vskip,类似于\setlength{\cftaftertoctitleskip}{12pt}。更新:此距离也 tocloft由 控制。因此,使用。tocloft\setlength{\cftaftertoctitleskip}{\spaceBetweenTitleAndText}

3)我想控制列表条目之间的 vskip 值(即 exlist 和 exlist2 行之间跳过 12pt),就像我\setlength{\cftbeforechapskip}{12pt}在 中所做的那样tocloft

4)我想控制点之间的空间,\renewcommand{\cftdotsep}{1.5}类似于tocloft

5) 我希望来自不同章节的列表之间有一个空格,就像在图表列表或表格列表中自动完成的那样。

我想我可以使用创建一个新的代码列表tocloft,但我真的很喜欢listings我使用颜色编码脚本的方式。

先感谢您!

答案1

我浏览了各种论坛,并想出了一个可以解决所有五个问题的解决方案。请参阅下面的代码以获取注释和解释。

\documentclass[12pt,letterpaper,twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\geometry{letterpaper,left=1.25in,top=1in,right=1in,bottom=1in,nohead}



\usepackage{tocloft} %allow us to change some of the table contents, list of (whatever)
\usepackage{caption}

%Found a nice reference here: https://texblog.org/2008/07/13/define-your-own-list-of/
%Let's define a new list called List of Code
\newlistof{code}{loc}{\MakeUppercase{List of Code}}
%{id of list}{abbreviation for the list}{name of list}

%%%%%%%%%%%%% Answer to Question #1 and #2. %%%%%%%%%%%%%
%Control the spacing before and after the title with the following commands,
%analogous to how we control the spacing in toc.
\setlength{\cftbeforeloctitleskip}{1in}
\setlength{\cftafterloctitleskip}{1in}

%Format the title of the list. Center with an \hfill on either side
\renewcommand{\cftloctitlefont}{\hfill\Large\bfseries} 
\renewcommand{\cftafterloctitle}{\hfill\space}

%%%%%%%%%%%%%%%%% Answer to Question #3 %%%%%%%%%%%%%%%
%Adjust the vskip before each new list entry with the following:
\setlength{\cftbeforecodeskip}{.5\normalbaselineskip}

%%%%%%%%%%%%%%%%% Answer to Question #4 %%%%%%%%%%%%%%%
%set the distance between TOC dots (default 4.5) with the following:
\renewcommand{\cftcodedotsep}{1.5} 



%%%%%%%%%%%%%%%%% Answer to Question #5 %%%%%%%%%%%%%%%
% a quick patch so a space is added between chapters (like is done in toc, lof, lot)
%https://tex.stackexchange.com/questions/461521/how-to-create-space-between-chapter-in-list-of-listings
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@chapter}% <cmd>
  {\addtocontents}% <search>
  {\addtocontents{loc}{\protect\addvspace{10\p@}}% Add per-chapter space in LoL
   \addtocontents}% <replace>
  {}{}% <success><failure>
\makeatother
%end of patch



% Note that all the changes we've made have been separate from the listings package.
% We will be using our own caption system based on a new environment instead of 
% using a caption system for lstlistings.
% Let's now make the new environment.
\newenvironment{coding}[1]{% require one input (the caption)
\refstepcounter{code} %increase the counter. Note that we reset it later (per chapter)
%
%
% now format the environment
\par %make sure it starts on a new line
\vspace{\baselineskip} %this was previously taken care of with \lstset, but we need
% to manually add it here to have the space be above our custom caption.
\noindent\textbf{Code \thechapter.\thecode: #1} %format the caption
\addcontentsline{loc}{code} %add entry to the list of code
{\protect\numberline{\thechapter.\thecode}#1}
\par}%
{} %Nothing special needs to happen in closing the environment.


\makeatletter
\@addtoreset{code}{chapter} %reset the list of code counter at each new chapter
\renewcommand{\p@code}{\thechapter.} % Want \autoref to include the chapter number.
\makeatother


\usepackage{listings} % minted is also an option, but I wanted to stick with 
% listings because it seems to be updated more regularly.
\usepackage{color}
\usepackage{xcolor}
%New colors defined below
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
%Code listing style named "mystyle"
\lstdefinestyle{mystyle}{% define my own style. Lots of options to choose from.
  backgroundcolor=\color{backcolour},   commentstyle=\color{codegreen},
  keywordstyle=\color{magenta},
  numberstyle=\tiny\color{codegray},
  stringstyle=\color{codepurple},
  basicstyle=\footnotesize,
  breakatwhitespace=false,         
  breaklines=true,                 
  captionpos=t, %caption position t or b
  keepspaces=true,                 
  numbers=left,                    
  numbersep=5pt,                  
  showspaces=false,                
  showstringspaces=false,
  showtabs=false,                  
  tabsize=2
}
\lstset{style=mystyle} %set the new style
\lstset{%aboveskip=\baselineskip, %don't want this aboveskip because it would put a 
% space between our custom caption and the code.
belowskip=\baselineskip}


%https://tex.stackexchange.com/questions/207744/no-autoref-name-for-appendix
\usepackage{hyperref} %for referencing the code
\newcommand*{\codeautorefname}{Code}



\begin{document}


\listofcode


\chapter{The First Chapter}
I've written some sample code in \autoref{code1}. I hope you enjoy!


\begin{coding}{This is my new environment caption}
\label{code1} %Include the label for cross referencing here.

%This example would insert an entire matlab script.
%I'm commenting it out for simplicity of posting here.
% \lstinputlisting[language=Matlab,%
% % caption=SigmaExtract.m,%      Don't include the caption here.
% % label={code:SigmaExtract},%   Don't include label here.
% ]{SigmaExtract.m} 

%Instead, I'll show a simple portion of script here:
\begin{lstlisting}[language=matlab]
function TestFunction()
%%% This is a sample function.

for i = 1:10
    fprintf('i = %0.0f\n',i)
end; clear i

end
\end{lstlisting}

\end{coding}


\begin{coding}{The second coding section}
\begin{lstlisting}[language=matlab]
function TestFunction()
%%% This is another sample function.

disp('Hello World')

end
\end{lstlisting}
\end{coding}


\chapter{Even more great content}

\begin{coding}{The third great function}
\begin{lstlisting}[language=matlab]
function j = TestFunction(i)
%%% This is another sample function.

j = i+1;

end
\end{lstlisting}
\end{coding}

\end{document}

代码列表(第 1 页): 代码列表(第 1 页)

一些示例代码及演示\autoref(第 2 页) 示例代码(附参考) (第 2 页)

最后一页为完整内容(第 3 页) 最后一页为完整内容(第 3 页)

相关内容