|
| 1 | +\usepackage{ifpdf} % duplicate use |
| 2 | + |
| 3 | +% When producing PDF, the ttfamily looks better in size \small, but with LaTeXML this becomes too small, |
| 4 | +% and the size is applied deep down on the HTML elements, making it hard to adjust using CSS. |
| 5 | +% Thus, the problem has to be addressed already when a listings language's 'basicstyle' is defined. |
| 6 | +% This needs to go before mlsshared.sty is loaded. |
| 7 | +\ifpdf |
| 8 | +\let\smallifpdf\small |
| 9 | +\else |
| 10 | +\let\smallifpdf\normalsize |
| 11 | +\fi |
| 12 | + |
| 13 | + |
| 14 | +% Comments in listings looks better and more distinct from the actual code if set in a variable-width sans-serif font. |
| 15 | +% However, just changing font family doesn't work for pdf. |
| 16 | +% Hence, we make a different choice of font depending on the target format. |
| 17 | +\ifpdf |
| 18 | +\let\sffamilyifhtml\relax |
| 19 | +\else |
| 20 | +\let\sffamilyifhtml\sffamily |
| 21 | +\fi |
| 22 | + |
| 23 | +\usepackage{listings} |
| 24 | +\usepackage{color} |
| 25 | +\usepackage[table]{xcolor} |
| 26 | + |
| 27 | +\definecolor{keywordcolor1} {rgb}{0.00, 0.20, 0.47} |
| 28 | +\definecolor{keywordcolor3} {rgb}{0.33, 0.24, 0.03} |
| 29 | +\definecolor{commentcolor} {rgb}{0.07, 0.46, 0.00} |
| 30 | + |
| 31 | +% It would be nice to have comments set in normal variable-width font without any sort of |
| 32 | +% column alignment by the listsings.sty package, just like the comments look in the LaTeXML build. |
| 33 | +% Adding the following \commentfullflexible to the 'commentstyle' |
| 34 | +% makes words look good, but the words are still aligned in mysterious ways. |
| 35 | +%\let\commentfullflexible\lst@column@fullflexible |
| 36 | + |
| 37 | +% See https://github.com/modelica-tools/listings-modelica/blob/master/listings-modelica.cfg |
| 38 | +% Note: Changed comment color from green to [rgb]{0,0.4,0} - since the other variant was too distracting |
| 39 | +% And added pure,impure,stream as keywords |
| 40 | +% Remove cross as red |
| 41 | +% Note: have basicstyle=\upshape\small\ttfamily in all languages, |
| 42 | +% except the dialect [short]modelica - which is used for inline listings. |
| 43 | +% Apart from that change that dialect should be identical to modelica |
| 44 | + |
| 45 | +% The morekeywords=[3] part is a bit unclear. |
| 46 | +% The current logic is that "operators" are marked specially, but not normal functions. |
| 47 | +% |
| 48 | +% Note that Integer is both a predefined type and a function; we cannot treat them differently. |
| 49 | +% Another solution would be to remove this completely. |
| 50 | +\lstdefinelanguage{modelica}{% Language for use with the lstlisting environment. |
| 51 | + basicstyle=\upshape\ttfamily\smallifpdf, % Font size for displayed code listings. |
| 52 | + alsoletter={}, |
| 53 | +% otherkeywords={-, =, +, [, ], (, ), \{, \}, :, *, !},% |
| 54 | + morekeywords=[1]{% Keywords not used to define code structure |
| 55 | + der,connect,assert,terminate,break,return,% |
| 56 | + false,true,and,not,or,% |
| 57 | + final,each,% |
| 58 | + flow,stream,% |
| 59 | + input,output,% |
| 60 | + discrete,parameter,constant,% |
| 61 | + }, |
| 62 | + morekeywords=[2]{% Keywords used to define code structure |
| 63 | + annotation,block,class,connector,constrainedby,% |
| 64 | + encapsulated,enumeration,else,elseif,elsewhen,end,% |
| 65 | + expandable,extends,external,for,% |
| 66 | + function,if,in,inner,initial,import,loop,model,operator,outer,% |
| 67 | + package,partial,record,redeclare,replaceable,% |
| 68 | + then,type,when,while,within,algorithm,equation,% |
| 69 | + protected,public,pure,impure,% |
| 70 | + },% |
| 71 | + % Note: initial is in both variants - depending on context, use first variant |
| 72 | + morekeywords=[3]{% Selected recognized names that are not actual keywords, including: |
| 73 | + % 3.7.2 #derivative-and-special-purpose-operators-with-function-syntax |
| 74 | + delay,cardinality,homotopy,semiLinear,inStream,actualStream,spatialDistribution,getInstanceName,% |
| 75 | + terminal,noEvent,smooth,sample,pre,edge,change,reinit,% |
| 76 | + % 3.7.3 #event-related-operators-with-function-syntax (except initial) |
| 77 | + previous,hold,subSample,superSample,shiftSample,backSample,noClock,firstTick,interval,% |
| 78 | + % 4.8 #predefined-types-and-classes |
| 79 | + Real,Integer,Boolean,String,% |
| 80 | + % Highlights from #built-in-array-functions |
| 81 | + promote,ndims,size,% |
| 82 | + % Assorted things from #state-machines -- what about all the other operators in this chapter? |
| 83 | + transition,initialState,% |
| 84 | + },% |
| 85 | + sensitive=true, % just in case |
| 86 | + comment=[l]{//}, % comment lines |
| 87 | + morecomment=[s]{/*}{*/}, % comment blocs |
| 88 | + morestring=[b]{'}, |
| 89 | + morestring=[b]{"}, |
| 90 | +}[keywords,comments,strings] |
| 91 | + |
| 92 | +\lstdefinelanguage[short]{modelica}[]{modelica}{% Language for use with the \lstinline command. |
| 93 | + basicstyle=\upshape\ttfamily, % Font size for inline code snippets. |
| 94 | +} |
| 95 | + |
| 96 | +% Special dialect of Modelica to use when there are URLs appearing outside string literals, to avoid the part |
| 97 | +% starting with '//' being treated as comment. |
| 98 | +\lstdefinelanguage[nocomment]{modelica}[short]{modelica}{% |
| 99 | + commentstyle=\upshape\ttfamily, % Camouflage comments as workaround for not being able to remove rest-of-line comment recognition. |
| 100 | +} |
| 101 | + |
| 102 | +% Note: within only a keyword in grammar |
| 103 | +\lstdefinelanguage{grammar}{% |
| 104 | + basicstyle=\upshape\ttfamily\smallifpdf, % size of fonts used for the code |
| 105 | + identifierstyle=\itshape, |
| 106 | + alsodigit={-}, |
| 107 | + breaklines=true, |
| 108 | + breakatwhitespace=true, |
| 109 | + morekeywords=[1]{% |
| 110 | + % Keywords corresponding to morekeywords=[1] for language=modelica |
| 111 | + der,connect,assert,terminate,break,return,% |
| 112 | + false,true,and,not,or,% |
| 113 | + final,each,% |
| 114 | + flow,stream,% |
| 115 | + input,output,% |
| 116 | + discrete,parameter,constant,% |
| 117 | + % Keywords corresponding to morekeywords=[2] for language=modelica |
| 118 | + annotation,block,class,connector,constrainedby,% |
| 119 | + encapsulated,enumeration,else,elseif,elsewhen,end,% |
| 120 | + expandable,extends,external,for,% |
| 121 | + function,if,in,inner,initial,import,loop,model,operator,outer,% |
| 122 | + package,partial,record,redeclare,replaceable,% |
| 123 | + then,type,when,while,within,algorithm,equation,% |
| 124 | + protected,public,pure,impure,% |
| 125 | + }, |
| 126 | + % Instead of using color to highlight BNF syntactical constructs as below, the production rule names |
| 127 | + % are set in italics, so that the syntactical constructs stand out by having upright shape. |
| 128 | +% alsoletter={|,\{,\},[,],(,)}, |
| 129 | +% morekeywords=[2]{|,\{,\},[,],(,)}, |
| 130 | + morekeywords=[3]{% |
| 131 | + letters,% |
| 132 | + }, |
| 133 | + morestring=[b]{"}, |
| 134 | + stringstyle=\color{keywordcolor1}, % For the grammar, fixed strings and keywords are the same kind of token. |
| 135 | +}[keywords,comments,strings] |
| 136 | + |
| 137 | +% Duplicate this definition here to avoid issue |
| 138 | +\lstdefinelanguage{FORTRAN77}{% Define custom language to avoid collision with predefined [77]Fortran. |
| 139 | + basicstyle=\upshape\ttfamily\smallifpdf, % size of fonts used for the code |
| 140 | + morekeywords=[1]{% |
| 141 | + ASSIGN,BACKSPACE,CALL,CHARACTER,% |
| 142 | + CLOSE,COMMON,COMPLEX,CONTINUE,DATA,DIMENSION,DO,DOUBLE,% |
| 143 | + ELSE,ELSEIF,END,ENDIF,ENDDO,ENTRY,EQUIVALENCE,EXTERNAL,% |
| 144 | + FILE,FORMAT,FUNCTION,GO,TO,GOTO,IF,IMPLICIT,% |
| 145 | + INQUIRE,INTEGER,INTRINSIC,LOGICAL,% |
| 146 | + OPEN,PARAMETER,PAUSE,PRECISION,PRINT,PROGRAM,READ,REAL,% |
| 147 | + RETURN,REWIND,STOP,SUBROUTINE,THEN,% |
| 148 | + WRITE,SAVE}, |
| 149 | + morekeywords=[2]{% |
| 150 | + ACCESS,BLANK,BLOCK,DIRECT,EOF,ERR,EXIST,% |
| 151 | + FMT,FORM,FORMATTED,IOSTAT,NAMED,NEXTREC,NUMBER,OPENED,% |
| 152 | + REC,RECL,SEQUENTIAL,STATUS,TYPE,UNFORMATTED,UNIT}, |
| 153 | + morekeywords=[3]{% |
| 154 | + INT,DBLE,CMPLX,ICHAR,CHAR,AINT,ANINT,% left out real |
| 155 | + NINT,ABS,MOD,SIGN,DIM,DPROD,MAX,MIN,AIMAG,CONJG,SQRT,EXP,LOG,% |
| 156 | + LOG10,SIN,COS,TAN,ASIN,ACOS,ATAN,ATAN2,SINH,COSH,TANH,LGE,LLE,LLT,% |
| 157 | + LEN,INDEX}, |
| 158 | + morekeywords=[4]{AND,EQ,EQV,FALSE,GE,GT,OR,LE,LT,NE,NEQV,NOT,TRUE},% |
| 159 | + sensitive=true, |
| 160 | + morecomment=[f]*, |
| 161 | + morecomment=[f]C, |
| 162 | + morecomment=[f]c, |
| 163 | + morestring=[d]", % not Fortran-77 standard, but allowed in Fortran-95 %% |
| 164 | + morestring=[d]' |
| 165 | +}[keywords,comments,strings] |
| 166 | + |
| 167 | +\lstdefinelanguage[MLS]{C}{% Define new dialect to avoid collision with predefined dialects of C. |
| 168 | + basicstyle=\upshape\ttfamily\smallifpdf, % size of fonts used for the code |
| 169 | + morekeywords=[1]{% |
| 170 | + auto,break,case,char,const,continue,default,do,double,% |
| 171 | + else,enum,extern,float,for,goto,if,int,long,register,return,% |
| 172 | + short,signed,sizeof,static,struct,switch,typedef,union,unsigned,% |
| 173 | + void,volatile,while}, |
| 174 | + morekeywords=[3]{% |
| 175 | + size_t}, |
| 176 | + sensitive=true, |
| 177 | + morecomment=[s]{/*}{*/}, |
| 178 | + morecomment=[l]//, % nonstandard |
| 179 | + morestring=[b]", |
| 180 | + morestring=[b]', |
| 181 | + moredelim=*[directive]\#, |
| 182 | + moredirectives={% |
| 183 | + define,elif,else,endif,error,if,ifdef,ifndef,line,% |
| 184 | + include,pragma,undef,warning} |
| 185 | +}[keywords,comments,strings,directives] |
| 186 | + |
| 187 | +\lstdefinelanguage{CSS}{ |
| 188 | + keywords={}, |
| 189 | + sensitive=true, |
| 190 | + morecomment=[l]{//}, |
| 191 | + morecomment=[s]{/*}{*/}, |
| 192 | + morestring=[b]', |
| 193 | + morestring=[b]", |
| 194 | + alsoletter={:}, |
| 195 | + alsodigit={-} |
| 196 | +} |
| 197 | + |
| 198 | +\lstset{% |
| 199 | + backgroundcolor=\color{white}, % choose the background color |
| 200 | + mathescape=true, |
| 201 | + breaklines=true, % automatic line breaking only at white-space |
| 202 | + keepspaces, % don't remove space such as those after closing parenthesis |
| 203 | + upquote=true, % Don't turn apostrophe/backtick into single quotation marks. |
| 204 | + captionpos=b, % sets the caption-position to bottom |
| 205 | + commentstyle=\color{commentcolor}\sffamilyifhtml, |
| 206 | + keywordstyle=\color{red}, % Unused keyword style in bright red to make it easy to detect and fix. |
| 207 | + keywordstyle=[1]\color{keywordcolor1}, |
| 208 | + keywordstyle=[2]\color{keywordcolor1}\bfseries, |
| 209 | + keywordstyle=[3]\color{keywordcolor3}, |
| 210 | + stringstyle=\color{black}, % string literal style |
| 211 | + language=[short]modelica, % Language that will be used by default, in particluar by plain \lstinline. |
| 212 | + showstringspaces=false, |
| 213 | + frame=lrtb, |
| 214 | + belowskip=0pt, |
| 215 | + defaultdialect=[MLS]C, |
| 216 | +} |
0 commit comments