# How can i execute several Commands in one text file and use it in notebook?

I want to execute several commands in one text file and use it in notebook by Get[] command. for example:

a=Plot[x^2 ,{x,0,2}];
b=ListPlot[{{0.327081,9.94393},{0.327081,0.102804}},Joined->True];
Show[a,b,PlotRange->All,,PlotLabel->hy]
a1=Plot[26x^2 - 19x,{x,0,3}];
b1=Plot[-2x^2 ,{x,2,3}];
Show[a1,b1,PlotRange->All,PlotLabel->sx]


I want to see both plots.But Mathematica run only last command in text file

• Why can't you run these commands from the notebook? May 2 '13 at 2:53
• Because my code is too long.i want to short it May 2 '13 at 3:05

Well, I know of a simple solution: Combine both graphics into a list of graphics.

a=Plot[x^2 ,{x,0,2}];
b=ListPlot[{{0.327081,9.94393},{0.327081,0.102804}},Joined->True];
a1=Plot[26x^2 - 19x,{x,0,3}];
b1=Plot[-2x^2 ,{x,2,3}];
{Show[a,b,PlotRange->All,PlotLabel->hy],Show[a1,b1,PlotRange->All,PlotLabel->sx]}


Kind of simple, I know, but it works. Use //Column or //Row or //TableForm if you need more structure for your graphs. I personally enjoy TableForm the most. As in:

{Show[a,b,PlotRange->All,PlotLabel->hy],Show[a1,b1,PlotRange->All,PlotLabel->sx]}//TableForm


There's another way also! By using Print[]:

a=Plot[x^2 ,{x,0,2}];
b=ListPlot[{{0.327081,9.94393},{0.327081,0.102804}},Joined->True];
Print[Show[a,b,PlotLabel->hy]]
a1=Plot[26x^2 - 19x,{x,0,3}];
b1=Plot[-2x^2 ,{x,2,3}];
Print[Show[a1,b1,PlotRange->All,PlotLabel->sx]]


Don't forget to remove the PlotRange->All on your first Show, it leads to errors because listplot and plot are different. You may use ListLinePlot also instead of Joined->True in listplot.

Your assertion is not correct (that Mathematica only runs the final line). Put your code in a text file (I called it "text.txt"). Then

<<"test.txt"

and it will run the code in the text file. It will not display the plots. You can do that from your notebook. For instance typing

a


shows the plot you have defined as a. You need to execute the Show commands in a notebook, though all the calculations are done when you Get the "test.txt" file. Of course we have already answered this same question for you here yesterday https://mathematica.stackexchange.com/questions/24338/how-can-i-decrease-number-of-lines-of-code-in-a-notebook/24340#24340