General+100 errors because of listings package

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Bliztz
Posts: 5
Joined: Mon Oct 11, 2010 7:06 pm

+100 errors because of listings package

Post by Bliztz »

First of all say hello because is my first post here, also I'd like to say sorry in advance because of my english (I'm spanish).

I'll explain my problem to see if someone here can help me with it. I need to include several code snippets from many languages in my document. For that purpose I'm using the listings package but when I try to compile the file it gives me losts of errors because of the special characters found within the code. No matter if I put the code within a lstlisting enviroment or include a complete file with lstinputlisting. How can this be solved?

Also looking through the errors the compiler gives (btw I'm using TeXnicCenter on Windows) I've also noticed the á,é,í,ó,ú,ñ characters are changed. This is probably related to an encoding issue. The files (the ones with the code) are stored using utf8 encoding, and I'm using latin1 for the latex document.

Thank you in advance for any help you could provide.
Last edited by Bliztz on Thu Oct 14, 2010 1:28 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

+100 errors because of listings package

Post by frabjous »

Well, obviously if these are utf8 characters, you need to save the file with utf8 encoding, not latin1. What version of TexnicCenter are you using? You need 2.0 for utf8.

If the errors persist, provide a minimal working example and post it here.
Bliztz
Posts: 5
Joined: Mon Oct 11, 2010 7:06 pm

+100 errors because of listings package

Post by Bliztz »

I've installed TexnicCenter 2.0 Alpha 3 and use utf8 as encoding, but still the same errors.

Leaving the enconding issue aside, what I wanna try to solve first are the listing package errors.

This document for example compiles with no errors (I included the same header as the one I'm actually using)

Code: Select all

\documentclass[a4paper, 11pt]{book}
\usepackage[spanish,es-tabla]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[nottoc, notlof, notlot]{tocbibind}
\usepackage{setspace}
\usepackage[plain*]{flexbib}
\usepackage{multirow}

\usepackage{color}
\definecolor{gray97}{gray}{.97}
\definecolor{gray75}{gray}{.75}
\definecolor{gray45}{gray}{.45}

\usepackage{listings}
\lstset{frame=Ltb,
     framerule=0pt,
     aboveskip=0.5cm,
     framextopmargin=3pt,
     framexbottommargin=3pt,
     framexleftmargin=0.4cm,
     framesep=0pt,
     rulesep=.4pt,
     backgroundcolor=\color{gray97},
     rulesepcolor=\color{black},
     stringstyle=\ttfamily,
     showstringspaces = false,
     basicstyle=\scriptsize\ttfamily,
     commentstyle=\color{gray45},
     keywordstyle=\bf,
     numbers=left,
     numbersep=15pt,
     numberstyle=\tiny,
     numberfirstline = false,
     breaklines=true,
}
\lstdefinestyle{PHP}{language=PHP,morekeywords={}}
								
\usepackage[pdftex]{graphicx}
\usepackage[breaklinks, pdfborder={0 0 0 }]{hyperref}
\usepackage[all]{hypcap}

\begin{document} 
\lstset{style=PHP}
\begin{lstlisting}[numbers=none]
<div id="main_content">
<noscript>
  <div class="alert">La página que estás viendo requiere para su funcionamiento el uso de JavaScript. Si lo ha deshabilitado de manera intencionada, por favor vuelva a activarlo.</div>
</noscript>
<div id="msg"></div>
<?php 
try {
  ob_start();
  if (isset($_GET['aplicacion'])) {
    include('include/aplicacion.php');
  } elseif (isset($_GET['turnos'])) {
  	include('include/turnos.php');
  } elseif (isset($_GET['administrar'])) {
  	include('include/administrar.php');
  } else {
  	include('include/inicio.php');
  }
}
catch (Exception $excepcion) {
  ob_clean();
  if (Usuario::esAdmin()) {
    echo '<div class="error">'.(string) $excepcion.'</div>'; 
  } else {
    echo '<div class="error">Se he producido un error durante la carga de la página, pongase en contacto con el administrador.</div>';
  }
}
ob_end_flush();
?>
</div>
\end{lstlisting} 
\end{document}
Still the editor changes the color of text after $ symbol so it seems it treats it as a special symbol instead of ignoring it. So i think this file being able to copile is still a coincidence.

All the errors are the same:
Extra ) or forgotten $
Command \ttfamily invalid in mathmode
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

+100 errors because of listings package

Post by gmedina »

Hi,
Bliztz wrote:...This document for example compiles with no errors (I included the same header as the one I'm actually using)...
That's completely useless information. Please, follow the link provided by frabjous and build a minimal working example allowing us to reproduce the errors.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Bliztz
Posts: 5
Joined: Mon Oct 11, 2010 7:06 pm

+100 errors because of listings package

Post by Bliztz »

This next file gives me 101 errors:

Code: Select all

\documentclass[a4paper, 11pt]{book}
\usepackage[spanish,es-tabla]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{color}
\definecolor{gray97}{gray}{.97}
\definecolor{gray75}{gray}{.75}
\definecolor{gray45}{gray}{.45}

\usepackage{listings}
\lstset{frame=Ltb,
     framerule=0pt,
     aboveskip=0.5cm,
     framextopmargin=3pt,
     framexbottommargin=3pt,
     framexleftmargin=0.4cm,
     framesep=0pt,
     rulesep=.4pt,
     backgroundcolor=\color{gray97},
     rulesepcolor=\color{black},
     stringstyle=\ttfamily,
     showstringspaces = false,
     basicstyle=\scriptsize\ttfamily,
     commentstyle=\color{gray45},
     keywordstyle=\bf,
     numbers=left,
     numbersep=15pt,
     numberstyle=\tiny,
     numberfirstline = false,
     breaklines=true,
}
\lstdefinestyle{PHP}{language=PHP,morekeywords={}}

\begin{document}
\lstset{style=PHP}
\begin{lstlisting}[numbers=none]
<li<?php if (isset($_GET['inicio'])) echo ' class="active"'; ?>><a href="/">Inicio</a></li>
\end{lstlisting}
Nos permitira mostrar de manera visual en que sección de la pagina nos encotramos desde el menu de navegacion

\begin{lstlisting}[numbers=none]
<li<?php if (isset($_GET['aplicacion'])) echo ' class="active"'; ?>><a href="" onclick="ventanas.abrir('/aplicacion/', 'Aplicación', 'height=715,width=960,resizable=no,menubar=no,location=no,scrollbars=no,toolbar=no,status=no'); return false;">Aplicación</a></li>
\end{lstlisting}
Abrira la pagina correspondiente a la aplicación en una nueva ventana con los parametros indicados.

\begin{lstlisting}[numbers=none]
<div id="login_button"><ul>
	<?php if (Usuario::logged()) {?>
	<li><a href="" onclick="usuario.logout(); return false;">Logout</a></li>
	<?php } else {?>
	<li><a href="" onclick="usuario.login(); return false;">Login</a></li>
	<?php }?>
</ul></div>
\end{lstlisting}
Mostrará uno u otro boton en función de si el usuario se encuentra actualmente identidicado en la pagina.\end{document}
\end{document}
i think it has something to do with lstlisting enviroment not ignoring special characters within it, treating for example $ as in math equations. The package has an option called matescape that is actually used to put math equations inside the code snippet but it is set to false by default, and even if a specify mathescape to false it gives me the same errors.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

+100 errors because of listings package

Post by gmedina »

There are two problems with your code:

1) You use latin1 as the input encoding but you said that you save your document using utf8 for your editor; that inconsistency will cause problems. If you declare utf8 as the encoding for your document (in your editor) then you should use utf8 for inputenc (see the example code below). You will always have to be consistent with the editor encoding and the one used for inputenc.

2) The listings package doesn't handle well national characters such as vowels with accents (vocales tildadas), umlauts (diéresis) and "eñes":
listings documentation wrote:Thus, if you use the a package that supports multibyte characters, such as
the CJK or ucs packages for Chinese and UTF-8 characters, you must avoid letting listings process the extended characters.
The solution is then to escape to LaTeX to process those characters; in the example below I used the vertical bar as a escape character, but you can use other character, as long as it doesn't appear in your code, of course (refer to the listings documentation for details about escaping to LaTeX).

