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 trying to use TeX macros to store multiple data such as website url/name. Here is an example :

\documentclass{article}

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

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

\begin{document}

  Name : \WebSiteName\Google \par
  Url  : \WebSiteUrl\Google

\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.

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

2 Answers 2

up vote 10 down vote accepted

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

\newcommand{\NewSite}[3]{\newcommand#1{{#2}{#3}}}

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

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
share|improve this answer

Thanks egreg.

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

File: websites.csv

id,     url,                   name
Google, http://www.google.com, Google Search

File: document.tex

\documentclass{minimal}

\usepackage{datatool}

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

% loads the database
\DTLloaddb{websites}{websites.csv}

\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}

  \WebSiteName{Google}
  \WebSiteUrl{Google}
  \WebSite{Google}

\end{document}
share|improve this answer

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.