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.

This question already has an answer here:

I want to define a command that defined other commands. A similar problem was considered in Dynamically define commands. However, I couldn't figure out how to adapt that solution to my needs because I need the functions that I dynamically define to be able to take arguments. Consider the following example. I want to be able to use \definecommand to dynamically define both \greetp and \greete. My attempt at a solution is given below, but commented out (I used question marks where I wasn't sure what to do). I would also like my dynamically defined commands to be able to take optional arguments if possible.

\documentclass{article}

\newcommand{\greetp}[1]{hello, #1.}
\newcommand{\greete}[1]{Hello, #1!}

%\newcommand{\definecommand}[2]
%{
%    \expandafter\def\csname#1p\endcsname{#2, ?.}
%    \expandafter\def\csname#1e\endcsname{#2, ?!}
%}
%\definecommand{greet}{Hello}

\begin{document}

\greetp{world}
\greete{world}

\end{document}
share|improve this question

marked as duplicate by Tobi, ChrisS, Jesse, Peter Jansson, Ludovic C. Dec 21 '13 at 10:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
I'm not sure wether we define it as duplicate or not … let the community decide it :-) –  Tobi Dec 21 '13 at 8:43

1 Answer 1

up vote 4 down vote accepted

You're almost there. You need to use the "##" syntax to refer to a nested argument:

\documentclass{article}

\newcommand\definecommand[2]{%%
  \expandafter\def\csname#1p\endcsname##1{#2$\rightarrow$##1}%%
  \expandafter\def\csname#1e\endcsname##1{#2$\leftarrow$##1}%%
}

\definecommand{greet}{Hello}
\pagestyle{empty}
\begin{document}

\greetp{A}

\greete{B}

\end{document}

enter image description here

share|improve this answer
    
Can you also add an example where the dynamically defined argument also takes an optional argument? Is it possible to use \newcommand to dynamically define my function? Or do I have to use a trick like the one in tex.stackexchange.com/questions/49056/optional-arguments-in-def. Also, I'm not sure what the protocol is here -- should I ask this in a new question? –  Stirling Dec 21 '13 at 6:12
    
@Stirling: It's very related, so for the sake of conciseness I suggest to edit your question accordingly. When extending the question, you should probably also untick the accept checkmark until your question has been answered completely. (To prevent frequent changes of the accepted answer, we usually wait for a few days before accepting. This also increases your chance of getting more alternatives and sometimes the really neat one just shows up late.) –  Daniel Dec 21 '13 at 7:15
    
@stirling It's certainly possible, but it would be nice to see an illustration of how you want to implement this. –  A.Ellett Dec 21 '13 at 16:12
    
@stirling It might be useful to look into key values to achieve the level of dynamism you want. But again, an example of the sort of thing you want would be useful to help point you in the right direction. –  A.Ellett Dec 21 '13 at 17:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.