![\lccode 在 latex 命令中激活](https://linux22.com/image/432677/%5Clccode%20%E5%9C%A8%20latex%20%E5%91%BD%E4%BB%A4%E4%B8%AD%E6%BF%80%E6%B4%BB.png)
为什么\lccode`~=`,
活动,
仅在环境定义中起作用,而在 LaTeX 命令中不起作用?我们使用活动,
来产生项目符号:
以下是 MWE:
\documentclass{book}
\usepackage{color}
\usepackage{xparse}
\makeatletter
\def\keybullet{\textbullet}
\newenvironment{keywords}{\bgroup\sloppy \begingroup\lccode`~=`, \lowercase{\endgroup\def~}{${}\hbox{\ \keybullet\ }{}$} \catcode`,=\active \noindent\textbf{Keywords}\hskip1em}{\egroup\par\addvspace{\baselineskip}}
\newcommand{\Keywords}[1]{\sloppy \begingroup\lccode`~=`, \lowercase{\endgroup\def~}{${}\hbox{\ \keybullet\ }{}$} \catcode`,=\active \noindent\textbf{Keywords}\hskip1em#1\par\addvspace{\baselineskip}}
\makeatother
\begin{document}
\begin{keywords} Harmonic balance, Multi-degrees-of-freedom systems, Fundamental harmonic, Higher harmonics, Energy method \end{keywords}
\Keywords{Harmonic balance, Multi-degrees-of-freedom systems, Fundamental harmonic, Higher harmonics, Energy method}
\end{document}
答案1
你需要吸收这个论点后类别代码的设置,否则当类别代码尚未改变时,逗号将被标记化。
\documentclass{book}
\newcommand{\keybullet}{\unskip\space\textbullet\space\ignorespaces}
\newcommand{\activecommabullet}{%
\begingroup\lccode`~=`,\lowercase{\endgroup\let~\keybullet}%
}
\newenvironment{keywords}
{%
\par\sloppy\activecommabullet\catcode`,=\active
\noindent\textbf{Keywords}\quad
}
{\par\addvspace{\baselineskip}}
\newcommand{\Keywords}{%
\par\sloppy
\activecommabullet
\catcode`,=\active
\noindent\textbf{Keywords}\quad
\Keywordsaux
}
\newcommand{\Keywordsaux}[1]{#1\par\addvspace{\baselineskip}}
\begin{document}
\begin{keywords}
Harmonic balance, Multi-degrees-of-freedom systems, Fundamental harmonic,
Higher harmonics, Energy method
\end{keywords}
\Keywords{Harmonic balance, Multi-degrees-of-freedom systems,
Fundamental harmonic, Higher harmonics, Energy method}
\end{document}
使用更简单的实现expl3
,不需要更改类别代码。
\documentclass{book}
%\usepackage{xparse} % not needed for LaTeX 2020-10-01 or later
\NewDocumentCommand{\keybullet}{}{ \textbullet\ }
\ExplSyntaxOn
\NewDocumentEnvironment{keywords}{b}
{
\rajesh_keywords:n { #1 }
}
{}
\NewDocumentCommand{\Keywords}{m}
{
\rajesh_keywords:n { #1 }
}
\cs_new_protected:Nn \rajesh_keywords:n
{
\par\sloppy\noindent\textbf{Keywords}\quad
\clist_set:Nn \l_tmpa_clist { #1 }
\clist_use:Nn \l_tmpa_clist { \keybullet }
\par\addvspace{\baselineskip}
}
\ExplSyntaxOff
\begin{document}
\begin{keywords}
Harmonic balance, Multi-degrees-of-freedom systems, Fundamental harmonic,
Higher harmonics, Energy method
\end{keywords}
\Keywords{Harmonic balance, Multi-degrees-of-freedom systems,
Fundamental harmonic, Higher harmonics, Energy method}
\end{document}