alhassy / org-special-block-extras

A number of new custom blocks and link types for Emacs' Org-mode ^_^
GNU General Public License v3.0
196 stars 15 forks source link

Need help with o-thread-blockcall #23

Open rusimody opened 2 years ago

rusimody commented 2 years ago

Thanks for some mind-blowing elisp work!

Just trying to figure out how to use o-thread-blockcall

Basically I want to use #begin_rename with a fixed list And then wrap it in #begin_eqnarray*

Currently I hacked this far Which basically cutpasted your rename block and hardcoded (one element of) the list I want replaced. goftex is the block I want -- gofer as Latex equations

(o-defblock goftex (list "-> to \\leftarrow") nil
   (s-replace-all
   (--map (cons (car it) (cadr it))
          (--map (s-split " to " (s-trim it))
                 (s-split "," list)))
   contents))

Obviously I'd prefer to reuse your rename not cutpaste it! The other thing is that in the current attempt, org is not recognizing #begin_eqnarray if it is mixed with my goftex. Result of that is that I get extra \( and \) enclosing the \leftarrow inside the begin{eqnarray*}

alhassy commented 2 years ago

Hey @rusimody, I'm glad you like it 😊

Admittedly, I thought o-thread-blockcall was a neat idea and so implemented it; but I, apparently , never actually use it for anything (i.e., for any blocks I've actually defined). It seems that it's broken; i.e., has not been maintained as other things have been improved. A nice thing to do, after fixing it, would be to add unit tests. 😣

But the idea of o-thread-blockcall is pretty straightforward, it's a mixture of thread-last and o--blockcall, so we can just use these two togehter to achieve your aim. 😁

(o-defblock align nil nil
   "A block for aligned equations."
    (format "\\begin{align*} %s \\end{align*}" raw-contents))

(o-defblock goftex (shorthands "-> to \\Rightarrow") nil
  "SHORTHANDS is a comma-separated list of β€˜to’-separated string-to-LaTeX pairs."
  (thread-last raw-contents          ;; Get the block's raw contents,
    (o--blockcall align)             ;; Place them in an β€˜align’ block,
    (o--blockcall rename shorthands) ;; Place that into a β€˜rename’ block
    (s-replace-regexp "#\\+.*" ""))) ;; (← implementation fudge factor)

Since the above (o-defblock align ...) is pretty barebones, here's a fancy one for you ---on the house πŸ˜‰

(o-defblock align nil (numbered nil)
  "A block for aligned equations.

  - NUMBERED: Non-nil if you want the equations to be numbered.
    - Lines that have a β€˜\\tag’, a display name, override any numbering.
  - Block contents are interpretted directly as LaTeX elements, not Org markup.
  - The ampersand character & determines where the equations align.
  - We provide a shorthand for beautifully coloured tags, and labels:
    -  β€œ :tag-π‘ͺ𝑢𝑳𝑢𝑼𝑹: 𝑻𝑰𝑻𝑳𝑬 ” β‰ˆ β€œ \\tag{$\\color{π‘ͺ𝑢𝑳𝑢𝑼𝑹}{𝑻𝑰𝑻𝑳𝑬}$} \\label{𝑻𝑰𝑻𝑳𝑬} \\\\ ”
    - Note, if a line does not have such a tag, it should end with β€˜\\\\’.
  - Finally, for MathJax to be loaded via Org, you should have some Math outside
    this block; e.g., $\\;$ anywhere in your Org document, or an \\eqref{β‹―} anywhere.

  Example use:

     #+begin_align
     y    &= m \\cdot x + b            &  e^{i \\cdot \\pi} + 1 = 0                :tag-pink: Weird Symbols
     rise &= slope \\cdot run + base   &  \\mathsf{fun\\; circle\\; stuff}         :tag-blue: Friendly English
     \\hline
     p ∧ q &≑ p ≑ q ≑ p ∨ q     &  \\min\\{p, q\\} = p \\;≑\\; q = \\max\\{p,q\\} :tag-gold: Golden Rule
     #+end_align
     Since 𝔹oolean equivalence, β€˜β‰‘β€™, is associative and symmetric; the \eqref{Golden Rule}
     can be parsed as a definition for (1) conjunction β€˜βˆ§β€™, (2) equivalence β€˜β‰‘β€™, and (3) disjunction β€˜βˆ¨β€™."
  (-let [n? (if numbered "" "*")]
    (format "\\begin{align%s}\n %s \n\\end{align%s}" n?
            (s-replace-regexp ":tag-\\(.*\\):\\( \\)*\\(.*\\)\\( \\)*"
                              "\\\\tag{$\\\\color{\\1}{\\\\text{\\3}}$} \\\\label{\\3} \\\\\\\\"
                              raw-contents)
            n?)))

With this new fancy definition loaded, you can type into an Org file...

(Note: $\\Rightarrow$ is implication.)
#+begin_goftex "== to \\quad\\equiv\\quad, -> to \\Rightarrow, TT to \\mathsf{true}"
p -> p &== TT    :tag-green: $->$-reflexivity
TT &== p -> p    :tag-blue: $TT$-definition
#+end_goftex

Then when you export (to HTML), you obtain...

hola-22
rusimody commented 2 years ago

OOOOO---FFFF!! Thanks for taking so much trouble. And that's some heavy duty macrology out there... It'll take me a bit to digest πŸ˜‚πŸ˜œ

Currently I am grappling with some more basic stuff I am getting some spurious stuff in the contents string Your blocks seem not to suffer it while mine (current attempts) do

Here's a hopefully small eg Took your stutter renamed mystutter and added a print. Thus:

(o-defblock mystutter (reps 2) nil
  "Output the CONTENTS of the block REPS many times"
  (-let [num (if (numberp reps) reps (string-to-number reps))]
    (print (format "contents: %s" contents))
    (s-repeat num contents)

    ))

With that stutter seems to work ok But my messages buffer shows:

"contents: 
#+end_export

The actual content of my blocks call
#+begin_export latex
"

Notice the "#+end..." ... "#begin..."???

Somehow your example blocks don't suffer this extra while mine do I can of course strip this stuff off. But I dont know what exactly it is and if its details can be different. So would like some clarity on that.

Also here's my current attempt of what I am trying to do. (My latex is weaker than my Lisp; And html side not worked out at all for now

(o-defblock double nil nil
        "Makes a minipage pair of haskell and python which is SSS separated"
        (let*  ((hp (s-split "SSS" contents))
            (h (car hp))
            (p (cadr hp))
            ;;(h' (blockcall rename rlist h))
            (hmp (format (pcase backend
                   (`latex
                    ;; the {3in} was {\\linewidth}
      "\\par 
          \\begin{minipage}[t]{3in} 
          \\begin{multicols}{%s}
          %s
          \\end{multicols}\\end{minipage}")

                                  (_
      "<div style=\"column-count: %s;\">%s</div>"))
             2 h))
                 (pmp (format (pcase backend
                   (`latex
       "\\par 
          \\begin{minipage}[t]{3in}
          \\begin{multicols}{%s}
          %s
          \\end{multicols}\\end{minipage}")
                                   (_
      "<div style=\"column-count: %s;\">%s</div>"))
             2 p)))
          (print (format "contents: %s" contents))
          (format "%s %s" hmp pmp))

        )

Being a teacher myself I'd say this is D-grade Lisp so far πŸ™ƒ

a. I just took your parallel block and started hacking on it b. The 3in is because I am still figuring out what goes there c. The html is completely unworked out; just copy of yours with the "%s"es removed to match the actual formats d. hp is the incoming haskell-python pair, h is haskell part p is python part hmp is haskell-minipage Likewise pmp e. The h' is for the yet to be done rename that I earlier talked of

Actually I want the haskell to be in begin{align} etc ie as math And the python to be in #+begin_src python ... But this part is not anywhere there yet since I am not getting much more basic stuff like passing parameters etc yet

rusimody commented 2 years ago

Aaahhh Just noticed your...

(s-replace-regexp "#\\+.*" ""))) ;; (← implementation fudge factor)

πŸ˜†πŸ˜‚

When and how to use??

Here's a mwe of what I want to do:

#+begin_double
id.x = x
SSS
def id(x): return x
#+end_double
rusimody commented 2 years ago

A nice thing to do, after fixing it, would be to add unit tests.

I did notice some typos, doc bugs etc. If you like I could send a pull request for these (the ones I immediately see)