1

Why does \lccode`~=`, with an active , work only in an environment definition but not in a LaTeXcommand? We are using an active , to produce bullets:

enter image description here

Here is an 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}

chsk
  • 3,667
Rajesh N
  • 829

1 Answers1

1

You need to absorb the argument after the setting of the category code, otherwise the commas would be tokenized when the category code has not yet been changed.

\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}

enter image description here

A simpler implementation with expl3, that needs no category code change.

\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}

egreg
  • 1,121,712