1

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.

0

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{}
\newread\mytextfile
\newcommand*\donomencl{}
\def\donomencl#1:#2\par{\nomenclature{#1}{#2}}
\newcommand*\lastreadline{\par}

\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 %
  \read\mytextfile to \nomenline
  \ifx\nomenline\lastreadline
  \else \expandafter\donomencl \nomenline\par
  \fi
\repeat
\closein\mytextfile

Some text 

\printnomenclature 

\end{document}
1
  • 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
0

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}
\usepackage{readarray}
%\usepackage{listofitems}% IS LOADED BY readarray
\begin{filecontents*}[overwrite]{nom.txt}
hPSCs: Human pluripotent stem cells
CMs: Cardiomyocytes
hPSC-CMs: hPSC-derived cardiomyocytes
\end{filecontents*}
\usepackage{nomencl}
\makenomenclature
\begin{document}
\readarraysepchar{\\}
\readdef{nom.txt}\nomdata
\setsepchar{\\/:}
\ignoreemptyitems
\readlist*\nomlist{\nomdata}
\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}

enter image description here

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

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.