将所有 JSON 格式的值设为红色

将所有 JSON 格式的值设为红色

我正在使用 latex beamer,并尝试将 JSON 格式true的键值flag也变为红色,但得到的只是黑色,如下面的屏幕截图所示。我怎样才能让它也变成红色?

在此处输入图片描述

代码:

\documentclass[xcolor=dvipsnames]{beamer}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usetheme{Antibes}
\usecolortheme[named=Maroon]{structure} 
\setbeamercovered{transparent}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{xcolor}
\usepackage{listings}

\newcommand\JSONnumbervaluestyle{\color{red}}
\newcommand\JSONstringvaluestyle{\color{red}}

% switch used as state variable
\newif\ifcolonfoundonthisline

\makeatletter

\lstdefinestyle{json}
{
  showstringspaces    = false,
  keywords            = {false,true},
  alsoletter          = 0123456789.,
  morestring          = [s]{"}{"},
  stringstyle         = \ifcolonfoundonthisline\JSONstringvaluestyle\fi,
  MoreSelectCharTable =%
    \lst@DefSaveDef{`:}\colon@json{\processColon@json},
  basicstyle          = \ttfamily,
  keywordstyle        = \ttfamily\bfseries,
}

% flip the switch if a colon is found in Pmode
\newcommand\processColon@json{%
  \colon@json%
  \ifnum\lst@mode=\lst@Pmode%
    \global\colonfoundonthislinetrue%
  \fi
}

\lst@AddToHook{Output}{%
  \ifcolonfoundonthisline%
    \ifnum\lst@mode=\lst@Pmode%
      \def\lst@thestyle{\JSONnumbervaluestyle}%
    \fi
  \fi
  %override by keyword style if a keyword is detected!
  \lsthk@DetectKeywords% 
}

% reset the switch at the end of line
\lst@AddToHook{EOL}%
  {\global\colonfoundonthislinefalse}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\title{Test}
\author{Alex XYZ}
\institute{University XYZ}
\date{20.01.16}



\begin{document}
\beamertemplatenavigationsymbolsempty

\begin{frame}
\titlepage
\end{frame}

\begin{frame}[fragile]{Service Provider - Request and Response}

\begin{lstlisting}[style=json]
{
  "mac": "10:B5:S3:06:C6:E9",
  "route":0,
  "latitude":53.834588, 
  "longitude":10.704048,  
  "time":"12.09.2015 13:45:00",
  "speed":3,
  "direction":"",
  "flag": "true"
}  
\end{lstlisting}

\end{frame}

\end{document}

答案1

如果你删除此行

  keywords            = {false,true},

字符串"true"被视为字符串并变为红色

相关内容