Re: Account for latex snippet width in fill paragraph

2021-02-09 Thread Matt Huszagh
Eric S Fraga  writes:

> I don't have this issue because I use visual-line-mode (together with
> org-indent-mode) and text reflows automatically.  Every paragraph is a
> single "line" in the org file but is (soft) wrapped automatically at the
> window edge.  But this may not suit you, of course.

This works very well. Thanks for the suggestion!

Matt



Account for latex snippet width in fill paragraph

2021-02-08 Thread Matt Huszagh
Hello,

I make extensive use of inline latex image snippets in my Org
buffers. One thing that has annoyed me for a while is that
`org-fill-paragraph' (unsurprisingly) uses the width of the underlying
text when determining the characters which make up each line. Because
the characters which make up the source of an image are typically wider
than the image itself, this creates short lines when many snippets are
used. For example, the following text

```
This gives some degree of temperature-stability because although
\(V_{\mathrm{BE}}\) changes somewhat with temperature, the change is
small. If the quiescent output voltage is designed for say
\(10V_{\mathrm{BE}}\), and \(V_{\mathrm{BE}}\) varies \(\SI{0.1}{V}\)
over temperature changes, then the quiescent point will vary
\(\SI{1}{V}\) over temperature changes. This isn't great, but may
sometimes be good enough.
```

appears something like

```
This gives some degree of temperature-stability because although
VBE changes somewhat with temperature, the change is
small. If the quiescent output voltage is designed for say
10VBE, and VBE varies 0.1V
over temperature changes, then the quiescent point will vary
1V over temperature changes. This isn't great, but may
sometimes be good enough.
```

Before I take a crack at this, has anyone else attempted to remedy this?
I wasn't able to find anything on the mailing list. Would anyone else be
interested in this functionality? I figure a proper solution would
appear to the user as a configurable variable that could enable filling
based on image width (this behavior might be undesirable in some
cases). My currently uninformed view is that it should be possible to
query the overlay image width and perform a fill based on that. Any
thoughts, concerns, etc. are appreciated.

Matt



Re: babel default header args as functions

2020-12-21 Thread Matt Huszagh
Bastien,

Any chance you've had the time to look at this? I know how much time you
put into org-mode, so no rush if you need more time! Let me know if
there's anything I can do on my end.

Thanks!
Matt



Re: [PATCH] babel latex headers and image generation commands

2020-10-24 Thread Matt Huszagh
Bastien  writes:

> sorry for the delayed answer.

No worries!

> Can you provide a patch against etc/ORG-NEWS announce this?

Attached. Let me know what you think.

Matt

>From 51fb3ef9843ae45884803142f150c5d2f4f4d4c9 Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Sat, 24 Oct 2020 09:36:56 -0700
Subject: [PATCH] etc/ORG-NEWS: Describe new behavior of babel LaTeX SVG images

---
 etc/ORG-NEWS | 22 ++
 1 file changed, 22 insertions(+)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 7f935bf52..b9b0c1271 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -35,6 +35,28 @@ omit a file description was to omit the header argument entirely,
 which made it difficult/impossible to provide a default value for
 =file-desc=.
 
+*** New behavior for babel LaTeX SVG image files
+
+Org babel now uses a two-stage process for converting latex source
+blocks to SVG image files (when the extension of the output file is
+~.svg~). The first stage in the process converts the latex block into
+a PDF file, which is then converted into an SVG file in the second
+stage. The TeX->PDF part uses the existing infrastructure for
+~org-babel-latex-tex-to-pdf~. The PDF->SVG part uses a command
+specified in a new customization,
+~org-babel-latex-pdf-svg-process~. By default, this uses inkscape for
+conversion, but since it is fully customizable, any other command can
+be used in its place. For instance, dvisvgm might be used here. This
+two-part processing replaces the previous use of htlatex to process
+LaTeX directly to SVG (htlatex is still used for HTML conversion).
+
+Conversion to SVG exposes a number of additional customizations that
+give the user full control over the contents of the latex source
+block. ~org-babel-latex-preamble~, ~org-babel-latex-begin-env~ and
+~org-babel-latex-end-env~ are new customization options added to allow
+the user to specify the preamble and code that preceedes and proceeds
+the contents of the source block.
+
 ** New features
 *** =ob-python= improvements to =:return= header argument 
 
-- 
2.28.0



Re: babel default header args as functions

2020-10-14 Thread Matt Huszagh
rey-coyrehourcq  writes:

> I'm unfamilliar with patch by mail but i try to apply your patch to my melpa 
> local org 9.4 version used by doom emacs.
> Patch hang on Hunk #3, i attach the .rej file.

You might need to start from a different org commit. I believe
6a182b690f works. Otherwise, you could fix the merge conflicts (this is
described online, e.g., https://stackoverflow.com/a/16968982/5710525).

Finally, a very crude solution would be to manually apply the changes
yourself if you're not too conversant with git. The git solutions are
better, but since the changes in this patch are minimal it wouldn't take
too long.

I will at some point fix the conflicts myself and send a new patch, but
I'd prefer to wait until it gets the green light to merge so that I
don't have to do it multiple times.

Matt



Re: babel default header args as functions

2020-10-14 Thread Matt Huszagh
rey-coyrehourcq  writes:

> I'm interested by this functionality, do you know if it was merged or i need 
> to apply patch locally ?

Hi SR,

This hasn't been merged yet. I believe it's ready, but we're just
waiting on a maintainer to apply it upstream. If you're able to apply
the patch locally and provide feedback that always helps.

Matt



Re: [PATCH] Omit file description when :file-desc has nil value

2020-10-06 Thread Matt Huszagh
Kyle Meyer  writes:

> So, with the typo/spurious space change clean-ups, this looks good to
> me.  IIRC from a previous thread, you haven't yet completed the
> copyright paperwork.  Is that the case?

I've made those fixes and attached the updated patch.

I also sent you the paperwork separately (didn't post to the thread
since the PDF is too large).

Thanks
Matt

>From 7452f3e8315be63fa8ae160f6be00963bac898a7 Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Tue, 29 Sep 2020 14:11:59 -0700
Subject: [PATCH] lisp/ob-core.el: Allow passing empty vector to :file-desc to
 omit description

* doc/org-manual.org (Type): Document empty vector argument for
file-desc.
* etc/ORG-NEWS (New argument for ~file-desc~ babel header): Add entry
to NEWS.
* lisp/ob-core.el (org-babel--file-desc): Add new function to evaluate
file description value.
(org-babel-execute-src-block): Correctly evaluate file description
when executing src block.
(org-babel-insert-result): Correctly evaluate file description value
when inserting the result of src block execution into the buffer.
* testing/lisp/test-ob.el (test-ob/file-desc-header-argument): Add
test case for new behavior.
---
 doc/org-manual.org  |  8 +---
 etc/ORG-NEWS| 11 +++
 lisp/ob-core.el | 16 +++-
 testing/lisp/test-ob.el | 20 +++-
 4 files changed, 46 insertions(+), 9 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index e7d25b90e..ef2dad9ef 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -17482,10 +17482,12 @@ default behavior is to automatically determine the result type.
   #+end_example
 
   #+cindex: @samp{file-desc}, header argument
-  The =file-desc= header argument defines the description (see
-  [[*Link Format]]) for the link.  If =file-desc= is present but has no value,
+  The =file-desc= header argument defines the description (see [[*Link
+  Format]]) for the link.  If =file-desc= is present but has no value,
   the =file= value is used as the link description.  When this
-  argument is not present, the description is omitted.
+  argument is not present, the description is omitted.  If you want to
+  provide the =file-desc= argument but omit the description, you can
+  provide it with an empty vector (i.e., :file-desc []).
 
   #+cindex: @samp{sep}, header argument
   By default, Org assumes that a table written to a file has
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 5dc68cba4..7f935bf52 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -24,6 +24,17 @@ Earlier, IDs generated using =ts= method had a hard-coded format (i.e. =20200923
 The new option allows user to customise the format.
 Defaults are unchanged.
 
+*** New argument for ~file-desc~ babel header
+
+It is now possible to provide the =file-desc= header argument for a
+babel source block but omit the description by passing an empty vector
+as an argument (i.e., :file-desc []).  This can be useful because
+providing =file-desc= without an argument results in the result of
+=file= being used in the description.  Previously, the only way to
+omit a file description was to omit the header argument entirely,
+which made it difficult/impossible to provide a default value for
+=file-desc=.
+
 ** New features
 *** =ob-python= improvements to =:return= header argument 
 
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 7300f239e..075e3f928 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -646,6 +646,14 @@ a list with the following pattern:
   (replace-regexp-in-string
(org-src-coderef-regexp coderef) "" expand nil nil 1
 
+(defun org-babel--file-desc (params result)
+  "Retrieve file description."
+  (pcase (assq :file-desc params)
+(`nil nil)
+(`(:file-desc) result)
+(`(:file-desc . ,(and (pred stringp) val)) val)
+(`(:file-desc . []) nil)))
+
 ;;;###autoload
 (defun org-babel-execute-src-block ( arg info params)
   "Execute the current source code block.
@@ -749,8 +757,7 @@ block."
 		(let ((*this* (if (not file) result
 (org-babel-result-to-file
  file
- (let ((desc (assq :file-desc params)))
-   (and desc (or (cdr desc) result)))
+ (org-babel--file-desc params result)
 		  (setq result (org-babel-ref-resolve post))
 		  (when file
 			(setq result-params (remove "file" result-params))
@@ -2257,9 +2264,8 @@ INFO may provide the values of these header arguments (in the
 	 (setq result (org-no-properties result))
 	 (when (member "file" result-params)
 	   (setq result (org-babel-result-to-file
-			 result (when (assq :file-desc (nth 2 info))
-  (or (cdr (assq :file-desc (nth 2 info)))
-  result))
+			 result
+			 (org-babel--file-desc (nth 2 info) result)
 	((listp result))
 	(t (setq result (format "%S" result
   (if (and result-params (member "silent" result-params))
diff --git a/testing/lisp/test-ob

Re: [PATCH] Omit file description when :file-desc has nil value

2020-09-29 Thread Matt Huszagh
Kyle Meyer  writes:

> I'd be happy for you to take what I sent and work it into a proper
> patch.  Here are some other loose ends in addition to the manual update
> you mentioned:
>
>   * a NEWS entry for 9.5
>
>   * decide whether (:file-desc . []) should be handled explicitly rather
> than the current "any value that org-babel-read doesn't map to nil
> or a string"
>
>   * check that there's a test case for each :file-desc scenario

Let me know if I missed anything, or any other issues. I've decided to
handle the empty vector case explicitly. I think this behavior is
clearer.

Thanks,
Matt

>From 749fd5ade6b65f9d07e87b4af44ebb1afef2bee6 Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Tue, 29 Sep 2020 14:11:59 -0700
Subject: [PATCH] list/ob-core.el: Allow passing empty vector to :file-desc to
 omit description

* doc/org-manual.org (Type): Document empty vector argument for
file-desc.
* etc/ORG-NEWS (New argument for ~file-desc~ babel header): Add entry
to NEWS.
* lisp/ob-core.el (org-babel--file-desc): Add new function to evaluate
file description value.
(org-babel-execute-src-block): Correctly evaluate file description
when executing src block.
(org-babel-insert-result): Correctly evaluate file description value
when inserting the result of src block execution into the buffer.
* testing/lisp/test-ob.el (test-ob/file-desc-header-argument): Add
test case for new behavior.
---
 doc/org-manual.org  |  8 +---
 etc/ORG-NEWS| 13 -
 lisp/ob-core.el | 16 +++-
 testing/lisp/test-ob.el | 20 +++-
 4 files changed, 47 insertions(+), 10 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index e7d25b90e..a790f3225 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -17482,10 +17482,12 @@ default behavior is to automatically determine the result type.
   #+end_example
 
   #+cindex: @samp{file-desc}, header argument
-  The =file-desc= header argument defines the description (see
-  [[*Link Format]]) for the link.  If =file-desc= is present but has no value,
+  The =file-desc= header argument defines the description (see [[*Link
+  Format]]) for the link.  If =file-desc= is present but has no value,
   the =file= value is used as the link description.  When this
-  argument is not present, the description is omitted.
+  argument is not present, the description is omitted.  If you want to
+  provide the =file-disc= argument but omit the description, you can
+  provide it with an empty vector (i.e., :file-desc []).
 
   #+cindex: @samp{sep}, header argument
   By default, Org assumes that a table written to a file has
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 5dc68cba4..19f6af288 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -24,8 +24,19 @@ Earlier, IDs generated using =ts= method had a hard-coded format (i.e. =20200923
 The new option allows user to customise the format.
 Defaults are unchanged.
 
+*** New argument for ~file-desc~ babel header
+
+It is now possible to provide the =file-desc= header argument for a
+babel source block but omit the description by passing an empty vector
+as an argument (i.e., :file-desc []).  This can be useful because
+providing =file-desc= without an argument results in the result of
+=file= being used in the description.  Previously, the only way to
+omit a file description was to omit the header argument entirely,
+which made it difficult/impossible to provide a default value for
+=file-desc=.
+
 ** New features
-*** =ob-python= improvements to =:return= header argument 
+*** =ob-python= improvements to =:return= header argument
 
 The =:return= header argument in =ob-python= now works for session
 blocks as well as non-session blocks.  Also, it now works with the
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 7300f239e..075e3f928 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -646,6 +646,14 @@ a list with the following pattern:
   (replace-regexp-in-string
(org-src-coderef-regexp coderef) "" expand nil nil 1
 
+(defun org-babel--file-desc (params result)
+  "Retrieve file description."
+  (pcase (assq :file-desc params)
+(`nil nil)
+(`(:file-desc) result)
+(`(:file-desc . ,(and (pred stringp) val)) val)
+(`(:file-desc . []) nil)))
+
 ;;;###autoload
 (defun org-babel-execute-src-block ( arg info params)
   "Execute the current source code block.
@@ -749,8 +757,7 @@ block."
 		(let ((*this* (if (not file) result
 (org-babel-result-to-file
  file
- (let ((desc (assq :file-desc params)))
-   (and desc (or (cdr desc) result)))
+ (org-babel--file-desc params result)
 		  (setq result (org-babel-ref-resolve post))
 		  (when file
 			(setq result-params (remove "file" result-params))
@@ -2257,9 +2264,8 @@ INFO may provide the values of these header arguments (in the
 	 (setq result (org-no-p

Re: [PATCH] Omit file description when :file-desc has nil value

2020-09-24 Thread Matt Huszagh
Kyle Meyer  writes:

> But it's not a direct comparison against that use case and the use case
> you want to support.  The potential breakage of existing documents is a
> big factor to go against.

Yep, I agree. I think my phrasing could have just been better. I meant
to include the breakage as a factor against.

> Unfortunately, such a kludge is how I'd suggest to move forward.
> Perhaps an empty string, or perhaps any value (e.g., ":file-desc []")
> that org-babel-read won't treat as a string or nil (the two cases that
> mean something right now).  The rough patch below is an example of the
> latter.

I like this solution better than mine. I guess it's still a bit of a
hack, but it doesn't seem to be one that could break a use case, whereas
the empty string could conceivably be intended, though I'm still not
sure why.

> I'm not sure I get this.  What's next down the road in this scenario?
> With something like the above kludge, haven't we exhausted the cases for
> :file-desc?

Yes I think you're right. I was referring to my solution of an empty
string, which I didn't see a personal use for, but felt it might be a
valid use case for someone else. I really can't think of any reason the
empty vector would otherwise be valid.

> diff --git a/lisp/ob-core.el b/lisp/ob-core.el
> index 7300f239e..4483585a1 100644
> --- a/lisp/ob-core.el
> +++ b/lisp/ob-core.el
> @@ -646,6 +646,13 @@ (defun org-babel--expand-body (info)
>(replace-regexp-in-string
> (org-src-coderef-regexp coderef) "" expand nil nil 1
>  
> +(defun org-babel--file-desc (params result)
> +  (pcase (assq :file-desc params)
> +(`nil nil)
> +(`(:file-desc) result)
> +(`(:file-desc . ,(and (pred stringp) val)) val)
> +(_ nil)))
> +
>  ;;;###autoload
>  (defun org-babel-execute-src-block ( arg info params)
>"Execute the current source code block.
> @@ -749,8 +756,7 @@ (defun org-babel-execute-src-block ( arg info 
> params)
>   (let ((*this* (if (not file) result
>   (org-babel-result-to-file
>file
> -  (let ((desc (assq :file-desc params)))
> -(and desc (or (cdr desc) result)))
> +  (org-babel--file-desc params result)
> (setq result (org-babel-ref-resolve post))
> (when file
>   (setq result-params (remove "file" result-params))
> @@ -2257,9 +2263,8 @@ (defun org-babel-insert-result (result  
> result-params info hash lang)
>(setq result (org-no-properties result))
>(when (member "file" result-params)
>  (setq result (org-babel-result-to-file
> -  result (when (assq :file-desc (nth 2 info))
> -   (or (cdr (assq :file-desc (nth 2 info)))
> -   result))
> +  result
> +  (org-babel--file-desc (nth 2 info) result)
>   ((listp result))
>   (t (setq result (format "%S" result
>(if (and result-params (member "silent" result-params))
> diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
> index 648e9c115..e7a292de3 100644
> --- a/testing/lisp/test-ob.el
> +++ b/testing/lisp/test-ob.el
> @@ -1084,7 +1084,16 @@ (ert-deftest test-ob/file-desc-header-argument ()
>  (org-babel-execute-src-block)
>  (goto-char (point-min))
>  (should (search-forward "[[file:foo][bar]]" nil t))
> -(should (search-forward "[[file:foo][foo]]" nil t
> +(should (search-forward "[[file:foo][foo]]" nil t)))
> +  (should (string-match-p
> +(regexp-quote "[[file:foo]]")
> +(org-test-with-temp-text "
> +#+begin_src emacs-lisp :results file :file-desc []
> +  \"foo\"
> +#+end_src"
> +  (org-babel-next-src-block)
> +  (org-babel-execute-src-block)
> +  (buffer-substring-no-properties (point-min) (point-max))
>  
>  (ert-deftest test-ob/result-file-link-type-header-argument ()
>"Ensure that the result is a link to a file.

This patch looks good. I've tested it and it works well for me. Thanks
for coming up with a good solution! I think the one thing still missing
is some documentation in the info manual. Something along the lines of

 The ‘file-desc’ header argument defines the description (see *note
 Link Format::) for the link.  If ‘file-desc’ has no value, Org uses
 the generated file name for both the “link” and “description” parts
 of the link. If you want to omit the description (i.e., [[link]]),
 you can either omit the ‘file-desc’ header argument or provide it
 with an empty vector (i.e., :file-desc []).

Feel free to add this (or something else) to your patch. Or, if you'd
prefer that I created a patch for it, let me know.

Matt



Re: official orgmode parser

2020-09-16 Thread Matt Huszagh
"Gerry Agbobada"  writes:

> I'm currently toying with the idea of trying a tree-sitter parser for Org. 
> The very static nature of a shared object parser (knowing TODO keywords are 
> pretty dynamic for example) is a challenge I'm not sure to overcome ; to be 
> honest even without that I can't say I'll manage to do it.

A tree-sitter parser for org would be great! Please keep this list
posted on any developments you make on this front. I made some minimal
attempts at this a while back, but didn't get very far.

Matt



Re: basic org questions

2020-09-15 Thread Matt Huszagh
Emanuel Berg  writes:

> Yeah, but in LaTeX being left aligned is not some
> property of the table, everything is left-aligned,
> and if you want it otherwise, you put between
> \begin{center} and \end{center} ...

That page also describes how to set float parameters, which the table
can be within.

What is the latex your export is generating? What does your input org
file look like, and what sort of configuration parameters have you set?

Matt



Re: basic org questions

2020-09-15 Thread Matt Huszagh
Emanuel Berg via "General discussions about Org-mode."
 writes:

> Yes, after export to PDF, they are centered. they =
> the whole table items.

I think this link
(https://orgmode.org/manual/Tables-in-LaTeX-export.html) is the relevant
part of the documentation. I haven't used this though, so I can't help
you further.

In general, it would be much easier to help you if you specify these
things in the original question. The fact that you're asking about LaTeX
export processed to PDF is critical information. Also, please consult
the documentation before asking questions. There are gaps, but generally
it's well-written and a lot of people have put a lot of time into it.

Matt



Re: basic org questions

2020-09-15 Thread Matt Huszagh
Emanuel Berg via "General discussions about Org-mode."
 writes:

> Heh...? What's strange with that?
>
> Anyway that's what I mean.

I guess I'm just not sure what you mean. I was under the impression that
tables were always left-aligned and table alignment in the manual refers
to column alignment
(https://orgmode.org/manual/Column-Width-and-Alignment.html). But it's
certainly possible there's an org-mode feature I'm not aware of. Were
you referring to the tables being centered after export (I see from
another part of this chain that that's what you were referring to in the
3rd question) when you wanted them left-aligned? If that is the case, it
would really help if you specify that in your original
question. Appearance in an org-mode buffer and appearance in the PDF,
HTML, etc. after export are very different things with very different
configuration.

Matt



Re: basic org questions

2020-09-15 Thread Matt Huszagh
Emanuel Berg via "General discussions about Org-mode."
 writes:

> 2) How do I have tables not appear centered
> by default, but left-aligned?

Do you mean the alignment of columns, or the table itself? You can
control column alignment with ``, ``, ``, e.g.,

|  title   | description  |other desc |
|--+--+---|
|   |   ||
|some title| some description.|right desc |
| some other title | some longer description. | longer right desc |

If you mean the table itself, that would be very strange.

I'm not too sure about your other questions.

Matt



Re: [PATCH] Omit file description when :file-desc has nil value

2020-09-15 Thread Matt Huszagh
> Kyle Meyer  writes:
>
>> I also don't find the current behavior particularly intuitive.  (I'm
>> also not really a babel user, so my opinion probably shouldn't count for
>> much.)  If we were adding it today, I think what you describe would be
>> better, but, as you mention, breakage also now also weighs against
>> making a change here.
>>
>> In any case, I'd suggest raising the discussion on the list after the
>> 9.4 release.

Hello, just following up on this since 9.4 has been released. Thoughts?

Matt



Re: [PATCH] Omit file description when :file-desc has nil value

2020-09-09 Thread Matt Huszagh
Kyle Meyer  writes:

> I also don't find the current behavior particularly intuitive.  (I'm
> also not really a babel user, so my opinion probably shouldn't count for
> much.)  If we were adding it today, I think what you describe would be
> better, but, as you mention, breakage also now also weighs against
> making a change here.
>
> In any case, I'd suggest raising the discussion on the list after the
> 9.4 release.

Ok, I'll follow up on this then.

>>> Right, to reflect the current behavior established as a result of the
>>> above thread, I think that should be reworded to distinguish between an
>>> absent :file-desc header and one with no argument.  Sorry for not
>>> catching that when reviewing your initial patch.
>>
>> No worries, and I agree the documentation should be updated. I'm happy
>> to provide the patch myself, but I'd like to talk through whether the
>> current implementation is the correct one before I do.
>
> Thanks.  To avoid any confusion coming from this description making it
> into the 9.4 release, I've updated it in 4b2123fb7.

Thanks for fixing that Kyle.

Matt



Re: [PATCH] babel latex headers and image generation commands

2020-09-09 Thread Matt Huszagh
Bastien  writes:

> Prefer
>
>   * lisp/ob-latex.el (org-babel-latex-preamble): New option for LaTeX
>   preamble customization.
>
> "New option" is quite standard, an "option" being a customizable
> variable.  In this case, "New option" would probably be enough, given
> the name of the option is quite self-explanatory.  Also, some find it
> pedantic, but "LaTeX" is the correct spelling in a changelog I guess.

Fixed in new patch (attached).

> The first line of the docstring should contain a sentence, so you'd
> need to have a new paragraph after "runtime to the LaTeX preamble."

Also fixed. Making the first line a full sentence means that some lines
are a little longer than 80 characters. Is this acceptable?

> What for users who don't have inkscape?  

This is just a default, but I could use a dvisvgm command as the default
instead? Either way, converting a PDF to SVG will require an executable
outside Emacs, but I guess dvisvgm is more likely to be installed for
people using a texlive installation. My personal preference for inkscape
is because it should handle all PDF inputs, whereas there are some cases
where dvisvgm may fail (see
https://github.com/mgieseki/dvisvgm/issues/139) due to changes in
ghostscript. Still, dvisvgm generally does a very good job with PDF
inputs. Let me know your thoughts, I'd be happy to set the default to a
dvisvgm command instead.

Matt

>From 1ff86f2267b653dff225837ccf13ebf417f7ed03 Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Fri, 28 Aug 2020 22:26:05 -0700
Subject: [PATCH] ob-latex.el: Make latex to svg compilation very customizable

* lisp/ob-latex.el (org-babel-latex-preamble): New option for LaTeX
preamble customization.
(org-babel-latex-begin-env): New option for LaTeX document environment
begin customization.
(org-babel-latex-end-env): New option for LaTeX document environment
end customization.
(org-babel-latex-pdf-svg-process): New option for converting a pdf to
svg.
(org-babel-execute:latex): Add specific case for svg generation from
LaTeX block.
---
 lisp/ob-latex.el | 59 +---
 1 file changed, 56 insertions(+), 3 deletions(-)

diff --git a/lisp/ob-latex.el b/lisp/ob-latex.el
index 4b343dd14..6a4f7a6ba 100644
--- a/lisp/ob-latex.el
+++ b/lisp/ob-latex.el
@@ -66,7 +66,46 @@
   "LaTeX-specific header arguments.")
 
 (defcustom org-babel-latex-htlatex "htlatex"
-  "The htlatex command to enable conversion of latex to SVG or HTML."
+  "The htlatex command to enable conversion of LaTeX to SVG or HTML."
+  :group 'org-babel
+  :type 'string)
+
+(defcustom org-babel-latex-preamble
+  (lambda (_)
+"\\documentclass[preview]{standalone}
+\\def\\pgfsysdriver{pgfsys-tex4ht.def}
+")
+  "Closure which evaluates at runtime to the LaTeX preamble.
+
+It takes 1 argument which is the parameters of the source block."
+  :group 'org-babel
+  :type 'function)
+
+(defcustom org-babel-latex-begin-env
+  (lambda (_)
+"\\begin{document}")
+  "Closure which evaluates at runtime to the begin part of the document environment.
+
+It takes 1 argument which is the parameters of the source block.
+This allows adding additional code that will be ignored when
+exporting the literal LaTeX source."
+  :group 'org-babel
+  :type 'function)
+
+(defcustom org-babel-latex-end-env
+  (lambda (_)
+"\\end{document}")
+  "Closure which evaluates at runtime to the end part of the document environment.
+
+It takes 1 argument which is the parameters of the source block.
+This allows adding additional code that will be ignored when
+exporting the literal LaTeX source."
+  :group 'org-babel
+  :type 'function)
+
+(defcustom org-babel-latex-pdf-svg-process
+  "inkscape --pdf-poppler %f -T -l -o %O"
+  "Command used to convert a PDF file to an SVG file when executing a latex source block."
   :group 'org-babel
   :type 'string)
 
@@ -114,12 +153,26 @@ This function is called by `org-babel-execute-src-block'."
 			 (mapconcat #'identity headers "\n"
 	   (org-create-formula-image
 body out-file org-format-latex-options in-buffer)))
+	 ((string= "svg" extension)
+	  (with-temp-file tex-file
+		 (insert (concat (funcall org-babel-latex-preamble params)
+			 (mapconcat #'identity headers "\n")
+			 (funcall org-babel-latex-begin-env params)
+			 body
+			 (funcall org-babel-latex-end-env params
+	  (let ((tmp-pdf (org-babel-latex-tex-to-pdf tex-file)))
+  (let* ((log-buf (get-buffer-create "*Org Babel LaTeX Output*"))
+ (err-msg "org babel latex failed")
+ (img-out (org-compile-file
+	   tmp-pdf
+   (list org-babel-latex-pdf-svg-process)
+  

Re: babel default header args as functions

2020-09-09 Thread Matt Huszagh
Bastien  writes:

> Also, if we integrate the change, `eval-default-headers' would be
> better named `org-babel-eval-default-headers'.

I've changed the function name to `org-babel-eval-headers'. The reason
for dropping "default" is that this function is now used as part of
`org-babel-merge-params' on all headers, not just defaults in order to
avoid evaluating closures until we know they are needed. See
https://lists.gnu.org/archive/html/emacs-orgmode/2020-09/msg00464.html
for the new patch and more info.

Matt



Re: babel default header args as functions

2020-09-09 Thread Matt Huszagh
Tom Gillespie  writes:

> [...] I have a number of use
> cases that I can imagine would benefit greatly from being able to
> define a :header-args: :header (lambda () "yay!") property as a
> closure (and actually I assumed that it would just work that way if I
> tried to do it, clearly not though). I can't tell for sure if the
> patch enables this behavior though or whether I would still get a
> Wrong type argument error.

This should work. Do you have reason for believing it might not?

For example, set:

(setq org-babel-default-header-args:bash
  `((:var . (lambda ()
  "a='yay'"

Then in file.org:
#+begin_src bash :results output
echo $a
#+end_src

Executing this will yield:

#+RESULTS:
: yay

> [...] Looking
> at the patch it seems that it preserves the behavior of performing the
> evaluation of the closures at the source block, but I'm not 100% sure.

I'm not sure I completely understand what you mean here. However, the
closures are evaluated when point is at the source block, during the
source block evaluation, not when the default headers are declared. This
allows the closures to use context-dependent functionality (e.g. you can
call `org-element-at-point' inside the closure and retrieve whatever
information you want). Does this address your concern? Please clarify if
I've missed your point.

> If the default header closures are being evaluated before checking
> whether they have been superseded by the headers on a block then that
> is incorrect and they should not be evaluated until it is clear that
> they are the value of the header for that block and have not been
> superseded.

I've fixed my patch (attached) so that now closures are only evaluated
when they are used as part of the final set of headers.

>From 4a461a90ec4f3c5f9634b687a6685ea3ba74f168 Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Fri, 28 Aug 2020 11:05:59 -0700
Subject: [PATCH] ob-core.el: Add ability to use closures as default header
 arguments

* lisp/ob-core.el (org-babel-default-header-args): Document ability to
use closures.
(org-babel-eval-headers): New function to generate header arguments,
which adds the ability to evaluate closures during source block
execution or export.
(org-babel-merge-params): Only evaluate closures when we have our
final list of headers.
---
 lisp/ob-core.el | 60 -
 1 file changed, 55 insertions(+), 5 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 578622232..bef34d7c0 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -473,7 +473,35 @@ For the format of SAFE-LIST, see `org-babel-safe-header-args'."
 (defvar org-babel-default-header-args
   '((:session . "none") (:results . "replace") (:exports . "code")
 (:cache . "no") (:noweb . "no") (:hlines . "no") (:tangle . "no"))
-  "Default arguments to use when evaluating a source block.")
+  "Default arguments to use when evaluating a source block.
+
+This is a list in which each element is an alist.  Each key
+corresponds to a header argument, and each value to that header's
+value.  The value can either be a string or a closure that
+evaluates to a string.  The closure is evaluated when the source
+block is being evaluated (e.g. during execution or export), with
+point at the source block.  It is not possible to use an
+arbitrary function symbol (e.g. 'some-func), since org uses
+lexical binding.  To achieve the same functionality, call the
+function within a closure (e.g. (lambda () (some-func))).
+
+To understand how closures can be used as default header
+arguments, imagine you'd like to set the file name output of a
+latex source block to a sha1 of its contents.  We could achieve
+this with:
+
+(defun org-src-sha ()
+  (let ((elem (org-element-at-point)))
+(concat (sha1 (org-element-property :value elem)) \".svg\")))
+
+(setq org-babel-default-header-args:latex
+  `((:results . \"file link replace\")
+(:file . (lambda () (org-src-sha)
+
+Because the closure is evaluated with point at the source block,
+the call to `org-element-at-point' above will always retrieve
+information about the current source block.")
+
 (put 'org-babel-default-header-args 'safe-local-variable
  (org-babel-header-args-safe-fn org-babel-safe-header-args))
 
@@ -584,6 +612,19 @@ the outer-most code block.")
 
 (defvar *this*)
 
+(defun org-babel-eval-headers (headers)
+  "Compute header list set with HEADERS.
+
+Evaluate all header arguments set to functions prior to returning
+the list of header arguments."
+  (let ((lst nil))
+(dolist (elem headers)
+  (if (and (cdr elem)
+	   (functionp (cdr elem)))
+  (push `(,(car elem) . ,(funcall (cdr elem))) lst)
+(push elem lst)))
+lst))
+
 (defun org-babel-get-src-block-info ( l

Re: [PATCH] Omit file description when :file-desc has nil value

2020-09-03 Thread Matt Huszagh
Matt Huszagh  writes:

> This patch omits a file description when :file-desc has a nil
> value.

I've modified the patch to yield the same effect when executing a source
block.

Matt

>From 24d156e421973b5a97f1c797d48f1daa95348898 Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Wed, 2 Sep 2020 23:06:10 -0700
Subject: [PATCH] lisp/ob-core.el: Omit file description when :file-desc has
 nil value

* lisp/ob-core.el (org-babel-insert-result): Omit file description
when :file-desc value evaluates to nil.
(org-babel-execute-src-block): Perform the same functionality when
executing a src block.

The previous implementation makes it impossible to provide a default
:file-desc and in some cases override it to omit the description.
---
 lisp/ob-core.el | 9 +
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 578622232..02c0a153c 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -750,7 +750,8 @@ block."
 (org-babel-result-to-file
  file
  (let ((desc (assq :file-desc params)))
-   (and desc (or (cdr desc) result)))
+   (and (and desc (cdr desc))
+	(cdr desc)))
 		  (setq result (org-babel-ref-resolve post))
 		  (when file
 			(setq result-params (remove "file" result-params))
@@ -2257,9 +2258,9 @@ INFO may provide the values of these header arguments (in the
 	 (setq result (org-no-properties result))
 	 (when (member "file" result-params)
 	   (setq result (org-babel-result-to-file
-			 result (when (assq :file-desc (nth 2 info))
-  (or (cdr (assq :file-desc (nth 2 info)))
-  result))
+			 result (when (and (assq :file-desc (nth 2 info))
+	   (cdr (assq :file-desc (nth 2 info
+  (cdr (assq :file-desc (nth 2 info
 	((listp result))
 	(t (setq result (format "%S" result
   (if (and result-params (member "silent" result-params))
-- 
2.28.0



[PATCH] Omit file description when :file-desc has nil value

2020-09-03 Thread Matt Huszagh
Hello,

This patch omits a file description when :file-desc has a nil
value. Previously, the following src block

#+BEGIN_SRC asymptote :results value file :file circle.pdf :file-desc 
:output-dir img/
  size(2cm);
  draw(unitcircle);
#+END_SRC

would yield

#+RESULTS:
[[file:img/circle.pdf][circle.pdf]]

This makes it impossible (I think) to provide :file-desc with a default
value and prevent the description in some cases. This patch would cause
the same code block to execute to

#+RESULTS:
[[file:img/circle.pdf]]

I feel I may be missing something in regard to why this previously had
the functionality it did. Is there a use case I've missed? To me, the
documentation seems to indicate that my patch is the desired behavior:

   The =file-desc= header argument defines the description (see
   [[*Link Format]]) for the link.  If =file-desc= has no value, the
   "description" part of the link will be omitted.

Full disclaimer: I wrote this section of the documentation as part of
this patch:

https://lists.gnu.org/archive/html/emacs-orgmode/2020-07/msg00320.html

Thanks
Matt

>From edcfa85add6ac71a1e13b7731779ccf4a8e12868 Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Wed, 2 Sep 2020 23:06:10 -0700
Subject: [PATCH] lisp/ob-core.el: Omit file description when :file-desc has
 nil value

* lisp/ob-core.el (org-babel-insert-result): Omit file description
when :file-desc value evaluates to nil.

The previous implementation makes it impossible to provide a default
:file-desc and in some cases override it to omit the description.
---
 lisp/ob-core.el | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 578622232..55165ebc5 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2257,9 +2257,9 @@ INFO may provide the values of these header arguments (in the
 	 (setq result (org-no-properties result))
 	 (when (member "file" result-params)
 	   (setq result (org-babel-result-to-file
-			 result (when (assq :file-desc (nth 2 info))
-  (or (cdr (assq :file-desc (nth 2 info)))
-  result))
+			 result (when (and (assq :file-desc (nth 2 info))
+	   (cdr (assq :file-desc (nth 2 info
+  (cdr (assq :file-desc (nth 2 info
 	((listp result))
 	(t (setq result (format "%S" result
   (if (and result-params (member "silent" result-params))
-- 
2.28.0



Re: [PATCH] babel latex headers and image generation commands

2020-09-02 Thread Matt Huszagh
Matt Huszagh  writes:

> I've added a few changes to the patch that additionally allow custom the
> begin and end document environments. The purpose here is to allow latex
> code within the document environment that is ignored by the body
> export. For instance, I can set the page color with
> {\color{some-color}...} and this doesn't mess up latex exports.

I've fixed a minor bug with the previous patch.

>From 1bcd1d28dde6625d0c648c92243260b46433e1eb Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Fri, 28 Aug 2020 22:26:05 -0700
Subject: [PATCH] ob-latex.el: Make latex to svg compilation very customizable

* lisp/ob-latex.el (org-babel-latex-preamble): Add latex preamble
customization.
(org-babel-latex-begin-env): Add latex document environment begin
customization.
(org-babel-latex-end-env): Add latex document environment end
customization.
(org-babel-latex-pdf-svg-process): Add customization for converting a
pdf to svg.
(org-babel-execute:latex): Add specific case for svg generation from
latex block.
---
 lisp/ob-latex.el | 57 ++--
 1 file changed, 55 insertions(+), 2 deletions(-)

diff --git a/lisp/ob-latex.el b/lisp/ob-latex.el
index 4b343dd14..991873e2e 100644
--- a/lisp/ob-latex.el
+++ b/lisp/ob-latex.el
@@ -70,6 +70,45 @@
   :group 'org-babel
   :type 'string)
 
+(defcustom org-babel-latex-preamble
+  (lambda (_)
+"\\documentclass[preview]{standalone}
+\\def\\pgfsysdriver{pgfsys-tex4ht.def}
+")
+  "Closure which evaluates at runtime to the latex preamble.  It
+takes 1 argument which is the parameters of the source block."
+  :group 'org-babel
+  :type 'function)
+
+(defcustom org-babel-latex-begin-env
+  (lambda (_)
+"\\begin{document}")
+  "Closure which evaluates at runtime to the begin part of the
+document environment.  It takes 1 argument which is the
+parameters of the source block.  This allows adding additional
+code that will be ignored when exporting the literal latex
+source."
+  :group 'org-babel
+  :type 'function)
+
+(defcustom org-babel-latex-end-env
+  (lambda (_)
+"\\end{document}")
+  "Closure which evaluates at runtime to the end part of the
+document environment.  It takes 1 argument which is the
+parameters of the source block.  This allows adding additional
+code that will be ignored when exporting the literal latex
+source."
+  :group 'org-babel
+  :type 'function)
+
+(defcustom org-babel-latex-pdf-svg-process
+  "inkscape --pdf-poppler %f -T -l -o %O"
+  "Command used to convert a PDF file to an SVG file when
+executing a latex source block."
+  :group 'org-babel
+  :type 'string)
+
 (defcustom org-babel-latex-htlatex-packages
   '("[usenames]{color}" "{tikz}" "{color}" "{listings}" "{amsmath}")
   "Packages to use for htlatex export."
@@ -114,12 +153,26 @@ This function is called by `org-babel-execute-src-block'."
 			 (mapconcat #'identity headers "\n"
 	   (org-create-formula-image
 body out-file org-format-latex-options in-buffer)))
+	 ((string= "svg" extension)
+	  (with-temp-file tex-file
+		 (insert (concat (funcall org-babel-latex-preamble params)
+			 (mapconcat #'identity headers "\n")
+			 (funcall org-babel-latex-begin-env params)
+			 body
+			 (funcall org-babel-latex-end-env params
+	  (let ((tmp-pdf (org-babel-latex-tex-to-pdf tex-file)))
+  (let* ((log-buf (get-buffer-create "*Org Babel LaTeX Output*"))
+ (err-msg "org babel latex failed")
+ (img-out (org-compile-file
+	   tmp-pdf
+   (list org-babel-latex-pdf-svg-process)
+   extension err-msg log-buf)))
+(shell-command (format "mv %s %s" img-out out-file)
  ((string-suffix-p ".tikz" out-file)
 	  (when (file-exists-p out-file) (delete-file out-file))
 	  (with-temp-file out-file
 	(insert body)))
-	 ((and (or (string= "svg" extension)
-		   (string= "html" extension))
+	 ((and (string= "html" extension)
 	   (executable-find org-babel-latex-htlatex))
 	  ;; TODO: this is a very different way of generating the
 	  ;; frame latex document than in the pdf case.  Ideally, both
-- 
2.28.0



Re: [PATCH] babel latex headers and image generation commands

2020-09-02 Thread Matt Huszagh
Matt Huszagh  writes:

> Ok, I've finally gotten around to taking a crack at this. The patch is
> attached. Basically, it allows a lot more control when converting a
> latex source block into an svg image file.

I've added a few changes to the patch that additionally allow custom the
begin and end document environments. The purpose here is to allow latex
code within the document environment that is ignored by the body
export. For instance, I can set the page color with
{\color{some-color}...} and this doesn't mess up latex exports.

Please comment with any questions/concerns/thoughts.

Thanks!
>From 6dbd5ae840f02333f83d40a9c27be06968279563 Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Fri, 28 Aug 2020 22:26:05 -0700
Subject: [PATCH] ob-latex.el: Make latex to svg compilation very customizable

* lisp/ob-latex.el (org-babel-latex-preamble): Add latex preamble
customization.
(org-babel-latex-begin-env): Add latex document environment begin
customization.
(org-babel-latex-end-env): Add latex document environment end
customization.
(org-babel-latex-pdf-svg-process): Add customization for converting a
pdf to svg.
(org-babel-execute:latex): Add specific case for svg generation from
latex block.
---
 lisp/ob-latex.el | 57 ++--
 1 file changed, 55 insertions(+), 2 deletions(-)

diff --git a/lisp/ob-latex.el b/lisp/ob-latex.el
index 4b343dd14..359179476 100644
--- a/lisp/ob-latex.el
+++ b/lisp/ob-latex.el
@@ -70,6 +70,45 @@
   :group 'org-babel
   :type 'string)
 
+(defcustom org-babel-latex-preamble
+  (lambda (_)
+"\\documentclass[preview]{standalone}
+\\def\\pgfsysdriver{pgfsys-tex4ht.def}
+")
+  "Closure which evaluates at runtime to the latex preamble.  It
+takes 1 argument which is the parameters of the source block."
+  :group 'org-babel
+  :type 'function)
+
+(defcustom org-babel-latex-begin-env
+  (lambda (_)
+"\\begin{document}")
+  "Closure which evaluates at runtime to the begin part of the
+document environment.  It takes 1 argument which is the
+parameters of the source block.  This allows adding additional
+code that will be ignored when exporting the literal latex
+source."
+  :group 'org-babel
+  :type 'function)
+
+(defcustom org-babel-latex-end-env
+  (lambda (_)
+"\\end{document}")
+  "Closure which evaluates at runtime to the end part of the
+document environment.  It takes 1 argument which is the
+parameters of the source block.  This allows adding additional
+code that will be ignored when exporting the literal latex
+source."
+  :group 'org-babel
+  :type 'function)
+
+(defcustom org-babel-latex-pdf-svg-process
+  "inkscape --pdf-poppler %f -T -l -o %O"
+  "Command used to convert a PDF file to an SVG file when
+executing a latex source block."
+  :group 'org-babel
+  :type 'string)
+
 (defcustom org-babel-latex-htlatex-packages
   '("[usenames]{color}" "{tikz}" "{color}" "{listings}" "{amsmath}")
   "Packages to use for htlatex export."
@@ -114,12 +153,26 @@ This function is called by `org-babel-execute-src-block'."
 			 (mapconcat #'identity headers "\n"
 	   (org-create-formula-image
 body out-file org-format-latex-options in-buffer)))
+	 ((string= "svg" extension)
+	  (with-temp-file tex-file
+		 (insert (concat (funcall org-babel-latex-preamble params)
+			 (mapconcat #'identity headers "\n")
+			 (funcall org-babel-latex-begin-env)
+			 body
+			 (funcall org-babel-latex-end-env
+	  (let ((tmp-pdf (org-babel-latex-tex-to-pdf tex-file)))
+  (let* ((log-buf (get-buffer-create "*Org Babel LaTeX Output*"))
+ (err-msg "org babel latex failed")
+ (img-out (org-compile-file
+	   tmp-pdf
+   (list org-babel-latex-pdf-svg-process)
+   extension err-msg log-buf)))
+(shell-command (format "mv %s %s" img-out out-file)
  ((string-suffix-p ".tikz" out-file)
 	  (when (file-exists-p out-file) (delete-file out-file))
 	  (with-temp-file out-file
 	(insert body)))
-	 ((and (or (string= "svg" extension)
-		   (string= "html" extension))
+	 ((and (string= "html" extension)
 	   (executable-find org-babel-latex-htlatex))
 	  ;; TODO: this is a very different way of generating the
 	  ;; frame latex document than in the pdf case.  Ideally, both
-- 
2.28.0



Re: babel default header args as functions

2020-09-02 Thread Matt Huszagh
Matt Huszagh  writes:

> I've generated a patch for this. Please let me know your thoughts. I
> believe this adds valuable flexibility to default header
> arguments.

I've added an additional fix that makes this work during export too.

>From aec4e905d5d72f9a124adfde877835a783bd637b Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Fri, 28 Aug 2020 11:05:59 -0700
Subject: [PATCH] ob-core.el: Add ability to use closures as default header
 arguments

* lisp/ob-core.el (org-babel-default-header-args): Document ability to
use functions.
(eval-default-headers): New function to generate default header
arguments, which adds the ability to evaluate function arguments at
runtime.
(org-babel-get-src-block-info): Use new header argument evaluate
function when retreiving src block info.

* lisp/ob-exp.el (org-babel-exp-src-block): Must use new
eval-default-headers when exporting as well.

The closures are evaluated at runtime.
---
 lisp/ob-core.el | 32 ++--
 lisp/ob-exp.el  |  2 +-
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 578622232..4a22f17e7 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -473,7 +473,23 @@ For the format of SAFE-LIST, see `org-babel-safe-header-args'."
 (defvar org-babel-default-header-args
   '((:session . "none") (:results . "replace") (:exports . "code")
 (:cache . "no") (:noweb . "no") (:hlines . "no") (:tangle . "no"))
-  "Default arguments to use when evaluating a source block.")
+  "Default arguments to use when evaluating a source block.
+
+This is a list in which each element is an alist.  Each key
+corresponds to a header argument, and each value to that header's
+value.  The value can either be a string or a closure that
+evaluates to a string at runtime.  For instance, imagine you'd
+like to set the file name output of a latex source block to a
+sha1 of its contents.  We could achieve this with:
+
+(defun org-src-sha ()
+  (let ((elem (org-element-at-point)))
+(concat (sha1 (org-element-property :value elem)) \".svg\")))
+
+(setq org-babel-default-header-args:latex
+  `((:results . \"file link replace\")
+(:file . (lambda () (org-src-sha)")
+
 (put 'org-babel-default-header-args 'safe-local-variable
  (org-babel-header-args-safe-fn org-babel-safe-header-args))
 
@@ -584,6 +600,18 @@ the outer-most code block.")
 
 (defvar *this*)
 
+(defun eval-default-headers (headers)
+  "Compute default header list set with HEADERS.
+
+  Evaluate all default header arguments set to functions prior to
+  returning the list of header arguments."
+  (let ((lst nil))
+(dolist (elem (eval headers t))
+  (if (listp (cdr elem))
+  (push `(,(car elem) . ,(funcall (cdr elem))) lst)
+(push elem lst)))
+lst))
+
 (defun org-babel-get-src-block-info ( light datum)
   "Extract information from a source block or inline source block.
 
@@ -615,7 +643,7 @@ a list with the following pattern:
 	   (apply #'org-babel-merge-params
 		  (if inline org-babel-default-inline-header-args
 			org-babel-default-header-args)
-		  (and (boundp lang-headers) (eval lang-headers t))
+		  (and (boundp lang-headers) (eval-default-headers lang-headers))
 		  (append
 		   ;; If DATUM is provided, make sure we get node
 		   ;; properties applicable to its location within
diff --git a/lisp/ob-exp.el b/lisp/ob-exp.el
index 34caf9546..13277f64f 100644
--- a/lisp/ob-exp.el
+++ b/lisp/ob-exp.el
@@ -103,7 +103,7 @@ Assume point is at block opening line."
 		   (apply #'org-babel-merge-params
 			  org-babel-default-header-args
 			  (and (boundp lang-headers)
-   (symbol-value lang-headers))
+   (eval-default-headers lang-headers))
 			  (append (org-babel-params-from-properties lang)
   (list raw-params)))
 	  (setf hash (org-babel-sha1-hash info :export)))
-- 
2.28.0



Re: babel latex headers and image generation commands

2020-08-29 Thread Matt Huszagh
Bastien  writes:

> If you find the time to share your change as a _patch_ (not the whole
> updated Elisp function), that will allow more readers on this list to
> quickly understand what is at stake.

Ok, I've finally gotten around to taking a crack at this. The patch is
attached. Basically, it allows a lot more control when converting a
latex source block into an svg image file.

Specifically, this new method does not place any restrictions on the
latex contents (the old svg generation required the use of
\documentclass[preview]{standalone}), or on the specific generation
commands (it does require first compiling to pdf then to svg, though I
guess this could be generalized if necessary). Additionally, it allows
you to use a setup independent of the setup used for inline latex
snippets. I think this is important because snippets and latex source
blocks have different use cases. For instance, I use snippets for simple
inline math (e.g. \(x=6\)) and source blocks for complicated full-blown
latex pictures and sets of equations, etc. that are not supported by
latex fragments.

I'll present my use case as an example (see the patch to understand the
customizations).

;; this is particular to my use case, see `latex-preamble-by-backend'
(defun by-backend (blist)
  (let ((ret nil))
(if org-export-current-backend
(let* ((backend-name org-export-current-backend)
   (elem (assoc backend-name blist)))
  (if elem
  (setq ret (cdr elem
  (let ((elem (assoc t blist)))
(setq ret (cdr elem
(eval ret)))

;; custom function for preamble generation
(defun latex-preamble-by-backend (params)
  (concat "\\documentclass{"
  (cdr (assoc :class params))
  "}"
  "\\definecolor{fg}{rgb}{"
  (by-backend '((html . "0,0,0")
(t . (org-latex-color :foreground
  "}\n"
  "\\definecolor{bg}{rgb}{"
  (by-backend '((html . "1,1,1")
(t . (org-latex-color :background
  "}\n"
  "\\def\\pc{"
  (by-backend '((html . "100")
(t . "20")))
  "}\n"))

;; actual set customization
(setq org-babel-latex-preamble
  (lambda (params)
(latex-preamble-by-backend params)))

Now if I define a source block:

#+header: :class math :results file link replace :file tmp/some-file.svg
#+begin_src latex :hidden
{\color{fg}
\begin{equation*}
|z| = \sqrt{x^2 + y^2}
\end{equation*}
}
#+end_src

Using my customization above, this will turn into the following latex

\documentclass{math}\definecolor{fg}{rgb}{0.819608,0.721569,0.592157}
\definecolor{bg}{rgb}{0.0235294,0.137255,0.160784}
\def\pc{20}
\begin{document}{\color{fg}
\begin{equation*}
|z| = \sqrt{x^2 + y^2}
\end{equation*}
}\end{document}

which is then turned into an svg with inkscape, though this could be
dvisvgm, or whatever you want.

Math is a custom document class I've defined. But my function also
allows the setting of other classes (which is useful because some
classes work better for tikz-like images and others work better for
math). In fact, since the preamble generation function can use any of
the source block parameters, I could make it do a bunch of other
conditional stuff. I've also configured it to set my color scheme in a
backend-dependent way. This allows me to get color schemes that match my
emacs theme when generating images for viewing within emacs and another
color scheme (in my case black foreground, white background) when
exporting to html. Note that none of this is enforced on other
people. My patch simply allows you to achieve this sort of flexibility.

I think this patch needs some discussing. I had some trouble deciding on
the best way to implement this functionality because its hard to get
anything to be thematically consistent with the current latex execute
command, which feels like a collection of loosely-related functionality
(for instance, the htlatex backend generates a completely different
latex source than the imagemagick backend, even though the outputs could
be quite similar -- svg and png). I think there's a lot of room for
improvement in the latex execute function that could make it more
general and flexible, but it's hard to do this in a way that doesn't
break existing workflows. My patch tries to be minimally invasive to
these workflows, though it will break workflows in an easily recoverable
way for anyone using htlatex for svg output.

Anyway, I'd be curious to hear thoughts and I'd be interested to discuss
options for further refactoring the latex execute function.

Matt

On Fri, Aug 28, 2020 at 11:10 PM Matt Huszagh  wrote:

> Bastien  writes:
>
> > If you find the time to share your change as a _patch_ (not the whole
> > updated Elisp function), that will allow more readers on thi

Re: babel default header args as functions

2020-08-28 Thread Matt Huszagh
Matt Huszagh  writes:

> I've added the ability in my own configuration to use lambda functions
> that evaluate to a string as babel default header arguments, instead of
> just the plain strings currently allowed. Would anyone else be
> interested in this feature? Shall I prepare a patch?
>
> There are a number of use cases for this, but to give you an idea,
> here's one I'm using myself.
>
>   (setq org-babel-default-header-args:latex
> `((:file . (lambda ()
>  (concat "img/"
>  (sha1 (org-element-property :value 
> (org-element-at-point)))
>  (by-backend '((html . "-html") (t . "-org")))
>  ".svg")
>
> This computes a filename based on the hash of the block contents.

I've generated a patch for this. Please let me know your thoughts. I
believe this adds valuable flexibility to default header
arguments.

Thanks!
Matt

>From 3dfb1066b211fdcc5e3ea1da8d36aa115dde9f9b Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Fri, 28 Aug 2020 11:05:59 -0700
Subject: [PATCH] ob-core.el: Add ability to use closures as default header
 arguments

* lisp/ob-core.el (org-babel-default-header-args): Document ability to
use functions.
(eval-default-headers): New function to generate default header
arguments, which adds the ability to evaluate function arguments at
runtime.
(org-babel-get-src-block-info): Use new header argument evaluate
function when retreiving src block info.

The closures are evaluated at runtime.
---
 lisp/ob-core.el | 32 ++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 578622232..4a22f17e7 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -473,7 +473,23 @@ For the format of SAFE-LIST, see `org-babel-safe-header-args'."
 (defvar org-babel-default-header-args
   '((:session . "none") (:results . "replace") (:exports . "code")
 (:cache . "no") (:noweb . "no") (:hlines . "no") (:tangle . "no"))
-  "Default arguments to use when evaluating a source block.")
+  "Default arguments to use when evaluating a source block.
+
+This is a list in which each element is an alist.  Each key
+corresponds to a header argument, and each value to that header's
+value.  The value can either be a string or a closure that
+evaluates to a string at runtime.  For instance, imagine you'd
+like to set the file name output of a latex source block to a
+sha1 of its contents.  We could achieve this with:
+
+(defun org-src-sha ()
+  (let ((elem (org-element-at-point)))
+(concat (sha1 (org-element-property :value elem)) \".svg\")))
+
+(setq org-babel-default-header-args:latex
+  `((:results . \"file link replace\")
+(:file . (lambda () (org-src-sha)")
+
 (put 'org-babel-default-header-args 'safe-local-variable
  (org-babel-header-args-safe-fn org-babel-safe-header-args))
 
@@ -584,6 +600,18 @@ the outer-most code block.")
 
 (defvar *this*)
 
+(defun eval-default-headers (headers)
+  "Compute default header list set with HEADERS.
+
+  Evaluate all default header arguments set to functions prior to
+  returning the list of header arguments."
+  (let ((lst nil))
+(dolist (elem (eval headers t))
+  (if (listp (cdr elem))
+  (push `(,(car elem) . ,(funcall (cdr elem))) lst)
+(push elem lst)))
+lst))
+
 (defun org-babel-get-src-block-info ( light datum)
   "Extract information from a source block or inline source block.
 
@@ -615,7 +643,7 @@ a list with the following pattern:
 	   (apply #'org-babel-merge-params
 		  (if inline org-babel-default-inline-header-args
 			org-babel-default-header-args)
-		  (and (boundp lang-headers) (eval lang-headers t))
+		  (and (boundp lang-headers) (eval-default-headers lang-headers))
 		  (append
 		   ;; If DATUM is provided, make sure we get node
 		   ;; properties applicable to its location within
-- 
2.28.0



Re: incorrect documentation for file-desc header argument?

2020-07-29 Thread Matt Huszagh
Kyle Meyer  writes:

> Yeah, that looks to be the intended result of that thread.  That
> thread's patch was applied with a58a4f0ad (new source block header
> argument :filelinkdescr, 2012-03-27).  However, shortly after, that
> treatment was intentionally changed (670c7f31c, 2012-03-31):
>
> simplified implementation of :file-desc header argument
>   
> This will no longer insert the value of the :file header argument as
> the description if the description is left blank (as this changes
> the meaning of the :file header argument).
>   
> The desc handling logic is moved to the `org-babel-result-to-file'
> function.
>
> So, I'd say this is a documentation bug.

Thanks for the clarification Kyle. I've attached a patch that I believe
clarifies the documentation to match the current behavior.

Matt
>From a9cd13f3d7b120a24b3416f8f8ba0892a8e47221 Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Tue, 28 Jul 2020 22:10:12 -0700
Subject: [PATCH] org-manual.org: Modify file-desc header argument to match
 action

---
 doc/org-manual.org | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index b61644626..0f012d4df 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -17436,9 +17436,8 @@ default behavior is to automatically determine the result type.
 
   #+cindex: @samp{file-desc}, header argument
   The =file-desc= header argument defines the description (see
-  [[*Link Format]]) for the link.  If =file-desc= has no value, Org
-  uses the generated file name for both the "link" and
-  "description" parts of the link.
+  [[*Link Format]]) for the link.  If =file-desc= has no value, the
+  "description" part of the link will be omitted.
 
   #+cindex: @samp{sep}, header argument
   By default, Org assumes that a table written to a file has
-- 
2.27.0



Re: incorrect documentation for file-desc header argument?

2020-07-28 Thread Matt Huszagh
Matt Huszagh  writes:

> Hello,
>
> The org info documentation states
>
>  The ‘file-desc’ header argument defines the description (see *note
>  Link Format::) for the link.  If ‘file-desc’ has no value, Org uses
>  the generated file name for both the “link” and “description” parts
>  of the link.
>
> However, if I evaluate the
>
> #+BEGIN_SRC asymptote :results value file :file circle.pdf :output-dir img/
>   size(2cm);
>   draw(unitcircle);
> #+END_SRC
>
> I get
>
> #+RESULTS:
> [[file:img/circle.pdf]]
>
> I believe the documentation (and this is corroborated by the discussion
> of the original patch
> (https://lists.gnu.org/archive/html/emacs-orgmode/2012-04/msg00022.html)),
> that the intended result is
>
> #+RESULTS:
> [[file:img/circle.pdf][file:img/circle.pdf]]
>
> Is my assessment correct? Can anyone else confirm this bug?
>
> Thanks
> Matt

If this is indeed a bug, is there supposed to be a way to create the
current output?:

#+RESULTS:
[[file:img/circle.pdf]]



incorrect documentation for file-desc header argument?

2020-07-28 Thread Matt Huszagh
Hello,

The org info documentation states

 The ‘file-desc’ header argument defines the description (see *note
 Link Format::) for the link.  If ‘file-desc’ has no value, Org uses
 the generated file name for both the “link” and “description” parts
 of the link.

However, if I evaluate the

#+BEGIN_SRC asymptote :results value file :file circle.pdf :output-dir img/
  size(2cm);
  draw(unitcircle);
#+END_SRC

I get

#+RESULTS:
[[file:img/circle.pdf]]

I believe the documentation (and this is corroborated by the discussion
of the original patch
(https://lists.gnu.org/archive/html/emacs-orgmode/2012-04/msg00022.html)),
that the intended result is

#+RESULTS:
[[file:img/circle.pdf][file:img/circle.pdf]]

Is my assessment correct? Can anyone else confirm this bug?

Thanks
Matt



Re: patch: add custom latex->html conversion command

2020-02-16 Thread Matt Huszagh
Will do, thanks!

On Sun, Feb 16, 2020 at 5:10 PM Bastien  wrote:

> Hi Matt,
>
> Matt Huszagh  writes:
>
> > Thanks for the feedback. I've filled out the form you sent and sent it
> > to the email listed.
>
> Thanks!  It looks good.
>
> Let me know when you get the answer from the FSF.
>
> If you don't in four weeks, please ping them again cc'ing me.
>
> --
>  Bastien
>


Re: patch: add custom latex->html conversion command

2020-02-16 Thread Matt Huszagh
Thanks for the feedback. I've filled out the form you sent and sent it
to the email listed.

Adjusted patch below. I didn't add a value to the customization. I'm
honestly not very familiar with the customize interface since I never
use it. Is that a default value if the user selects string from the
customize interface? If it is I think it's probably best to leave it
blank since this is really meant to be open-ended. For instance, I doubt
the example I give would work on windows, so that might be more
confusing than helpful in that case. What do you think? Happy to set it
to whatever.

Matt

>From 6b2495c8aef0b67fd00ad27a0056e79f42c23c06 Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Sun, 16 Feb 2020 16:52:02 -0800
Subject: [PATCH] add custom command option when converting latex fragments to
 html

* lisp/org.el (org-latex-to-html-convert-command): Add custom command
option to convert a latex fragment directly into HTML.
(org-format-latex): Add condition for this command to org-format-latex.
(org-format-latex-as-html): Command that ultimately does the
conversion work. It uses the user-specified command and applies to
the latex fragment. It then returns the resulting HTML.
* lisp/ox-html.el (org-html-with-latex): Document 'html symbol.
(org-html-format-latex): This custom HTML conversion, like mathjax,
doesn't require preprocessing.
(org-html-latex-fragment): Add condition to org-html-latex-fragment
for html symbol.

This allows you to set a custom command
`org-latex-to-html-convert-command' that will take as input a latex
fragment and use it to generate html for export. This is very
open-ended in the sense that you can use any shell-command you
want. I've added the ability in order to use latexml, but you could
use any other tool that generates HTML output text.
---
 lisp/org.el | 29 +
 lisp/ox-html.el |  9 +++--
 2 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 97ce7ec43..7cc9e8687 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -3203,6 +3203,22 @@ When using LaTeXML set this option to
 	  (const :tag "None" nil)
 	  (string :tag "\nShell command")))
 
+(defcustom org-latex-to-html-convert-command nil
+  "Command to convert LaTeX fragments to HTML.
+This command is very open-ended: the output of the command will
+directly replace the latex fragment in the resulting HTML.
+Replace format-specifiers in the command as noted below and use
+`shell-command' to convert LaTeX to HTML.
+%i: The latex fragment to be converted.
+
+For example, this could be used with LaTeXML as
+\"latexmlc 'literal:%i' --profile=math --preload=siunitx.sty 2>/dev/null\"."
+  :group 'org-latex
+  :package-version '(Org . "9.5")
+  :type '(choice
+	  (const :tag "None" nil)
+	  (string :tag "\nShell command")))
+
 (defcustom org-preview-latex-default-process 'dvipng
   "The default process to convert LaTeX fragments to image files.
 All available processes and theirs documents can be found in
@@ -15613,6 +15629,10 @@ Some of the options can be changed using the variable
 		(if (string= (match-string 0 value) "$$")
 			(insert "\\[" (substring value 2 -2) "\\]")
 		  (insert "\\(" (substring value 1 -1) "\\)"
+		 ((eq processing-type 'html)
+		  (goto-char beg)
+		  (delete-region beg end)
+		  (insert (org-format-latex-as-html value)))
 		 ((assq processing-type org-preview-latex-process-alist)
 		  ;; Process to an image.
 		  (cl-incf cnt)
@@ -15778,6 +15798,15 @@ inspection."
   ;; Failed conversion.  Return the LaTeX fragment verbatim
   latex-frag)))
 
+(defun org-format-latex-as-html (latex-frag)
+  "Convert latex to html with a custom conversion command.
+`LATEX-FRAG' is the latex fragment
+Set the custom command with `org-latex-to-html-convert-command'."
+  (let ((cmd (format-spec org-latex-to-html-convert-command
+			  `((?i . ,latex-frag)
+(message "Running %s" cmd)
+(setq shell-command-output (shell-command-to-string cmd
+
 (defun org--get-display-dpi ()
   "Get the DPI of the display.
 The function assumes that the display has the same pixel width in
diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index ea6aa63c3..b5cbf4cfc 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -776,6 +776,8 @@ e.g. \"tex:mathjax\".  Allowed values are:
   `verbatim'Keep everything in verbatim
   `mathjax', t  Do MathJax preprocessing and arrange for MathJax.js to
 be loaded.
+  `html'Use `org-latex-to-html-convert-command' to convert
+LaTeX fragments to HTML.
   SYMBOLAny symbol defined in `org-preview-latex-process-alist',
 e.g., `dvipng'."
   :group 'org-export-html
@@ -2776,12 +2778,13 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
 (defun org

Re: patch: add custom latex->html conversion command

2020-02-16 Thread Matt Huszagh
Bastien,

Thanks for the reply. I’ll look into it. However, I’d initially ruled that
out because the final output is meant to be an image. So, to get just text
insertion into the html output I imagine would be a bit of a hack. For
instance what to set image-converter to etc.

Matt

On Sun, Feb 16, 2020 at 4:06 PM Bastien  wrote:

> Hi Matt,
>
> Matt Huszagh  writes:
>
> > The patch below allow's you to provide your own shell command the
> > generates HTML from a latex fragment and places the output directly in
> > the exported HTML file.
>
> Can you get the same output by configuring a new symbol in
> `org-preview-latex-process-alist' then setting `org-html-with-latex'
> to this new symbol?
>
> I've not tested it, so perhaps it does not work.
>
> But any patch about adding latexmlc support should surely look in this
> direction first.
>
> HTH,
>
> --
>  Bastien
>


patch: add custom latex->html conversion command

2020-02-15 Thread Matt Huszagh
Hi all,

The patch below allow's you to provide your own shell command the
generates HTML from a latex fragment and places the output directly in
the exported HTML file.

I've added this feature to be able to use latexml for processing latex
fragments to HTML. Unfortunately, mathjax can't process macros from the
siunitx package, but latexml can. Here's how I use it

  (setq org-html-with-latex 'html)
  (setq org-latex-to-html-convert-command "latexmlc 'literal:%i' --profile=math 
--preload=siunitx.sty 2>/dev/null")

Any/all feedback appreciated!

>From 056d23d9e5caa6fc22907014e0128519fcc84b6e Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Sat, 15 Feb 2020 18:42:11 -0800
Subject: [PATCH] add custom command option when converting latex fragments to
 html

This allows you to set a custom command
`org-latex-to-html-convert-command' that will take as input a latex
fragment and use it to generate html for export. This is very
open-ended in the sense that you can use any shell-command you want. I
envisioned this for use with latexml, but there's nothing preventing
you from using something else.
---
 lisp/org.el | 30 ++
 lisp/ox-html.el |  9 +++--
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 97ce7ec43..94557bf86 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -3203,6 +3203,22 @@ When using LaTeXML set this option to
 	  (const :tag "None" nil)
 	  (string :tag "\nShell command")))
 
+(defcustom org-latex-to-html-convert-command nil
+  "Command to convert LaTeX fragments to HTML.
+This command is very open-ended: the output of the command will
+directly replace the latex fragment in the resulting HTML.
+Replace format-specifiers in the command as noted below and use
+`shell-command' to convert LaTeX to HTML.
+%i: The latex fragment to be converted.
+
+For example, this could be used with LaTeXML as
+\"latexmlc 'literal:%i' --profile=math --preload=siunitx.sty 2>/dev/null\"."
+  :group 'org-latex
+  :version "26.1"
+  :type '(choice
+	  (const :tag "None" nil)
+	  (string :tag "\nShell command")))
+
 (defcustom org-preview-latex-default-process 'dvipng
   "The default process to convert LaTeX fragments to image files.
 All available processes and theirs documents can be found in
@@ -15613,6 +15629,10 @@ Some of the options can be changed using the variable
 		(if (string= (match-string 0 value) "$$")
 			(insert "\\[" (substring value 2 -2) "\\]")
 		  (insert "\\(" (substring value 1 -1) "\\)"
+		 ((eq processing-type 'html)
+		  (goto-char beg)
+		  (delete-region beg end)
+		  (insert (org-format-latex-as-html value)))
 		 ((assq processing-type org-preview-latex-process-alist)
 		  ;; Process to an image.
 		  (cl-incf cnt)
@@ -15778,6 +15798,16 @@ inspection."
   ;; Failed conversion.  Return the LaTeX fragment verbatim
   latex-frag)))
 
+(defun org-format-latex-as-html (latex-frag)
+  "Convert latex to html with a custom conversion command.
+`LATEX-FRAG' is the latex fragment
+Set the custom command with `org-latex-to-html-convert-command'."
+  (let ((cmd (format-spec
+	  org-latex-to-html-convert-command
+	  `((?i . ,latex-frag)
+(message "Running %s" cmd)
+(setq shell-command-output (shell-command-to-string cmd
+
 (defun org--get-display-dpi ()
   "Get the DPI of the display.
 The function assumes that the display has the same pixel width in
diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index ea6aa63c3..b5cbf4cfc 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -776,6 +776,8 @@ e.g. \"tex:mathjax\".  Allowed values are:
   `verbatim'Keep everything in verbatim
   `mathjax', t  Do MathJax preprocessing and arrange for MathJax.js to
 be loaded.
+  `html'Use `org-latex-to-html-convert-command' to convert
+LaTeX fragments to HTML.
   SYMBOLAny symbol defined in `org-preview-latex-process-alist',
 e.g., `dvipng'."
   :group 'org-export-html
@@ -2776,12 +2778,13 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
 (defun org-html-format-latex (latex-frag processing-type info)
   "Format a LaTeX fragment LATEX-FRAG into HTML.
 PROCESSING-TYPE designates the tool used for conversion.  It can
-be `mathjax', `verbatim', nil, t or symbols in
+be `mathjax', `verbatim', `html', nil, t or symbols in
 `org-preview-latex-process-alist', e.g., `dvipng', `dvisvgm' or
 `imagemagick'.  See `org-html-with-latex' for more information.
 INFO is a plist containing export properties."
   (let ((cache-relpath "") (cache-dir ""))
-(unless (eq processing-type 'mathjax)
+(unless (or (eq processing-type 'mathjax)
+(eq processing-type 'html))
   (let ((bfn

babel default header args as functions

2020-02-07 Thread Matt Huszagh
I've added the ability in my own configuration to use lambda functions
that evaluate to a string as babel default header arguments, instead of
just the plain strings currently allowed. Would anyone else be
interested in this feature? Shall I prepare a patch?

There are a number of use cases for this, but to give you an idea,
here's one I'm using myself.

  (setq org-babel-default-header-args:latex
`((:file . (lambda ()
 (concat "img/"
 (sha1 (org-element-property :value 
(org-element-at-point)))
 (by-backend '((html . "-html") (t . "-org")))
 ".svg")

This computes a filename based on the hash of the block contents.

Matt



babel comma escape with :wrap

2020-02-07 Thread Matt Huszagh
There appears to be no way to disable the comma escape when using :wrap
for a babel source block.

I'm essentially trying to replicate this example from the manual

 #+NAME: attr_wrap
 #+BEGIN_SRC sh :var data="" :var width="\\textwidth" :results output
   echo "#+ATTR_LATEX: :width $width"
   echo "$data"
 #+END_SRC

 #+HEADER: :file /tmp/it.png
 #+BEGIN_SRC dot :post attr_wrap(width="5cm", data=*this*) :results drawer
   digraph{
   a -> b;
   b -> c;
   c -> a;
   }
 #+end_src

 #+RESULTS:
 :RESULTS:
 #+ATTR_LATEX :width 5cm
 [[file:/tmp/it.png]]
 :END:

But, my result type is a link not a drawer (which are mutually
exclusive). So, to get the same wrapping effect, I need to use the :wrap
argument. However, the comma escape negates attr_wrap's effect with this
code

 (let ((wrap
(lambda (start finish  no-escape no-newlines
  inline-start inline-finish)
  (when inline
(setq start inline-start)
(setq finish inline-finish)
(setq no-newlines t))
  (let ((before-finish (copy-marker end)))
(goto-char end)
(insert (concat finish (unless no-newlines "\n")))
(goto-char beg)
(insert (concat start (unless no-newlines "\n")))
(unless no-escape
  (org-escape-code-in-region
   (min (point) before-finish) before-finish))
(goto-char end
  [...]

But, since this file uses lexical binding (as it should) there appears
to be no way to set no-escape. I searched through the changelog a bit
and that seems to be a relic from an old version. At the very least,
that no-escape conditional should be made unconditional. However, I do
think there should be a way to avoid the comma escape, but I'm not sure
of the best way to do it. I'm more than happy to write the code, but I'm
curious what people think would be the best way to do it.

Maybe :wrap could take a special keyword argument (e.g. no-escape) that
would stop the effect. Thoughts?

Best,
Matt



babel link bug

2020-02-07 Thread Matt Huszagh
The patch below fixes a bug with the behavior of link without file for
babel source blocks. All explained in patch message, but let me know if
any concerns.

>From 25d363bbc3cd7122287364f25f9b5d653bcae232 Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Fri, 7 Feb 2020 23:09:48 -0800
Subject: [PATCH] ob-core.el: fix silent ouput of babel link format
Cc: emacs 

The file type and link format are distinct according to the
manual. Previous code required file type in order for link to
work. This is distinct from the file header argument that is required
for link to work.

To see why this is a bug, try the code under link in the manual

 #+begin_src shell :results link :file "download.tar.gz"
 wget -c "http://example.com/download.tar.gz;
 #+end_src

This will download the file but will not generate any results.
---
 lisp/ob-core.el | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 1a0122192..53168edc8 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -709,8 +709,7 @@ block."
 			   (not (listp r)))
 			  (list (list r))
 			r)))
-	  (let ((file (and (member "file" result-params)
-			   (cdr (assq :file params)
+	  (let ((file (cdr (assq :file params
 		;; If non-empty result and :file then write to :file.
 		(when file
 		  ;; If `:results' are special types like `link' or
-- 
2.25.0



Re: latex fragments in html export

2020-02-05 Thread Matt Huszagh
Nvm, I found the issue.



latex fragments in html export

2020-02-05 Thread Matt Huszagh
Hello,

Has anyone had success getting the html export to process latex
fragments? It's leaving mine completely alone and I can't figure out
why. Latex babel source blocks work fine. I've set

(setq org-html-with-latex luasvgm)

luasvgm is a process-alist element I defined. I went through ox-html and
set up debug points in a bunch of functions meant to process latex
fragments, but none of them are being called.

Thanks
Matt



Re: babel latex headers and image generation commands

2020-02-04 Thread Matt Huszagh
I've thought about this more and the solution I presented above isn't
quite sufficient for me. I need something where I get complete control
over what goes in the latex source block on a block-by-block basis. In
other words, I don't want a user-configurable option like
org-format-latex-header plus a list of user-configurable packages in
every block.

I could add an option alongside the option I introduced above that tells
the execute function to only compose the tex file from the body (btw,
this doesn't have to be manual for the user, just define a class and use
snippets), but at some point I wonder how much it makes sense to keep
adding options to this function since it'll just make it harder to
maintain.

What do people think? If there's any interest, I'm more than happy to
put in the extra time and add this functionality to
latex-execute. Otherwise, I'll just advise the function for my own
specific needs.

I do think there's a real use-case here: namely, complete case-by-case
control over the source latex and its output, including export backend
dependent things like colors. But, maybe this is just me and the
existing functionality is fine with people.

Matt



Re: babel latex headers and image generation commands

2020-02-04 Thread Matt Huszagh
Ok, here's an implementation that seems to be working pretty well so
far.

`org-latex-img-process` is the new customization. Most of the execute
function is unaltered, but I've added the condition:
```
 ((and (not imagemagick)
   (assoc extension org-latex-img-process))
```

Here's the change in full.

```
(setq org-latex-pdf-process
  '("latexmk -f -interaction=nonstopmode -output-directory=%o %f"))

(setq org-latex-img-process
  '(("svg" . ("dvisvgm %f -P -n -b min -o %O"

(defun org-babel-execute:latex (body params)
  "Execute a block of Latex code with Babel.
This function is called by `org-babel-execute-src-block'."
  (setq body (org-babel-expand-body:latex body params))
  (if (cdr (assq :file params))
  (let* ((out-file (cdr (assq :file params)))
 (extension (file-name-extension out-file))
 (tex-file (org-babel-temp-file "latex-" ".tex"))
 (border (cdr (assq :border params)))
 (imagemagick (cdr (assq :imagemagick params)))
 (im-in-options (cdr (assq :iminoptions params)))
 (im-out-options (cdr (assq :imoutoptions params)))
 (fit (or (cdr (assq :fit params)) border))
 (height (and fit (cdr (assq :pdfheight params
 (width (and fit (cdr (assq :pdfwidth params
 (headers (cdr (assq :headers params)))
 (in-buffer (not (string= "no" (cdr (assq :buffer params)
 (org-latex-packages-alist
  (append (cdr (assq :packages params)) 
org-latex-packages-alist)))
(cond
 ((and (string-suffix-p ".png" out-file) (not imagemagick))
  (org-create-formula-image
   body out-file org-format-latex-options in-buffer))
 ((string-suffix-p ".tikz" out-file)
  (when (file-exists-p out-file) (delete-file out-file))
  (with-temp-file out-file
(insert body)))
 ((and (not imagemagick)
   (assoc extension org-latex-img-process))
  (with-temp-file tex-file
(insert (concat
 (org-latex-make-preamble
  (org-export-get-environment (org-export-get-backend 
'latex))
  org-format-latex-header
  'snippet)
 (if headers
 (concat "\n"
 (if (listp headers)
 (mapconcat #'identity headers "\n")
   headers) "\n")
   "")
 "\\begin{document}"
 body
 "\\end{document}")))
  (let ((tmp-pdf (org-babel-latex-tex-to-pdf tex-file)))
(when (file-exists-p out-file) (delete-file out-file))
(let* ((log-buf (get-buffer-create "*Org Babel LaTeX Output*"))
   (err-msg "fix")
   (img-out (org-compile-file
 tmp-pdf
 (cdr (assoc "svg" org-latex-img-process))
 extension err-msg log-buf)))
  (shell-command (format "mv %s %s" img-out out-file)
 ((and (or (string= "svg" extension)
   (string= "html" extension))
   (executable-find org-babel-latex-htlatex))
  ;; TODO: this is a very different way of generating the
  ;; frame latex document than in the pdf case.  Ideally, both
  ;; would be unified.  This would prevent bugs creeping in
  ;; such as the one fixed on Aug 16 2014 whereby :headers was
  ;; not included in the SVG/HTML case.
  (with-temp-file tex-file
(insert (concat
 "\\documentclass[preview]{standalone}
\\def\\pgfsysdriver{pgfsys-tex4ht.def}
"
 (mapconcat (lambda (pkg)
  (concat "\\usepackage" pkg))
org-babel-latex-htlatex-packages
"\n")
 (if headers
 (concat "\n"
 (if (listp headers)
 (mapconcat #'identity headers "\n")
   headers) "\n")
   "")
 "\\begin{document}"
 body
 "\\end{document}")))
  (when (file-exists-p out-file) (delete-file out-file))
  (let ((default-directory (file-name-directory tex-file)))
(shell-command (format "%s %s" org-babel-latex-htlatex 
tex-file)))
  

babel latex headers and image generation commands

2020-02-03 Thread Matt Huszagh
I spent some time today trying to get latex babel source blocks to work
for me and discovered that calling `org-babel-execute:latex` ignores the
:headers header if the output file is a png without setting imagemagick
to t. It's easy to see this in the source code: the conditions mentioned
above leads to calling org-create-formula-image without passing in the
headers. I think this is a bug, although maybe I missed it somewhere in
the documentation? Here's a MWE if you want it

#+header: :file "test.png"
#+header: :headers '("\\def\\hello{hello}")
#+begin_src latex :results output file link
\hello
#+end_src

doesn't work, but

#+header: :file "test.png"
#+header: :imagemagick t
#+header: :headers '("\\def\\hello{hello}")
#+begin_src latex :results output file link
\hello
#+end_src

does.

However, this got me thinking that I wish executing latex blocks behaved
a bit more like latex fragment previews. Particularly, the ability to
customize `image-converter`. So, I'm thinking about adding a
customization option that allows a user to use the image-converter
portion of an existing org-preview-latex-process-alist entry (I guess
the most obvious choice would be
org-preview-latex-default-process). Although I guess I could just add a
new variable like what we have with org-latex-pdf-process. Maybe that's
better since we'd only be using one part of the
latex-process-alist. What are people's thoughts on this?



preview src blocks that generate image files

2020-01-26 Thread Matt Huszagh
Hi,

I'm considering adding the ability to display an image preview overlay of
source blocks that generate an image file, much in the same way that
latex fragments can be previewed.

Is anyone else interested in this feature? Any general thoughts/feature
requests?

Matt



Re: Issues with nested begin..end blocks in inline math environments

2019-12-16 Thread Matt Huszagh
Nicolas Goaziou  writes:

> I don't think this patch is a good idea, as it mixes elements from
> different types: LaTeX fragment (inline), and LaTeX environment (block).
> For example, the regexp you modify is used as a paragraph separator,
> which doesn't make sense for inline LaTeX.

No worries. Thanks for taking a look at this anyway.

Matt



Re: Issues with nested begin..end blocks in inline math environments

2019-12-14 Thread Matt Huszagh

I'm submitting this as a patch. I've used it on hundreds of latex
fragments over the past week or so and haven't experienced any issues
(which is expected since the change is small).

>From a699b699ed4132839c39f1152868bb13364422c7 Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Sat, 14 Dec 2019 19:54:41 -0800
Subject: [PATCH] org-element.el: allow environment blocks in math delimiters

* lisp/org-element.el (org-element--latex-begin-environment): Add a
non-capturing block for `\(' or `$' so that previously recognized
latex environments can also appear within an inline math environment.

* lisp/org-element.el (org-element--latex-end-environment): Match the
begin environment noncapturing block with `$' or `\)'.
---
 lisp/org-element.el | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lisp/org-element.el b/lisp/org-element.el
index 110ff5624..6d7ec32c6 100644
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -,14 +,14 @@ containing `:key', `:value', `:begin', `:end', `:post-blank' and
  Latex Environment
 
 (defconst org-element--latex-begin-environment
-  "^[ \t]*begin{\\([A-Za-z0-9*]+\\)}"
+  "^[ \t]*\\(?:(\\|\\$\\)?begin{\\([A-Za-z0-9*]+\\)}"
   "Regexp matching the beginning of a LaTeX environment.
 The environment is captured by the first group.
 
 See also `org-element--latex-end-environment'.")
 
 (defconst org-element--latex-end-environment
-  "end{%s}[ \t]*$"
+  "end{%s}[ \t]*\\(?:)\\|\\$\\)?$"
   "Format string matching the ending of a LaTeX environment.
 See also `org-element--latex-begin-environment'.")
 
-- 
2.24.0



Re: Removing horizontal space in latex fragments

2019-12-14 Thread Matt Huszagh
Nicolas Goaziou  writes:

> So, has anyone settled on which one to apply?

My vote goes for keeping the newlines to improve readability in the
generated tex file. But, again, I'm more than happy to be overuled.

> Minor nitpick:
>
>   (if (string-suffix-p string "\n") ...)
>
> is slightly less low-level.

Appreciate the nitpick; your version is better!

I've attached an updated patch.

Best,
Matt

>From bdb93a13a43d90ad6e66449797111e836a67a219 Mon Sep 17 00:00:00 2001
From: Matt Huszagh 
Date: Thu, 5 Dec 2019 23:25:32 -0800
Subject: [PATCH] org.el: Remove leading/trailing whitespace from latex
 fragment

* lisp/org.el (org-create-formula-image): Ensure user input ends
with a % character to remove trailing whitespace. Also, add %
characters between macros and newlines purely visual.
---
 lisp/org.el | 11 ---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 9b84592ba..ae686e330 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16554,12 +16554,17 @@ a HTML file."
 	(setq bg (org-latex-color :background))
   (setq bg (org-latex-color-format
 		(if (string= bg "Transparent") "white" bg
+;; remove tex \par at end of snippet to avoid trailing
+;; whitespace
+(if (string-suffix-p string "\n")
+(aset string (- (length string) 1) ?%)
+  (setq string (concat string "%")))
 (with-temp-file texfile
   (insert latex-header)
   (insert "\n\\begin{document}\n"
-	  "\\definecolor{fg}{rgb}{" fg "}\n"
-	  "\\definecolor{bg}{rgb}{" bg "}\n"
-	  "\n\\pagecolor{bg}\n"
+	  "\\definecolor{fg}{rgb}{" fg "}%\n"
+	  "\\definecolor{bg}{rgb}{" bg "}%\n"
+	  "\n\\pagecolor{bg}%\n"
 	  "\n{\\color{fg}\n"
 	  string
 	  "\n}\n"
--
2.24.0


Re: Issues with nested begin..end blocks in inline math environments

2019-12-07 Thread Matt Huszagh
"Fraga, Eric"  writes:
> The identification of LaTeX fragments is somewhat fragile (in my
> experience).  I would suggest you enclose complex LaTeX code fragments
> within an #+begin_export latex ... #+end_export environment.
>
> Assuming your export target is LaTeX and/or PDF.

Changing the beginning and end regexes to the following seems to work
well (I've just added a non-capturing group for \(\) or $$ so that
begin-end blocks can be nested inside inline math environments).

(setq org-element--latex-begin-environment "^[ 
\t]*\\(?:(\\|\\$\\)?begin{\\([A-Za-z0-9*]+\\)}")
(setq org-element--latex-end-environment "end{%s}[ 
\t]*\\(?:)\\|\\$\\)?$")

I'm going to test this a bit before submitting it as a patch to make
sure it doesn't cause any issues. If you have any tricky fragments you
want to test it on please let me how it works!



Re: Issues with nested begin..end blocks in inline math environments

2019-12-07 Thread Matt Huszagh
"Fraga, Eric"  writes:
> The identification of LaTeX fragments is somewhat fragile (in my
> experience).  I would suggest you enclose complex LaTeX code fragments
> within an #+begin_export latex ... #+end_export environment.
>
> Assuming your export target is LaTeX and/or PDF.

Thanks for the suggestion. Unfortunately these are mostly just used as
latex previews in the org buffer. I'm going to try to improve support
for this when I get the time since I use this capability extensively. If
you have any thoughts/suggestions on best ways to improve this I'd be
happy to hear them.



Issues with nested begin..end blocks in inline math environments

2019-12-06 Thread Matt Huszagh
I'm experiencing incorrect and seemingly inconsistent behavior when nesting
`\begin` `\end` environments inside `\(\)` or `$$`. For example, the
following is valid latex code:
```
\(\begin{aligned}
b_n &= \frac{1}{\pi} \int_{0}^{\pi} x \sin{\left(nx\right)}dx \\
b_0 &= 0 \\
\int_{0}^{\pi} udv &= \left[uv\right]_{0}^{\pi}
  - \int_{0}^{\pi} vdu \\
dv &= \sin{\left(nx\right)}dx \\
v(x) &= \int \sin{\left(nx\right)}dx \\
&= -\frac{\cos{\left(nx\right)}}{n} \\
\left[uv\right]_{0}^{\pi} &=
  -\left[\frac{x\cos{\left(nx\right)}}{n}\right]_{0}^{\pi} \\
&= \left\{\begin{alignedat}{2}
  \frac{\pi}{n}&, && \quad \text{$n$ odd} \\
  -\frac{\pi}{n}&, && \quad \text{$n$ positive even} \\
\end{alignedat}\right. \\
\int_{0}^{\pi} vdu &=
  \int_{0}^{\pi} -\frac{\cos{\left(nx\right)}}{n} dx \\
&= -\frac{1}{n^2} \left.\sin{\left(nx\right)}\right|_{0}^{\pi} \\
&= 0 \\
b_{n\in \mathbb{Z}^+} &= \left\{\begin{alignedat}{2}
  \frac{1}{n}&, && \quad \text{$n$ odd} \\
  -\frac{1}{n}&, && \quad \text{$n$ positive even} \\
\end{alignedat}\right. \\
\end{aligned}\)
```
But this confuses org which is generating images for the `\begin{aligned}`
at the top and the `\text` macros, but nothing else. Iterations on this
sometimes work fine, however. For example,
```
\(\begin{aligned}
b_{n\in \mathbb{Z}^+} &= \left\{\begin{alignedat}{2}
  \frac{1}{n}&, && \quad \text{$n$ odd} \\
  -\frac{1}{n}&, && \quad \text{$n$ positive even} \\
\end{alignedat}\right. \\
\end{aligned}\)
```
typesets correctly. Has anyone else had trouble with the latex fragment
regex's?


Re: Removing horizontal space in latex fragments

2019-12-06 Thread Matt Huszagh
Thanks for the reply Eric. The thing I like about the newlines is that the
generated tex files are slightly easier to read. However, this is really
minor. I've created 2 separate patches: one keeping the newlines and the
other without. I'm happy to defer to you or anyone else in regard to which
is preferable.

On Thu, Dec 5, 2019 at 2:24 PM Fraga, Eric  wrote:

> On Thursday,  5 Dec 2019 at 11:03, Matt Huszagh wrote:
> > Is anyone else interested in this modification? Should I submit it as a
> > patch?
>
> I think so.  And I am not sure all those \n's are necessary.  Without
> them, you can probably also remove many of the %s.
> --
> Eric S Fraga via Emacs 27.0.50, Org release_9.2.6-544-gd215c3
>


remove-newlines.patch
Description: Binary data


keep-newlines.patch
Description: Binary data


Removing horizontal space in latex fragments

2019-12-05 Thread Matt Huszagh
I've modified the behavior of `org-create-formula-image' so that
`\definecolor' etc do not create unnecessary whitespace in the output PDF.
Here's the relevant change:

```
...
;; remove tex \par at end of line
(if (string= (substring string -1 nil) "\n")
(aset string (- (length string) 1) ?%)
  (setq string (concat string "%")))
(with-temp-file texfile
  (insert latex-header)
  (insert "\n\\begin{document}\n"
 "\\definecolor{fg}{rgb}{" fg "}%\n"
 "\\definecolor{bg}{rgb}{" bg "}%\n"
 "\n\\pagecolor{bg}%\n"
 "\n{\\color{fg}\n"
 string
 "\n}\n"
 "\n\\end{document}\n"))
...
```

This is useful if you (like me) have replaced the default document class
with standalone and are using dvisvgm. The change removes leading left and
right space, which is especially useful when using inline math mixed with
normal text. It shouldn't make a difference if using Imagemagick's convert
as a backend since that can get rid of whitespace boundaries.

Is anyone else interested in this modification? Should I submit it as a
patch?

Matt


latex preview parallel processes

2019-11-03 Thread Matt Huszagh
Hello. I have an org document with many latex snippets for which I use
the org latex preview facility. As the document has gotten bigger, any
changes requiring a rebuild of all images (e.g. changing
`org-format-latex-header') takes longer and longer to the point where
it now takes me over an hour to generate all images. Since all images
are mutually independent, I was wondering if it's possible to run
these tasks in parallel to make use of my multiple CPU cores. If this
is not currently possible, what would it take to get this working and
how could I help?

Thanks
Matt