12

If my understanding that it is possible to specify the programming language for the listings globally as well as on the listing by listing basis is correct, than the following should work:

\documentclass[11pt,letterpaper]{book} \usepackage{listings} \begin{document} \lstloadlanguages{SQL,Basic} %Listings manual, pp. 11, 13 \lstset{ frameround=fttt ,language=SQL ,numbers=left ,breaklines=true ,showstringspaces=false ,basicstyle=\small } \begin{lstlisting}[language=Basic,label={lst:ssrscurmon}] =MonthName(DatePart(DateInterval.Month, Today())) \end{lstlisting} \end{document} 

But it instead throws

Package Listings Error: Couldn't load requested language \lstloadlanguages{SQL,Basic} 

How can I specify Basic for some listings in the document, where the majority are in SQL?

3
  • 1
    Seems that Basic is missing in the list of available languages for some reason. Also, I recommend you move the \lstloadlanguages and the \lstset{ to the preamble _before_ \begin{document}`. Commented Nov 13, 2014 at 19:14
  • P. 4 of the Listings user guide instructs to put the \lstset inside the document body. Why do you recommend putting it in the preamble? Commented Nov 13, 2014 at 19:43
  • 3
    Yes, there is an example of how one can use lstset after \begin{document}. My recommendation was more about \lstloadlanguages as I can not see any reason to load that in the main documents. Even lstset, IMHO, should be used only for document wide settings and style= settings be used for customization, although it is possible to use lstset to change the settings mid-document. Commented Nov 13, 2014 at 21:10

2 Answers 2

10

This can be considered a bug in listings; in lstdvrs.dtx, more specifically.

The lstdvrs.dtx file is where all listings languages and dialects are defined. In particular, a language called Basic (together with only one dialect, Visual) is defined there.

The problem is that, even though the Basic language comes with only one dialect (Visual), the default dialect for Basic is defined nowhere in lstdvrs.dtx. Therefore, if you simply specify

language=Basic 

listings has no clue which language you're referring to! The same problem arises, for the same reason, if you try to load the Assembler language without also specifying a dialect.

To fix the problem, you have two options:

  1. Whenever you want to use Basic, specify its only dialect (Visual) also:

    language={[Visual]Basic} 
  2. Fix that bug in listings yourself by defining the default dialect for Basic:

    \lstset{defaultdialect=[Visual]Basic} 

    (As Peter notes, this should preferably be done in your preamble. You should endeavour to separate style from content; the former should go in the preamble, whereas the latter should go in the body of your document.)

    Then you should be able to use the Basic language without mishap. No need to use \lstloadlanguages at all (although, as pointed out by Peter, you may still want to load the languages you use right after loading the listings package, for efficiency reasons; see the note at the bottom of section 2.2 in the documentation).


enter image description here

\documentclass[11pt,letterpaper]{book} \usepackage{listings} \lstset{ defaultdialect=[Visual]Basic ,frameround=fttt ,language=SQL ,numbers=left ,breaklines=true ,showstringspaces=false ,basicstyle=\small } \begin{document} \begin{lstlisting}[language=Basic,label={lst:ssrscurmon}] =MonthName(DatePart(DateInterval.Month, Today())) \end{lstlisting} \end{document} 
6
  • It seems that when \lstloadlanguages is used, compilation time is much shorter. Of course my machine could be acting up, but that is my impression. Commented Nov 13, 2014 at 20:45
  • 2
    @ajeh: Yep, the documentation (P. 11) states that it is much faster to load several languages with one command rather than each language being loaded on demand. Commented Nov 13, 2014 at 21:07
  • @PeterGrill I didn't know anything about that. Feel free to edit my answer. Commented Nov 13, 2014 at 21:24
  • @Jubobs: I didn't either (just discovered it today while trying to research this question). I started to edit your answer, but then realized that you don't actually use \lstloadlanguages in your answer, so not sure it makes sense to add that comment. Commented Nov 13, 2014 at 21:34
  • @PeterGrill Ok, fair enough. I'll read more about it and see if I should mention it somewhere later. Thanks. Commented Nov 13, 2014 at 21:35
4

As you can see on Table 1 (page 13) of the listings documentation, only Visual Basic is supported; this can also be seen in the file lstlang1.sty, where you find the definition

\lst@definelanguage[Visual]{Basic} { ... } 

You need the to load [Visual](Basic); the complete code:

\documentclass[11pt,letterpaper]{book} \usepackage{listings} \lstloadlanguages{SQL,[Visual]Basic} %Listings manual, pp. 11, 13 \lstset{ frameround=fttt ,language=SQL ,numbers=left ,breaklines=true ,showstringspaces=false ,basicstyle=\small } \begin{document} \begin{lstlisting}[language={[Visual]Basic},label={lst:ssrscurmon}] =MonthName(DatePart(DateInterval.Month, Today())) \end{lstlisting} \end{document} 

The output:

enter image description here

2
  • 1
    The line \lst@definelanguage[Visual]{Basic} doesn't itself indicate that Basic can only be used in conjunction with Visual. Rather, it's the fact that no default dialect is defined for the Basic language. Commented Nov 13, 2014 at 20:00
  • Lack of underline under Visual in the table on p. 13 should have been a hint to me, but I missed it. Commented Nov 13, 2014 at 20:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.