1

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

I've tried using the listings package

\documentclass[12pt]{report}

%Packages

\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\usepackage{geometry}
\usepackage{color}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{sectsty}
\usepackage[squaren,Gray]{SIunits}
\usepackage[french]{babel}
\usepackage{listings}
\geometry{hmargin=2.5cm,vmargin=2cm}
\usepackage{fancyhdr}
\pagestyle{fancy}
%\definecolor{bdx}{rgb}{0.682, 0.145, 0.451}
\definecolor{bdx}{rgb}{0.674, 0.152, 0.451}

\sectionfont{\centering\color{bdx}}
\subsectionfont{\color{bdx}}
\subsubsectionfont{\centering}
\renewcommand{\thesection}{\Roman{section}.}
\renewcommand{\thesubsection}{\arabic{subsection} - }
\renewcommand{\headrulewidth}{0pt}

%Page de titre
\setcounter{tocdepth}{2}
\title{\color{bdx}{\textbf{Test}}
\author{John D}
\date{\today}

% 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

\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{deepgrey}{rgb}{0.25,0.25,0.25}



% AMPL style for highlighting

\lstdefinelanguage{ampl}
{
keywords={}
}

\newcommand\amplstyle{\lstset{
language=ampl,
backgroundcolor=\color{white},
basicstyle=\ttm,
emph={subject to, minimize, reset},          % Custom highlighting
otherkeywords={set,param,var,arc,integer,minimize,maximize,subject,to,node,sum,in,Current,complements,integer,solve_result_num,IN,contains,less,suffix,INOUT,default,logical,sum,Infinity,dimen,max,symbolic
,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
keywordstyle=\small\ttb\color{deepblue},
emphstyle=\small\ttb\color{deepred},    % Custom highlighting style
morestring=[b]",
stringstyle=\color{deepgreen},
commentstyle=\ttm\color{deepgrey},
frame=tb,                         % Any extra options here
showstringspaces=false            % 
}}


% AMPL environment
\lstnewenvironment{ampl}[1][]
{
\amplstyle
\lstset{#1}
}
{}

% AMPL for external files
\newcommand\amplexternal[2][]{{
\amplstyle
\lstinputlisting[#1]{#2}}}

% AMPL for inline
\newcommand\amplinline[1]{{\amplstyle\lstinline!#1!}}

I've tried printing out :

\begin{ampl}
set A;
reset;
option solver gurobi;
var j >= 0;
minimize cost;
subject to bob : 
    "But why ?"
\end{ampl}

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 ?

1

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:

\newcommand\amplstyle{\lstset{
    ...
    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.