我想使用 listing 包将几行 G 代码(用于 3D 打印)添加到我的 latex 文件中。不幸的是,g 代码不受支持,因此没有突出显示的命令。我尝试手动添加命令关键字,如下所示,但它并没有按照我预期的方式运行。
\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}
结果是:
如何让 Latex 识别关键字,即使关键字后面没有分隔符?
提前致谢。
答案1
关键字会一直到第一个“非数字或非字母”。因此,您需要将数字改为其他:
\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}
但是有了这个设置,G1 关键字不再起作用。
如果您确实希望每个出现的单个字母都以粗体显示,您可以尝试以下操作:
\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}