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

I have some latex code that goes a bit like this:

% Sp defines a generic command for an arbitrary space, 
% and all of the others just delegate the bulk of their
% work to it so that when I decide to change the formatting,
% I can in one place.

% Some common vectors, so I don't have to repeatedly type
% \vec{x}_1:

The work I'm doing is in linear algebra, where I need to use these vectors a lot. So, defining shorter commands seems pretty natural. But when papers start getting longer, I end up with either half the alphabet being defined as different commands -- one for each letter.

So I tried to define a command that just defines another command so that I could just put it in a loop...


and several variations thereupon... But the closest I came to a working model was when the error that the compiler gave me changed to missing \begin{document}.

Can someone explain or give a source to learn more about what I should be doing (or if there's a better way after all) to define dynamic commands? Note that this specific instance is just one example. I'm interested in doing LaTeX metaprogramming, not rendering vectors nicely specifically.

share|improve this question
up vote 12 down vote accepted

Using \# simply defines \#. You need something that, given a name, becomes the command with that name. Such a thing exists as a TeX primitive command: \csname ... \endcsname

  Some vectors: $\x$, $\y$.

If you have to define that many vectors, you might consider representing them by bold letters rather than \vec. Formulas look less cluttered that way.

share|improve this answer
Perfect! Thanks. Yes, I agree, I have \renewcommand{\vec}[1]{\textbf{#1}} defined above this section for just that reason -- Not sure if that's the best way but it seemed logical at the time. Much obliged. Lot to learn about latex. – scott_fakename Nov 17 '13 at 8:08

To add to Dan's answer, you may do the following to avoid the repeating \MkVec{..}'s.

    \expandafter\MkVec \i     }

Now, with this implementation, you can maintain/modify your list of related macros in a single action. Should you decide \x should mean something else, simply take it off \lst. (For me, \x is \times.) whereas adding new entries only requires the typing of single leeters. In a different scenario, say you decide later that the macro should really be called \xvec, \yvec, etc. instead. Do this


Then, in the definition of \MkVec, add \str immediately after #1.


The for loop remains the same.

The string can be added before #1 as well. So by extension, you can generate lists of micro that replace pages of definitions like

\def\dx{\partial x}
\def\dy{\partial y}
\def\dz{\partial z}

where each entry has to be maintained individually.

However, I haven't had any luck with Greek letters with this approach.

Oh, you might be interested in getting this package which enables you to add numbers after the string name of a macro. The package includes a good introduction as to how to use it and it offers us insights of meta-programming with lateX.

EDIT: Commands such as \ddix and \dix do interfere with each other under the \MkVec structure. You would need to do the for loop immediately after each instance of \MkVec like functions. For example

        \expandafter \MKa \i
        \expandafter \MKb \i    }  

may NOT work. But the following will.

        \expandafter \MKa \i    }
        \expandafter \MKb \i    }  

One last thing. I think I need to include this for others who may come across this discussion. To use @ in document, one would need to include \makeatletter before using it and \makeatother when finished. In packages, for example, .sty files, the declaration is optional and should be avoided.

share|improve this answer
Thanks, Joseph! I couldn't figure out how to put blocks of codes together. And now I know! – Argyll Jan 10 '14 at 9:01

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.