Here's your code with the modifications I suggested (I assume that your editor is set to utf8):

Code: Select all

\documentclass[a4paper, 11pt]{book}
\usepackage[spanish,es-tabla]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{color}
\definecolor{gray97}{gray}{.97}
\definecolor{gray75}{gray}{.75}
\definecolor{gray45}{gray}{.45}

\usepackage{listings}
\lstset{frame=Ltb,
     framerule=0pt,
     aboveskip=0.5cm,
     framextopmargin=3pt,
     framexbottommargin=3pt,
     framexleftmargin=0.4cm,
     framesep=0pt,
     rulesep=.4pt,
     backgroundcolor=\color{gray97},
     rulesepcolor=\color{black},
     stringstyle=\ttfamily,
     showstringspaces = false,
     basicstyle=\scriptsize\ttfamily,
     commentstyle=\color{gray45},
     keywordstyle=\bf,
     numbers=left,
     numbersep=15pt,
     numberstyle=\tiny,
     numberfirstline = false,
     breaklines=true,
    escapeinside={||}
}
\lstdefinestyle{PHP}{language=PHP,morekeywords={}}

\begin{document}
\lstset{style=PHP}
\begin{lstlisting}[numbers=none]
<li<?php if (isset($_GET['inicio'])) echo ' class="active"'; ?>><a href="/">Inicio</a></li>
\end{lstlisting}
Nos permitirá mostrar de manera visual en qué sección de la pagina nos encotramos desde el menú de navegación

