我用它\lstlistoflistings
来创建列表。除了格式问题外,一切都生成得很好。我试图修复它,但没有足够的知识。
格式问题如下
答案1
您可能正在加载tocloft
和更改相对于图形的数字空间;因此,只需告诉 TeX 将列表标题视为图形即可。
\documentclass{article}
\usepackage{listings,chngcntr,tocloft}
\AtBeginDocument{%
\counterwithin*{lstlisting}{section}
\counterwithin*{lstlisting}{subsection}
\counterwithin*{lstlisting}{subsubsection}
\renewcommand{\thelstlisting}{%
\ifnum\value{subsection}=0
\thesection.\arabic{lstlisting}%
\else
\ifnum\value{subsubsection}=0
\thesubsection.\arabic{lstlisting}%
\else
\thesubsubsection.\arabic{lstlisting}%
\fi
\fi
}%
}
\addtolength{\cftfignumwidth}{2em}
\makeatletter
\let\l@lstlisting\l@figure
\makeatother
\begin{document}
\lstlistoflistings
\section{A section}
\begin{lstlisting}[caption=Caption]
\caption{section.lstlisting}
\end{lstlisting}
\subsection{A subsection}
\begin{lstlisting}[caption=Caption]
\caption{subsection.lstlisting}
\end{lstlisting}
\begin{lstlisting}[caption=Caption]
\caption{subsection.lstlisting}
\end{lstlisting}
\subsubsection{A subsubsection}
\begin{lstlisting}[caption=Caption]
\caption{subsubsection.lstlisting}
\end{lstlisting}
\subsection{A subsection}
\begin{lstlisting}[caption=Caption]
\caption{subsection.lstlisting}
\end{lstlisting}
\section{A section}
\begin{lstlisting}[caption=Caption]
\caption{section.lstlisting}
\end{lstlisting}
\end{document}
答案2
listings
定义\l@lstlisting
为
\def\l@lstlisting#1#2{\@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}}
上述宏确定每个列表的 ToC 相关条目的格式。第三个参数 ( 2.3em
) 用于分配 内编号的宽度\lstlistoflistings
。您可以将其修改为所需的长度,如下所示(例如3.5em
):
\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings}
\AtBeginDocument{\renewcommand{\thelstlisting}{\thesubsubsection.\arabic{lstlisting}}}
\makeatletter
\def\l@lstlisting#1#2{\@dottedtocline{1}{1.5em}{3.5em}{#1}{#2}}
\makeatother
\begin{document}
\tableofcontents
\lstlistoflistings
\section{A section}
\subsection{A subsection}
\subsubsection{A subsubsection}
\begin{lstlisting}[caption={First listing}]
A
\end{lstlisting}
\end{document}