Mathematica Stack Exchange is a question and answer site for users of Mathematica. 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

Is there a nice way to define output formatting for my expression, a product from n = k to j with n ≠ m? I would like to use something like this here (however Mathematica doesn't evaluate it):

$$\prod _{n=k,n\neq m}^j \frac{\lambda _n}{\lambda _n-\lambda _m}$$

My work around at the moment is: (adopted from Sum or Product with Exclusions)

$$\prod _n^{\text{DeleteCases[Range[k, j], m]}} \frac{\lambda _n}{\lambda _n-\lambda _m}$$

share|improve this question

To make it possible to enter the expression the way you want, and also display it that way, I'll create an InputAlias for it similar to the already existing shortcut escprodtesc. Then I also define the format for TraditionalForm output to look the way you enter it.

The first step is to define your choice of modified product function, called restrictedProduct here. I decided to go with the approach based on Piecewise in which factors for which the added condition is violated are replaced by 1. Before implementing the actual product, I also prevent it from evaluating its arguments and to work with local variables as Product does:

SetAttributes[restrictedProduct, HoldAll]
   restrictedProduct] = {"LocalVariables" -> {"Product", {2, 2}}, 
   "ArgumentsPattern" -> {_, _, _}};

restrictedProduct[f_, range_, condition_] := 
 Product[Piecewise[{{f, condition}, {1, True}}], range]

restrictedProduct /: 
 MakeBoxes[restrictedProduct[f_, {i_, i0_, i1_}, condition_], 
  TraditionalForm] :=
 TemplateBox[{ToBoxes[i], ToBoxes[i0], RowBox[{ToBoxes[condition]}], 
   ToBoxes[i1], ToBoxes[f]},
    DisplayFunction :> (RowBox[{
                    RowBox[{#, "=", #2}]}, {#3}}], #4], #5}] & ),
    InterpretationFunction :> (RowBox[{"restrictedProduct", "[", 
            RowBox[{#5, ",", "{", #, ",", #2, ",", #4, "}", ",", #3}], 
            "]"}] & )]

The last definition is the output format in TraditionalForm - it contains a TemplateBox which allows it to become an editable expression that can be executed after you copy such output into a new input cell. The DisplayFunction and InterpretationFunction options are crucial to associate the two-dimensional box form with the input arguments of the function restrictedProduct.

Now I use the same association in defining the input alias that can be set for the current notebook as follows:

aliases = Options[EvaluationNotebook[], InputAliases];
newAliases = 
  Join[InputAliases /. 
    aliases, {"condProd" -> 
     TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]", 
       "\[Placeholder]", "\[Placeholder]", "\[Placeholder]"}, 
      "conditionalProduct", DisplayFunction :> 
            GridBox[{{RowBox[{#1, "=", #2}]}, {#3}}], #4], #5}] &),
      InterpretationFunction :> (RowBox[{"restrictedProduct", "[", 
           RowBox[{#5, ",", "{", #1, ",", #2, ",", #4, "}", ",", #3}],
            "]"}] &)
SetOptions[EvaluationNotebook[], InputAliases -> newAliases];

Now we can start using this. In a new input cell, type



In the template that appears, enter the desired limits, and note that the condition is inserted in the bottom box.



Instead of evaluating the expression, you can now also display it in held form as follows:


$$\prod_{\stackrel{i = 1}{i\ne 3}}^{10} i$$

You can then, if needed, copy and paste this output to get at first the unevaluated expression in StandardForm, which then can be evaluated too:

restrictedProduct[i, {i, 1, 10}, i != 3]


share|improve this answer
Wow, I assumed I just missed a build-in capability of Mathematica, but it's quite a bit more difficult. Thank you very much indeed! – AteTheSputnik Jan 19 '13 at 8:11

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.