[Tex/LaTex] Fortran syntax highlighting in listings

codecolorformattinghighlightinglistings

I've got some Fortran 90 source code I want to put into my thesis appendix. For this I'm using the listings package. Is it possible to have colored syntax highlighting for the Fortran code?

Best Answer

You have to set the language and the kind of highlighting, e.g.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{lmodern}
\usepackage{listings}
\lstset{language=[90]Fortran,
  basicstyle=\ttfamily,
  keywordstyle=\color{red},
  commentstyle=\color{green},
  morecomment=[l]{!\ }% Comment only with space after !
}
\begin{document}

\begin{lstlisting}
! Der folgende Fortran-Code ist bei Wikipedia geklaut.
SUBROUTINE test( Argument1, Argument2, Argument3 )
   REAL,              INTENT(IN) :: Argument1
   CHARACTER(LEN= *), INTENT(IN) :: Argument2
   INTEGER,           INTENT(IN), OPTIONAL :: Argument3
   ! This makes sense
END SUBROUTINE
\end{lstlisting}
\end{document}

For more information about the used settings with \lstset see the manual of listings package.

BTW: There are several alternative packages for syntax highlighting. With fortran there seems to be even a package to print Fortran programs. But I've never used this, it cannot be used for syntax highlighting and was made to be used with plainTeX. So maybe if you want to try an alternative, e.g., highlight would be ab better suggestion.