有关 lstlisting 的几个错误

有关 lstlisting 的几个错误

在我的学士论文中,我使用了lstlistinglistingspackage 附带的突出显示的代码。

不幸的是,我遇到了一些错误,但我不知道为什么会出现这些错误。尤其是因为输出结果与预期一致。在生成的 PDF 中,突出显示、标题和引用功能都运行良好。

这是我的最小工作示例:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[super, comma, numbers, square, sort]{natbib}
\usepackage{listings}
\usepackage{color}  
\usepackage[svgnames]{xcolor}
\usepackage{caption}
\definecolor{sh_comment}{rgb}{0.12, 0.38, 0.18 } %adjusted, in Eclipse: {0.25, 0.42, 0.30 } = #3F6A4D
\definecolor{sh_keyword}{rgb}{0.37, 0.08, 0.25}  % #5F1441
\definecolor{sh_string}{rgb}{0.06, 0.10, 0.98} % #101AF9
\newcommand{\lstJava}[1]{\lstinline[language=Java,breaklines=true,basicstyle= \listingsfontinline]$#1$}

\begin{document}

% define some fancy Code formatting according to the Java Eclipse formatting
\lstset {
    frame=single,
    rulesepcolor=\color{black},
    showspaces=false,showtabs=false,tabsize=2,
    numberstyle=\tiny,numbers=left,
    basicstyle= \listingsfont,
    stringstyle=\color{sh_string},
    keywordstyle = \color{sh_keyword}\bfseries,
    commentstyle=\color{sh_comment}\itshape,
    captionpos=b,
    xleftmargin= -2.0cm, xrightmargin=0.5cm,
    lineskip=-0.3em,
    escapebegin={\lstsmallmath}, escapeend={\lstsmallmathend},
    linewidth=1.25\textwidth
}

\begin{minipage}{\textwidth}
    \captionsetup{width=1.25\textwidth}
    \lstdefinelanguage{XML}
    { % adjust the formatting for XML files
      morestring=[b]",
      morestring=[s]{>}{<},
      morecomment=[s]{<?}{?>},
      stringstyle=\color{sh_string},
      identifierstyle=\color{sh_comment},
      keywordstyle=\color{sh_keyword},
      morekeywords={xmlns,version,type, servlet, display, name, xsi, web, app, class, init, param, value, schemaLocation, load, on, startup, mapping, url, pattern, id} % list custom attributes
    }
    % Here the 1st and 2nd error will appear (line 47)
    \begin{lstlisting}[language=XML, caption={xml: Definition of available GET Methods for Java}, label={Jersey}]

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:
"foo"
</web-app>
    \end{lstlisting}
\end{minipage}

Refering Listing \ref{Jersey} on Page \pageref{Jersey}. \\

Refering Listing \ref{lst:Jersey_Hello_java} on Page \pageref{lst:Jersey_Hello_java}. 

\begin{minipage}{\textwidth}
    \captionsetup{width=1.25\textwidth}
    % here the 3rd error will appear (line 63)
    \begin{lstlisting}[language=Java, caption={Hello.java: Definition der verfügbaren GET Methoden der REST-Schnittstelle mit Jersey für Java}, label={lst:Jersey_Hello_java}]
System.out.println("Hello World");
    \end{lstlisting}
\end{minipage}

Refering Listing \ref{lst:Jersey_Test_java}. Refering \ref{lst:Jersey_Test_Ausgabe} on page \pageref{lst:Jersey_Test_Ausgabe}.

\begin{minipage}{\textwidth}
    \captionsetup{width=1.25\textwidth}
    % here the 4th error will appear (line 73)
    \begin{lstlisting}[language=Java, caption={Test.java: Implementierung eines einfachen Jersey Klienten zum Abfragen der Ressource über Volltext, XML und HTML.}, label={lst:Jersey_Test_java}]
System.out.println("Hello World");
    \end{lstlisting}
\end{minipage}

\begin{minipage}{\textwidth}
    \captionsetup{width=1.25\textwidth}
    % here the last error will appear (line 81)
    \begin{lstlisting}[language=Java, caption={Konsolen-Ausgabe bei Ausführung von Test.java.}, label={lst:Jersey_Test_Ausgabe}]
Hello World
<?xml version="1.0"?><hello> Hello World</hello>
<html><title>Hello World</title><body><h1>Hello World</h1></body></html>
    \end{lstlisting}
\end{minipage}

\end{document}

编译时(如果这很重要:我正在使用编辑器 sublime text)LaTeX 会抛出错误:

There were errors in your LaTeX source

./test.tex:47: Package Listings Error: Illegal type `s'. [...ble GET Methods for Java}, label={Jersey}]]
./test.tex:47: Undefined control sequence. [...ble GET Methods for Java}, label={Jersey}]]
./test.tex:63: Undefined control sequence. [...für Java}, label={lst:Jersey_Hello_java}]]
./test.tex:73: Undefined control sequence. [... und HTML.}, label={lst:Jersey_Test_java}]]
./test.tex:81: Undefined control sequence. [...t.java.}, label={lst:Jersey_Test_Ausgabe}]]

答案1

首先,我可以确认,\def\listingsfont{\ttfamily}使用 MiKTeX 2.9 上的 pdfLaTeX 编译时没有错误,编辑器:TeXworks。

其次,您是否尝试过一个非常基本的示例?然后添加语法高亮的格式?

\documentclass{article}

\usepackage{listings}

\lstset{language=Java,
    basicstyle=\ttfamily,
    showspaces=true,
    numbers=left,
}
\begin{document}

\begin{lstlisting}[frame=single]
    System.out.println("Hello World");
\end{lstlisting}

\end{document}

相关内容