\begin{lstlisting}[numbers=none]
<li<?php if (isset($_GET['aplicacion'])) echo ' class="active"'; ?>><a href="" onclick="ventanas.abrir('/aplicacion/', 'Aplicaci|ó|n', 'height=715,width=960,resizable=no,menubar=no,location=no,scrollbars=no,toolbar=no,status=no'); return false;">Aplicaci|ó|n</a></li>
\end{lstlisting}
Abrirá la página correspondiente a la aplicación en una nueva ventana con los parámetros indicados.

\begin{lstlisting}[numbers=none]
<div id="login_button"><ul>
   <?php if (Usuario::logged()) {?>
   <li><a href="" onclick="usuario.logout(); return false;">Logout</a></li>
   <?php } else {?>
   <li><a href="" onclick="usuario.login(); return false;">Login</a></li>
   <?php }?>
</ul></div>
\end{lstlisting}
Mostrará uno u otro botón en función de si el usuario se encuentra actualmente identidicado en la página.\end{document}
\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Bliztz
Posts: 5
Joined: Mon Oct 11, 2010 7:06 pm

Re: +100 errors because of listings package

Post by Bliztz »

Thank you for your help, this solve the problem with the code I put within the lstlisting enviroment but I'm still having problems with the code I include using \lstinputlisting.

Saerching through google I've found the listingutf8 package, but I cant manage to make it works.

Is there maybe a fast way to change these files encondig before including them in the latex document using the command above so they dont give problems?

EDIT:
Many suggest the use of xetex. The thing is I really don't know ho to use it or configure TexnicCenter to work with it.
Bliztz
Posts: 5
Joined: Mon Oct 11, 2010 7:06 pm

+100 errors because of listings package

Post by Bliztz »

I make it works ^^.

In case anyone else have the same problem just use this:

Code: Select all

\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstset{
     inputencoding=utf8,
     extendedchars=false
}
TexnicCenter 2.0 and XeLaTex => PDF output profile
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

+100 errors because of listings package

Post by localghost »

Now that the problem is solved, please be so kind and mark the topic accordingly as clearly written in the Board Rules (to be read before posting). Please keep that in mind for the future so that further reminders will not be necessary.


Best regards and welcome to the board
Thorsten
Post Reply