2

I want to modify the catcode of comma, with the following code

\catcode`\,=13 \def,{$\cdot$}

I get the following error message:

 ! Missing control sequence inserted.
    <inserted text> 
                    \inaccessible 
    l.43 \printkeywords

how to change the input comma to output \cdot in the particular environment?

below is the MWE

\documentclass{book}
\usepackage{color}
\begin{document}
\definecolor{gray}{cmyk}{0,0,0,0.3}
\newbox\keybox
\newbox\tempbox
\newdimen\tempdima

\newenvironment{keywords}{\global\setbox\keybox\vbox\bgroup}{\egroup}

\def\printkeywords{\par%
                    \setbox\tempbox\hbox{Keywords}%Zusammenfassung
                        \tempdima\wd\tempbox%
                        \advance\tempdima4.2mm%
                        \advance\tempdima4.2mm%
                        \noindent\fboxsep0pt\colorbox{gray}{\vbox to 13pt{\hsize\tempdima\vfill\noindent\hspace*{4.2mm}Keywords\vfill}}
                         \endgraf\vspace*{2.8pt}%
                         {\catcode`\,=13
                         \def,{$\cdot$}
                         \noindent\ignorespaces\unvcopy\keybox\endgraf}
                 }




\begin{keywords}
Keyword 1, Keyword 1, Keyword 2
\end{keywords}

\chapter{Sample Chapter}

Text, Text, Text, Text, 


\section{Section 1}

\printkeywords


\section{Section 2}

\printkeywords
  • you are just showing the output of a box\unvcopy\keybox (which would be better as \usebox in latex) so no macros are expanded and no catcodes are looked up, the content has already been typeset before it was saved. – David Carlisle Jan 25 '18 at 9:39
2

There are two problems in your attempt:

  1. when you do \setbox\keybox=\vbox{...}, the text is already typeset and changing the category code of , at time of printing will do nothing

  2. the category code of , when the definition of \printkeywords is absorbed is 12, so you can't do \def,{$\cdot$}.

You have to change the category code in the keywords environment.

\documentclass{book}
\usepackage{color}
\begin{document}
\definecolor{gray}{cmyk}{0,0,0,0.3}
\newbox\keybox
\newbox\tempbox
\newdimen\tempdima

\newenvironment{keywords}{\global\setbox\keybox\vbox\bgroup
  \begingroup\lccode`~=`, \lowercase{\endgroup\def~}{${}\cdot{}$\ignorespaces}%
  \catcode`,=\active
}{\egroup}

\def\printkeywords{\par%
                    \setbox\tempbox\hbox{Keywords}%Zusammenfassung
                        \tempdima\wd\tempbox%
                        \advance\tempdima4.2mm%
                        \advance\tempdima4.2mm%
                        \noindent\fboxsep0pt\colorbox{gray}{\vbox to 13pt{\hsize\tempdima\vfill\noindent\hspace*{4.2mm}Keywords\vfill}}
                         \endgraf\vspace*{2.8pt}%
                         \noindent\ignorespaces\unvcopy\keybox\endgraf
                 }




\begin{keywords}
Keyword 1, Keyword 1, Keyword 2
\end{keywords}

\chapter{Sample Chapter}

Text, Text, Text, Text, 


\section{Section 1}

\printkeywords

\end{document}

A different implementation:

\documentclass{book}
\usepackage{color}
\usepackage{xparse}

\definecolor{vgray}{cmyk}{0,0,0,0.3}

\ExplSyntaxOn
\NewDocumentCommand{\keywords}{m}
 {
  \clist_gset:Nn \g_venkatesan_keywords_clist { #1 }
 }
\NewDocumentCommand{\printkeywords}{}
 {
  \par\noindent
  \group_begin:
  \setlength{\fboxsep}{2pt}
  \colorbox{vgray}{\,Keywords\,}
  \group_end:
  \\*[3pt]
  \clist_use:Nn \g_venkatesan_keywords_clist { ${}\cdot{}$ }
  \par
 }
\ExplSyntaxOff

\begin{document}

\keywords{Keyword 1, Keyword 1, Keyword 2}

\chapter{Sample Chapter}

Text, Text, Text, Text, 

\section{Section 1}

\printkeywords

\end{document}

enter image description here

  • I'll add later a possibly better implementation – egreg Jan 25 '18 at 9:47
1
\usepackage{xparse}
\usepackage{environ}

\newenviron{keywords}{%
  <Do whatever you want>
  \processkeywords{\BODY}
  ...
}

\ExplSyntaxOn

\NewDocumentCommand \processkeywords { O{\ $\cdot$\ } m }
 {
  \clist_set:NV \l_tmpa_clist { #2 }
  \clist_use:Nn \l_tmpa_clist { #1 }
 }

\ExplSyntaxOff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.