lstlisting - 仅适用于特定语言的宏

lstlisting - 仅适用于特定语言的宏

我目前正在使用以下代码进行 G 代码格式化。不幸的是,我不知道如何为新的“gcode”语言启用宏。

\usepackage{xcolor}
\usepackage{listings}
\definecolor{light-gray}{gray}{0.95}
\definecolor{light-gray2}{gray}{0.90}

\lstdefinelanguage{gcode}{
    morekeywords={G1, G21, G28, G92, G90, M82, M107, M109, M104}, columns=fullflexible, basicstyle=\ttfamily,
    backgroundcolor=\color{light-gray},frame=tb,framesep=8pt,framerule=0.4pt, belowskip=0em
}   

\makeatletter
\lst@CCPutMacro
\lst@ProcessOther {"45}{%
    \lst@ttfamily
    {\textbf{E}}% used with ttfamily
    {\textbf{E}}}% used with other fonts
\lst@ProcessOther {"53}{%
    \lst@ttfamily
    {\textbf{S}}% used with ttfamily
    {\textbf{S}}}% used with other fonts  
\lst@ProcessOther {"54}{%
    \lst@ttfamily
    {\textbf{T}}% used with ttfamily
    {\textbf{T}}}% used with other fonts  
\lst@ProcessOther {"46}{%
    \lst@ttfamily
    {\textbf{F}}% used with ttfamily
    {\textbf{F}}}% used with other fonts                
\lst@ProcessOther {"58}{%
    \lst@ttfamily
    {\textbf{X}}% used with ttfamily
    {\textbf{X}}}% used with other fonts         
\lst@ProcessOther {"59}{%
    \lst@ttfamily
    {\textbf{Y}}% used with ttfamily
    {\textbf{Y}}}% used with other fonts
\lst@ProcessOther {"5A}{%
    \lst@ttfamily
    {\textbf{Z}}% used with ttfamily
    {\textbf{Z}}}% used with other fonts         
\@empty\z@\@empty
\makeatother  

此宏 + gcode 语言的工作方式如下。问题是 CCPutMacro 适用于所有语言。例如 language=java 并且我希望它仅在 langiage = gcode 时工作。

\begin{lstlisting}[language = gcode, numbers = none, escapechar = !] 
 G1 X0.00000 Y0.00000 E0.00000
 G1 X2.00000 Y4.00000 E0.02000
 G1 X4.00000 Y4.00000 E0.02500
 G1 X2.00000 Y0.00000 E0.04500
 G1 X4.00000 Y0.00000 E0.05000
 G1 X6.00000 Y4.00000 E0.07000
 G1 X8.00000 Y4.00000 E0.07500
 G1 X6.00000 Y0.00000 E0.09500
\end{lstlisting}

在此处输入图片描述

答案1

定义一个新环境gcode并仅在那里执行该\lst@CCPutMacro部分。

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{lmodern}

\definecolor{light-gray}{gray}{0.95}
\definecolor{light-gray2}{gray}{0.90}

\lstdefinelanguage{gcode}{
    morekeywords={G1, G21, G28, G92, G90, M82, M107, M109, M104},
    columns=fullflexible,
    basicstyle=\ttfamily,
    backgroundcolor=\color{light-gray},
    frame=tb,
    framesep=8pt,
    framerule=0.4pt,
    belowskip=0em,
}

\makeatletter
\def\gcodeletters{%
  \lst@CCPutMacro
  \lst@ProcessOther {"45}{%
    \lst@ttfamily
    {\textbf{E}}% used with ttfamily
    {\textbf{E}}}% used with other fonts
  \lst@ProcessOther {"53}{%
    \lst@ttfamily
    {\textbf{S}}% used with ttfamily
    {\textbf{S}}}% used with other fonts  
  \lst@ProcessOther {"54}{%
    \lst@ttfamily
    {\textbf{T}}% used with ttfamily
    {\textbf{T}}}% used with other fonts  
  \lst@ProcessOther {"46}{%
    \lst@ttfamily
    {\textbf{F}}% used with ttfamily
    {\textbf{F}}}% used with other fonts                
  \lst@ProcessOther {"58}{%
    \lst@ttfamily
    {\textbf{X}}% used with ttfamily
    {\textbf{X}}}% used with other fonts         
  \lst@ProcessOther {"59}{%
    \lst@ttfamily
    {\textbf{Y}}% used with ttfamily
    {\textbf{Y}}}% used with other fonts
  \lst@ProcessOther {"5A}{%
    \lst@ttfamily
    {\textbf{Z}}% used with ttfamily
    {\textbf{Z}}}% used with other fonts         
  \@empty\z@\@empty
}
\makeatother
\lstnewenvironment{gcode}[1][]{\gcodeletters\lstset{language=gcode,#1}}{}

\begin{document}

\begin{gcode}[numbers = none, escapechar = !] 
 G1 X0.00000 Y0.00000 E0.00000
 G1 X2.00000 Y4.00000 E0.02000
 G1 X4.00000 Y4.00000 E0.02500
 G1 X2.00000 Y0.00000 E0.04500
 G1 X4.00000 Y0.00000 E0.05000
 G1 X6.00000 Y4.00000 E0.07000
 G1 X8.00000 Y4.00000 E0.07500
 G1 X6.00000 Y0.00000 E0.09500
\end{gcode}

\begin{lstlisting}{language=Java}
XYE
\end{lstlisting}

\end{document}

在此处输入图片描述

相关内容