You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 with comment=[l]{!}, applying the gray italic comment style.
  • Description Text: The moredelim=[s]["][\color{gamsComment}\itshape] line tells listings to treat any text wrapped in double quotes as a special block, applying the same comment style as line comments. This ensures:
    1. The description text gets your desired comment color/style.
    2. 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 moredelim rule with regex-based matching (note: listings regex support is limited, but you can use moredelim=[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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:45:50