0

Output Before Setting Main Font

Before setting the main font using the fontspec package, the escapechar was working properly. Below is the output picture:
Output before setting the main font
and below is my MWE:

\documentclass{memoir}

\usepackage{bigfoot} % to allow verbatim in footnote
\usepackage[numbered,framed]{matlab-prettifier}
\let\ph\mlplaceholder % shorter macro
\lstMakeShortInline"

\lstset{
    style              = Matlab-editor,
    basicstyle         = \mlttfamily,
    escapechar         = ",
    mlshowsectionrules = true,
}
\begin{document}
    
    \begin{lstlisting}[caption = {For educational purposes}]
        % example of while loop using placeholders
        while "\ph{condition}"
        if "\ph{something-bad-happens}"
        break
        else
        % do something useful
        end
        % do more things
        end
    \end{lstlisting}
    
\end{document}

Output After Setting Main Font

After setting the Times New Roman as my main font, the escapechar are now appearing as open boxes, which is something I do not like. Below is the output picture:
Output after setting the main font
and below is my MWE:

% !TeX TS-program = xelatex
\documentclass{memoir}

\usepackage{bigfoot} % to allow verbatim in footnote
\usepackage[numbered,framed]{matlab-prettifier}
\let\ph\mlplaceholder % shorter macro
\lstMakeShortInline"

\lstset{
    style              = Matlab-editor,
    basicstyle         = \mlttfamily,
    escapechar         = ",
    mlshowsectionrules = true,
}
\usepackage{fontspec}% added
\setmainfont{Times New Roman}
\begin{document}
    
    \begin{lstlisting}[caption = {For educational purposes}]
        % example of while loop using placeholders
        while "\ph{condition}"
        if "\ph{something-bad-happens}"
        break
        else
        % do something useful
        end
        % do more things
        end
    \end{lstlisting}
    
\end{document}

How do I get the proper escapechar in the presence of the main font?

8
  • as the log files says Missing character: There is no 〈 (U+2329) in font TimesNewRoman:mode=node. You could try e.g. TeX Gyre Termes instead. Aug 4, 2022 at 10:41
  • Is not there any way or alternative using Times New Roman as my main font?
    – itc
    Aug 4, 2022 at 10:48
  • 1
    you can probably redefine \phClDelim@mlpr and \phOpDelim@mlpr to use some other symbol, or switch the font. Aug 4, 2022 at 10:50
  • 1
    Do: \setmainfont{Times New Roman} \makeatletter \renewcommand\phOpDelim@mlpr{$\langle$} \renewcommand\phClDelim@mlpr{$\rangle$} \makeatother
    – Cicada
    Aug 4, 2022 at 13:01
  • 1
    Alternatively. find a font with the angle bracket symbols, e.g., FreeSans, and do: \setmainfont{Times New Roman} \newfontface\fontangles{FreeSans} \renewcommand\textlangle{{\fontangles 〈}} \renewcommand\textrangle{{\fontangles 〉}}.
    – Cicada
    Aug 4, 2022 at 13:37

1 Answer 1

2

matlab-prettifier package is using the (text version of the math) symbols and as delimiters in its \placeholder command.

\newcommand\mlplaceholder[1]
{%
...   \phOpDelim@mlpr%
...    #1
...   \phClDelim@mlpr%
}

with the delimiter macros being defined as:

\newcommand\phOpDelim@mlpr{\textlangle}
\newcommand\phClDelim@mlpr{\textrangle}

Times New Roman does not have those symbols.

A math font, like Asana-Math, has them, but we want to keep Times New Roman.

The default math mode fonts in latex also have them, so we can redefine the delimiters to use the math version instead of the text companion version:

\makeatletter
\renewcommand\phOpDelim@mlpr{$\langle$}
\renewcommand\phClDelim@mlpr{$\rangle$}
\makeatother

Or, alternatively, we can use those symbols from another (Unicode) font which has them, FreeSans say. We leave the matlab commands unchanged as original, and instead redefine \textlangle and \textrangle directly:

\newfontface\fontangles{FreeSans}
\renewcommand\textlangle{{\fontangles 〈}}
\renewcommand\textrangle{{\fontangles 〉}}

Alternatively again, using a legacy font, those symbols are in slots 60 and 62 of the TS1 encoding scheme:

\newcommand\mytextlangle{{\usefont{TS1}{cmr}{m}{n}\char60}}
\newcommand\mytextrangle{{\usefont{TS1}{cmr}{m}{n}\char62}}

angle brackets

MWE

\documentclass{memoir}

\usepackage{bigfoot} % to allow verbatim in footnote
\usepackage[numbered,framed]{matlab-prettifier}
\let\ph\mlplaceholder % shorter macro
\lstMakeShortInline"

\lstset{
    style              = Matlab-editor,
    basicstyle         = \mlttfamily,
    escapechar         = ",
    mlshowsectionrules = true,
}
\usepackage{fontspec}% added
\setmainfont{Times New Roman}
\newfontface\fontangles{FreeSans}
\renewcommand\textlangle{{\fontangles 〈}}
\renewcommand\textrangle{{\fontangles 〉}}
%\makeatletter
%\renewcommand\phOpDelim@mlpr{$\langle$}
%\renewcommand\phClDelim@mlpr{$\rangle$}
%\makeatother
\newcommand\mytextlangle{{\usefont{TS1}{cmr}{m}{n}\char60}}
\newcommand\mytextrangle{{\usefont{TS1}{cmr}{m}{n}\char62}}

\begin{document}
    
    \begin{lstlisting}[caption = {For educational purposes}]
        % example of while loop using placeholders
        while "\ph{condition}" 
        if "\ph{something-happens}"
        break 
        else
        % do something useful
        end
        % do more things
        end
    \end{lstlisting}

legacy:  \mytextlangle x\mytextrangle

\end{document}

Note that fontspec is indeed working perfectly, and the escape character is also working perfectly. Any font not having those symbols will produce the same effect as Times New Roman.

1
  • This is an excellent solution @Cicada. All the solutions worked out perfectly!
    – itc
    Aug 4, 2022 at 14:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .