1

I am on Linux and I have this document that I compile using xelatex main.tex:

\documentclass[12pt]{article}

\usepackage{color}
\usepackage{xcolor}
    \definecolor{code_linenumbers_b}{RGB}{215,215,215}
    \definecolor{code_linenumbers}{RGB}{255,255,255}
    \definecolor{code_background}{RGB}{250,250,250}
    \definecolor{code_comments}{RGB}{185,185,185}
    \definecolor{code_borders}{RGB}{86,211,239}

\usepackage{mathspec}
    \setallmonofonts[
        %Path=.,
        Extension=.ttf,
        Scale=0.8,
        UprightFont=*--regular,
        BoldFont=*--bold,
        ItalicFont=*--italic
    ]{liberation-mono}

\usepackage{listings}
    %Global
    \lstset{
        %frame
        frame=l,
        captionpos=b,
        numbersep=3.5mm,
        xleftmargin=0.35cm,
        xrightmargin=0cm,
        framesep=0mm,
        framexleftmargin=2.5mm,
        framerule=0.5mm,
        %colors
        backgroundcolor=\color{code_background},
        basicstyle=\ttfamily\color{black},
        numberstyle=\ttfamily\bfseries\color{code_linenumbers},
        fillcolor=\color{code_linenumbers_b},
        commentstyle=\color{code_comments},
        keywordstyle=\color{black}\bfseries,
        rulecolor=\color{code_borders},
        %code format
        showspaces=false,
        breaklines=true,
        breakatwhitespace=true,
        tabsize=3,
        columns=flexible
    }

\begin{document}
    \lstinputlisting[
        language=C,
        caption=\lstinline{main.c},
        label=r001,
        numbers=left,
        xleftmargin=1.15cm,
        framesep=8mm
    ]{main.c}

\end{document}

and my main.c file looks like this:

#include <stdio.h>

int main(){

    //            ir
    //            ↓
    //        012345678
    //      0     *
    //      1    ***
    //      2   *****
    //      3  *******
    // ir → 4 *********

    return 0;
}

But after compiling (with no errors or warnings) I get the resulting main.pdf where arrows (U+2193) & (U+2192) are not properly positioned:

enter image description here

All the *.ttf fonts are inside the same folder than the main.tex and main.c.

ziga@ziga-laptop:mwe$ ls -l
total 468
-rw-r--r-- 1 ziga ziga 120308 May 21 17:30 liberation-mono--bold-italic.ttf
-rw-r--r-- 1 ziga ziga 107620 May 21 17:30 liberation-mono--bold.ttf
-rw-r--r-- 1 ziga ziga 126308 May 21 17:30 liberation-mono--italic.ttf
-rw-r--r-- 1 ziga ziga 110204 May 21 17:29 liberation-mono--regular.ttf
-rw-r--r-- 1 ziga ziga    229 May 21 18:45 main.c
-rw-r--r-- 1 ziga ziga   1479 May 21 18:50 main.tex

and they do have a glyphs for arrows (U+2193) & (U+2192) which I double checked by opening all the *.ttf files with Fontforge:

enter image description here

enter image description here

xelatex is an UTF-8 engine and should support UTF-8 encoded multibyte characters... So why don't they render properly?

71GA
  • 3,573
  • 1
    You should use keepspaces option in \lstset. – hair-splitter May 21 '20 at 19:52
  • Thank you! This solved half of my problem. Now arrows are aligned properly but are still not the same color as the rest of the comments. Any Idea on how to solve this as well? – 71GA May 21 '20 at 20:07

1 Answers1

1

I use this method: The 'listings' package and UTF-8

An MWE:

\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{listings}
\makeatletter
\lst@InputCatcodes
\def\lst@DefEC{%
 \lst@CCECUse 
 \lst@ProcessLetter
  ^^^^2192^^^^2193^^00}
\lst@RestoreCatcodes
\makeatother
\lstset{
   basicstyle=\ttfamily,
   keepspaces,
   commentstyle=\color{gray},
   extendedchars
}
\begin{document}
    \lstinputlisting[language=C]{main.c}
\end{document}