Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In my Bachelor Thesis I'm using highlighted code using lstlisting, which comes with the listingspackage.

Unfortunately I'm getting some errors and I cannot figure out why they appear. Especially because the output is as expected. Highlighting, caption and refering are working fine in the resulted pdf.

Here is my minimum working example:

\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}

When compiling (if that matters: I'm using the editor sublime text) LaTeX will throw the errors:

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}]]
share|improve this question

migrated from stackoverflow.com Jun 21 '13 at 11:20

This question came from our site for professional and enthusiast programmers.

1  
I don't get any errors with your code when I define \def\listingsfont{\ttfamily} –  cgnieder Jun 21 '13 at 11:41
    
Also \listingsfontinline should be defined. –  egreg Jun 21 '13 at 11:49
    
Thank you for your fast answers. Using cgnieders definition I got rid of the Undefined control sequence error. Unfortunately the Package Listings Error won't disappear. I also tried egreg's advice and included: \def\listingsfontinline{\ttfamily}. I guess the using of listingsfontinline is wrong. But I'm quite new to LateX and I dind't find suitable documentation about listingsfontinline –  Markus Jun 21 '13 at 12:24
    
@Markus is your TeX distribution up to date? –  cgnieder Jun 22 '13 at 16:39
    
latex --version sais: pdfTeX using libpoppler 3.141592-1.40.3-2.2 (Web2C 7.5.6) kpathsea version 3.5.6 Copyright 2007 Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX). Kpathsea is copyright 2007 Karl Berry and Olaf Weber. There is NO warranty. Redistribution (...) Compiled with libpng 1.2.46; using libpng 1.2.49 Compiled with zlib 1.2.3; using zlib 1.2.3 Compiled with libpoppler –  Markus Jun 24 '13 at 12:58

1 Answer 1

up vote 1 down vote accepted

First, I can confirm, no errors with \def\listingsfont{\ttfamily}, compiled with pdfLaTeX on MiKTeX 2.9, Editor: TeXworks.

Second, have you tried a very basic example? And then adding formatting for syntax highlighting?

\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}
share|improve this answer
    
Thank you for your answer! With your example above I do not get any errors, too. This helped me to identify the line which causes the error. When defining the highlighting for XML it comes to the line: morestring=[s]{>}{<}, As soon as I remove this line I do not get any errors. I'm new to latex and the above code isn't from me and I don't know what this line is responsible for. But when I remove this line, my highlighting still works fine. Therefore the solution for me is to just keep it out. Thank you for your help! –  Markus Jun 28 '13 at 6:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.