Graphics, Figures & Tableslistings | Problems with Greek Letters

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
marius63
Posts: 6
Joined: Fri Oct 28, 2011 12:23 pm

listings | Problems with Greek Letters

Post by marius63 »

Is there a bug reporting/discussing channel for this? Is this it?

Nice Ada source code, with Greek letter variables, UTF-8 encoded:
nice_ada_code.png
nice_ada_code.png (21.12 KiB) Viewed 9131 times
And here is how LaTeX typesets it, via package listings:
ada_code_latex.png
ada_code_latex.png (30.14 KiB) Viewed 9131 times
Note how line 49 is perfectly typeset, but the variables at the start of lines 42, 43 have moved to end of the previous line.

Any help greatly appreciated. Thanks.
Last edited by localghost on Fri Oct 28, 2011 5:30 pm, edited 1 time in total.

Recommended reading 2024:

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

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

User avatar
Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Re: listings | Problems with Greek Letters

Post by Stefan Kottwitz »

Hi Mario,

perhaps show a small compilable code example which produced that for you, so we also know your listings settings, possibly we would see the cause. Looking only at the output doesn't help much.

Stefan
LaTeX.org admin
marius63
Posts: 6
Joined: Fri Oct 28, 2011 12:23 pm

listings | Problems with Greek Letters

Post by marius63 »

Thanks. LaTeX input and contents of the source code file follows.

Code: Select all

\documentclass[12pt,a4paper]{report}
\usepackage{ucs}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstloadlanguages{Ada,C}
\usepackage{textgreek}

\begin{document}

\lstset{
language=Ada,
basicstyle={\rmfamily\scriptsize},
numbers=left,
numberstyle=\tiny,
breaklines=true,
showstringspaces=false,
extendedchars=false
}

\lstinputlisting{adacode.txt}

\end{document}
adacode.txt

Code: Select all

      A : Real_Vector (1 .. N);
      λ : Real_T := 1.0;
      α : Real_T := 1.0;
      I : Real_Matrix := Unit_Matrix (N, 1, 1);
      M : Real_Matrix_Ptr_T;
      Items : Page_Item_Array_T (1 .. N);
   begin
      --  Setup the matrix
      M := new Real_Matrix (1 .. N, 1 .. N);
      M.all := λ * I + α * Transpose (W.all);
User avatar
Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Re: listings | Problems with Greek Letters

Post by Stefan Kottwitz »

I see! With the standard listings package, UTF-8 characters are a problem, they need to be escaped somehow. There's the package listingsutf8 you could use. Perhaps work with math symbols instead of Unicode character.

Stefan
LaTeX.org admin
marius63
Posts: 6
Joined: Fri Oct 28, 2011 12:23 pm

Re: LaTeX package listings bug with greek letters

Post by marius63 »

Thanks. I checked listingsutf8 but from its documentation it seems it requires the source code to be able to be transformed into a one byte encoding. That sounds useless and silly. If the source code could be represented in one byte I would not be using UTF8 in the first place. Maybe they are thinking of a special one byte encoding that combines ASCII and Greek. But then we would loose Latin1 or other codes that are also used in the source code (Portuguese literals for instance).

You cannot use \lamba et al. either because the source code is in a distinct language from LaTeX (then it would not compile).

The solution is Unicode. I am only interested in fixing the Unicode in LaTeX. Thanks.
User avatar
shadgrind
Posts: 140
Joined: Thu Jul 16, 2009 12:39 am

listings | Problems with Greek Letters

Post by shadgrind »

Have you tried adding inputencoding=utf8 in your \lstset command? Another thing to try is \usepackage[utf8x]{inputenc}, instead of \usepackage[utf8]{inputenc}, in the preamble.
System: TeX Live 2012, Fedora 18 x86_64, GNU Emacs 24.2
marius63
Posts: 6
Joined: Fri Oct 28, 2011 12:23 pm

listings | Problems with Greek Letters

Post by marius63 »

shadgrind wrote:Have you tried adding inputencoding=utf8 in your \lstset command? Another thing to try is \usepackage[utf8x]{inputenc}, instead of \usepackage[utf8]{inputenc}, in the preamble.
Thanks. Yes I've tried. No luck:-(
User avatar
shadgrind
Posts: 140
Joined: Thu Jul 16, 2009 12:39 am

listings | Problems with Greek Letters

Post by shadgrind »

One alternative is to use XeTeX, so that you could use TrueType fonts with real Unicode support. The minted package seems to have better Unicode support than the listings package. This worked for me with your adacode.txt file:

Code: Select all

\documentclass[12pt,a4paper]{report}
\usepackage{fontspec}
\usepackage{minted}
\setmainfont{DejaVu Serif}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}

\begin{document}
\usemintedstyle{emacs}
\inputminted[linenos=true]{ada}{adacode.txt}

\end{document}
Then compile like this:

Code: Select all

xelatex -shell-escape myfile.tex
The shell escape is necessary because minted depends on the Pygments Python library and utilities.
System: TeX Live 2012, Fedora 18 x86_64, GNU Emacs 24.2
marius63
Posts: 6
Joined: Fri Oct 28, 2011 12:23 pm

Re: listings | Problems with Greek Letters

Post by marius63 »

Great! Now I'm on a complex, critical mission and cannot change installation midway, but definitely will try XeLaTeX in the future. Thanks a lot.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

listings | Problems with Greek Letters

Post by localghost »

It works if you allow escaping to math mode thus typeset all Greek letter as math symbols.

Code: Select all

\documentclass[12pt,a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\lstloadlanguages{Ada,C}
\lstset{%
  language=Ada,
  basicstyle=\scriptsize,
  numbers=left,
  numberstyle=\tiny,
  breaklines=true,
  showstringspaces=false,
  mathescape=true
}

\begin{document}
  \lstinputlisting{adacode.txt}
\end{document}
But this requires modifications to your Ada code.

Code: Select all

  A : Real_Vector (1 .. N);
  $\lambda$ : Real_T := 1.0;
  $\alpha$ : Real_T := 1.0;
  I : Real_Matrix := Unit_Matrix (N, 1, 1);
  M : Real_Matrix_Ptr_T;
  Items : Page_Item_Array_T (1 .. N);
begin
  --  Setup the matrix
  M := new Real_Matrix (1 .. N, 1 .. N);
  M.all := $\lambda$ * I + $\alpha$ * Transpose (W.all);
For details see Section 4.14 of the listings manual.


Thorsten
Post Reply