突出显示单词的第一次出现。使其不区分大小写

突出显示单词的第一次出现。使其不区分大小写

第一次提问。

我正在实施来自这个问题的egregs答案:突出显示特定单词的第一次出现

%%% Code to set up special term treatment
\makeatletter
\newcommand{\specialterms}[1]{%
  \@for\next:=#1\do
  {\@namedef{specialterm@\detokenize\expandafter{\next}}{}}%
}
\newcommand\term[1]{%
  \@ifundefined{specialterm@\detokenize{#1}}
    {#1}{\emph{#1}\global\expandafter\let\csnamespecialterm@\detokenize{#1}\endcsna    me\relax}%
}
\makeatother

%%% Here we define the special terms we want
\specialterms{foo,bar,baz}

这会强调列表中第一次提到的单词\specialterms。目前,这个功能运行良好。我希望它能够识别 中列出的单词的大写版本\specialterms,以便更好地处理句子中的第一个单词。使用上面的示例,我希望\term{Foo}在不将 Foo 添加到列表中的情况下强调它\specialterms

我尝试了以下操作:

\@ifundefined{specialterm@\MakeLowercase{\detokenize{#1}}}

但是这不起作用。我想我需要添加一些命令\csnames\expandafter您有的东西,但我对这些命令实际作用的理解非常有限。任何建议都将不胜感激。我可能应该在原始帖子中将其作为评论发布,但由于我刚加入该网站,我没有足够的声誉来执行此操作。

答案1

我们可以检查该术语的小写版本。

\documentclass{article}

%%% Code to set up special term treatment
\makeatletter
\newcommand{\specialterms}[1]{%
  \@for\next:=#1\do
    {\@namedef{specialterm@\detokenize\expandafter{\next}}{}}%
}
\newcommand\term[1]{%
  \specialterm@lower{#1}% save the lowercased term in \specialterm@current
  \@ifundefined{specialterm@\specialterm@current}
    {#1}% not the first occurrence
    {% first occurrence
     \emph{#1}% print it in italics
     % then undefine the macro
     \global\expandafter\let\csname specialterm@\specialterm@current\endcsname\relax
    }%
}
\newcommand{\specialterm@lower}[1]{%
  \begingroup\edef\next{\detokenize{#1}}%
  \edef\x{\endgroup\lowercase{\def\noexpand\specialterm@current{\next}}}\x
}
\makeatother

%%% Here we define the special terms we want
\specialterms{foo,bar,baz}

\begin{document}

First occurrence of \term{foo} and second occurrence of \term{foo}.

First occurrence of \term{baz}. \term{Bar} and
again \term{bar} and \term{baz} and \term{foo}.

\end{document}

在此处输入图片描述

一个更简单的实现expl3,它也适用于UTF-8(但不是必需的)。

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{xparse}

%%% Code to set up special term treatment
\ExplSyntaxOn
\NewDocumentCommand{\specialterms}{m}
 {
  \clist_map_inline:nn { #1 }
   {
    \bool_new:c { \__lasse_boolean_name:n { ##1 } _bool }
   }
 }

\NewDocumentCommand{\term}{m}
 {
  \bool_if:cTF { \__lasse_boolean_name:n { #1 } _bool }
   {
    #1
   }
   {
    \emph { #1 }
    \bool_gset_true:c { \__lasse_boolean_name:n { #1 } _bool }
   }
 }

% syntactic sugar
\cs_new:Nn \__lasse_boolean_name:n
 {
  g_lasse_specialterms_ \str_lowercase:n { #1 }
 }
\ExplSyntaxOff

%%% Here we define the special terms we want
\specialterms{foo,bar,báz,bäz}

\begin{document}

First occurrence of \term{foo} and second occurrence of \term{foo}.

First occurrence of \term{báz}. \term{Bar} and
again \term{bar} and \term{báz} and \term{foo}.

\term{Bäz} \term{bäz}

\end{document}

答案2

这是一个解决方案

\documentclass{article}

\newcommand*{\term}[1]{%
\lowercase{\expandafter\ifx\csname mt\detokenize{#1}term\endcsname\relax}
\emph{#1}%
\lowercase{\expandafter\let\csname mt\detokenize{#1}term\endcsname\empty}%
\else#1\fi}
\begin{document}
First occurrence of \term{foo} and second occurrence of \term{foo}.

First occurrence of \term{baz}. Now \term{bar} and
again \term{bar} and \term{baz} and \term{foo}. \term{fOo}


First occurrence of \term{BOM}. Now \term{bOm} and
again \term{BOm} and \term{bom} and \term{FOo} and \term{fOo}.
\end{document}

相关内容