1

I like to parameterize the call to a macro. Please see the minimal example below. I think it is self-explaining.

Ultimately, I am searching for a way to call a macro by a string (kind of reflection).

\documentclass{article}

\def\hello{Hello}

\def\trello{Trello}

\def\test#1{#1llo}

\begin{document}

\test{ha} % should print Hallo \test{tre % should print Trello

\end{document}

aiquita
  • 93
  • 4
    Do you want \def\test#1{\csname #1llo\endcsname}? (The LaTeX version of this would be \newcommand*{\test}[1]{\csname #1llo\endcsname}. This only works if you add a closing brace to \test{tre to make it read \test{tre}.) \csname ...\endcsname allows you to 'build' the name of a macro/control sequence from 'variable input'. Note that \csname ...\endcsname will become \relax (i.e. basically do nothing) if the control sequence is undefined. It will not raise an error. https://tex.stackexchange.com/q/39380/35864 might be interesting. – moewe Jan 01 '22 at 16:56
  • 1
    Another option is to include \usepackage{etoolbox} and then you can define \def\test#1{\csuse{#1llo}} and \test{he} (note that this needs to be he, not ha as in your MWE) and \test{tre} (note that this needs the trailing }) will invoke the desired macros. – Peter Grill Jan 01 '22 at 20:04
  • 5

0 Answers0