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}
alfC
  • 14,350

2 Answers2

9

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.

morbusg
  • 25,490
  • 4
  • 81
  • 162
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}
nickie
  • 4,378
  • 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 09 '15 at 19:36