This question already has an answer here:

I have set a new matcode environment in order to have some matlab code in my .tex file, using the matlab-prettifier package (combined with the listings package). However, I have a problem with the tilde symbol.

\documentclass{article}

\usepackage[english,greek]{babel}
\usepackage[utf8]{inputenc}
\usepackage{kerkis}

\usepackage{xcolor}
\usepackage{listings}
\makeatletter
% Avoid the spurious error
\AtBeginDocument{%
    \expandafter\def\expandafter\originalTeX\expandafter{%
        \expandafter\@tempcnta\number253\expandafter\relax\originalTeX}%
}
% apply the fix
\addto\extrasgreek{%
    \@tempcnta=128
    \@whilenum\@tempcnta<253\do{%
        \expandafter\babel@savevariable\expandafter{\expandafter\lccode\the\@tempcnta}%
        \lccode\@tempcnta=\@tempcnta
        \advance\@tempcnta\@ne
    }%
}

\usepackage{matlab-prettifier}

 \lstnewenvironment{matcode}
    {
    \selectlanguage{english} 
    \fontfamily{cmr} \fontsize{8}{10}\selectfont
    \lstset{
     language=matlab-pretty,
                style=Matlab-editor,
                basicstyle = \ttfamily,
                keepspaces=true,
                escapechar=\#}
    }
    {
    \normalsize
    \selectlanguage{greek}
    }

    \begin{document}
    \begin{matcode}
    % This is a comment.
    [~, a ] 
    \end{matcode}

    \end{document}

If I use the kerkis.sty package, then the result is going to be this one, where I miss the tilde symbol (apparently the tilde symbol is not supported by kerkis): enter image description here

Ignoring (by commenting) the kerkis.sty package, the result is going to be this one: enter image description here

Although the tilde symbol is somewhat displaced, at least it is visible. As you can see, I tried to load the cmr font inside the matcode environment, but I had no luck with that. So my question is:

How can I load the cmr font inside my matcode environment, without commenting the command \usepackage{kerkis}?

Edit: In order to override this problem and make the tilde symbol appear, I used the escapechar # and I replaced the tilde symbol in my .tex file, with the command #\texttildelow#, but this is not a neat solution for sure.

marked as duplicate by jubobs, Werner, Svend Tveskæg, egreg environments Aug 27 '15 at 19:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.