4

How can I get the language currently set for the listings (aka lstlistings) package, e.g. via \lstset{language=Python,basicstyle=\color{blue},backgroundcolor=\color{lightgray}}?


Details:

I want to use the value in my own command that also puts some lstinline text into the index, under the right language.

I tried variables like \language, \lst.language, ... But they do not contain the value.

Hence I introduced a new variable via e.g. \newcommand{\lstLanguage}{Python} in each chapter, and used \lstset{language=\lstLanguage,basicstyle=\color{blue},backgroundcolor=\color{lightgray}} instead. But that causes the error

​ ! Package Listings Error: language Python undefined

4
+50

It is stored in \lst@language. The following minimal example shows how to get the currently set listings language from LaTeX and yields tex python:

\documentclass{article}

\usepackage{listings}

\makeatletter
\newcommand{\getlang}{\lst@language}
\makeatother

\begin{document}

\lstset{language=tex}



\getlang

\lstset{language=Python,basicstyle=\color{blue},backgroundcolor=\color{lightgray}}


\getlang


\end{document}

Your Answer

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

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