GeneralSyntax of \lstdefinelanguage in Listings Package is wrong!

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
peterwilson_69
Posts: 4
Joined: Fri Feb 26, 2010 11:05 am

Syntax of \lstdefinelanguage in Listings Package is wrong!

Post by peterwilson_69 »

What is the syntax for \lstdefinelanguage with respect to creating a new language, based on an existing language?

The syntax in the documentation does not work - gives errors.

Code: Select all

\lstdefinelanguage{[MyDialect]{C++}[Visual]{C++}}
:evil: DOES NOT WORK! :evil:

So what am I missing?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Syntax of \lstdefinelanguage in Listings Package is wrong!

Post by localghost »

Before you claim that a package (or its manual) contains an error, you should read the manual very carefully. The way you use the mentioned command is completely wrong. See Sections 2.4 (Languages, p. 11) and 3.2 (Language definitions, p. 20f) of the listings manual. Finally it's always helpful to see some code in the sense of minimal working example [1]. This makes solving the problem much easier.

[1] View topic: Avoidable mistakes


Best regards
Thorsten
peterwilson_69
Posts: 4
Joined: Fri Feb 26, 2010 11:05 am

Syntax of \lstdefinelanguage in Listings Package is wrong!

Post by peterwilson_69 »

I standby my criticism of the manual. In section 4.18 of the manual it clearly states the syntax for creating a new language based on an existing one.
If the language definition is based on another definition, you must specify the whole [<base dialect>]{<and base language>}.

Code: Select all

\lstdefinelanguage{[WilsonPB]C++[Visual]C++}
{
	morekeywords={Normalize, DotProduct, CrossProduct}
}
\lstloadlanguages{[Visual]C++}
\lstloadlanguages{[WilsonPB]C++}
...produces the following error:

Code: Select all

! Package Listings Error: Couldn't load requested language.
The following languages weren't loadable:
    ,c++$wilsonpb
This may cause errors in the sequel.
Which begs the question: Did I actually use the command correctly after all - since it appears to fail on loading this new language? It's all so confusing.

So what problem am I really trying to solve here? I want to index keywords differently based on different language definitions. There is no minimal working example in the manual, or indeed anywhere else on the Internet! Seriously, I've spent two days looking - nothing!

Here are my macros for indexing languages differently:

Code: Select all

\newcommand{\indexCPP}[1]{\index{C++!#1@\texttt{#1}}}
\newcommand{\indexWilsonPB}[1]{\index{WilsonPB!#1@\texttt{#1}}}
The thing no-one explains is how to associate each command with the correct language. Here's what I thought would work, but does not:

Code: Select all

\lstset{indexstyle=[1]\indexCPP, indexstyle=[2]\indexWilsonPB,
	classoffset=0,
		language={[Visual]C++},
		index=[1][keywords],
	classoffset=1,
		alsolanguage=WilsonPB,
		index=[2][keywords],
	classoffset=0
}
Simple variations of this are not working for me either, and without a single working example anywhere, I'm finding it difficult.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Syntax of \lstdefinelanguage in Listings Package is wrong!

Post by localghost »

peterwilson_69 wrote:[...] There is no minimal working example in the manual, or indeed anywhere else on the Internet! Seriously, I've spent two days looking - nothing! [...]
You completely misunderstood the meaning of a MWE. Of course there is no source anywhere because you have to build it on your own. Instructions how to do that and important things to consider can be found behind the links I gave in my last reply. It's in your own interest to follow them because otherwise it is impossible for others to give specific help.
Post Reply