Fortran 77: Specify more than one comment identifier in LaTeX

You aren’t selecting the Fortran-77 dialect in your listing. According to the language-definition manual, the c and C comments were removed for Fortran-90 and later, since otherwise CONTAINS could start a comment. The listing version of Fortran 77 does not define ! comments, which I believe your compiler is making available as an extension to Fortran-77.

Therefore, when you select language=[77]fortran, you don’t get ! comments, and when you select language=fortran, you don’t get c or C comments. However, when you select a language for an individual listing, it reloads that language’s comment rules and overrides your global setting in \listset. So, adding the missing definition in the global \lstset and removing the unnecessary language= from the individual listings works:

\documentclass{article}
\usepackage{libertinus} % Or your font of choice
\usepackage{listings,xcolor}

\lstset{language=[77]Fortran,
  showstringspaces=false,
  keywordstyle=\color{blue},
  stringstyle=\color{purple},
  commentstyle=\color{red},
  breakatwhitespace=false,
  morecomment=[l]!
}

\begin{document}

\begin{lstlisting}
    program main
    implicit integer (A-Z)
c   Asking it to calculate
    x = y+1 !The equation
    end program
\end{lstlisting}

\end{document}

Listing sample

And so does adding the missing comment style after loading the language. Note that, within an option, any setting containing brackets must be enclosed in braces.

\begin{lstlisting}[language={[77]fortran},
                   morecomment={[l]!}]

This is what you should do, removing the morecomment= line from \lstset, if you’re mixing listings in different languages and ! should not start a comment in all of them. If you have to do this a lot, you might define your own environment that wraps this.

You might, in theory, also define your own custom dialect of Fortran listings, for Fortran-77 with Fortran-90 comments.


Your morecomment setting is saying that comments begin with ! and end with c. Do this instead.

morecomment=[l]{!}

C is already defined as a comment in the fortran77 style so you shouldn't need to declare it.

All of this is untested.