1

Whenever words as 'Varicer', 'Lobuli', 'Haptocytter', 'Fibrose' is used in the document after the wordlist, they should be in italic font, is there a way to make a function with starting point in this:

\nomenclature{Varicer}{explanation}
\nomenclature{Lobuli}{explanation}
\nomenclature{Haptocytter}{explanation}
\nomenclature{Fibrose}{explanation}

Which goes through the above list and check for the word throughout the text and makes it italic font?

The stucture of my Project, is in more files and here is a minimal example:

main.tex

\documentclass[10pt,a4paper]{article}
\usepackage[a4paper,top=2cm, bottom=3cm, left=3cm, right=3cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{subfiles}
\usepackage{graphicx}
\graphicspath{{images/}{../images/}}
\usepackage[danish]{babel}

%---Wordlist---%
\usepackage[danish]{nomencl} %package to make wordlist
\usepackage{xpatch}
\patchcmd{\thenomenclature} %makes indent in front of all entries
  {\leftmargin\labelwidth}
  {\leftmargin\labelwidth\itemindent 1em }
  {}{}
\makenomenclature %Makes the word list
\setlength\nomlabelwidth{3cm} %distance to explanation text
\renewcommand{\nomname}{Wordlist} %Title of wordlist
\renewcommand{\nompreamble}{Introduction text}
%---Wordlist---%


\begin{document}

\subfile{01}
\subfile{02}

\end{document}

01.tex

\documentclass[main.tex]{subfiles}
\graphicspath{{./img/}{../img}}
\begin{document}

\nomenclature{Varicer}{explanation}
\nomenclature{Lobuli}{explanation}
\nomenclature{Haptocytter}{explanation}
\nomenclature{Fibrose}{explanation}
\printnomenclature

\section{Test1}
Som text Varicer blabla Lobuli.

\end{document}

02.tex

\documentclass[main.tex]{subfiles}
\graphicspath{{./img/}{../img}}
\begin{document}

\section{Test 2}
blablabla Varicer blabla Lobuli bla.

\end{document}

NB: Compile from main file

This is the result: enter image description here

5
  • I think this answer may be a duplicate, tex.stackexchange.com/questions/580936/…, if you change \autohighlightStyleA and B to \textit. – Steven B. Segletes Mar 11 at 10:19
  • 1
    However, it might be easier to \newcommand\Varicer{\textit{Varicer}} and use your editor to change all occurrences of Varicer to \Varicer{}. – Steven B. Segletes Mar 11 at 10:22
  • Thank you for your suggestions, I will try your first suggestion. In the second case I could also just replace Variacer by \textit{Variacer}. The wordlist can be extremely long, so I would like to avoid search and replace procedure. – User123456789 Mar 11 at 10:31
  • That the wordlist could be long is a key part that I did not understand earlier. Thanks for clarifying. – Steven B. Segletes Mar 11 at 10:52
  • Can't get it to work apparently – User123456789 Mar 11 at 12:38
2

Since the OP was unable to exploit my referenced answer at Automatic text highlighting based on a dictionary, I show it below.

I modify the call to \nomenclature to add each word to \thewordlist, which I then use as the argument to \setsepchar, to set up the listofitems search in the manner of the referenced question. I also changed the highlighting from color to italic, per the OP's question. I made it so that if the word from the list appears as its own word OR as part of a larger word, it will be italicized.

I invoke the tokencycle after defining the nomenclature, because the nomenclature is used to build the word list. Thus, my modified call to \nomenclature also passes the argument as \textit, so as to get the nomenclature entries themselves also in italic.

\documentclass[10pt,a4paper]{article}
\usepackage[a4paper,top=2cm, bottom=3cm, left=3cm, right=3cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{subfiles}
\usepackage{graphicx}
\graphicspath{{images/}{../images/}}
\usepackage[danish]{babel}

%---Wordlist---%
\usepackage[danish]{nomencl} %package to make wordlist
\usepackage{xpatch}
\patchcmd{\thenomenclature} %makes indent in front of all entries
  {\leftmargin\labelwidth}
  {\leftmargin\labelwidth\itemindent 1em }
  {}{}

\makenomenclature %Makes the word list
\setlength\nomlabelwidth{3cm} %distance to explanation text
\renewcommand{\nomname}{Wordlist} %Title of wordlist
\renewcommand{\nompreamble}{Introduction text}
%---Wordlist---%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{tokcycle,listofitems}

\let\svnomenclature\nomenclature
\renewcommand\nomenclature[2]{\addtowordlist{#1}\svnomenclature{\textit{#1}}{#2}}
\def\thewordlist{}
\newcommand\addtowordlist[1]{\ifx\thewordlist\empty\def\thewordlist{#1}
  \else\edef\thewordlist{\thewordlist||#1}\fi}

\newcommand\testdict{%
  \if\relax\detokenize\expandafter{\currentword}\relax\else
    {\ignoreemptyitems
      \greadlist\dictcompA{\currentword}}%
    \readlist\dictcompB{\currentword}%
    \ifnum\listlen\dictcompA[]=0\relax
      \addcytoks[1]{\autohighlightStyleA}%
      \addcytoks[1]{\expandafter{\currentword}}
    \else
      \ifnum\listlen\dictcompB[]>1\relax
        \addcytoks[1]{\autohighlightStyleB}%
        \addcytoks[1]{\expandafter{\currentword}}
      \else
        \addcytoks[1]{\currentword}%
      \fi
    \fi
  \fi
  \gdef\currentword{}%
}
\makeatletter
\Characterdirective{\tctestifcatnx A#1{\g@addto@macro\currentword{#1}}
  {\testdict\addcytoks{#1}}}
\stripgroupingtrue
\Groupdirective{\testdict\groupedcytoks{\processtoks{#1}\testdict}}
\Macrodirective{\g@addto@macro\currentword{#1}}
\Spacedirective{\testdict\addcytoks{#1}}
\makeatother
\newcommand\autohighlightStyleA{\textit}
\newcommand\autohighlightStyleB{\textit}

\begin{document}

\nomenclature{Varicer}{explanation}
\nomenclature{Lobuli}{explanation}
\nomenclature{Haptocytter}{explanation}
\nomenclature{Fibrose}{explanation}
\printnomenclature

\expandafter\setsepchar\expandafter{\thewordlist}
\def\currentword{}\tokencyclexpress

\section{Test1}
Som text Varicer blabla Lobuli.

\section{Test 2}
blablabla Varicer blabla Lobuli bla.

\endtokencyclexpress
\end{document}

enter image description here

p.s. WARNING: the process is case dependent and so will not pick up on words in the list if they appear in lowercased form in the document.

6
  • Nice, especially the detail that it format the word regardless of it being in a longer word. However, I think the reason I couldn't get it to work, and still can't is due to the file structure. So I will continue working on that problem. – User123456789 Mar 12 at 21:46
  • Would it be possible to make it not case dependent (indifferent between lower- and uppercased letters) as well? – User123456789 Mar 12 at 22:12
  • @User123456789 Right now it is case dependent, and TeX tends to be poorly equipped to change case in an expandable way. One thing you could do right now with no changes in the code is when you call on \nomeclature with a word, also call on \addtowordlist{...} with the alternate-case version of the word. That way, both cases of the word will be checked. – Steven B. Segletes Mar 12 at 22:57
  • @User123456789 See tex.stackexchange.com/questions/173481/… for some discussion on case changing. – Steven B. Segletes Mar 12 at 22:58
  • Since it checks for the word being a part of a longer word, I think another approach could be to check for "aricer" instead of "Varicer", Checking the word not including the first letter. It may be faster since the lists of words it has to check would be half as long as the idea adding multiple versions of the same word to the list. But I may think your idea is the "safest" procedure in TeX. I'll try doing both, to see what is the best procedure, when I get it to work, with my stupid subfiles structure. – User123456789 Mar 13 at 13:51

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.