# How to create two \newcommands from one variable #1?

Consider the following code snippets:

a) Text to be printed in some document using command \defPrimitiveRoots

 \Snippet{\defPrimitiveRoots}{Integers with primitive roots}{
Let $n,k$ be integers, and $p$ a prime. Then $n$ possesses primitive roots if and only if $n$ is of the form $2, 4, p^k$ or $2p^k$.
}


b) A further call to \srem

 \newcommand{\Snippet}[3]{
\newcommand{#1}{
\srem{#2}{
#3
}
}
}


c) A further call to the \shadedrem environment.

 \newcommand{\srem}[2]{
\textbf{#1} \newline #2
}


Finally, in a document the text is written with

 \defPrimitiveRoots


I want to automatically make two commands 1) \cshPrimitiveRoots ( which is the same as \defPrimitiveRoots above ) and 2) \beaPrimitiveRoots where the text is formatted such that it prints nicely on a beamer slidesheet.

Question: I suppose I have to change this part of the code

 \newcommand{\Snippet}[3]{
\newcommand{#1}{
\srem{#2}{
#3
}
}
}


to something like:

 \newcommand{\Snippet}[3]{
% #1 = defPrimitiveRoots this should become cshPrimitiveRoots
% PrimitiveRoots is of course a variable.
\newcommand{#1}{
\srem{#2}{
#3
}
}

% #1 = defPrimitiveRoots this should become beaPrimitiveRoots
\newcommand{#1}{
% I know how to do setup for beamer slide here.
}
}


How is this done?

• Your question is a little bit unclear, but I think you mean \expandafter\newcommand\csname csh#1\endcsname{....} etc. – user31729 Nov 5 '15 at 12:40
• Apparently my solution was useless to you, so I deleted it -- it was working for me but due to code unavailable to me of no use to you :-( – user31729 Nov 5 '15 at 14:36

\documentclass{article}

\makeatletter
\newcommand{\SnippetA}[3]{%
\@namedef{csh#1}{\srem{#2}{#3}}%
\@namedef{bea#1}{\srem{#2}{ #3 }}%
}%

\newcommand{\Snippet}[3]{%
% #1 = defPrimitiveRoots this should become cshPrimitiveRoots
% PrimitiveRoots is of course a variable.
\@namedef{csh#1}{\srem{#2}{#3}}%
% #1 = defPrimitiveRoots this should become beaPrimitiveRoots
\@namedef{bea#1}{%
% I know how to do setup for beamer slide here.
}%
}
\makeatother
\providecommand\srem[2]{%
This is the first argument: #1\par
This is the second argument: #2}

\begin{document}

\Snippet{PrimitiveRoots}{Arg one}{Arg two}

\SnippetA{APrimitiveRoots}{Arg one}{Arg two}

\cshPrimitiveRoots

\beaPrimitiveRoots

\cshAPrimitiveRoots

\beaAPrimitiveRoots
\end{document}

• I am going to try this. – nilo de roock Nov 5 '15 at 13:37
• defPrimitiveRoots is in .tex file and uses a package topics which contains the \Snippet command the topics package uses \input to read the actual definition snippet text. - I added the SnippetA code and everything compiles. Then I defined the snippet using SnippetA and I got an undefined control sequence in the snippet definition file. – nilo de roock Nov 5 '15 at 13:50
• give a complete example – user2478 Nov 5 '15 at 13:54
• I think I got it working now :-) If in error I update the question with all the files. – nilo de roock Nov 5 '15 at 13:56
• I got it working, thank you very much for answering. – nilo de roock Nov 5 '15 at 17:46