7

What should I put in the preamble (preferably packages) for this to work as expected with its unicode characters?

\documentclass{article}
\usepackage{listings}
\begin{document}
  $α$ α
  \begin{lstlisting}
     α = 1.;
  \end{lstlisting}
\end{document}

Note that alpha (α) appears in three contexts: as text, as math and as code (listings).

(I can use lualatex if it simplifies things)


For a higher challenge, in this case I introduce a second level of difficulty, by introducing a second kind of alpha (𝛼: 0x1d6fc Mathematical italic small alpha vs. α: Greek small letter alpha). To see to what degree TeX can deal with the nuance (e.g. by make 𝛼 into $\alpha$, or by making 𝑝 --0x1D45D Mathematical italic small p-- into $p$):

\documentclass[]{article}
\usepackage[]{listings}
\begin{document}
$α$ α $𝛼$ 𝛼 𝑝
\begin{lstlisting}
α = 1.
𝛼 = 2.
\end{lstlisting}
\end{document}
  • AFAIK listings can only handle single byte chars plus stuff that can be represented in latin1. So someone has to write a fully utf8 aware version of listings – daleif Oct 3 '13 at 7:29
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – jub0bs Mar 1 '14 at 21:55
  • Well, I am afraid that by marking an answer as Accepted when there the problem wasn't solved I will discourage new answers with solutions and updates in the future. The first answer doesn't address the limitation in listings, and the second doesn't scale well. – alfC Mar 1 '14 at 22:20
5

For the listings part of the question, you'll need something like the following (adapting the answers from the questions that @morbusg pointed to). This works in xelatex. I agree with @morbusg that the tricky part will be to find fonts that contain your glyphs, especially typewriter and math fonts.

\documentclass{article}
\usepackage{fontspec}
\setmainfont{FreeSerif}
\usepackage{listings}
\lstset{inputencoding=utf8,extendedchars=true}
\makeatletter
\lst@InputCatcodes
\def\lst@DefEC{%
 \lst@CCECUse \lst@ProcessLetter
  ^^^^03b1% α
  ^^^^^^01d6fc% 𝛼
  ^^^^^^01d45d% 𝑝
  ^^00}
\lst@RestoreCatcodes
\makeatother
\begin{document}
\begin{lstlisting}
α = 1.
𝛼 = 2.
𝑝 = 3.
\end{lstlisting}
\end{document}
  • This works! Do you know 1) an alternative to FreeSerif that looks like the default TeX font (Computer Modern) but it is as complete as FreeSerif 2) A monospaced font that is as complete as FreeSerif (for example, something that displays: 𝟭 (U+1D7ED)) – alfC Oct 16 '14 at 19:07
  • inputencoding=utf8 is not required in XeLaTeX or LuaTeX since both engines do not use the package inputenc. The documentation of listings state that nothing happens if this package is not loaded – Pieter Stroobants Jan 9 '15 at 19:36
8

You can use and packages for use with and , but unfortunately doesn't work with so it has been suggested on this site to use packages like instead. (UTF8 for listings, The 'listings' package and UTF-8, Having problems with listings and UTF-8. Can it be fixed?, verbments (listings alternative) and UTF-8, UTF-8 (BMP character set) support in listings., etc. etc.)

But otherwise:

\documentclass{article}
\usepackage{fontspec,unicode-math}
\setmainfont{Cambria}
\setmathfont{Cambria Math}
\setmonofont{Consolas}
\begin{document}
$\alpha$, $α$, $𝛼$, α
\end{document}

enter image description here

Note that text fonts don't include (all of) the mathematical glyphs. Especially not the mathematical alphanumeric range.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.