### Smalltalk syntax highlighting for Latex

Hi. For papers I usually write code in black and white. Even when you may want colors in books or thesis, you still may not want to add colors. However, for my thesis (I have to finish it beginning of Augoust!!), I do want to make the code a little bit nicer and so I did some Smalltalk syntax highlighting with the listings package. The listings package provides lots of languages out of the box, but not Smalltalk. Because of that, I searched ways to have it. Not sure if this would be helpful for someone else, but just in case, I wanted to share it with you.
Basically, I can detect (and therefore, choose a color/style): globals/classes (start with uppercase), keywords, comments, strings, symbols and numbers.  At the end of the post, I paste all the latex code so that you can take the code.

## Examples:

This is a plain example:

\begin{code}
fuelExample
sourceArray := Array
with: 'a string'
with: Transcript
with: 42
with: #foo.
&quot;Store to a file&quot;
FLSerializer serialize: sourceArray toFileNamed: 'example.FL'.
&quot;Check that the materialized Transcript is the right singleton instance.&quot;
\end{code}


And the pdf looks like this:

This is how you can define pieces of bold, italics and underline:

\begin{code}
&lt;b&gt;fuelExample&lt;/b&gt;
sourceArray := Array
with: 'a string'
with: Transcript
with: 42
with: #foo.
&quot;Store to a file&quot;
&lt;i&gt;FLSerializer serialize: sourceArray toFileNamed: 'example.FL'.&lt;/i&gt;
&quot;Check that the materialized Transcript is the right singleton instance.&quot;
\end{code}


The resulting pdf looks like this:
This is another environment if you want to show number lines:

\begin{codeWithLineNumbers}
&lt;b&gt;fuelExample&lt;/b&gt;
sourceArray := Array
with: 'a string'
with: Transcript
with: 42
with: #foo.
&quot;Store to a file&quot;
&lt;i&gt;FLSerializer serialize: sourceArray toFileNamed: 'example.FL'.&lt;/i&gt;
&quot;Check that the materialized Transcript is the right singleton instance.&quot;
\end{codeWithLineNumbers}


And this is the output:

## Customization

As you can see at the end, in the latex code, you are able to customize almost everything, the fonts, the fonts sizes, the colors and styles, etc. I won’t go in detail to show you how to customize everything. If you are interested in this post, you probably know latex and therefore you will know what and where to change.

## Wanna improve it?

Disclaimer: I am *not* an expert in latex at all, in fact, quite a newbie. To do what I showed in this post, I needed the help of Lukas Renggli and Luc Frabresse, and also surf a lot of forums. If you improve this, please share it with me!! Moreover, I wanted to share this work with that they did in Pharo By Example. But so far I didn’t have time. If you do it, please share it with me!!

For simplicity, I have zipped together the example used in this blog and a .tex with the listings macros.

## Code

&lt;/pre&gt;
% Source Code
\usepackage{color}
\usepackage{textcomp}
\usepackage{listings}

\definecolor{source}{gray}{0.85}% my comment style

