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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 3 down vote accepted

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)


share|improve this answer
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 posting your answer, you agree to the privacy policy and terms of service.

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