General ⇒ +100 errors because of listings package
+100 errors because of listings package
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.
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
+100 errors because of listings package
If the errors persist, provide a minimal working example and post it here.
+100 errors because of listings package
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}
All the errors are the same:
Extra ) or forgotten $
Command \ttfamily invalid in mathmode
+100 errors because of listings package
That's completely useless information. Please, follow the link provided by frabjous and build a minimal working example allowing us to reproduce the errors.Bliztz wrote:...This document for example compiles with no errors (I included the same header as the one I'm actually using)...
+100 errors because of listings package
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}
+100 errors because of listings package
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":
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).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.
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}
Re: +100 errors because of listings package
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.
+100 errors because of listings package
In case anyone else have the same problem just use this:
Code: Select all
\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstset{
inputencoding=utf8,
extendedchars=false
}
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
+100 errors because of listings package
Best regards and welcome to the board
Thorsten
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10