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.

Hi I have installed the listings package in order to display my code. I am running the following code within my document:

\begin{lstlisting}
public static void main(String[] args) {
System.out.println("Hello World");
}
\end{lstlisting}

But that is giving me the following error:

! LaTeX Error: Environment lstlisting undefined.

I have mentioned \usepackage{listings} so I really dont know what the problem can be. Would anyone be kind enough to help or guide me the right way?

EDIT: Minimum working example:

\documentclass[a4paper,12pt]{book}

\usepackage{times,a4wide}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{listings}

\begin{document}

\begin{lstlisting}
public static void main(String[] args) {
System.out.println("Hello World");
}
\end{lstlisting}

\end{document}  
share|improve this question
    
Can you take a look at this question? tex.stackexchange.com/questions/107534/… –  Mario S. E. Apr 30 '13 at 0:07
1  
Welcome to TeX.sx! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  egreg Apr 30 '13 at 0:07
    
@MarioS.E. Errrm why? –  Matt9Atkins Apr 30 '13 at 0:08
    
@Matt9Atkins because I had a problem like that once, and even though I was loading the package with \usepackage I had to synchronize MiKTeX –  Mario S. E. Apr 30 '13 at 0:09
    
@Matt9Atkins Well, actually... are you using MikTeX? –  Mario S. E. Apr 30 '13 at 0:10
show 9 more comments

1 Answer 1

up vote 1 down vote accepted

Based on the information you have given us, and due to the fact that your MWE runs OK in several of our systems (and even yours when pasted into a new document) I humbly conclude that there must be a typo somewhere in your original document.

As an immediate workaround, you could use the verbatim environment:

\begin{verbatim}
public static void main(String[] args) {
System.out.println("Hello World");
}
\end{verbatim}

But as mentioned before, this code runs perfectly on my system:

\documentclass{article}

\usepackage{listings}

\begin{document}

\begin{lstlisting}
public static void main(String[] args) {
System.out.println("Hello World");
}
\end{lstlisting}

\end{document}
share|improve this answer
add comment

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.