[Tex/LaTex] Use unicode character U+2192 in LaTeX

symbolsunicode

I have the following document (excerpts):

\documentclass[a4paper]{article}

% Paketimporte
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{eurosym}
\usepackage{titlesec}
\usepackage[pdftex]{graphicx}
\usepackage{float} 
\usepackage{url}
\usepackage{wrapfig}
\usepackage{csquotes}
\usepackage{glossaries}
\usepackage[markup=nocolor,deletedmarkup=xout]{changes}
\usepackage{listings}
\usepackage{color}
\usepackage{amssymb}

\DeclareUnicodeCharacter{00A0}{ }
\DeclareUnicodeCharacter{207C6}{\dash}
\DeclareUnicodeCharacter{2192}{\dash}

\begin{document}    
% Titelblatt
\maketitle
\pagebreak

% Inhaltsverzeichnis
\tableofcontents
\pagebreak

\begin{lstlisting}
    foo → bar
\end{lstlisting}
\printglossaries
\end{document}

Which produces the following error when invoking pdflatex:

Package inputenc Error: Unicode char �\lst@FillFixed@\lst@EC� (U+207C6)(inputenc) not set up for use with LaTeX. foo →

I already googled for this error and found some similar, but not equal issues, which encourage to use \ DeclareUnicodeCharacter or \newunicodechar, but which does not work.
The file in question is also UTF-8 encoded:

$ file -i test.tex
test.tex.tex: text/x-tex; charset=utf-8

How can I get latex to display the character?

Best Answer

listings doesn't really have UTF-8 support, so what happens when it finds is essentially random.

You need not to declare the character if you load textcomp, because LaTeX knows how to translate it to \textrightarrow, but you need to teach it explicitly to listings.

\documentclass[a4paper]{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{textcomp}

\usepackage{listings}

\lstset{
  literate={→}{\textrightarrow}1
}

\begin{document}

foo → bar

\begin{lstlisting}
foo → bar
\end{lstlisting}

\end{document}

enter image description here

Related Question