Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
1  
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
add comment

1 Answer

up vote 9 down vote accepted

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.

share|improve this answer
    
Exactly what I needed! A big thank you Peter, for your prompt answer and for your corrections! –  bblue Oct 19 '11 at 7:18
2  
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.