3

I asked a question a while back on how to typeset programs as beautiful math. The suggestion I got was to use listings. But this has been unsatisfactory.

Basically my 'programs' are in a gray area between programming and math. They are ASCII text at some low level but the idea is that they should look like math equations:

  • = signs properly lined up
  • math fonts and spacing
  • suitable replacement of ASCII by math artifacts as in Knuth-WEB

Here is the look with listings and math (alignat) interleaved, to show roughly what I would like to have.

pdflatex output

The latex source

\documentclass[conference,a4paper]{IEEEtran}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{mystyle}
\newcommand\scalemath[2]{\scalebox{#1}{\mbox{\ensuremath{\displaystyle #2}}}}
\newcommand{\gufx}[1] {\scalemath{1.1}{\text{\,\guilsinglleft{#1}\guilsinglright}}\,\,}
\date{}
\title{A Title}

\begin{document}

\section{Examples} \label{sec:org31daa8a} Haskell eg1 \begin{HSK} swaptree.(Leaf.x) = Leaf.x swaptree.(Br.l.r) = Br.(swaptree.r).(swaptree.l) \end{HSK}

Math eg1 {\small \begin{alignat}{3} &swaptree.(\textbf{Leaf}.x)&&= \textbf{Leaf}.x\ &swaptree.(\textbf{Br}.l.r)&&= \textbf{Br}.(swaptree.r).(swaptree.l) \end{alignat}}

Haskell eg2 \begin{HSK} c : Int -> Int -> Int 0 c (r+1) = 0 n c 0 = 1 (n+1) c r = n c r + n c (r-1) \end{HSK}

Math eg 2 \begin{alignat}{4} &c &&: \mathbb{Z} \rightarrow \mathbb{Z} \rightarrow \mathbb{Z}\ &0 &&\gufx{c} (r+1) &&= 0\ &n &&\gufx{c} 0 &&= 1\ &(n+1) &&\gufx{c} r &&= n \gufx{c} ! r + n \gufx{c} !(r-1) \end{alignat}

Haskell eg3 \begin{HSK} ce : [t] -> Int -> [[t]] [] cE (r+1)= [] xs cE 0 = [[]] (x::xs)cE r = xs cE r ++ [x::y| y <- xs cE (r-1)] \end{HSK}

Math eg3 \begin{alignat}{5} &ce &&&&: [t] \rightarrow \mathbb{Z} \rightarrow [[t]]\ &[,] &&\gufx{ce}(r!+!1)&&= [,]\ &xs &&\gufx{ce} 0 &&= [[,]]\ &(x::xs) &&\gufx{ce} r &&= xs \gufx{ce} r +!!!!!+,, [x::y \mid y \in (xs \gufx{ce} !(r!-!1))]\ \end{alignat} \end{document}

The associated style file mystyle.sty

\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{listings}
\usepackage{bbding}
\usepackage[fleqn]{calculation}
\usepackage{tikz-qtree}
\usepackage[figurename=Code, font={small,it}, belowskip=-8pt, aboveskip=0pt]{caption}
\captionsetup{skip=0.333\baselineskip} 
%\usepackage{arevmath}

\DeclareFontEncoding{MDA}{}{} \DeclareFontSubstitution{MDA}{cmr}{m}{n}

\lstnewenvironment{HSK} {\lstset{language=Haskell, basicstyle=\footnotesize\ttfamily, identifierstyle=\footnotesize\itshape, keywordstyle=\footnotesize\itshape\bfseries, aboveskip=5pt,belowskip=5pt, flexiblecolumns=false, morekeywords={ctype}, frame=single, breaklines=true, basewidth={0.5em,0.45em}, frameshape={RYR}{Y}{Y}{RYR}, literate={+}{{\texttt{+}}}1 {/}{{$/$}}1 {}{{\texttt{}}}1 {=}{{\texttt{=}}}1 {:}{{\texttt{:}}}1 {::}{{\texttt{::}}}2 {>}{{$>$}}1 {<}{{$<$}}1 {\}{{$\lambda$}}1 {\\}{{\char\\\char\}}1 {->}{{$\rightarrow$}}4 {>=}{{$\geq$}}2 {<-}{{{\scriptsize$\in$}}}1 {=}{{=}}3 {.}{{,.}}1 %{Int}{{${\mathbf{Z}}$}}2 %{...}{{$\ldots$}}2 {,}{{\texttt{,}}}1 {<=}{{$\leq$}}2 {=>}{{$\Rightarrow$}}2 %{\ .}{{$\circ$}}2 {\ .\ }{{$\circ$}}2 {>>}{{>>}}2 {>>=}{{>>=}}2 {|}{{$\mid$}}1 {?}{{\textbf{?}}}1 }}{}

\lstdefinestyle{HSKstyle} { language=Haskell, basicstyle=\footnotesize\ttfamily, identifierstyle=\footnotesize\itshape, keywordstyle=\footnotesize\itshape\bfseries, literate={+}{{\texttt{+}}}1 {/}{{$/$}}1 {}{{\texttt{}}}1 {=}{{\texttt{=}}}1 {:}{{\texttt{:}}}1 {::}{{\texttt{::}}}2 {->}{{$\rightarrow$}}4 } \newcommand{\HSKinline}[1]{\lstinline[style=HSKstyle]{#1}}

As one can see the math hacks are quite horrible! The holy grail is of course if I could just drop in the source code as in in a suitably tailored alignat (or whatever) mode. I am assuming this is quite hard!

But a few things would help:

  1. Sed like replacements
    • -> to \rightarrow
    • Tree to $\mathbf{Tree}$
    • Int to $\mathbb{Z}$ etc
    • Better if all upcased names can be so handled but if thats harder just a list of
      mathbf names in some setup place
  2. operator specs rather than \, \! hacks
  3. `identifier` to identifier in guillemet with operator spacing
  4. Newlines to be prepended by \\
  5. Insert && before = (but not before ==)
  6. To be able to restart alignats aligning without a new alignat
    Just see example 3: The first line is compelled to be aligned along with the remaining 3. It should be independent.

PS Thanks to egreg for the encouragement to ask.

Please ignore the visible frames or lack thereof

Rusi
  • 353

0 Answers0