我想修改逗号的 catcode,使用以下代码
\catcode`\,=13 \def,{$\cdot$}
我收到以下错误消息:
! Missing control sequence inserted.
<inserted text>
\inaccessible
l.43 \printkeywords
如何在特定环境中将输入逗号更改为输出 \cdot?
以下是 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
答案1
您的尝试存在两个问题:
当你这样做时,文本已经排版,并且在打印时
\setbox\keybox=\vbox{...}
更改类别代码不会产生任何影响,
,
被吸收时的定义类别代码\printkeywords
为12,所以不能这样做\def,{$\cdot$}
。
您必须更改keywords
环境中的类别代码。
\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}
不同的实现:
\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}
答案2
\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