From 4459595f44adb15a1e88e63ee7e97e574fbb3a79 Mon Sep 17 00:00:00 2001
From: Chloe Brown <chloe.brown.00@outlook.com>
Date: Wed, 23 Apr 2025 16:37:39 +0100
Subject: Improve multiline let formatting.

---
 main.tex | 42 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 34 insertions(+), 8 deletions(-)

(limited to 'main.tex')

diff --git a/main.tex b/main.tex
index c2eaafe..0e0c6ff 100644
--- a/main.tex
+++ b/main.tex
@@ -171,21 +171,47 @@
 \newcommand\letkw{\ensuremath{\mathsf{let}}}
 \newcommand\lettm[3]{\letkw~{#2} \coloneq {#1}~\mathsf{in}~{#3}}
 \ExplSyntaxOn
-\NewDocumentCommand \squid_letpart {m m}
-  { \letkw & {#1} = \cr
-    & {#2} \cr
-    \mathsf{in} &
+%% args:
+%%   1. Do I need a & on the first line?
+%%   2. Binder name
+%%   3. Is value multiline?
+%%   4. Value
+\NewDocumentCommand \squid_letpart {m m m m}
+  { \IfBooleanTF{#3}
+      { \letkw \IfBooleanTF{#1}{&}{\nobreakspace} {#2} = \cr
+        & {#4} \cr
+        \mathsf{in}
+      }
+      { \letkw \IfBooleanTF{#1}{&}{\nobreakspace} {#2} = {#4} \nobreakspace \mathsf{in}
+      }
   }
-\NewDocumentCommand \squid_contlet {> {\SplitArgument{1}{=}} m}
-  { \squid_letpart #1 \squid_checklet [\cr]
+%% args:
+%%   1. Do I need a & on the first line?
+%%   2. Binder name
+%%   3. Is value multiline?
+%%   4. Value
+%%   ... further processing
+\NewDocumentCommand \squid_contlet {m m s m}
+  { \squid_letpart {#1} {#2} {#3} {#4}
+    \IfBooleanTF{#3}
+      { \squid_checklet [\BooleanFalse] }
+      { \squid_checklet [\BooleanTrue] }
   }
+%% args:
+%%   1. If something, do I need a new line?
+%%   2. Is the next arg not a binder?
+%%   ... further processing
 \NewDocumentCommand \squid_checklet {o s m}
   { \IfBooleanTF{#2}
-      { \IfNoValueF{#1}{#1} & {#3}
+      { \IfNoValueF{#1}{\IfBooleanT{#1}{\cr}} & {#3}
         \end{array}
         \wlog{End~let}
       }
-      { \IfNoValueF{#1}{#1} \squid_contlet {#3}
+      { \IfNoValueTF{#1}
+          { \squid_contlet \BooleanTrue {#3} }
+          { \IfBooleanTF{#1}{\cr}{&}
+            \squid_contlet {#1} {#3}
+          }
       }
   }
 \NewDocumentCommand \dolet {O{t}}
-- 
cgit v1.2.3

