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.

    % Default definitions
    % Specific type definitions
            \entryname @\string\texttt{{\string\textbackslash}\entryname}}}{}
            \entryname @\string\texttt{{\string\textbackslash}\entryname}}}{}
         \def\macro@format{\textbackslash#2\par\textbackslash end#2}
            \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:


which should append the following code to my existing macro:


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.


... (unchanged as it was before)


  • 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
  • Sure, thank you for your answer :). I will give it a bit more thought and eventually ask a new question about it. – ienissei Apr 5 '12 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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