19

I would like to specify two different lstset settings in one latex file: one for java code, one for html. Is this somehow possible?

The solution of https://stackoverflow.com/questions/2654810/how-to-reset-lstset-settings-for-listings does not seem to be appropriate for my document because I have more than just one html listing.

migrated from stackoverflow.com Aug 11 '11 at 15:13

This question came from our site for professional and enthusiast programmers.

18

Every lstlistings block can have its own formatting commands. So:

\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\begin{document}
\begin{lstlisting}
  Foo
\end{lstlisting}

\begin{lstlisting}[basicstyle=\sffamily]
  Bar
\end{lstlisting}

\lstset{basicstyle=\rmfamily}

\begin{lstlisting}
  Baz
\end{lstlisting}

\end{document}

If you want to use two distinct listings and resetting the listings like this manually is a hassle, just create some new environments with lstnewenvironment for example:

\lstnewenvironment{sflisting}{\lstset{basicstyle=\sffamily}}{}
\lstnewenvironment{ttlisting}{\lstset{basicstyle=\ttfamily}}{}

page 42 of the listings manual has the details

  • Precision : it's the page 42 of the listings manual. – Benoît Fraikin Dec 21 '18 at 16:40
  • @BenoîtFraikin It appears as if the listings manual has grown by a couple of pages since 2011... – Seamus Dec 22 '18 at 17:40
8

I use a style definition for every language, here is an example for makefiles:

\lstdefinestyle{makefile}
{
    numberblanklines=false,
    language=make,
    tabsize=4,
    keywordstyle=\color{red},
    identifierstyle= %plain identifiers for make
}

To include this style, I use

\lstinputlisting[
    inputencoding=latin1, 
    firstline=1, 
    %lastline=10,
    caption={[Makefile]Makefile, foobar},
    style=makefile
]{./src/makefile}

Other styles have another value for language(and other settings for keywords, tabsizes etc.)

5
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{bera}
\usepackage{listings}
\lstdefinestyle{tt}{basicstyle=\small\ttfamily,keywordstyle=\bfseries,language=[LaTeX]{TeX}}
\lstdefinestyle{rm}{basicstyle=\ttfamily,keywordstyle=\slshape,language=[LaTeX]{TeX}}

\begin{document}
\begin{lstlisting}[style=tt]
\documentclass{foo}
\end{lstlisting}

\begin{lstlisting}[style=rm]
\documentclass{bar}
\end{lstlisting}

\begin{lstlisting}
  Baz
\end{lstlisting}

\end{document}

enter image description here

  • How can I combine the flexibility of your answer with \lstnewenvironment, such that I can keep my custom environment, yet switch the languages e.g. \begin{customenv}[language=bash] and have another one like \begin{customenv}[language=java] and have another one like \begin{customenv} that defaults to [language=java] or just plain without syntax highlighting? – Jonathan Komar Feb 10 '15 at 14:52

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.