1

I am currently using following code for G-code formatting. Unfortunately I have no idea how do I enable the macro just for the new "gcode" language.

\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  

This macro + gcode language works as follows. Problem is the CCPutMacro works for all languages. For example language=java and i want it to only work when 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}

enter image description here

3
  • 1
    Well, what is "gcode" and "new gcode"? Please explain better ...
    – Mensch
    Dec 18 '18 at 3:54
  • 1
    You should extend your sample to a complete but minimal, compilable document that shows the result you get and where it differs from what you want.
    – siracusa
    Dec 18 '18 at 4:01
  • I added example code + package and colors. Dec 18 '18 at 11:21
2

Define a new environment for gcode and execute the \lst@CCPutMacro part only there.

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

enter image description here

3
  • Thank you for your answer! I think I finally got it how environments work in LaTeX. Much appreciated answer. Dec 18 '18 at 12:16
  • @JanVorisek You're welcome. By the way, are you related to Jan Václav Voříšek (1791-1825)?
    – egreg
    Dec 18 '18 at 12:18
  • I am not as far as I know. It's nice to hear someone knows about czech composers and musicians though :-) Dec 18 '18 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.