1

With listings, it is possible to identify \\ as letter using the following literate entry

{\\\\}{{\textcolor{IndianRed1}{\textbackslash{}\textbackslash{}}}}{1}

However, I want to do the same thing with \ to customize its color. Attempting to write

{\}{{\textcolor{IndianRed1}{}}}{1} % \

Does not work since \} is identified in its conventional way. Is it possible to customize \ in a similar fashion to customizing \\?

\documentclass[aspectratio=169, xcolor={x11names}, t, handout]{beamer}

\usepackage{calc}

\usetheme{Dresden}
\usefonttheme{professionalfonts}
\renewcommand{\sfdefault}{ppl}

\usepackage[T1]{fontenc}
\renewcommand{\encodingdefault}{T1}

\usepackage{nicematrix}
\usepackage{booktabs}
\newlength{\widthWidest}

\usepackage{listings}

% ========== MATLAB codes packages =========
% BEGIN_FOLD

\usepackage[]{matlab-prettifier}
% BEGIN_FOLD

% Inline code
% BEGIN_FOLD

\lstdefinestyle{matlab-inline}{
    style=Matlab-editor,
    basicstyle=\linespread{0.8}\mlttfamily\color{DodgerBlue3},
    alsoletter={*,\&,\begin},
    literate=
    *{\\\\}{{\textcolor{IndianRed1}{\textbackslash{}\textbackslash{}}}}{1}, % \\
    {\}{{\textcolor{IndianRed1}{}}}{1} % \
}

\lstset{style=matlab-inline}

% END_FOLD

% END_FOLD

% END_FOLD

\begin{document}

\begin{frame}[fragile, environment=frame, allowframebreaks=0.99]

    \lstinline|\\\\|
    
    \lstinline|\|
    
\end{frame}

\end{document}
1
  • If four \ are needed for \\ , how many \ do you think, you would need for \ ? ;-)
    – cabohah
    Feb 6, 2023 at 13:52

1 Answer 1

2

If you want backslashs to be colored you need only one literate:

literate=*{\\}{{\textcolor{IndianRed1}{\textbackslash}}}{1} % all \ in IndianRead1 regardless of the number

Note, that the first argument needs to double the backslash (ask you've already done in your example for double-backslash). So

\documentclass[aspectratio=169, xcolor={x11names}, t, handout]{beamer}

\usepackage{calc}

\usetheme{Dresden}
\usefonttheme{professionalfonts}
\renewcommand{\sfdefault}{ppl}

\usepackage[T1]{fontenc}
\renewcommand{\encodingdefault}{T1}

\usepackage{nicematrix}
\usepackage{booktabs}
\newlength{\widthWidest}

\usepackage{listings}

% ========== MATLAB codes packages =========
% BEGIN_FOLD

\usepackage[]{matlab-prettifier}
% BEGIN_FOLD

% Inline code
% BEGIN_FOLD

\lstdefinestyle{matlab-inline}{
    style=Matlab-editor,
    basicstyle=\linespread{0.8}\mlttfamily\color{DodgerBlue3},
    alsoletter={*,\&,\begin},
    literate=*{\\}{{\textcolor{IndianRed1}{\textbackslash}}}{1} % \
}

\lstset{style=matlab-inline}

% END_FOLD

% END_FOLD

% END_FOLD

\begin{document}

\begin{frame}[fragile, environment=frame, allowframebreaks=0.99]

one: \lstinline|\|, two: \lstinline|\\|, three: \lstinline|\\\|, four: \lstinline|\\\\|



    
\end{frame}

\end{document}

already results in:

frame with one, two, three, four backslashs in red

You must log in to answer this question.

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