LaTeX listings包:如何按位置而非分隔符定义注释?
Solution for Custom GAMS Language in Listings (with Description-as-Comment Support)
Got it, let's tackle this problem with the listings package and GAMS code formatting. Here's a practical solution to get those post-declaration descriptions recognized as comments, with proper styling and no accidental keyword highlighting:
Step 1: Define the Custom GAMS Language in Your Preamble
First, add this to your LaTeX preamble to set up the GAMS language definition, including handling both line comments (!) and the double-quoted description text:
\usepackage{listings} \usepackage{xcolor} % Define custom colors for GAMS elements \definecolor{gamsKeyword}{RGB}{0, 51, 102} % Dark blue for keywords \definecolor{gamsComment}{RGB}{102, 102, 102} % Gray for comments/descriptions \definecolor{gamsBackground}{RGB}{248, 248, 248} % Light gray background (optional) % Custom GAMS language definition \lstdefinelanguage{GAMS}{ % Core GAMS keywords (add more as needed) keywords={Variable, Variables, Equation, Equations, Model, Solve, Display, Set, Sets, Parameter, Parameters, Scalar, Scalars, Positive}, keywordstyle=\color{gamsKeyword}\bfseries, % Handle standard GAMS line comments (starts with !) comment=[l]{!}, commentstyle=\color{gamsComment}\itshape, % Treat double-quoted descriptions as comment-style blocks moredelim=[s]["][\color{gamsComment}\itshape], % Optional: Make keyword matching case-insensitive (adjust if needed) sensitive=false, % Optional formatting tweaks basicstyle=\ttfamily\small, backgroundcolor=\color{gamsBackground}, numbers=left, numberstyle=\tiny\color{gamsComment}, frame=single, breaklines=true }
Step 2: Use the Custom Language in Your Document
Now you can insert GAMS code blocks with proper formatting, where both line comments and post-declaration descriptions are styled as comments, and keywords don't get highlighted inside descriptions:
\begin{lstlisting}[language=GAMS] Set i "Set of production plants" /PlantA, PlantB, PlantC/; Parameter capacity(i) "Maximum output per plant" /PlantA 500, PlantB 750, PlantC 600/; Variable x(i) "Units produced at each plant" /PlantA.L 200/; Positive Variable y(i) "Positive production only"; Equation obj "Minimize total production cost"; obj.. sum(i, 1.2*capacity(i)*x(i)) =E= 15000; Model production_plan "Optimal production model" /all/; Solve production_plan using LP minimizing obj; Display x.l, y.m; ! This is a standard line comment \end{lstlisting}
How It Works
- Keyword Highlighting: We define core GAMS keywords and apply a bold dark blue style to them.
- Line Comments: The standard GAMS line comment (
!) is handled withcomment=[l]{!}, applying the gray italic comment style. - Description Text: The
moredelim=[s]["][\color{gamsComment}\itshape]line tellslistingsto treat any text wrapped in double quotes as a special block, applying the same comment style as line comments. This ensures:- The description text gets your desired comment color/style.
- Any text inside the quotes (even if it matches a keyword) won't be highlighted as a keyword.
Optional Tweaks
- If you need to handle double quotes in other contexts (rare in standard GAMS), you can refine the
moredelimrule with regex-based matching (note:listingsregex support is limited, but you can usemoredelim=[il][regex]for inline matching if needed). - Adjust the colors, font sizes, or frame styles to match your document's design.
内容的提问来源于stack exchange,提问作者Niclas von Caprivi
相关产品推荐
相关产品推荐

