我的列表未生成

我的列表未生成

我知道有一些“类似”的问题,但对我来说都不起作用。我使用 tufte-book.cls 模板以及“listings”包来包含代码列表。出于某种原因,“列表列表”保持为空,即使其他一切都正常。

我有一个多文件项目,但重现问题真正需要的两个文件是:

  1. main.tex(主文件)
  2. chapter1.tex(从主文件中引用)

我的精简项目(仍然存在问题)如下:

文件:main.tex

\documentclass{tufte-book} % Use the tufte-book class which in turn uses the tufte-common class

\usepackage{booktabs} % Better horizontal rules in tables

\usepackage{graphicx} % Needed to insert images into the document
\graphicspath{{graphics/}} % Sets the default location of pictures

\newcommand{\blankpage}{\newpage\hbox{}\thispagestyle{empty}\newpage} % Command to insert a blank page

\usepackage[toc,page]{appendix}

\usepackage{listings} % Required for insertion of code

\lstloadlanguages{Java} % Load Java syntax for listings, for a list of other languages supported see: ftp://ftp.tex.ac.uk/tex-archive/macros/latex/contrib/listings/listings.pdf
\lstset{language=Java, % Use Java in this example
        frame=tb, % t=top, b=bottom, l=left, r=right
        numbers=left, % Line numbers on left
        firstnumber=1, % Line numbers start with line 1
        numberstyle=\small\color{Blue}, %\tiny\color{Blue}, % Line numbers are blue and small
        stepnumber=1 % Line numbers go in steps of 1
}

% Creates a new command to include Java code, the first parameter is the filename of the code (without .java), the second parameter is the caption

\newcommand{\javacode}[2]{
\lstinputlisting[caption=#2,label=#1]{#1.java}
}

\title{Hello World} % Title of the book

\author[~]{First Last} % Author

\publisher{Good Books Press} % Publisher

\begin{document}

\frontmatter

\maketitle % Print the title page

\lstlistoflistings % Print a list of listings

\chapter{Introduction} % The asterisk leaves out this chapter from the table of contents

\section{A book}
A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog.

\mainmatter

\include{chapter1}

\backmatter

\end{document}

文件:chapter1.tex

\chapter{My Chapter 1}
\label{ch:1}

\newthought{Let's get our feet wet.} Mpla mpla.

\section{Hello World!}
\label{sec:hello_world}

A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog.

\javacode{java/HelloWorld}{Java implementation of Hello World}

Testing with a reference to Listing \ref{java/HelloWorld}.

A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog.

\begin{lstlisting}[caption=Test 2,label=test2]  % Start your code-block

Some code
More code
\end{lstlisting}

A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog.

唯一缺少的文件是 tufte-book.cls,它是标准文件。

答案1

添加以下代码(我包含了示例中的两行,以便您知道在哪里进行添加):

\usepackage{listings} % Required for insertion of code

\makeatletter
\renewcommand{\lstlistoflistings}{%
  \ifthenelse{\equal{\@tufte@class}{book}}
    {\chapter *{\lstlistlistingname}}
    {\section *{\lstlistlistingname}}%
  \@starttoc{lol}%
}
\contentsuse{lstlisting}{lol}
\let\l@lstlisting\@tufte@lof@line
\makeatother

\lstloadlanguages{Java} % Load Java syntax for listings

之后你会得到

在此处输入图片描述

相关内容