How to list code in Latex

  • How to create  a customized language highlighting scheme in Latex
  • How to use “{” and “}” in lstinline

How to create  a customized language highlighting scheme in Latex 

 

Here is an example scheme I used to define a customized graph language, which I call “graphit”. The template is borrowed from others’ latex repo. Once you have the template, you can just use

begin{lstlisting}[language=graphit]

end{lstlisting}

for bigger blocks of code

or

\lstinline{ }

for shorter lines of code

———————————-  customized language definition ———————————

\definecolor{gray}{gray}{0.5}
\definecolor{key}{rgb}{0,0.5,0}

 

\def\OPTL{\textrm{$[$}}
\def\OPTR{\textrm{$]$}}

\definecolor{lightbackground}{rgb}{.98,.98,.97}
\definecolor{darkgray}{rgb}{.3,.3,.3}
\definecolor{darkred}{rgb}{.6,0,0}
\definecolor{darkgreen}{rgb}{0,.6,0}
\definecolor{darkblue}{rgb}{0,0,.6}

\lstdefinelanguage{graphit}{%
%% List of keywords
keywords={[1]},%for,do,while,if,else,break,continue,return
keywords={[2]func,end,element,const,var,%
vertexset,edgeset,vector,%
void,char,short,long,int,float,double,boolean,size_t},
keywords={[3]from, to, apply, modified},
keywords={[4],schedule, s1 %
},
%emph={main,producer,consumer,master,selector,compute,hscan,sync_av},
%% List of abbreviations
% literate={<=}{{$\leq$}}1 {>=}{{$\geq$}}1 {!=}{{$\neq$}}1 {*}{{$\times$}}1,
literate={[OPT[}{{\OPTL}}1 {]OPT]}{{\OPTR}}1,
%{\#}{{\textbf{\color{darkgreen}\#}}}1,
%% List of strings
string=[b]”,
%% List of comment strings
comment=[l]//,
morecomment=[s]{/*}{*/},
%% Special character for LaTeX
mathescape=true,
%% Definition du style
flexiblecolumns=true,
tabsize=2,
captionpos=b,
% abovecaptionskip=3mm,
% belowcaptionskip=3mm,
frame=single,
framerule=0pt,
aboveskip=1pt,
belowskip=1pt,
framesep=1pt,
basicstyle=\scriptsize\ttfamily,
keywordstyle={[1]\color{darkred}},
keywordstyle={[2]\color{blue}},
keywordstyle={[3]\color{black}\bfseries},
keywordstyle={[4]\color{darkred}\bfseries},
%keywordstyle=\fontseries{bx}\fontfamily{cmss}\fontshape{n}\selectfont,
% numberstyle=\footnotesize,
% basicstyle=,
% keywordstyle=\sbf,
% numberstyle=,
emphstyle=\slshape,
%identifierstyle=\color{black},
commentstyle=\color{darkgray},
stringstyle=\color{darkgreen}
}

%\lstset{language=pencil,backgroundcolor=\color{lightbackground}}
\lstset{language=graphit,backgroundcolor=\color{lightbackground},%
belowskip=.5em, aboveskip=.5em}

How to use “{” and “}” in lstinline

you can use “|” instead of “{” as delimiters for the start and end of lstinline

\lstinline | text{ } |

per this post

https://tex.stackexchange.com/questions/264293/cant-escape-curly-braces-in-lstinline

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s