![如何让 Latex 识别列表中没有分隔符的单个关键字?](https://linux22.com/image/333972/%E5%A6%82%E4%BD%95%E8%AE%A9%20Latex%20%E8%AF%86%E5%88%AB%E5%88%97%E8%A1%A8%E4%B8%AD%E6%B2%A1%E6%9C%89%E5%88%86%E9%9A%94%E7%AC%A6%E7%9A%84%E5%8D%95%E4%B8%AA%E5%85%B3%E9%94%AE%E5%AD%97%EF%BC%9F.png)
我想使用 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}