3

first time asking a question.

I'm implementing egregs answer from this question: Highlight First Occurrence of a Particular Word

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

This emphasises the first mention of the words in the \specialterms list. This is working fine at the moment. I want it to recognise capitalised versions of the words listed in \specialterms, so that it works better with the first word in a sentence. Using the example over, I want \term{Foo} to be emphasised without having to add Foo to the \specialterms list.

I tried the following:

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

This does not work however. I think I need to add some \csnames and \expandafter commands and what have you, but my understanding of what these commands actually do is very limited. Any suggestions would be most appreciated. I should maybe have posted this as a comment in the original thread, but as I just joined the site I don't have enough reputation to do this.

  • Welcome to LaTeX! Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Regarding your question, have you had a look at mforbes' answer to the same question? It uses the glossaries package and it does support capitalisation of terms. – JP-Ellis Feb 13 '16 at 09:32

2 Answers2

4

We can examine the lowercased version of the term.

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

enter image description here

A simpler implementation with expl3, which also works with UTF-8 (not required, though).

\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}
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
egreg
  • 1,121,712
0

Here is a solution

\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}
touhami
  • 19,520