% my string style
\newcommand{\myStringStyle}[1]{{\footnotesize\sffamily\color{violet!100!black} #1}}
%\newcommand{\myStringStyle}[1]{{\footnotesize\sffamily\color{black!100!black} #1}}

% my symbol style
\newcommand{\mySymbolStyle}[1]{{\footnotesize\sffamily\color{violet!100!black} #1}}
%\newcommand{\mySymbolStyle}[1]{{\footnotesize\sffamily\color{black!100!black} #1}}

% my keyword style
\newcommand{\myKeywordStyle}[1]{{\footnotesize\sffamily\color{green!70!black} #1}}
%\newcommand{\myKeywordStyle}[1]{{\footnotesize\sffamily\color{black!70!black} #1}}

% my global style
\newcommand{\myGlobalStyle}[1]{{\footnotesize\sffamily\color{blue!100!black} #1}}
%\newcommand{\myGlobalStyle}[1]{{\footnotesize\sffamily\color{black!100!black} #1}}

% my number style
\newcommand{\myNumberStyle}[1]{{\footnotesize\sffamily\color{brown!100!black} #1}}
%\newcommand{\myNumberStyle}[1]{{\footnotesize\sffamily\color{black!100!black} #1}}

\lstset{
language={},
% characters
tabsize=3,
escapechar={!},
keepspaces=true,
breaklines=true,
alsoletter={\#},
literate={\$}{{{\$}}}1,
breakautoindent=true,
columns=fullflexible,
showstringspaces=false,
% background
frame=single,
aboveskip=1em, % automatic space before
framerule=0pt,
basicstyle=\footnotesize\sffamily\color{black},
keywordstyle=\myKeywordStyle,% keyword style
frame=single,%
backgroundcolor=\color{source},
% numbering
stepnumber=1,
numbersep=10pt,
numberstyle=\tiny,
numberfirstline=true,
% caption
captionpos=b,
% formatting (html)
moredelim=[is][\bfseries]{&lt;b&gt;}{&lt;/b&gt;},
moredelim=[is][\textit]{&lt;i&gt;}{&lt;/i&gt;},
moredelim=[is][\underbar]{&lt;u&gt;}{&lt;/u&gt;},
moredelim=[is][\color{red}\uwave]{&lt;wave&gt;}{&lt;/wave&gt;},
moredelim=[is][\color{red}\sout]{&lt;del&gt;}{&lt;/del&gt;},
moredelim=[is][\color{blue}\underbar]{&lt;ins&gt;}{&lt;/ins&gt;},
% smalltalk stuff
% morecomment=[s][\myvs]{|}{|},
morestring=[b][\myStringStyle]',
moredelim=[is][]{&lt;sel&gt;}{&lt;/sel&gt;},
moredelim=[is][]{&lt;rcv&gt;}{&lt;/rcv&gt;},
moredelim=[is][\itshape]{&lt;symb&gt;}{&lt;/symb&gt;},
moredelim=[is][\scshape]{&lt;class&gt;}{&lt;/class&gt;},
morekeywords={true,false,nil,self,super,thisContext},
identifierstyle=\idstyle,
}

\makeatletter
\newcommand*\idstyle[1]{%
\expandafter\id@style\the\lst@token{#1}\relax%
}
\def\id@style#1#2\relax{%
\ifnum\pdfstrcmp{#1}{\#}=0%
% this is a symbol
\mySymbolStyle{\the\lst@token}%
\else%
\edef\tempa{\uccode#1}%
\edef\tempb{#1}%
\ifnum\tempa=\tempb%
% this is a global
\myGlobalStyle{\the\lst@token}%
\else%
\the\lst@token%
\fi%
\fi%
}
\makeatother
%\newcommand{\ct}{\lstinline[backgroundcolor=\color{white}]}
%\newcommand{\needlines}[1]{\Needspace{#1\baselineskip}}
\newcommand{\lct}{\texttt}

\lstnewenvironment{code}{%
\lstset{%
% frame=lines,
frame=single,
framerule=0pt,
mathescape=false
}%
\noindent%
\minipage{\linewidth}%
}{%
\endminipage%
}%
\lstnewenvironment{codeWithLineNumbers}{%
\lstset{%
% frame=lines,
frame=single,
framerule=0pt,
mathescape=false,
numbers=left
}%
\noindent%
\minipage{\linewidth}%
}{%
\endminipage%
}%

&amp;nbsp;

\newenvironment{codeNonSmalltalk}
{\begin{alltt}\sffamily}
{\end{alltt}\normalsize}
&lt;pre&gt;


## 5 thoughts on “Smalltalk syntax highlighting for Latex”

1. heli says:

hi, I like your macro, but I have one problem: i can’t insert a $in the code. for example: Transcript show:$”
do you have any idea, how to fix this?
best regards heli

1. Ok, I have updated the code and the zipped example with the sources.

1. heli says:

cool thank you