9

I have an article with many keywords mixed throughout. I also have a list of those keywords and their definitions (this list needn't appear in the document, and I can put it in a CSV or any format as necessary). Is there any way to have LaTeX search for these keywords in the document and highlight them?

Two possible options:

  1. Place any keywords found in the document in italics.

  2. (preferred) Create a \footnote wherever the keywords appears in the document, with the definition or use some other form of automated cross-reference (for instance, against a glossary).

  • Welcome to TeX.sx! Your question got migrated here from a different stackexchange site. Please also register yourself here and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. – Martin Scharrer Jul 8 '11 at 8:38
13

With XeLaTeX it's possible via the xesearch package.

\documentclass[border=1]{standalone}
\usepackage{fontspec}
\usepackage{xesearch}

\SearchList{list1}{\textit{#1}}{keyword,something}

\begin{document}\Huge
This is text that shows a keyword,\\
which is something special.
\end{document}

The result is shown below

here

| improve this answer | |
  • 2
    Does this also work if a macro expands to keyword or something? – Aditya Jul 8 '11 at 15:53
  • @Aditya: I just tested that and yes it works. It also works if e.g. key is expanded by one macro and word by some other as long the result is keyword. – Martin Scharrer Jul 8 '11 at 17:01
2

No, LaTeX can't search through the whole document for you to highlight specific words. You need to format them by yourself. You might want to define a custom macro for this which you can change later:

\newcommand*{\kw}[1]{\emph{#1}}
%\newcommand*{\kw}[1]{\emph{#1}\footnote{#1: ...}

You could use the Search&Replace feature of your Text/LaTeX editor to add this macro around keywords. Using tools like sed or Perl you could do this for all words in your CSV file at once.

For showing the definition of the keyword you can use the glossaries or acronym packages.

| improve this answer | |
  • 1
    With XeLaTeX one could try and use xesearch. – egreg Jul 8 '11 at 8:44
1

Use soul:

\usepackage{soul}
...
\hl{this text is highlighted}

documentation

via

| improve this answer | |
  • Since microtype might be thought of as a replacement for soul I wonder if there is a way to do this with microtype? – N.N. Jul 8 '11 at 8:46
  • 1
    @N.N.: I wouldn't call microtype "a replacement for soul", it does heck more than it. AFAIK both allow for letterspacing and microtype does it better. Also the benefit of soul is the formatting of longer text while still allowing line breaks, which is kind of not required for single keywords (OK, you loose hyphenation, but I think soul doesn't support that either). Anyway, besides the question title, the main issue seems not which actual highlighting macro to use but how to apply it automatically to all keywords in a text. – Martin Scharrer Jul 8 '11 at 9:00

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.