I want to create a FORTRAN cell in Mathematica.

pr = "PROGRAM test
Integer::a, b
print*,  a, ' +', b, ' = ', a + b
print*,  a, ' *', b, ' = ', a * b
END Program test

GFortran[inp_] := (
Export["temp1.f90", inp, "Text"];
Column@ReadList["!gfortran temp1.f90 && a.exe", String])

The function works fine:

DeleteFile[{"temp1.f90", "a.exe"}]

enter image description here

Now with a cell

 pr, "Program",
 Evaluatable -> True, 
 CellEvaluationFunction -> (GFortran[#] &), 
 CellFrameLabels -> {{None, "Fortran"}, {None, None}}]]; 
SelectionMove[EvaluationNotebook[], Previous, CellContents])

enter image description here

File temp1.f90 is created, but not compiled, so there is no output. What could be wrong?


I changed GFortran to

GFortran[inp_] := (
Export["temp1.f90", inp, "Text"];
Get["!gfortran temp1.f90"];
Column@ReadList["!a.exe", String])

Now the FORTRAN cell will have output, but only if temp1.f90 was compiled before cell execution

  • $\begingroup$ What happens if you try something generic like ReadList["!pause", String], or a simple file copy, etc., to see if the command is being run? $\endgroup$
    – Mr.Wizard
    Jul 11, 2012 at 1:35
  • $\begingroup$ For CellEvaluationFunction -> ReadList["!pause", String] output is {Press any key to continue . . .}(,TextForm) $\endgroup$
    – FDSg
    Jul 11, 2012 at 1:45
  • $\begingroup$ By the way, I think you mean to have pr, "Program", in your TextCell expression. $\endgroup$
    – Mr.Wizard
    Jul 11, 2012 at 2:19
  • 1
    $\begingroup$ What happens if you use GFortran[inp_, _] := and CellEvaluationFunction -> GFortran ? $\endgroup$
    – Mr.Wizard
    Jul 11, 2012 at 2:21
  • $\begingroup$ It worked! Changed to GFortran[inp_, _] now everything compiles. $\endgroup$
    – FDSg
    Jul 11, 2012 at 2:32

1 Answer 1


Since the CellEvaluationFunction is supposed to accept two arguments, the second being the format type, I recommend defining:

GFortran[inp_, _] := . . .

and then using it directly:

CellEvaluationFunction -> GFortran

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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