Colored Haskell Listings in LaTeX

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Colored Haskell Listings in LaTeX

Artem Pelenitsyn
Dear Cafe,

In his recent Stitch manuscript,
Richard Eisenberg mentions that he uses lhs2TeX to typeset Haskell listings, but I'm not aware of the support for colors in lhs2TeX. Can anyone suggest how to get such a nice code highlighting (presumably with lhs2TeX).

--
Best wishes,
Artem

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Colored Haskell Listings in LaTeX

Oleg Grenrus
Hi Artem,

I answer with inline lhs2Tex file. Hopefully it helps in your typed-code
type-settings!

cheers, Oleg.

\documentclass{article}
%include polycode.fmt

\usepackage{hyperref}
\hypersetup{pdfborder={0 0 0}}

% in lhs2TeX.style there are
%
% \newcommand{\Conid}[1]{{\mathit #1}}
% \newcommand{\Varid}[1]{{\mathit #1}}
% \newcommand{\anonymous}{\_}
%
% We can renew these

\usepackage{xcolor}
\definecolor{darkred}{rgb}{.5,0,0}
\definecolor{darkgreen}{rgb}{0,0.5,0}
\definecolor{darkblue}{rgb}{0,0,.5}
\definecolor{color4}{rgb}{0,.4,.4}
\definecolor{color5}{rgb}{.4,.4,0}

\renewcommand{\Conid}[1]{{\color{darkblue}\mathit #1}}

% however types and constructors look the same, we can differentiate
them though

%format Foo = "{\color{darkred}\mathit Foo}"
%format MkFoo = "{\color{darkgreen}\mathit Foo}"

% Note how I "cheat" making MkFoo render as Foo!

% We can also highlight operators
%format + = "\mathbin{\color{color4}+}"

% or symbols (note I also make thing look prettier)"
%format plusFoo = "{\color{color5}\mathit plus_{Foo}}"
%format ColorsInLhs2TeX = "{\text{Colors in lhs2\TeX}}"

\begin{document}

An example of colorful lhs2\TeX\ file.
See the source at
\url{https://github.com/phadej/gists/blob/master/posts/2018-06-21-colors-in-lhs2tex.tex}%
\footnote{It's named tex to trick \emph{Pandoc} in my blog setup},
and the result PDF at
\url{https://github.com/phadej/gists/blob/master/pdf/ColorsInLhs2TeX.pdf}

\begin{code}
module ColorsInLhs2TeX where

newtype Foo = MkFoo Int

plusFoo :: Foo -> Foo -> Int
plusFoo (MkFoo n) (MkFoo m) = n + m
\end{code}
\end{document}


On 21.06.2018 18:29, Artem Pelenitsyn wrote:

> Dear Cafe,
>
> In his recent Stitch manuscript,
> https://cs.brynmawr.edu/~rae/papers/2018/stitch/stitch.pdf
> <https://cs.brynmawr.edu/%7Erae/papers/2018/stitch/stitch.pdf>
> Richard Eisenberg mentions that he uses lhs2TeX to typeset Haskell
> listings, but I'm not aware of the support for colors in lhs2TeX. Can
> anyone suggest how to get such a nice code highlighting (presumably
> with lhs2TeX).
>
> --
> Best wishes,
> Artem
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Colored Haskell Listings in LaTeX

Artem Pelenitsyn
Dear Oleg,

Thanks for the reply! It works modulo line 24 where, it seems, your mail client broke the line which happened to be inside comment and that drives pdflatex mad. The gist is fine though, so I put the link here:

Your answer shows the approach one could apply to tune the color of various lexical element. I wonder if there are prebaked set of definitions covering the whole (or most of) grammar: literals, comments, type variables…  {- Obviously, RIchard has one :) -}

--
Best, Artem

On Thu, 21 Jun 2018 at 19:22 Oleg Grenrus <[hidden email]> wrote:
Hi Artem,

I answer with inline lhs2Tex file. Hopefully it helps in your typed-code
type-settings!

cheers, Oleg.

\documentclass{article}
%include polycode.fmt

\usepackage{hyperref}
\hypersetup{pdfborder={0 0 0}}

% in lhs2TeX.style there are
%
% \newcommand{\Conid}[1]{{\mathit #1}}
% \newcommand{\Varid}[1]{{\mathit #1}}
% \newcommand{\anonymous}{\_}
%
% We can renew these

\usepackage{xcolor}
\definecolor{darkred}{rgb}{.5,0,0}
\definecolor{darkgreen}{rgb}{0,0.5,0}
\definecolor{darkblue}{rgb}{0,0,.5}
\definecolor{color4}{rgb}{0,.4,.4}
\definecolor{color5}{rgb}{.4,.4,0}

\renewcommand{\Conid}[1]{{\color{darkblue}\mathit #1}}

% however types and constructors look the same, we can differentiate
them though

%format Foo = "{\color{darkred}\mathit Foo}"
%format MkFoo = "{\color{darkgreen}\mathit Foo}"

% Note how I "cheat" making MkFoo render as Foo!

% We can also highlight operators
%format + = "\mathbin{\color{color4}+}"

% or symbols (note I also make thing look prettier)"
%format plusFoo = "{\color{color5}\mathit plus_{Foo}}"
%format ColorsInLhs2TeX = "{\text{Colors in lhs2\TeX}}"

\begin{document}

An example of colorful lhs2\TeX\ file.
See the source at
\url{<a href="https://github.com/phadej/gists/blob/master/posts/2018-06-21-colors-in-lhs2tex.tex%7D%" rel="noreferrer" target="_blank">https://github.com/phadej/gists/blob/master/posts/2018-06-21-colors-in-lhs2tex.tex}%
\footnote{It's named tex to trick \emph{Pandoc} in my blog setup},
and the result PDF at
\url{https://github.com/phadej/gists/blob/master/pdf/ColorsInLhs2TeX.pdf}

\begin{code}
module ColorsInLhs2TeX where

newtype Foo = MkFoo Int

plusFoo :: Foo -> Foo -> Int
plusFoo (MkFoo n) (MkFoo m) = n + m
\end{code}
\end{document}


On 21.06.2018 18:29, Artem Pelenitsyn wrote:
> Dear Cafe,
>
> In his recent Stitch manuscript,
> https://cs.brynmawr.edu/~rae/papers/2018/stitch/stitch.pdf
> <https://cs.brynmawr.edu/%7Erae/papers/2018/stitch/stitch.pdf>
> Richard Eisenberg mentions that he uses lhs2TeX to typeset Haskell
> listings, but I'm not aware of the support for colors in lhs2TeX. Can
> anyone suggest how to get such a nice code highlighting (presumably
> with lhs2TeX).
>
> --
> Best wishes,
> Artem
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Colored Haskell Listings in LaTeX

Richard Eisenberg-4
The source code for my thesis is public, and it includes this file: https://github.com/goldfirere/thesis/blob/master/tex/rae.fmt

But there weren't colors in my thesis. (Or, at least, the code wasn't syntax highlighted with colors.)

These days, I use that fmt file with these definitions in the tex file:

\newcommand{\keyword}[1]{\textcolor{BlueViolet}{\textbf{#1}}}
\newcommand{\id}[1]{\textsf{\textsl{#1}}}
\newcommand{\varid}[1]{\textcolor{Sepia}{\id{#1}}}
\newcommand{\conid}[1]{\textcolor{OliveGreen}{\id{#1}}}

I hope this is helpful!
Richard

On Jun 21, 2018, at 5:20 PM, Artem Pelenitsyn <[hidden email]> wrote:

Dear Oleg,

Thanks for the reply! It works modulo line 24 where, it seems, your mail client broke the line which happened to be inside comment and that drives pdflatex mad. The gist is fine though, so I put the link here:

Your answer shows the approach one could apply to tune the color of various lexical element. I wonder if there are prebaked set of definitions covering the whole (or most of) grammar: literals, comments, type variables…  {- Obviously, RIchard has one :) -}

--
Best, Artem

On Thu, 21 Jun 2018 at 19:22 Oleg Grenrus <[hidden email]> wrote:
Hi Artem,

I answer with inline lhs2Tex file. Hopefully it helps in your typed-code
type-settings!

cheers, Oleg.

\documentclass{article}
%include polycode.fmt

\usepackage{hyperref}
\hypersetup{pdfborder={0 0 0}}

% in lhs2TeX.style there are
%
% \newcommand{\Conid}[1]{{\mathit #1}}
% \newcommand{\Varid}[1]{{\mathit #1}}
% \newcommand{\anonymous}{\_}
%
% We can renew these

\usepackage{xcolor}
\definecolor{darkred}{rgb}{.5,0,0}
\definecolor{darkgreen}{rgb}{0,0.5,0}
\definecolor{darkblue}{rgb}{0,0,.5}
\definecolor{color4}{rgb}{0,.4,.4}
\definecolor{color5}{rgb}{.4,.4,0}

\renewcommand{\Conid}[1]{{\color{darkblue}\mathit #1}}

% however types and constructors look the same, we can differentiate
them though

%format Foo = "{\color{darkred}\mathit Foo}"
%format MkFoo = "{\color{darkgreen}\mathit Foo}"

% Note how I "cheat" making MkFoo render as Foo!

% We can also highlight operators
%format + = "\mathbin{\color{color4}+}"

% or symbols (note I also make thing look prettier)"
%format plusFoo = "{\color{color5}\mathit plus_{Foo}}"
%format ColorsInLhs2TeX = "{\text{Colors in lhs2\TeX}}"

\begin{document}

An example of colorful lhs2\TeX\ file.
See the source at
\url{<a href="https://github.com/phadej/gists/blob/master/posts/2018-06-21-colors-in-lhs2tex.tex%7D%" rel="noreferrer" target="_blank" class="">https://github.com/phadej/gists/blob/master/posts/2018-06-21-colors-in-lhs2tex.tex}%
\footnote{It's named tex to trick \emph{Pandoc} in my blog setup},
and the result PDF at
\url{https://github.com/phadej/gists/blob/master/pdf/ColorsInLhs2TeX.pdf}

\begin{code}
module ColorsInLhs2TeX where

newtype Foo = MkFoo Int

plusFoo :: Foo -> Foo -> Int
plusFoo (MkFoo n) (MkFoo m) = n + m
\end{code}
\end{document}


On 21.06.2018 18:29, Artem Pelenitsyn wrote:
> Dear Cafe,
>
> In his recent Stitch manuscript,
> https://cs.brynmawr.edu/~rae/papers/2018/stitch/stitch.pdf
> <https://cs.brynmawr.edu/%7Erae/papers/2018/stitch/stitch.pdf>
> Richard Eisenberg mentions that he uses lhs2TeX to typeset Haskell
> listings, but I'm not aware of the support for colors in lhs2TeX. Can
> anyone suggest how to get such a nice code highlighting (presumably
> with lhs2TeX).
>
> --
> Best wishes,
> Artem
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Colored Haskell Listings in LaTeX

Artem Pelenitsyn
Thanks, RIchard!

I talked about your Stitch paper, not the thesis. But thanks for the reference: the %format things from there are also useful to me.

The newcommands you gave almost worked for me. There are a couple of points:

- newcommand{\varid} → \renewcommand{\Varid}, and the same for conid (thanks to Oleg again, for leading me to that); otherwise it does nothing;

- keyword: again, it does nothing to me as is given; I wasn't able to find TeX'y way to do that, so I went to %subst:

%subst keyword a = "\textcolor{BlueViolet}{\textbf{" a "}}"

The resulting minimal-working-example is on this gist:

Still, I would be happy to look at the whole preamble of Stitch: I believe, there could be something more to steal :)

--
Best wishes,
Artem

On Sat, 23 Jun 2018 at 05:32 Richard Eisenberg <[hidden email]> wrote:
The source code for my thesis is public, and it includes this file: https://github.com/goldfirere/thesis/blob/master/tex/rae.fmt

But there weren't colors in my thesis. (Or, at least, the code wasn't syntax highlighted with colors.)

These days, I use that fmt file with these definitions in the tex file:

\newcommand{\keyword}[1]{\textcolor{BlueViolet}{\textbf{#1}}}
\newcommand{\id}[1]{\textsf{\textsl{#1}}}
\newcommand{\varid}[1]{\textcolor{Sepia}{\id{#1}}}
\newcommand{\conid}[1]{\textcolor{OliveGreen}{\id{#1}}}

I hope this is helpful!
Richard

On Jun 21, 2018, at 5:20 PM, Artem Pelenitsyn <[hidden email]> wrote:

Dear Oleg,

Thanks for the reply! It works modulo line 24 where, it seems, your mail client broke the line which happened to be inside comment and that drives pdflatex mad. The gist is fine though, so I put the link here:

Your answer shows the approach one could apply to tune the color of various lexical element. I wonder if there are prebaked set of definitions covering the whole (or most of) grammar: literals, comments, type variables…  {- Obviously, RIchard has one :) -}

--
Best, Artem

On Thu, 21 Jun 2018 at 19:22 Oleg Grenrus <[hidden email]> wrote:
Hi Artem,

I answer with inline lhs2Tex file. Hopefully it helps in your typed-code
type-settings!

cheers, Oleg.

\documentclass{article}
%include polycode.fmt

\usepackage{hyperref}
\hypersetup{pdfborder={0 0 0}}

% in lhs2TeX.style there are
%
% \newcommand{\Conid}[1]{{\mathit #1}}
% \newcommand{\Varid}[1]{{\mathit #1}}
% \newcommand{\anonymous}{\_}
%
% We can renew these

\usepackage{xcolor}
\definecolor{darkred}{rgb}{.5,0,0}
\definecolor{darkgreen}{rgb}{0,0.5,0}
\definecolor{darkblue}{rgb}{0,0,.5}
\definecolor{color4}{rgb}{0,.4,.4}
\definecolor{color5}{rgb}{.4,.4,0}

\renewcommand{\Conid}[1]{{\color{darkblue}\mathit #1}}

% however types and constructors look the same, we can differentiate
them though

%format Foo = "{\color{darkred}\mathit Foo}"
%format MkFoo = "{\color{darkgreen}\mathit Foo}"

% Note how I "cheat" making MkFoo render as Foo!

% We can also highlight operators
%format + = "\mathbin{\color{color4}+}"

% or symbols (note I also make thing look prettier)"
%format plusFoo = "{\color{color5}\mathit plus_{Foo}}"
%format ColorsInLhs2TeX = "{\text{Colors in lhs2\TeX}}"

\begin{document}

An example of colorful lhs2\TeX\ file.
See the source at
\url{<a href="https://github.com/phadej/gists/blob/master/posts/2018-06-21-colors-in-lhs2tex.tex%7D%" rel="noreferrer" target="_blank">https://github.com/phadej/gists/blob/master/posts/2018-06-21-colors-in-lhs2tex.tex}%
\footnote{It's named tex to trick \emph{Pandoc} in my blog setup},
and the result PDF at
\url{https://github.com/phadej/gists/blob/master/pdf/ColorsInLhs2TeX.pdf}

\begin{code}
module ColorsInLhs2TeX where

newtype Foo = MkFoo Int

plusFoo :: Foo -> Foo -> Int
plusFoo (MkFoo n) (MkFoo m) = n + m
\end{code}
\end{document}


On 21.06.2018 18:29, Artem Pelenitsyn wrote:
> Dear Cafe,
>
> In his recent Stitch manuscript,
> https://cs.brynmawr.edu/~rae/papers/2018/stitch/stitch.pdf
> <https://cs.brynmawr.edu/%7Erae/papers/2018/stitch/stitch.pdf>
> Richard Eisenberg mentions that he uses lhs2TeX to typeset Haskell
> listings, but I'm not aware of the support for colors in lhs2TeX. Can
> anyone suggest how to get such a nice code highlighting (presumably
> with lhs2TeX).
>
> --
> Best wishes,
> Artem
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.