# Loop through lines of a .txt file to generate nomenclature

I am a complete newbie in the world of macros but I need to define a command that takes the content of a .txt file and uses it to add nomenclature entries. My .txt file looks like this:

hPSCs: Human pluripotent stem cells
CMs: Cardiomyocytes
hPSC-CMs: hPSC-derived cardiomyocytes


And I want my command to essentially do the following:

\nomenclature{hPSCs}{Human pluripotent stem cells}
\nomenclature{CMs}{Cardiomyocytes}
\nomenclature{hPSC-CMs}{hPSC-derived cardiomyocytes}


For all the lines of the .txt file. In other words, I want to:

• Open .txt file
• Read each line of the .txt file. Then:
• Split the text in two parts at delimiter, in my case ":". Let's called them 1 and 2
• Use the two parts to build add a nomenclature entry. So it would be \nomenclature{1}{2}

It would me take no time to do it in any programming language, but I really don't know where to start in Latex. I'd be grateful if you could help me.

Here is a somewhat simpler solution, using the basic TeX \read command. The file is supposed to be in nominal.txt.

\documentclass{article}

\usepackage{nomencl}
\makenomenclature
% Check that we don't use existing commands
\newcommand*\mytextfile{}
\newcommand*\donomencl{}
\def\donomencl#1:#2\par{\nomenclature{#1}{#2}}

\begin{document}
% \nomenclature{hPSCs}{Human pluripotent stem cells}
% \nomenclature{CMs}{Cardiomyocytes}
% \nomenclature{hPSC-CMs}{hPSC-derived cardiomyocytes}

\openin\mytextfile=nomencl.txt
\loop\unless\ifeof\mytextfile %
\else \expandafter\donomencl \nomenline\par
\fi
\repeat
\closein\mytextfile

Some text

\printnomenclature

\end{document}

• Works perfectly as expected, thank you. Would you mind explaining (briefly) the code? I really want to be more confident with macros – lutai May 10 at 8:00

Here, I redefine \nomenclature just to show (detokenize) what is being invoked. However, the approach reads the file, parses by line with a : separator., and constructs a \nomenclature invocation using the data from the two parts of the line.

\documentclass{article}
\usepackage[T1]{fontenc}
\begin{filecontents*}[overwrite]{nom.txt}
hPSCs: Human pluripotent stem cells
CMs: Cardiomyocytes
hPSC-CMs: hPSC-derived cardiomyocytes
\end{filecontents*}
\usepackage{nomencl}
\makenomenclature
\begin{document}
\setsepchar{\\/:}
\ignoreemptyitems
\def\tmp{}
\foreachitem\z\in\nomlist[]{%
\itemtomacro\nomlist[\zcnt,1]\theacr
\itemtomacro\nomlist[\zcnt,2]\thedescr
\def\tmp{\expandafter\nomenclature\expandafter{\theacr}}%
\expandafter\tmp\expandafter{\thedescr}%
}

Testing nomenclature

\printnomenclature
\end{document}


• Ended up using the simpler approach, but thank you! – lutai May 10 at 8:01