\lccode 在 latex 命令中激活

\lccode 在 latex 命令中激活

为什么\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}

相关内容