4

I want to colorize my source code. I followed this page and everything works fine.

My problem is that I am using 4 different programming languages: Have I to define the \lstset{} tag separately for each language or is there a way to declare more than one programming language within its language field?

\lstset{ %
  %...
  language=java,              % this works only for one programming language. 
                              % How to declare more than one? 
                              % Or have I to declare \lstset{} for each PL? 
  %...
}
5

Ideally one should define a new listings environment for each of the languages. That is, create a listing for python, one for java, one for c and one for php. For each of these, we use \lstnewenvironment:

\lstnewenvironment{python}
  {\lstset{language=python,...}}
  {}
\lstnewenvironment{java}
  {\lstset{language=java,...}}
  {}
\lstnewenvironment{clang}
  {\lstset{language=c,...}}
  {}
\lstnewenvironment{php}
  {\lstset{language=php,...}}
  {}

where ... denotes other tailored settings specific to the language in question. For more detail on this, see the section 5.14 Environments of the listings documentation.

The above now allows you to use (say)

enter image description here

\documentclass{article}
\usepackage{listings}

\lstnewenvironment{python}
  {\lstset{language=python}}
  {}

\begin{document}

\begin{python}
print "This line will be printed."
\end{python}

\end{document}
  • Thank you sir. One last thing if I may: you said ideally: does this mean it is impossible to declare the 4 languages within the field language? To be honest, I tried several combinations but no one worked, I got errors. – user72507 Jan 6 '16 at 8:04
  • 1
    @Begueradj: Ideally refers to the fact that your LaTeX code will match the syntax that you're trying to achieve. It's more clear to write python code inside a python environment than it would be to write it in a standard listings environment where you've \lstset{...} the language in the preamble. You'd have to switch between languages otherwise. – Werner Jan 6 '16 at 8:08
3

Of course there also exists a solution with minted

Therefor you can define different code environments which is pretty easy with minted

\usepackage{minted}
\usemintedstyle{vs}

\newminted{kotlin}{%
    breakbytoken,%
    breaklines,%
    autogobble,%
    frame=lines,%
    label=Kotlin,%
    framesep=2\fboxsep%
}

\newminted{mysql}{%
    breakbytoken,%
    breaklines,%
    autogobble,%
    label=SQL,%
}

For every environment you are able to define different parameters.

To use your defined environments one uses \begin{language+'code'}

\begin{kotlincode}
        data class Customer(val name: String, val email: String, val company: String)
\end{kotlincode}

The full example would look like this:

\documentclass{article}

\usepackage{minted}
\usemintedstyle{vs}

\newminted{kotlin}{%
    breakbytoken,%
    breaklines,%
    autogobble,%
    frame=lines,%
    label=Kotlin,%
    framesep=2\fboxsep%
}

\newminted{mysql}{%
    breakbytoken,%
    breaklines,%
    autogobble,%
    label=SQL,%
}

\begin{document}
    \begin{kotlincode}
            data class Customer(val name: String, val email: String, val company: String)
    \end{kotlincode}
\end{document}
  • Indeed, that would be a perfect solution but that package does not work on my OS. Thank you very much for the effort and the idea though. – user72507 Jan 6 '16 at 12:31
  • 1
    I assume you don't have pygments installed? This works on every OS but requires python – Rico Jan 6 '16 at 12:33
  • 1
    I installed pygments and did a few settings. Now minted works perfectly fine. Thank you very much for the help. – user72507 Jan 10 '16 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy