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.
One way to do achieve this would be to define otherkeywords
for these cases.
otherkeywords={attribute=, xmlns=}
Code:
\documentclass[a4paper, 12pt]{article}
\usepackage{listings, color}
\definecolor{forestgreen}{RGB}{34,139,34}
\definecolor{orangered}{RGB}{239,134,64}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\lstdefinestyle{XML} {
language=XML,
extendedchars=true,
breaklines=true,
breakatwhitespace=true,
emph={},
emphstyle=\color{red},
basicstyle=\ttfamily,
columns=fullflexible,
commentstyle=\color{gray}\upshape,
morestring=[b]",
morecomment=[s]{<?}{?>},
morecomment=[s][\color{forestgreen}]{<!--}{-->},
keywordstyle=\color{orangered},
stringstyle=\ttfamily\color{black}\normalfont,
tagstyle=\color{darkblue}\bf,
morekeywords={attribute,xmlns,version,type,release},
otherkeywords={attribute=, xmlns=},
}
\begin{document}
\begin{lstlisting}[style=XML]
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<note attribute="main" xmlns="http://www.w3.org/1999/xhtml">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Dont forget me this weekend!</body>
</note>
\end{lstlisting}
\end{document}
Best Answer
Seems like a job for either the
listings
or theminted
packages; both are customizable and offer Java as one of the predefined languages.Using
listings
you can define your own set of rules for syntax highlighting for Android (see Sections 3.2 and 4.18 Language definitions of the package documentation)); Android is not one of the predefined languages in minted either, so you will also have to define a new language.Additionally, with each one of the packages you can easily produce a list of your listings using
\lstlistoflistings
(withlistings
) or\listoflistings
(withminted
), and you can input your code from external files using\lstinputlisting
(withlistings
) or\inputminted
(withminted
).Since
minted
usesPygments
, it provides far superior syntax highlighting compared tolistings
; on the other hand,listings
offers automatic breaking of long lines and, as far as I know, this feature is not offered byminted
.