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

I want to make a function that takes a function as a parameter and prints an integral with the function inside. I've tried this:

L[f_, a_, b_] := HoldForm[Integrate[f[x], {x, a, b}]]

L[E^-Sqrt[#] &, 0, 1]

But it gives me this:

$$\int_0^1 \left(e^{-\sqrt{\text{#1}}}\text{&}\right)[x] \, dx$$

I want the integrand to look normal, that's all, so I want the variable x to be substituted inside it, but not evaluated any further. Is this possible?

share|improve this question
It seems to work if you just remove HoldForm, since it's a delayed set, f is equal to your input function when it evaluates, and since x is only mentioned inside Integrate, there should be no problems. – jVincent May 14 '12 at 14:13
@jVincent The OP needs the integral to stay unevaluated, and merely display as an integral. – Szabolcs May 14 '12 at 14:14
My bad, I read through it to fast. – jVincent May 14 '12 at 14:15
up vote 12 down vote accepted

This works nicely:

L[f_, a_, b_] := HoldForm[Integrate[#, {\[FormalX], a, b}]] &[f[\[FormalX]]]

Note that I used \[FormalX] to prevent conflicts with the usual x, which may have had a previous definition. Try L[E^-Sqrt[#] &, 0, 1] with this definition:


share|improve this answer

This is what you need:

Block[{Integrate, HoldForm}, L[E^-Sqrt[#] &, 0, 1]]
share|improve this answer
Undergoing succintness training? +1 – Rojo May 15 '12 at 0:21
@Rojo I wish :) – Leonid Shifrin May 15 '12 at 8:33
fun = E^-Sqrt[#] &;

I'd suggest something simpler, like

L=Composition[HoldForm, Integrate];

So you would do

L[fun[x], {x, 0, 1}];

and you explicitly choose the integrand. Note that this version won't work if your integrand has a value. For that, look at the other good answers.

Furthermore, if you want the output to be evaluatable, so that if you copy-paste it or reevaluate the output cell you get the actual integral, just replace HoldForm with Defer

L=Composition[Defer, Integrate];
share|improve this answer

If you only want to change the displayed form of the expression, you can create a custom MakeBoxes call to format it as you want:

MakeBoxes[L[f_, a_, b_], StandardForm] ^:= 
RowBox[{SubsuperscriptBox["\[Integral]", ToBoxes[a], ToBoxes[b]], 
ToBoxes[f["x"]], RowBox[{"\[DifferentialD]", "x"}]}]

In this way, L[f,a,b] will remain an object in computations, but whenever the front-end displays it, it will look like you desired, just like Power[x,2] will display like a raised power, but remain the expression if you manipulate it.

share|improve this answer
I was looking at that, but the documentation about boxes is confusing and I don't understand it. – Matt Gregory May 14 '12 at 14:23
Boxes are the structures used to describe the displayed form of the content in the front-end. I agree that it's difficult to get a grasp of how it all works from the documentation. A nice way to get a feel for what boxes are is to select a cell and use Cell>Show Cell expression (or Shift-ctrl-E), this shows you the box form that is interpreted as the shown expression. – jVincent May 14 '12 at 14:42

You could use the following:

L[f_, a_, b_] := Block[{x}, With[{expr = f[x]}, HoldForm[Integrate[expr, {x, a, b}]]]]

Using Block makes sure that this will work even if x has a value. With is commonly used for expression injection.

share|improve this answer

Also it is possible to use ReplacePart and Defer:

L[f_, a_, b_] := ReplacePart[Defer[Integrate[1, {x, a, b}]], {1, 1} -> f[x]]
share|improve this answer

Here is one more way. Try:

 L[f_, a_, b_] :=
         Style[Defer@Integrate[f, {x, a, b}], 
               FontFamily -> "Tahoma", 
               FontSize -> 24, Bold

L[Exp[x], 0, \[Infinity]]

L[Sin[x], 0, \[Pi]]

Mathematica graphics

share|improve this answer

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.