Is it possible to “program” a command wrapper?

I have the following doubt: Is it ever possible to define a command that takes a word as an argument and executes the command with that same name?

For instance, something like

\newcommand{\wrapper}[1]{%
\@nameuse{#1}%
}


in such a way calling \wrapper{hrule} gets converted into \hrule.

Motivation

The main objective I have with this is to "assign" values to previously defined commands, as I load its values via a CSV file using datatools:

\assignto{somevar}{value} --> \renewcommand{\somevar}{value}

\assignto{othervar}{command} --> \renewcommand{\othervar}{\command}

This code snippet was the first idea I had, but it doesn't work.

\newcommand{\assignto}[2]{%
\@ifundefined{#2}{%
\renewcommand{\@nameuse{#1}}{#2}%
}{%
\renewcommand{\@nameuse{#1}}{\@nameuse{#2}}%
}%
}


Sample error output:

! LaTeX Error: \@nameuse {unidad} undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.

• It is worth pointing out that you should give your automatically created macros some "namespace". Imagine someone writes \wrapper{def}{ghi}. This is guaranteed to break LaTeX with your idea (and with Werner's solution as well). – yo' Sep 21 '15 at 6:44
• @yo' For example, putting them a prefix? If I write \wrapper{def}{ghi}, both the command and the value can have, I don't know, int_ as prefix to differentiate them among other valid TeX names. – logo_writer Sep 21 '15 at 13:44
• @logo_writer Exactly like that. I would recommend sticking to LaTeX2e standard and use @ in the name, something like logodata@#1. – yo' Sep 21 '15 at 13:55

The answer to your first question comes from the LaTeX kernel, which provides \@namedef specifically for this reason. Therefore, in your preamble you can use

\makeatletter
\let\wrapper\@namedef
\makeatother


and then use

\wrapper{abc}{def}% --> \def\abc{def}


Your use of \assignto seems different though, as it should convert the second argument to either a value (which may be text), or to a command. For this one could use some e-TeX:

\documentclass{article}

\makeatletter
\newcommand{\assignto}[2]{%
\ifcsname #2\endcsname
\@namedef{#1}{\csname #2\endcsname}%
\else
\@namedef{#1}{#2}%
\fi
}
\makeatother

\begin{document}

\newcommand{\abc}{ABC}

\assignto{AAA}{aaa}\AAA

\assignto{BBB}{abc}\BBB

\end{document}


To use a more LaTeX-like approach, you can use etoolbox which provides a host of wrapper macros:

\documentclass{article}

\usepackage{etoolbox}

\newcommand{\assignto}[2]{%
\ifcsdef{#2}{%
\csdef{#1}{\csuse{#2}}%
}{%
\csdef{#1}{#2}%
}%
}

\begin{document}

\newcommand{\abc}{ABC}

\assignto{AAA}{aaa}\AAA

\assignto{BBB}{abc}\BBB

\end{document}

• Your MWE works flawlessly! Thanks! It was the idea I had in mind! When I want to use it with datatool it loads data strangely as if it read the last value, though, as I iterate through the DB. Nonetheless, this basically works with "hardcoded" data, so I'm accepting this. Thanks again! – logo_writer Sep 21 '15 at 6:30

I'd avoid using the same interface for two distinct jobs. In your proposed syntax, \assignto{\foo}{text} would see that \text is defined (assuming you load amsmath) and you don't want that \foo expands to some mysterious value, do you?

\documentclass{article}

\makeatletter
\newcommand\assignto{\@ifstar\assignto@expand\assignto@simple}
\newcommand{\assignto@simple}[2]{\@namedef{#1}{#2}}
\newcommand{\assignto@expand}[2]{%
\expandafter\expandafter\expandafter\assignto@expand@aux
\expandafter\expandafter\expandafter{\csname#2\endcsname}{#1}}
\newcommand\assignto@expand@aux[2]{\assignto@simple{#2}{#1}}
\makeatother

\begin{document}

\newcommand{\abc}{ABC}

\assignto{AAA}{aaa}\AAA

\assignto{BBB}{abc}\BBB

\assignto*{CCC}{abc}\CCC

\end{document}


Now you have no ambiguity in what abc should mean.

• I see, with this I can choose whether to expand the value assigned or not. Nice! – logo_writer Sep 22 '15 at 0:35