自定义列表页码不起作用

自定义列表页码不起作用

所以我使用这个特定的 latex 模板已经有一段时间了,我不断地添加和更改它,使它成为我想要的。最近我尝试重新格式化以删除 TOC、LOF、LOT 和 LOL 中的前导点,我设法通过更新命令删除了所有前导点,\cftdot但这似乎对 LOL 不起作用。

我使用软件包\titlecontents中的命令titletoc更改了 LOL 的设置,但这导致页码与列表的其余部分不对齐。我之前曾尝试过\hfill解决\titlerule*[0.0em]{.}这个\contentspage问题,但都没有成功。

当前的工作示例正在用 进行演示\hfill

\documentclass[12pt]{article}

\usepackage{geometry}
\geometry{letterpaper}
\geometry{margin = 1.0in}
 \usepackage[parfill]{parskip}
\usepackage{setspace}
\singlespacing

\usepackage[utf8x]{inputenc}
\usepackage{microtype}
\usepackage{listings}
\usepackage{graphicx}
\usepackage[justification = centering]{caption}
\usepackage[section]{placeins}
\usepackage{float}

\usepackage[nottoc,notlof,notlot]{tocbibind}
\usepackage[titles]{tocloft}
\usepackage{titletoc}
\renewcommand{\cftsecfont}{\rmfamily\mdseries\upshape}
\renewcommand{\cftsecpagefont}{\rmfamily\mdseries\upshape}
\renewcommand{\cftdot}{}
\renewcommand\lstlistlistingname{List of Scripts}
\renewcommand\lstlistingname{Script}
\contentsuse{lstlisting}{lol}
\titlecontents{lstlisting}[1.5em]
        {\hspace{2.3em}}
        {\contentslabel{2.3em}}
        {\hspace*{-2.3em}}
        {\hfill\contentspage}

\usepackage{hyperref}
\pdfoutput=1 
\hypersetup{
    unicode=false,
    pdftoolbar=true,
    pdfmenubar=true,
    pdffitwindow=true,
    pdfstartview={FitH},
    pdftitle={Title},
    pdfauthor={Author},
    pdfsubject={Subject},
    pdfcreator={Producer},
    pdfproducer={Producer},
    pdfdisplaydoctitle=true,
    pdfnewwindow=true,
    colorlinks=true,
    linkcolor=black,
    citecolor=black,
    filecolor=black,
    urlcolor=black
}

\begin{document}

\tableofcontents
\listoffigures
\listoftables
\lstlistoflistings


\section{Here is A Section}

\begin{figure}[H]
\centering
\includegraphics[width = 2cm]{boats}
\caption{A boat}
\end{figure}

\begin{table} [H]
\begin{center}
\begin{tabular}{ c | c }
Title 1 & Title 2 \\ \hline 
Stuff & More stuff 
\end{tabular}
\caption{A Table}
\end{center}
\end{table}

\begin{lstlisting}[language = Matlab, basicstyle = \scriptsize, numberstyle = \scriptsize, caption = A Listing]
%%
clear;clc;close all;format compact;
%%
x = 5;
y = 6;
x + y = z;
disp(z);
\end{lstlisting}

\end{document}

如下例所示,列表的页码出于某种原因缩进,我还没能弄清楚。我相信这很容易,但我就是没找到原因。
LaTeX 输出示例

需要注意的是,我使用的包和修改比这多得多,但这些是最基本的,仍然可以正确编译。

答案1

tocloft仅与标准ToC、命令LoFLoT文件以及使用定义的命令和文件挂钩\newlistof

所有这些都不包括LoL在使用类似宏的传统设置的\l@...

我们listings.sty发现

\def\l@lstlisting#1#2{\@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}}

这是内容列表中行设置的相关宏。在更深的一层我们发现\@dottedtocline这是一个 LaTeX 内核宏,用作\@dotsep两个内容行点之间的分隔空间。默认值为4.5。通过将此数字增加到 5000(这真的很大),这些点很可能在这里看不见。

我不想进行\@dotsep全局更改,因此我修补了该命令(使用组来删除使用\l@lstlisting后的设置)\l@lstlisting

\makeatletter % Patching to prevent interference
\xpatchcmd{\l@lstlisting}{\@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}}{%
  \begingroup\renewcommand{\@dotsep}{5000}\@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}\endgroup}{%
}{}
\makeatother

以下是代码(titletoc相关内容已注释掉)

\documentclass[12pt]{article}

\usepackage{geometry}
\geometry{letterpaper}
\geometry{margin = 1.0in}
 \usepackage[parfill]{parskip}
\usepackage{setspace}
\singlespacing

\usepackage[utf8x]{inputenc}
\usepackage{microtype}
\usepackage[demo]{graphicx}
\usepackage[justification = centering]{caption}
\usepackage[section]{placeins}
\usepackage{float}

\usepackage[nottoc,notlof,notlot]{tocbibind}
\usepackage{listings}
\usepackage[titles]{tocloft}
\usepackage{xpatch}
%\usepackage{titletoc}
\renewcommand{\cftsecfont}{\rmfamily\mdseries\upshape}
\renewcommand{\cftsecpagefont}{\rmfamily\mdseries\upshape}
\renewcommand{\cftdotsep}{\cftnodots}

\renewcommand\lstlistlistingname{List of Scripts}
\renewcommand\lstlistingname{Script}


\makeatletter % Patching to prevent interference
\xpatchcmd{\l@lstlisting}{\@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}}{%
  \begingroup\renewcommand{\@dotsep}{5000}\@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}\endgroup}{%
}{}
\makeatother
%\contentsuse{lstlisting}{lol}
%\titlecontents{lstlisting}[1.5em]
%        {\hspace{2.3em}}
%        {\contentslabel{2.3em}}
%        {\hspace*{-2.3em}}
%        {\hfill\contentspage}

\usepackage{hyperref}
\pdfoutput=1 
\hypersetup{
    unicode=false,
    pdftoolbar=true,
    pdfmenubar=true,
    pdffitwindow=true,
    pdfstartview={FitH},
    pdftitle={Title},
    pdfauthor={Author},
    pdfsubject={Subject},
    pdfcreator={Producer},
    pdfproducer={Producer},
    pdfdisplaydoctitle=true,
    pdfnewwindow=true,
    colorlinks=true,
    linkcolor=black,
    citecolor=black,
    filecolor=black,
    urlcolor=black
}

\begin{document}

\tableofcontents
\listoffigures
\listoftables
\lstlistoflistings


\section{Here is A Section}

\begin{figure}[H]
\centering
\includegraphics[width = 2cm]{boats}
\caption{A boat}
\end{figure}

\begin{table} [H]
\begin{center}
\begin{tabular}{ c | c }
Title 1 & Title 2 \\ \hline 
Stuff & More stuff 
\end{tabular}
\caption{A Table}
\end{center}
\end{table}

\begin{lstlisting}[language = Matlab, basicstyle = \scriptsize, numberstyle = \scriptsize, caption = A Listing]
%%
clear;clc;close all;format compact;
%%
x = 5;
y = 6;
x + y = z;
disp(z);
\end{lstlisting}

\end{document}

在此处输入图片描述

相关内容