如何让列表将 \ 识别为字母?

如何让列表将 \ 识别为字母?

使用以下条目listings可以识别为字母\\literate

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

但是,我想做同样的事情来自\定义它的颜色。尝试编写

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

不起作用,因为\}是按照常规方式识别的。是否可以采用\与 类似的方式进行自定义\\

\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

如果您希望反斜杠有颜色,那么您只需要一个literate

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

请注意,第一个参数需要双反斜杠(要求您在示例中已经完成了双反斜杠)。因此

\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}

已经导致:

带有一个、两个、三个、四个红色反斜杠的框架

相关内容