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 loadedArray | sourceArray := Array with: 'a string' with: Transcript with: 42 with: #foo. "Store to a file" FLSerializer serialize: sourceArray toFileNamed: 'example.FL'. "Load from the file" loadedArray := FLMaterializer materializeFromFileNamed: 'example.FL'. "Check that the materialized Transcript is the right singleton instance." loadedArray second show: loadedArray first; flush. self validateMaterializedGraph: loadedArray. \end{code}
And the pdf looks like this:
This is how you can define pieces of bold, italics and underline:
\begin{code} <b>fuelExample</b> | sourceArray loadedArray | sourceArray := Array with: 'a string' with: Transcript with: 42 with: #foo. "Store to a file" <i>FLSerializer serialize: sourceArray toFileNamed: 'example.FL'.</i> "Load from the file" <u>loadedArray := FLMaterializer materializeFromFileNamed: 'example.FL'.</u> "Check that the materialized Transcript is the right singleton instance." loadedArray second show: loadedArray first; flush. self validateMaterializedGraph: loadedArray. \end{code}
The resulting pdf looks like this:
This is another environment if you want to show number lines:
\begin{codeWithLineNumbers} <b>fuelExample</b> | sourceArray loadedArray | sourceArray := Array with: 'a string' with: Transcript with: 42 with: #foo. "Store to a file" <i>FLSerializer serialize: sourceArray toFileNamed: 'example.FL'.</i> "Load from the file" <u>loadedArray := FLMaterializer materializeFromFileNamed: 'example.FL'.</u> "Check that the materialized Transcript is the right singleton instance." loadedArray second show: loadedArray first; flush. self validateMaterializedGraph: loadedArray. \end{codeWithLineNumbers}
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
</pre> % Source Code \usepackage{color} \usepackage{textcomp} \usepackage{listings} \definecolor{source}{gray}{0.85}% my comment style \newcommand{\myCommentStyle}[1]{{\footnotesize\sffamily\color{gray!100!white} #1}} %\newcommand{\myCommentStyle}[1]{{\footnotesize\sffamily\color{black!100!white} #1}} % 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 commentstyle=\myCommentStyle,% comment style frame=single,% backgroundcolor=\color{source}, % numbering stepnumber=1, numbersep=10pt, numberstyle=\tiny, numberfirstline=true, % caption captionpos=b, % formatting (html) moredelim=[is][\bfseries]{<b>}{</b>}, moredelim=[is][\textit]{<i>}{</i>}, moredelim=[is][\underbar]{<u>}{</u>}, moredelim=[is][\color{red}\uwave]{<wave>}{</wave>}, moredelim=[is][\color{red}\sout]{<del>}{</del>}, moredelim=[is][\color{blue}\underbar]{<ins>}{</ins>}, % smalltalk stuff morecomment=[s][\myCommentStyle]{"}{"}, % morecomment=[s][\myvs]{|}{|}, morestring=[b][\myStringStyle]', moredelim=[is][]{<sel>}{</sel>}, moredelim=[is][]{<rcv>}{</rcv>}, moredelim=[is][\itshape]{<symb>}{</symb>}, moredelim=[is][\scshape]{<class>}{</class>}, 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% }% &nbsp; \newenvironment{codeNonSmalltalk} {\begin{alltt}\sffamily} {\end{alltt}\normalsize} <pre>
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
Hi Heli. I found the solution in: http://tex.stackexchange.com/questions/18452/use-in-lstlisting.
But basically it is: literate={\$}{{{\$}}}1.
Also, notice that the code I pasted in this post is quite outdated. I will paste a new one soon.
Ok, I have updated the code and the zipped example with the sources.
cool thank you
How dou you highlight the latex code?