# Use TeX macros to store multiple data

I'm trying to use TeX macros to store multiple data such as website url/name. Here is an example :

\documentclass{article}

\makeatletter
\newcommand*\WebSiteName[1]{\@WebSiteName\expandafter#1}
\newcommand*\WebSiteUrl[1]{\@WebSiteUrl\expandafter#1}
\newcommand*\@WebSiteName[2]{#2}
\newcommand*\@WebSiteUrl[2]{#1}
\makeatother

\begin{document}

\end{document}


On this example, \WebSiteName\Google should write "Google Search" and \WebSiteUrl\Google should write "http://www.google.com".

But the problem is that \WebSiteName\Google writes http://www.google.comGoogle Search". I try to use \expandafter to combine two arguments in only one. Maybe that's the wrong method.

-
Related, maybe a dupe: tex.stackexchange.com/q/48938/8057 (Perhaps at another level of abstraction...) –  krlmlr Dec 11 '12 at 20:53
Welcome to TeX.sx! –  Kurt Dec 11 '12 at 20:53
Thanks Kurt. :) –  dieze Dec 13 '12 at 19:19

You're misplacing \expandafter: it must go before \@WebSiteName and \@WebSiteUrl. But it turns out that the two auxiliary macros are already in the LaTeX kernel:

\makeatletter
\newcommand{\WebSiteName}{\expandafter\@secondoftwo}
\newcommand{\WebSiteUrl}{\expandafter\@firstoftwo}
\makeatother




When \WebSiteName\Google is found, it becomes

\expandafter\@secondoftwo\Google


and then \Google is expanded, giving

\@secondoftwo{http://www.google.com}{Google Search}


and, finally

Google Search

-

Thanks egreg.

Finally, after a little search, i used a different approch using the package datatool.

File: websites.csv

id,     url,                   name


File: document.tex

\documentclass{minimal}

\usepackage{datatool}

% set "," as separator between each entry
% "," is the default
% to use <tab> as separator, use \DTLsettabseparator
\DTLsetseparator{,}

\newcommand*\WebSiteName[1]{%
\DTLgetvalueforkey{\temp}{name}{websites}{id}{#1}%
\temp}

\newcommand*\WebSiteUrl[1]{%
\DTLgetvalueforkey{\temp}{url}{websites}{id}{#1}%
\temp}

\newcommand*\WebSite[1]{%
\WebSiteName{#1}~\WebSiteUrl{#1}}

\begin{document}

% show database
\DTLdisplaydb{websites}