[Tex/LaTex] How to install new listing language definitions

linuxlistingsmiktextexlivewindows

I Would like to extend the list of supported languages for lstlisting environment from listings package. I have a definition – for example pddl language defined in *.sty file.

How to add this language definition to my tex installation? I would like to know the procedure both for Windows (MikTex) and linux (TexLive)

On Windows, I was already partially succesful. I enabled the new language by adding the *.sty file to C:\Program Files (x86)\MiKTeX 2.9\tex\latex\listings folder. However, it worked only after running the MikTex update.

So what procedure is correct for Windows and what for linux?

EDIT: mimimum example:

\documentclass[a4paper,12pt]{article}
\usepackage{listings}   
\begin{document}
\begin{lstlisting}
(:durative-action load-truck
    :parameters (?obj - obj ?truck - truck ?location - location)
    :duration (= ?duration 2)
    :condition (and
        (over all (at ?truck ?location))
        (at start (at ?obj ?location))
    }
    :effect(and
        (at start (not (at ?obj ?location)))
        (at end (in ?obj ?truck))
    )
)
\end{lstlisting}
\end{document}

Target (add [language=pddl]):

\documentclass[a4paper,12pt]{article}
\usepackage{listings}   
\begin{document}
\begin{lstlisting}[language=pddl]
(:durative-action load-truck
    :parameters (?obj - obj ?truck - truck ?location - location)
    :duration (= ?duration 2)
    :condition (and
        (over all (at ?truck ?location))
        (at start (at ?obj ?location))
    }
    :effect(and
        (at start (not (at ?obj ?location)))
        (at end (in ?obj ?truck))
    )
)
\end{lstlisting}
\end{document}

This leads to Couldn't load requested language error without language installation.

Best Answer

Quoting from the manual of the listings package, p. 43:

Where should I put my language definition?

If you need the language for one particular document, put it into the preamble of that document. Otherwise create the local file lstlang0.sty or add the definition to that file, but use \lst@definelanguage instead of \lstdefinelanguage.

I would rephrase the last sentence as follows:

“Otherwise, add the definition to the local file lstlang0.sty (creating it if necessary), but use \lst@definelanguage instead of \lstdefinelanguage.”

And where should “local files” like lstlang0.sty be stored (and created if necessary)? There are two possibilities: the personal texmf tree and the local (machine-wide) texmf tree.

  • Use the local (machine-wide) texmf tree if you have enough administrative priviledges and you want to make a file available to all users of your computer. To learn where the root of the local texmf tree is located on your machine, type

    kpsewhich -var-value TEXMFLOCAL
    

    at a terminal (emulator) prompt.

  • Use your personal texmf tree if you are not allowed to modify the local tree on your computer, or if you don’t want to share the file in question with other users; indeed, each user’s personal texmf tree is located somewhere inside that user’s home directory. To learn what the path to the root of your personal texmf tree is on your machine, type

    kpsewhich -var-value TEXMFHOME
    

    at a terminal (emulator) prompt.

Whatever tree you decide to use, if we denote its root by MYTEXMF, the lstlang0.sty file should be placed under MYTEXMF/tex/latex/, either directly inside that directory or inside a sub-(sub-…-sub-)directory thereof. It might be necessary to create the relevant directories (i.e., tex and/or tex/latex).

For example, let’s say you decide to use your personal texmf tree. On my system,

kpsewhich -var-value TEXMFHOME

returns

/Users/gustavo/Library/texmf

(where /Users/gustavo is my home directory): this means that I should create a file having the following full pathname

/Users/gustavo/Library/texmf/tex/latex/lstlang0.sty

(creating also intermediate directories as necessary) and store my personal language definitions in this file.