TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

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

This question already has an answer here:

I've declared a lot of math operators lately, e.g. \DeclareMathOperator{\Hom}{Hom}, \DeclareMathOperator{\End}{End} and so forth. This is getting a bit annoying and seems automatable: what I'd really like is to have this behavior for every capitalized word, e.g. if I type \Foo in a document I'd like for this to be interpreted as a math operator without having to specify it in advance. Is there an easy way to do this?

share|improve this question

marked as duplicate by Peter Grill, Svend Tveskæg, Peter Jansson, Gonzalo Medina, egreg Feb 12 '14 at 20:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Every command must be defined before usage. – egreg Feb 12 '14 at 19:04
@egreg: but can't I do something like define a large class of operators at once in a loop? I think I remember seeing someone do something like this. (The hackiest thing would be to, say, declare all three- and four-letter capitalized math operators in advance.) – Qiaochu Yuan Feb 12 '14 at 19:07
Does that mean that \RequirePackage should be reinterpreted as a math operator ;^) ? – Steven B. Segletes Feb 12 '14 at 19:07

Example for such a loop: \DeclareMathOperators takes a comma separated list of math operator names without backslash and turns them to math operator commands. Spaces and empty entries in the list are removed.


    % remove spaces
      \@firstofone{\expandafter\zap@space\@tmp} \@empty
      % declare math operator
      % \wlog prints into the .log file
      % or \typeout can be used for console output                      
      \wlog{* Math operator "\@backslashchar \@tmp" defined.}%

share|improve this answer

You mean something like this? You cannot get closer to what you want than defining all used terms in a cycle.



\@for \op:={Dom,Hom}\do{


\[ \Dom A \neq \Hom B \]


We use LaTeX's \@for since pgffor makes grouping which makes definitions local and then it doesn't work. For each element of the list, we prepare the definition as \DMOtemp->\DeclareMathOperator{\Dom}{Dom}, and then we execute it. The \@ifundefined is quite necessary since if we wanted to re-define something, it would have bad consequences (the thing being re-defined would get executed at the line with \edef and the whole thing would probably fail). On the other hand, \csname blabla\endcsname for \blabla being undefined leaves just \blabla there, which is quite correct.

share|improve this answer

Here's some example how to define a comma separated list of math operators:






share|improve this answer

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