Rendering LaTeX math from Fortran comments

Hi all
Does anybody know how can we use latex in fortran source code?


Welcome, Milad!

What exactly do you mean by use LaTeX in Fortran? Do you mean to convert LaTeX math to a Fortran expression? Or maybe to evaluate it and return the result? Or something else?

Hi @ELNS, please take a look at this link, I think this is what you are asking for?

I meant that how can we use latex syntax in fortran language comments , just like how we did it in python docstrings. For example consider that i wrote a subroutine which is implementation of a mathematical expression, so i would like to represent symbolic mode of that expression within my source code.

Or do you mean writing results in an output file using the LaTeX syntax ? And then calling the pdflatex compiler from Fortran ? This could be done with:

call execute_command_line ("pdflatex my_fortran_output.tex")

(Fortran 2008 and later)

can we use it for one liner mathematical expressions?

This one line command:

$ pdflatex "\documentclass{article} \begin{document} \begin{equation} f(x) = 0 \iff x = 1 \end{equation} \end{document}"

is generating a file whose default name is texput.pdf. But I don’t know if it is what you want…

I still don’t get it. Let’s consider this snippet:

real function square(x)
  ! Returns the square of a real number.
  ! $f(x) = x^2$
  real, intent(in) :: x
  square = x**2
end function square

Notice the LaTeX math in the “docstring”. Let’s assume what you are trying to do works. What does the result do and what does it look like?

1 Like

The objective could be to include LaTeX expressions in a documentation generated with a tool like Doxygen:

If you use the Sublime code editor, the SublimeFortran Package provides hover over popups for inline Latex math in comments.


Alternatively, I do want to provide good Fortran wrappers for SymEngine to be able to manipulate symbolic expressions from Fortran. I already started at, but didn’t have time to finish. If anybody wants to help, I would appreciate it.


That’s exactly what i meant.
Now what if we build the documentation of this subroutine using a tool such as sphinx-fortran?
will the symbolic mode of this latex line be generated ?

1 Like

I don’t know if sphinx-fortran can do it, but FORD can.


If you use the FORD documentation generator, it is possible to include Latex in comments, see here:


Thank you all for your valuable tips :pray: :pray: :pray:

1 Like

Not sure if I have understood the question, but if this is about documentation alone, Doxygen works well with LaTeX. There’s some more discussion here.