# How to read from an external dictionary in LaTeX?

I'm wondering how to define a LaTeX command `\x` that prints "def1" when I write `\x{name1}`, reading from an external (dictionary) file having a structure like

``````{name1}{def1}
{name2}{def2}
``````

etc.

-
Welcome to TeX.SE. I assume you meant `\x{name2}`, right? If so, please correct. I would have but just wanted to make sure. Also, is modifying the external file an option? –  Peter Grill Oct 18 '11 at 23:24

If the external file `dict.dat` is organized as

``````\entry{name1}{def1}
\entry{name2}{def2}
``````

then

``````\newcommand{\entry}[2]{%
\expandafter\newcommand\csname entry@#1\endcsname{#2}}
\newcommand{\x}[1]{\csname entry@#1\endcsname}
\input{dict.dat}
``````

will do what you're looking for.

-
Exactly what I needed! A big thank you Peter, for your prompt answer and for your corrections! –  bblue Oct 19 '11 at 7:18
Perhaps adding error checking, such as `\expandafter\ifx\csname entry@#1\endcsname\relax\ERROR\else\csname entry@#1\endcsname\fi` would be nice. –  Bruno Le Floch Oct 19 '11 at 7:21
@bblue: If this answers your question, you can check the ticker under the counter to accept it. –  ℝaphink Oct 19 '11 at 9:18
@Raphink: Yes, sure! Thank you. –  bblue Oct 19 '11 at 13:20