Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

All is in the title. I would like to add a color to the text before a character with listing package.

For example, I would like to add a color to text between the beginning of a line and : or =, in a makefile.

I tried to use moredelimiters but I did not succeed.

A working example

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}
\definecolor{keycolor}{RGB}{172, 42, 42}
\definecolor{mbleu}{RGB}{64,96,127}
\definecolor{vimvert}{RGB}{46, 139, 87}

\lstdefinestyle{global}{
  basicstyle=\ttfamily\scriptsize\color{black!90},%
  stringstyle=\itshape\color{magenta},%
  showstringspaces=false,%
  keywordstyle={\bfseries\color{keycolor}},%
  commentstyle=\color{blue}\slshape,%
  framexleftmargin=1mm,%
  backgroundcolor=\color{black!2},%
}

\lstdefinestyle{makefile}{
  style=global,%
  morecomment=[l][commentstyle]{\#},%
  emphstyle={\color{vimvert}},%
  moredelim=[s][emphstyle]{\$(}{)}%
}

\begin{document}

\begin{lstlisting}[style=makefile]
.SUFFIXES: .inc .f .f90 .F

# all CPP processed fortran files have the extension .f90
SUFFIX=.f90

#-----------------------------------------------------------------------
# this release should be fpp clean
# we now recommend fpp as preprocessor
# if this fails go back to cpp
CPP_=fpp -f_com=no -free -w0 $*.F $*$(SUFFIX)

vasp: $(SOURCE) $(FFT3D) $(INC) main.o 
    rm -f vasp
    $(FCL) -o vasp main.o  $(SOURCE)   $(FFT3D) $(LIB) $(LINK)
makeparam: $(SOURCE) $(FFT3D) makeparam.o main.F $(INC)
    $(FCL) -o makeparam  $(LINK) makeparam.o $(SOURCE) $(FFT3D) $(LIB)
zgemmtest: zgemmtest.o base.o random.o $(INC)
    $(FCL) -o zgemmtest $(LINK) zgemmtest.o random.o base.o $(LIB)
dgemmtest: dgemmtest.o base.o random.o $(INC)
    $(FCL) -o dgemmtest $(LINK) dgemmtest.o random.o base.o $(LIB) 
ffttest: base.o smart_allocate.o mpi.o mgrid.o random.o ffttest.o $(FFT3D) $(INC)
    $(FCL) -o ffttest $(LINK) ffttest.o mpi.o mgrid.o random.o smart_allocate.o base.o $(FFT3D) $(LIB)
kpoints: $(SOURCE) $(FFT3D) makekpoints.o main.F $(INC)
    $(FCL) -o kpoints $(LINK) makekpoints.o $(SOURCE) $(FFT3D) $(LIB)

clean:  
    -rm -f *.g *.f *.o *.L *.mod ; touch *.F
\end{lstlisting}

\end{document}
share|improve this question
Could you please add a MWE, so that we have a basis for experimenting? – Daniel Nov 6 '12 at 13:30
I add a short example. Actually, at then end I would like the code to look like vim syntax coloring. – Ger Nov 6 '12 at 14:01
@hpesoj626: I think your comment is the most common solution. Can you provide an answer please? – Marco Daniel Mar 9 at 19:51
@MarcoDaniel Done. Thanks. – hpesoj626 Mar 10 at 3:03

1 Answer

.SUFFIXES is a keyword in the pre-defined language [gnu]make. SUFFIX is pre-defined in ACSL but loading the languages through a combination of language and alsolanguage and classoffset keys don't seem to give the desired effect. I suggest that you define your makefile as in the following work-arounds.

Work-around 1

In this solution, the colon, : is also colored. Just set up your \lstdefinestyle as follows:

\lstdefinestyle{global}{
basicstyle=\ttfamily\scriptsize\color{black!90},%
stringstyle=\itshape\color{magenta},%
showstringspaces=false,%
keywordstyle={[1]\bfseries\color{keycolor}},%
keywordstyle={[2]\bfseries\color{mbleu}},%
commentstyle=\color{blue}\slshape,%
framexleftmargin=1mm,%
backgroundcolor=\color{black!2},%
}

\lstdefinestyle{makefile}{
otherkeywords={.SUFFIXES},
alsoletter={:},
morekeywords=[1]{SUFFIX, CPP_},
morekeywords=[2]{vasp:,makeparam:,zgemmtest:,dgemmtest:,ffttest:,kpoints:,clean:},
style=global,%
morecomment=[l][commentstyle]{\#},%
emphstyle={\color{vimvert}},%
moredelim=[s][\color{vimvert}]{\$(}{)}%
}

Output

enter image description here

Explanations

  • otherkeywords defines keywords that contain other characters, or start with digits.
  • alsoletter makes : a letter, otherwise, words containing these symbols will not be recognized as words.
  • You can make separate lists of morekeywords or moreemph by the syntax morekeywords=[<number>]{<list of keywords>} or moreemph=[<number>]{<list of emphs>}. Here I have chosen keywords but you can modify according to your preference. You have then to match this list with a corresponding keywordstyle or emphstyle.

Work-around 2

You might not want the colon colored. In this case, you can use (guess what?) moredelim as in moredelim=[is][\color{mbleu}]{/*}{*/}, From the manual:

More general delimiters can be defined by the key moredelim. Legal types are l and s. These types can be preceded by an i, but this time only the delimiters are discarded from the output.

And here is the full code

%\documentclass{article}
\documentclass[preview,border=5]{standalone}

\usepackage{geometry}
\geometry{margin=1in}
\usepackage[procnames]{listings}
\usepackage{xcolor}
\definecolor{keycolor}{RGB}{172, 42, 42}
\definecolor{mbleu}{RGB}{64,96,127}
\definecolor{vimvert}{RGB}{46, 139, 87}

\lstdefinestyle{global}{
basicstyle=\ttfamily\scriptsize\color{black!90},%
stringstyle=\itshape\color{magenta},%
showstringspaces=false,%
keywordstyle=\bfseries\color{keycolor},%
commentstyle=\color{blue}\slshape,%
framexleftmargin=1mm,%
backgroundcolor=\color{black!2},%
    }

\lstdefinestyle{makefile}{
otherkeywords={.SUFFIXES},
morekeywords={SUFFIX, CPP_,},
moredelim=[is][\color{mbleu}]{/*}{*/},
style=global,%
morecomment=[l][commentstyle]{\#},%
emphstyle={\color{vimvert}},%
moredelim=[s][\color{vimvert}]{\$(}{)}%
}

\begin{document}

\begin{lstlisting}[style=makefile]
.SUFFIXES: .inc .f .f90 .F

# all CPP processed fortran files have the extension .f90
SUFFIX=.f90

#-----------------------------------------------------------------------
# this release should be fpp clean
# we now recommend fpp as preprocessor
# if this fails go back to cpp
CPP_=fpp -f_com=no -free -w0 $*.F $*$(SUFFIX)

/*vasp*/: $(SOURCE) $(FFT3D) $(INC) main.o 
        rm -f vasp
        $(FCL) -o vasp main.o  $(SOURCE)   $(FFT3D) $(LIB) $(LINK)
/*makeparam*/: $(SOURCE) $(FFT3D) makeparam.o main.F $(INC)
        $(FCL) -o makeparam  $(LINK) makeparam.o $(SOURCE) $(FFT3D) $(LIB)
/*zgemmtest*/: zgemmtest.o base.o random.o $(INC)
        $(FCL) -o zgemmtest $(LINK) zgemmtest.o random.o base.o $(LIB)
/*dgemmtest*/: dgemmtest.o base.o random.o $(INC)
        $(FCL) -o dgemmtest $(LINK) dgemmtest.o random.o base.o $(LIB) 
/*ffttest*/: base.o smart_allocate.o mpi.o mgrid.o random.o ffttest.o $(FFT3D) $(INC)
        $(FCL) -o ffttest $(LINK) ffttest.o mpi.o mgrid.o random.o smart_allocate.o base.o $(FFT3D) $(LIB)
/*kpoints*/: $(SOURCE) $(FFT3D) makekpoints.o main.F $(INC)
        $(FCL) -o kpoints $(LINK) makekpoints.o $(SOURCE) $(FFT3D) $(LIB)
/*clean*/:  
        -rm -f *.g *.f *.o *.L *.mod ; touch *.F
\end{lstlisting}

\end{document}

Output

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.