1

I'm trying to extend the example for multilanguage support shown in here: https://tex.stackexchange.com/a/42791/7561

The current solution

As it is right now, one needs to define the macro using

 \newlangcommand{\uno}

and then declare de different outputs one language at a time, like

\addtolangcommand{\uno}{english}{one}
\addtolangcommand{\uno}{french}{une}

The expected behavior

However, I want to use that in a single call. For example

\newmlcommand[english=one, french=une]{\uno}

That should declare the \uno macro, and then make the \addtolangcommand once per pair of language string in the optional argument.

The problem

I defined that macro as

\newcommand{\newmlcommand}[2][]{%
  \newlangcommand#2%
  \pgfkeys{/mlcommands, command=#2, #1}%
}

and thought that I will use pgfkeys to handle the languages. The idea was to do it with:

\pgfkeys{
  /mlcommands/.is family, /mlcommands,
  command/.get=\langcmd,
  .unknown/.code = {%    
    \addtolangcommand{\langcmd}{\pgfkeyscurrentname}{#1}
  }%
}

That is for every pair of language=string I will execute the \addtolangcommand and assign them to the \langcmd temporal macro that holds the original macro passed to \newmlcommand.

However, I'm can't get the delay expansion of the macros right. My idea is to execute

\addtolangcommand{\langcmd}{language}{string} 

that was passed using the key pair language=string. To achieve that, the \addlangcommand should be on hold, and expand both \langcmd into the original macro definition and \pgfkeyscurrentname into the language string before executing the \addtolangcommand. Note that the \langcmd is set as parameter using the key pair command=\macro that is set in the options in \newmlcommand.

However I can't get it right.

Full code

Here are my attempts:

\documentclass{article}

\usepackage[french, english, spanish]{babel}
\usepackage{pgfkeys}

\makeatletter
% https://tex.stackexchange.com/a/42791/7561
\newcommand{\newlangcommand}[1]{%
  \newcommand#1{%
    \@ifundefined{\string#1\languagename}
    {``No def of \texttt{\string#1} for \languagename''}
    {\@nameuse{\string#1\languagename}}%
  }%
}
\newcommand{\addtolangcommand}[3]{%  
  \@namedef{\string#1#2}{#3}}    

\def\langcmd{}
\pgfkeys{
  /mlcommands/.is family, /mlcommands,
  command/.get=\langcmd,
  .unknown/.code = {%    
    % tried this idea: save it in a macro and execute it later
    % but, the expansion is still a problem
    % \def\tmp{\noexpand\addtolangcommand{\langcmd}{\pgfkeyscurrentname}{#1}}%
    % \tmp%
    % and this idea: try to delay the expansion of addtolangcommand
    % \expandafter\expandafter\expandafter\expandafter\addtolangcommand\expandafter\expandafter{\langcmd}\expandafter{\pgfkeyscurrentname}{#1}%
    % and the naive aplication doesn't work either
    \addtolangcommand{\langcmd}{\pgfkeyscurrentname}{#1}
  }%
}

\newcommand{\newmlcommand}[2][]{%
  \newlangcommand#2%
  \pgfkeys{/mlcommands, command=#2, #1}%
}
\makeatother

% How to use (original)
%\newlangcommand{\uno}
%\addtolangcommand{\uno}{english}{one}
%\addtolangcommand{\uno}{french}{une}

% or the new (non working way)
\newmlcommand[english=one, french=une]{\uno}


\begin{document}

\selectlanguage{english}
\uno  

\selectlanguage{french}
\uno  

\selectlanguage{spanish}
\uno  

\end{document}
  • It is a bit confusing. Which one should be expanded ? That looks like a TeX problem independent from pgfkeys – percusse Aug 29 '16 at 20:41
  • Well, I'm trying to do the expansion, in such a way that everytime each pair english=string executes \addtolangcommand{\cmd}{english}{string} where \cmd is the macro passed as parameter in command=\macro in the definition of the \newmlcommand. – adn Aug 29 '16 at 21:05
  • I don't know what it should do but the annoying part can be used as \expandafter\expandafter\expandafter\addtolangcommand\expandafter\expandafter\expandafter{\expandafter\langcmd\expandafter}\expandafter{\pgfkeyscurrentname}{#1} – percusse Aug 29 '16 at 21:51
  • That compiles, but does not achieve what I'm trying to do. I added more details to see if that clarifies the problem. – adn Aug 29 '16 at 22:14
1

I got it.

There were two things:

  • The macro expansion needed the 2n-1 rule for the \expandafter, and protect all the brackets before the parameters.
  • The macro inside the pgfkeys didn't work with the .get key, so I did a manual \def.

After that the macro \addtolangcommand will be expanded after the parameters.

Solution

\documentclass{article}

\usepackage[spanish, english]{babel}

\usepackage{pgfkeys}

% https://tex.stackexchange.com/a/42791/7561
\makeatletter
\newcommand{\newlcommand}[1]{%
  \newcommand#1{%
    \@ifundefined{\string#1\languagename}
    {``No def of \texttt{\string#1} for \languagename''}
    {\@nameuse{\string#1\languagename}}%
  }%
}
\newcommand{\addtolangcommand}[3]{%
  \@namedef{\string#1#2}{#3}}

%\def\langcmd{}
\pgfkeys{
  /mlcommands/.is family, /mlcommands,
  command/.code={\def\langcmd{#1}},
  .unknown/.code = {%    
    \expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\addtolangcommand\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter
    {\expandafter\expandafter\expandafter\langcmd\expandafter\expandafter\expandafter}%
    \expandafter\expandafter\expandafter{\expandafter\pgfkeyscurrentname\expandafter}\expandafter
    {#1}
  }%
}

\newcommand{\newmlcommand}[2][]{%
  \newlcommand#2%
  \pgfkeys{/mlcommands, command=#2, #1}%
}

\makeatother


\begin{document}

\newmlcommand[english=ONE, spanish=UNO]{\uno}
\selectlanguage{english}
\uno

\selectlanguage{spanish}
\uno

\end{document}

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.