# Create a macro that appends some code an existing command, with arguments

As I am working on improving my answer to Different approach to literate programming for LaTeX, I have created a macro that automatically processes two arguments and formats indexes accordingly. The arguments are strings (not control sequences), with #1 being the type of macro and #2 being the name of the macro.

Here is a sample of the macro I am using. It relies on etoolbox and xstring.

\newcommand{\set@macro@type}[2]{
\StrSubstitute{#2}{@}{"@}[\entryname]
% Default definitions
\def\macro@type{#1}
\def\macro@format{#2}
\def\macro@index{#2@\string\texttt{#2}}
% Specific type definitions
\ifstrequal{#1}{m}
{\def\macro@type{}
\def\macro@format{\textbackslash#2}
\def\macro@index{%
\entryname @\string\texttt{{\string\textbackslash}\entryname}}}{}
\ifstrequal{#1}{l}
{\def\macro@type{length}
\def\macro@format{%
\entryname @\string\texttt{{\string\textbackslash}\entryname}}}{}
\ifstrequal{#1}{e}
{\def\macro@type{environment}
\def\macro@format{\textbackslash#2\par\textbackslash end#2}
\def\macro@index{%
\entryname @\string\texttt{{\string\textbackslash}\entryname}}}{}}


However, I would like to make it so that the user can add new types of macros with an interface such as:

\addmacro{<name>}{<type>}{<format>}{<index>}


which should append the following code to my existing macro:

\ifstrequal{#1}{<name>}
{\def\macro@type{<type>}
\def\macro@format{<format>}
\def\macro@index{<index>}}


Ideally, the \addmacro command could be starred, too, as I have an additional switch to toggle depending on the user's wishes.

So, my question is: how can I implement a macro that appends some code at the end of the main macro, and that can be used repeatedly. I would prefer to keep using etoolbox if possible (I have seen \apptocmd in the documentation but can't get it to work). The main difficulty which I am encountering is with the arguments #1 and #2 of \set@macro@style, which the user must be able refer to in some way. How could this be implemented? An explanation would be very welcome too.

-

You could probably place an empty macro without arguments (\setmacrotypeend) at the end of \set@macro@type and append code to this with \ltx@GlobalAppendToMacro from the ltxcmds package. About necessary expansion see Comparing an argument to a string when argument is a result of a command with etoolbox. I did not test this, but for a comment it is too long and needs too much format.

\newcommand{\setmacrotypeend}{}

\newcommand{\set@macro@type}[2]{
... (unchanged as it was before)
\def\setmacrotypefirstargument{#1}
\setmacrotypeend%
}

\RequirePackage{ltxcmds}


Thank you very much for your answer. I adapted the general idea to etoolbox and it works fine. One question however: do you know how I should write a boolean switch in \addmacro (created earlier with newif, so something like \thingtrue)? \expandafter does not work there. –  ienissei Apr 5 '12 at 7:11
@ienissei \newif\ifname \newcommand{\addmacro}[4]{\ltx@GlobalAppendToMacro{\setmacrotypeend}{\ifname ... \else ...\fi}} \nametrue or \namefalse seems to work, i.e. I just do not understand it at the moment. Could you ask this as new question, please? (Even if I still would not see the issue, surely somebody here will provide a solution, which is more unlikely for a question in a comment.) (Maybe I should just get some sleep, better yet, should have gotten more of it last night, sorry.) –  Stephen Apr 5 '12 at 18:48