2

I want to add a few lines of G-Code (used for 3D-printing) to my latex file using the listing package. Unfortunately, g-code is not supported as a language and therefore has no highlighted commands. I tried adding the command keywords manually, as you can see below, but it didn't quiet work out the way I intended.

\documentclass[
    12pt
    ]{scrreprt}
\usepackage{listings}
\usepackage{color}

\begin{document}

\lstset{
 morekeywords={E, T, G1, F, X, Y}
}
\begin{lstlisting}
T0
G1 E2 F80 
G1 Z0.400 F7800.00
G1 F300.000 E-1.00000

G1 X88.172 Y88.172 F7800.000
G1 E1.00000 F300.000 
G1 X111.828 Y88.172 E0.01225 F360.000
G1 X111.828 Y111.828 E0.01225
\end{lstlisting}    

\end{document}

results in:

enter image description here

How can I make latex recognize keywords, even if they are not followed by a delimiter?

Thanks in advance.

1

Keywords goes until the first "non-digit or non-letter". So you need to make the number to other:

\documentclass[
    12pt
    ]{scrreprt}
\usepackage{listings}
\usepackage{color}


\begin{document}

\lstset{
 morekeywords={E, T, G1, F, X, Y},
 alsoother={0,1,2,3,4,5,6,7,8,9}
}
\begin{lstlisting}
T0
G1 E2 F80
G1 Z0.400 F7800.00
G1 F300.000 E-1.00000

G1 X88.172 Y88.172 F7800.000
G1 E1.00000 F300.000
G1 X111.828 Y88.172 E0.01225 F360.000
G1 X111.828 Y111.828 E0.01225
\end{lstlisting}

\end{document}

enter image description here

But with this setting the G1 keyword no longer work.

If you really want every occurance of the single letter to be bold you could try this:

\documentclass[
    12pt
    ]{scrreprt}
\usepackage{listings}
\usepackage{color}

\makeatletter
\lst@CCPutMacro
    \lst@ProcessOther {"45}{%
      \lst@ttfamily
         {\textbf{E}}% used with ttfamily
         {\textbf{E}}}% 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         
    \@empty\z@\@empty
\makeatother  
\begin{document}

\lstset{
 morekeywords={G1},
 }
\begin{lstlisting}
T0
G1 E2 F80
G1 Z0.400 F7800.00
G1 F300.000 E-1.00000

G1 X88.172 Y88.172 F7800.000
G1 E1.00000 F300.000
G1 X111.828 Y88.172 E0.01225 F360.000
G1 X111.828 Y111.828 E0.01225
\end{lstlisting}

\end{document}

enter image description here

  • Thanks. Quite alot of code, but it works. Could you give me some background on the "\lst@ProcessOther{}" function and the arguments like "45 and so on? Couldn't find it in the listings documentation... – chrizz May 13 '16 at 10:02
  • The numbers are the positions of the chars (in hexadecimal), the function is the function listings uses internally when processing a char. You won't find it in the documentation, I found it by looking in the code. – Ulrike Fischer May 13 '16 at 11:11

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.