kosmikus / lhs2tex

Preprocessor for typesetting Haskell sources with LaTeX
Other
99 stars 28 forks source link

polytext mode #66

Open nomeata opened 6 years ago

nomeata commented 6 years ago

I really like lhs2Tex for its vertical alignment, but somtimes other features get into the way. I would therefore love to have a polytext mode that does

I use this file right now

%if not polytt_read
%let polytt_read = True

\newcommand{\ensuretext}[1]{\ifmmode\text{#1}\else{#1}\fi}

%subst verb a       = "\texttt{" a "}"
%subst verbatim a   = "\begin{tabbing}'n" a "'n\end{tabbing}'n"
%subst verbnl       = "\\'n "

%subst comment a    = "\mbox{\onelinecomment " a "}"
%subst nested a         = "\mbox{\commentbegin " a " \commentend}"
%subst code a           = "\begin{hscode}\SaveRestoreHook'n" a "\ColumnHook'n\end{hscode}\resethooks'n"
%subst column c a       = "\column{" c "}{" a "}%'n"
%subst fromto b e t     = "\>[" b "]{}" t "{}\<[" e "]%'n"
%subst left             = "@{}>{\hspre}l<{\hspost}@{}"
%subst centered         = "@{}>{\hspre}c<{\hspost}@{}"
%subst dummycol         = "@{}l@{}"
%subst newline      = "\\'n"
%subst blankline        = "\\[\blanklineskip]%'n"
%subst indent n         = "\hsindent{" n "}"

%format {               = "{\char123}"
%format }               = "{\char125}"
%format ...             = "{\ldots}"

% The first space should be \;, the fourth a quad
\newcounter{spaces}
\newcommand{\spacestart}{\setcounter{spaces}{0}}
\newcommand{\nextspace}{%
  \stepcounter{spaces}%
  \ifnum\value{spaces}=1\;\allowbreak\fi%
  \ifnum\value{spaces}=4\quad\quad\fi%
  }

%let autoSpacing    = False
%subst dummy        = "\cdot"
%subst inline a     = "\ensuretext{\ttfamily{}" a "}"
%subst hskip a          = "\hskip" a "em\relax"
%subst pragma a         = "\mbox{\enskip\{-\#" a " \#-\}\enskip}"
%subst tex a            = a
%subst numeral a    = a
%subst keyword a    = "\textbf{" a "}"
%subst spaces a     = "{\spacestart}" a
%subst special a    = a
%subst space        = "{\nextspace}"
%subst conid a      = a
%subst varid a      = a
%subst consym a     = a
%subst varsym a     = a
%subst backquoted a     = "`" a "`"
%subst char a       = "{\textquotesingle}" a "{\textquotesingle}"
%subst string a     = "\char34\textit{\!" a "}{\char34}"

%% hack, to avoid loading lhs2TeX.fmt from lhs2TeX.sty
%let lhs2tex_lhs2tex_fmt_read = True
%include lhs2TeX.sty

% copied from polycode.fmt
\makeatletter

\newcommand{\hsnewpar}[1]%
  {{\parskip=0pt\parindent=0pt\par\vskip #1\noindent}}

% can be used, for instance, to redefine the code size, by setting the
% command to \small or something alike
\newcommand{\hscodestyle}{\ttfamily}

% The command \sethscode can be used to switch the code formatting
% behaviour by mapping the hscode environment in the subst directive
% to a new LaTeX environment.

\newcommand{\sethscode}[1]%
  {\expandafter\let\expandafter\hscode\csname #1\endcsname
   \expandafter\let\expandafter\endhscode\csname end#1\endcsname}

\newenvironment{plainhscode}%
  {\hsnewpar\abovedisplayskip
   \advance\leftskip\mathindent
   \hscodestyle
   \def\hspre{}%
   \def\hspost{}%
   \pboxed}%
  {\endpboxed%
   \hsnewpar\belowdisplayskip
   \ignorespacesafterend}

% Here, we make plainhscode the default environment.

\newcommand{\plainhs}{\sethscode{plainhscode}}
\plainhs

\makeatother
%endif

It gives me almost all of my wishes, with the exception of the last point (the space-counting hack is an attempt to mitigate that, but does not work nicely.

Some thing could be simplified further, I think.

I would appreciate if such a style could be included in lhs2tex. Or, as a first step, it would be great if I could say autoSpacing = False but (something like) autoIndent = True.

nomeata commented 1 year ago

Writing another paper with the same desired layout, and looking at the code what can be done.

I don’t quite understand in display in polyCode why with autoSpacing = False the indentation (the stuff that inserts calls to \hsindent) doesn’t seem to work. The leftIndent function seems to run no matter what…

Ah, this seems to help:

diff --git a/src/MathPoly.lhs b/src/MathPoly.lhs
index 54e66cb..8e6ce2d 100644
--- a/src/MathPoly.lhs
+++ b/src/MathPoly.lhs
@@ -345,8 +345,9 @@ As a final step, the current line is placed on the stack.
 >       Blank                   -> loop True stack ls -- ignore blank lines
 >    {-| Poly x || trace (show x) False -> undefined |-}
 >       Poly []                 -> loop True stack ls -- next line
->       Poly (((_n,_c),[],_ind):rs)
->         | first               -> loop True stack (Poly rs:ls) -- ignore leading blank columns
+>       Poly (((_n,_c),ts,_ind):rs)
+>         | first, all (not . isNotSpace . token) ts
+>                               -> loop True stack (Poly rs:ls) -- ignore leading blank columns
 >       Poly p@(((n,c),ts,_ind):rs)
 >         | first               -> -- check indentation
 >                                  let -- step 1: shrink stack

With autoSpacing = False the space tokens are still there, and it would be reasonable for the leftIndent code to treat columns with leading spaces as indentation columns.