I'm trying to include AMPL syntax highlighting in a report i'm writing.

I've tried using the listings package



%\definecolor{bdx}{rgb}{0.682, 0.145, 0.451}
\definecolor{bdx}{rgb}{0.674, 0.152, 0.451}

\renewcommand{\thesubsection}{\arabic{subsection} - }

%Page de titre
\author{John D}

% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % for normal


% AMPL style for highlighting


emph={subject to, minimize, reset},          % Custom highlighting
,Initial,div,min,table,LOCAL,else,option,then,OUT,environ,setof     ,union,all,exists,shell_exitcodeuntil,binary,forall,solve_exitcodewhile     ,by,if,solve_messagewithin,check,in,solve_result},             % Add keywords here
emphstyle=\small\ttb\color{deepred},    % Custom highlighting style
frame=tb,                         % Any extra options here
showstringspaces=false            % 

% AMPL environment

% AMPL for external files

% AMPL for inline

I've tried printing out :

set A;
option solver gurobi;
var j >= 0;
minimize cost;
subject to bob : 
    "But why ?"

But this does not highlight the words correctly, it seems to go to the first recognition. For example, "reset" should be emphasized, but is not and the substring "set" is highlighted..

How could I work around this ?


You are using the otherkeywords option to add more keywords, but this is wrong. otherkeywords should be used to add keywords which include other symbols, i.e. characters other than letters or digits that do not usually occur in identifiers.

After using the correct option morekeywords you get the correct result:

    emph={subject to, minimize, reset},          % Custom highlighting
    morekeywords={set,param,var, ...},           % Add keywords here

enter image description here

| improve this answer | |
  • 1
    Brilliant, thanks ! This is exactly the explanation I needed ;) – olirwin Nov 11 '19 at 9:04

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.