There are at least two approaches to follow this idea with multiple supporting Emacs modules.
You may write code in one buffer, and subsequent transition into the other one (
C-x o) as demonstrated in many of the examples e.g., by Cyprien Rusu’s Fortran video tutorials (example 4:30 mm:ss tuto 2) for compilation and execution
You may opt for Emacs org mode (a modern installation of Emacs already includes this as a major mode by default) to write/compile/execute your Fortran code in an
For illustration of option 2, I add file
example.org as a MWE file below:
#+OPTIONS: toc:nil # disable a table of content if exported to .html
* This is a chapter
Text may be normal, /italic/, *bold*, _underlined_, or =monospace font=.
The following defines a simple Fortran program. Current Emacs orgmode eases
the input of such a block about /source code/ by input =C+c C+ s=.
With =fortran=, the syntax highlighting is enabled. For the code's eventual
execution, it is exported into file =hello.f90=, in orgmode's parlance
"tangled", which will reside in the same folder as the current .org mode.
#+begin_src fortran :tangle hello.f90
integer :: i
do i = 1, 5
if (mod(i, 2) == 0) then
print *, i, " is an even number"
print *, i, " is an odd number"
end program EvenNumbers
The following block calls the compiler with bash. Move the cursor to the
block, and execute the command by =C+c C+c=.
gfortran hello.f90 -o check -Wall -Wpedantic
The executable is used (again =C+c C+c=), the additional parameter in the
header instructs the format shall be like the one obtained on the CLI:
#+begin_src sh :results output
: 1 is an odd number
: 2 is an even number
: 3 is an odd number
: 4 is an even number
: 5 is an odd number
(Apparently, discourse does not yet recognize the org format.)
I like that this approach provides syntax highlighting, and increases the indentation for the next line once the condition is defined. It is possible to tangle multiple separate blocks eventually into one source code file in common; this allows to keep some annotations (like commenting lecture notes) with cross references and personal comments separate from the
.f90 source code eventually shared. Or you opt for the export of this plain ASCII file from Emacs in as
.tex (and if you have
.html , e.g., by
C-c C-e h o.
Let’s assume I replace
else if in the Fortran block and tangle this again. The second block (about the compilation) is going to report the anticipated warnings in a buffer opened automatically as soon as I call
John Kitchin (CMU) is one of the proponents of this approach of reproducible research and demonstrates some benefits in his brief video here (at 11:00 mm:ss onward for basics, at 14:09 mm:ss a specific example for Fortran).