Fonts & Character SetsInserting tildes in source code files.

Information and discussion about fonts and character sets (e.g. how to use language specific characters)
Post Reply
turbomac
Posts: 34
Joined: Sat Oct 10, 2009 9:38 pm

Inserting tildes in source code files.

Post by turbomac »

Hi everyone:

I'm using listings to write some algorithms, but there're some words that use tildes. Kile gives me errors when I try to put them.

This is the code I want to put in the document:

Code: Select all

\begin{lstlisting}
if (condición){
instrucciones...
}
And this is the error that Kile gives:

Code: Select all

Package utf8x Error: MalformedUTF-8sequence if(condición)
Any suggestions?
THanks in advance.
Last edited by turbomac on Tue Jul 06, 2010 5:59 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Inserting tildes in source code files.

Post by frabjous »

I don't see any tildes anywhere in that code.

But I can recreate your problem. It seems to be some weird incompatibility with the listings package, since there is no problem if you don't use the lstlisting environment.

Two things to try, as I see it.

The first is to compile with XeLaTeX and remove the inputenc line altogether. (Use the Fontspec package to choose a font with the appropriate characters.)

Another thing you could try, if you want to keep using regular latex/pdflatex and don't need to use any unusual utf8 characters, is to save the file as Latin 1 (ISO-8859-1) encoding. (See "convert to" in the file menu of Kile.) and change the inputenc line to:

\usepackage[latin1]{inputenc}
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Inserting tildes in source code files.

Post by localghost »

If you don't write in a certain programming language, I would suggest to use either algorithm2e or algorithms. Then your problem might not occur.


Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Inserting tildes in source code files.

Post by gmedina »

HI,

the OP used "tildes" to refer to accents (in this case, the acute accent used in Spanish). To handle those special characters, the best way to go is to escape to LaTeX (See Section 4.14 Escaping to LaTeX of the package documentation); a little example:

Code: Select all

\documentclass{book}
\usepackage[utf8]{inputenc}

\usepackage{listings}
\lstset{escapeinside=||}

\begin{document}

\begin{lstlisting}
if (condici|ó|n){
instrucciones...
}
\end{lstlisting}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
turbomac
Posts: 34
Joined: Sat Oct 10, 2009 9:38 pm

Inserting tildes in source code files.

Post by turbomac »

gmedina wrote:HI,

the OP used "tildes" to refer to accents (in this case, the acute accent used in Spanish). To handle those special characters, the best way to go is to escape to LaTeX (See Section 4.14 Escaping to LaTeX of the package documentation); a little example:

Code: Select all

\documentclass{book}
\usepackage[utf8]{inputenc}

\usepackage{listings}
\lstset{escapeinside=||}

\begin{document}

\begin{lstlisting}
if (condici|ó|n){
instrucciones...
}
\end{lstlisting}

\end{document}
It worked!! THanks a lot!!!
Post Reply