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}$$


1 Answer 1


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]


  • $\begingroup$ 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! $\endgroup$ Commented Jan 19, 2013 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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