我定义了一个宏,使用以下方法突出显示下一个单词
\def!##1 {\hkw{##1} }%
在这个范围内,!
是一个活动字符。所以写起来没问题。
\emph{This is a !good example}
但不是
\emph{This example is !good}
它会抱怨什么! Argument of ! has an extra }.
。
有没有一种好的方法可以让它!
考虑下一个空间之前的所有内容,但最多将当前组中的所有内容作为其参数?
答案1
我拿了马丁的回答回答相关问题并稍微简化一下:
使用此代码
\documentclass{article}
% next word parser, based on https://tex.stackexchange.com/a/11925/15107
\makeatletter
\def\consumenextword#1{%
\begingroup
\let\@cmd#1%
\def\@selectedword{}%
\consumenextword@
}
\def\consumenextword@{%
\futurelet\ntoken\consumenextword@@
}
\def\consumenextword@@{%
\ifcase 0%
\ifx\ntoken\@sptoken 0\else
\ifcat a\ntoken 1\else
\ifcat 0\ntoken 2\fi% test of token is catcode "other"
\fi\fi
\relax
\expandafter\consumenextword@end
\or
\expandafter\consumenextword@add
\else
\expandafter\consumenextword@checknum
\fi
}
% Checks if token is a number (ASCII 48-57)
\def\consumenextword@checknum#1{%
\ifcase 0%
\ifnum`#1>47
\ifnum`#1<58 1\fi\fi
\relax
\def\next{\consumenextword@end#1}%
\else
\def\next{\consumenextword@add{#1}}%
\fi
\next
}
\def\consumenextword@add#1{%
\edef\@selectedword{\@selectedword#1}%
\consumenextword@
}
\def\consumenextword@end{%
\@cmd\@selectedword%
\endgroup%
}
\makeatother
\catcode`!=\active
\def!{\consumenextword{\textbf}}
\begin{document}
This is a !good example, and this !is2. Also works at the end of the !Line
And well well in an \textit{!argument}.
\end{document}
我得到这个输出: