-
Notifications
You must be signed in to change notification settings - Fork 0
/
automatic_latex_fragment_toggling_in_org-mode.html
251 lines (221 loc) · 12.9 KB
/
automatic_latex_fragment_toggling_in_org-mode.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html lang="en">
<head>
<!-- 2022-07-23 Sat 05:11 -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Automatic Latex Fragment Toggling in org-mode</title>
<meta name="generator" content="Org mode">
<meta name="author" content="ivanaf">
<link rel="stylesheet" type="text/css" href="css/org.css"/>
<link rel="icon" href="ico/favicon.ico" type="image/x- icon">
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2020 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div class="head">
<div class="title">
<p>
<a href="index.html">Ivanaf</a>
</p>
</div>
<menu>
<ul class="org-ul">
<li><a href="index.html">Home</a></li>
<li><a href="journal.html">Journal</a></li>
<li><a href="about.html">About</a></li>
<li><a href="resume.html">Resume</a></li>
<li><a href="portfolio.html">Portfolio</a></li>
<li><a href="contact.html">Contact</a></li>
<li><a href="projects_ideas.html">Messy Ideas</a></li>
</ul>
</menu>
</div>
<p>
</p><h1>
Automatic Latex Fragment Toggling in org-mode
</h1><p>
</p>
<p>
<span class=page-date> <small>
2019-05-25, updated 2019-05-25 — <a href='journal.html#emacs' class='emacs tagbutton'>emacs</a> <a href='journal.html#tech' class='tech tagbutton'>tech</a> <a href='journal.html#blog' class='blog tagbutton'>blog</a>   <a href="magically_setup_emacsclient.html">⇦Magically setup emacsclient</a> – <a href="animating_gifs_in_orgmode.html">Animating gifs in orgmode⇨</a>
</small> </span>
</p>
<p>
Automatic toggle latex fragments in orgmode when point is in them.
</p>
<p>
I found a solution by <a href="http://slumpy.org/blog/2017-02-01-automatic-latex-preview-in-org-mode/">Konstantin Kliakhandler</a>, based on a <a href="http://kitchingroup.cheme.cmu.edu/blog/2015/10/09/Automatic-latex-image-toggling-when-cursor-is-on-a-fragment/">solution premade by John
Kitchin</a>.
</p>
<p>
The code worked fine, however, it was fairly slow, and sometimes it would issue too many commands at once (for example, when it was scrolling).
</p>
<p>
Therefore, I decided to improve it and make it faster!
</p>
<p>
The first thing I did was to add a <code>while-no-input</code> to the beginning of the code, so that it can be interrupted with user input. The second thing was to add a <code>(run-with-idle-timer 0.05 nil</code> This adds a delay of .05 seconds before displaying or removing the image. This is particularly useful when scrolling, because, together with the <code>while-no-input</code> it stops the function from running multiple times while you scroll, or move around the text quickly, or when you take multiple actions that take less than 0.05 seconds.
</p>
<p>
At last, I improved the performance considerably by using the trick I learned from <a href="https://github.com/Fuco1/org-inline-image/blob/master/org-inline-image.el">org-inline-image package</a>, i.e that we can get the overlay at point and filter it by a specific property. This way instead of going through a potentially very large list of overlays, we make use of a function that was implemented in C, which is likely much faster, and we only have to go through all the overlays that are defined at point, which are likely much fewer than the number of overlays on the buffer.
</p>
<p>
At last I decided to byte compile the function, because, why not? It will be run at the end of every input anyway.
</p>
<p>
Putting this all together:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">defvar</span> <span class="org-variable-name">org-latex-fragment-last</span> nil
<span class="org-doc">"Holds last fragment/environment you were on."</span>)
(<span class="org-keyword">defun</span> <span class="org-function-name">my/org-latex-fragment--get-current-latex-fragment</span> ()
<span class="org-doc">"Return the overlay associated with the image under point."</span>
(car (--select (eq (overlay-get it 'org-overlay-type) 'org-latex-overlay) (overlays-at (point)))))
(<span class="org-keyword">defun</span> <span class="org-function-name">my/org-in-latex-fragment-p</span> ()
<span class="org-doc">"Return the point where the latex fragment begins, if inside</span>
<span class="org-doc"> a latex fragment. Else return false"</span>
(<span class="org-keyword">let*</span> ((el (org-element-context))
(el-type (car el)))
(<span class="org-keyword">and</span> (<span class="org-keyword">or</span> (eq 'latex-fragment el-type) (eq 'latex-environment el-type))
(org-element-property <span class="org-builtin">:begin</span> el))))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-latex-fragment-toggle-auto</span> ()
<span class="org-comment-delimiter">;; </span><span class="org-comment">Wait for the s</span>
(<span class="org-keyword">interactive</span>)
(<span class="org-keyword">while-no-input</span>
(run-with-idle-timer 0.05 nil 'org-latex-fragment-toggle-helper)))
(<span class="org-keyword">defun</span> <span class="org-function-name">org-latex-fragment-toggle-helper</span> ()
<span class="org-doc">"Toggle a latex fragment image "</span>
(<span class="org-keyword">condition-case</span> nil
(<span class="org-keyword">and</span> (eq 'org-mode major-mode)
(<span class="org-keyword">let*</span> ((begin (my/org-in-latex-fragment-p)))
(<span class="org-keyword">cond</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">were on a fragment and now on a new fragment</span>
((<span class="org-keyword">and</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">fragment we were on</span>
org-latex-fragment-last
<span class="org-comment-delimiter">;; </span><span class="org-comment">and are on a fragment now</span>
begin
<span class="org-comment-delimiter">;; </span><span class="org-comment">but not on the last one this is a little tricky. as you edit the</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">fragment, it is not equal to the last one. We use the begin</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">property which is less likely to change for the comparison.</span>
(not (= begin
org-latex-fragment-last)))
<span class="org-comment-delimiter">;; </span><span class="org-comment">go back to last one and put image back</span>
(<span class="org-keyword">save-excursion</span>
(goto-char org-latex-fragment-last)
(<span class="org-keyword">when</span> (my/org-in-latex-fragment-p) (org-toggle-latex-fragment))
<span class="org-comment-delimiter">;; </span><span class="org-comment">now remove current imagea</span>
(goto-char begin)
(<span class="org-keyword">let</span> ((ov (my/org-latex-fragment--get-current-latex-fragment)))
(<span class="org-keyword">when</span> ov
(delete-overlay ov)))
<span class="org-comment-delimiter">;; </span><span class="org-comment">and save new fragment</span>
(<span class="org-keyword">setq</span> org-latex-fragment-last begin)))
<span class="org-comment-delimiter">;; </span><span class="org-comment">were on a fragment and now are not on a fragment</span>
((<span class="org-keyword">and</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">not on a fragment now</span>
(not begin)
<span class="org-comment-delimiter">;; </span><span class="org-comment">but we were on one</span>
org-latex-fragment-last)
<span class="org-comment-delimiter">;; </span><span class="org-comment">put image back on</span>
(<span class="org-keyword">save-excursion</span>
(goto-char org-latex-fragment-last)
(<span class="org-keyword">when</span> (my/org-in-latex-fragment-p)(org-toggle-latex-fragment)))
<span class="org-comment-delimiter">;; </span><span class="org-comment">unset last fragment</span>
(<span class="org-keyword">setq</span> org-latex-fragment-last nil))
<span class="org-comment-delimiter">;; </span><span class="org-comment">were not on a fragment, and now are</span>
((<span class="org-keyword">and</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">we were not one one</span>
(not org-latex-fragment-last)
<span class="org-comment-delimiter">;; </span><span class="org-comment">but now we are</span>
begin)
(<span class="org-keyword">save-excursion</span>
(goto-char begin)
<span class="org-comment-delimiter">;; </span><span class="org-comment">remove image</span>
(<span class="org-keyword">let</span> ((ov (my/org-latex-fragment--get-current-latex-fragment)))
(<span class="org-keyword">when</span> ov
(delete-overlay ov)))
(<span class="org-keyword">setq</span> org-latex-fragment-last begin)))
<span class="org-comment-delimiter">;; </span><span class="org-comment">else not on a fragment</span>
((not begin)
(<span class="org-keyword">setq</span> org-latex-fragment-last nil)))))
(<span class="org-warning">error</span> nil)))
(add-hook 'post-command-hook 'org-latex-fragment-toggle-auto)
(<span class="org-keyword">setq</span> org-latex-fragment-toggle-helper (byte-compile 'org-latex-fragment-toggle-helper))
(<span class="org-keyword">setq</span> org-latex-fragment-toggle-auto (byte-compile 'org-latex-fragment-toggle-auto))
</pre>
</div>
<p>
And here is an example!
<img src="./gif/Screencast-2019-05-25T054725-0400.gif" alt="Screencast-2019-05-25T054725-0400.gif">
</p>
</div></div>
<br>
<div class="comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'ivanaf'; // Required - Replace '<example>' with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
var showComments = function() {
var button = document.getElementById('comment-button')
button.style.display = 'none'
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
};
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<button id="comment-button" onclick="showComments()">Show comments</button>
</div>
<div><div>
</div>
<div id="postamble" class="status">
<p class="author">Author: Ivan Tadeu Ferreira Antunes Filho</p>
<p class="date">Date: 2022-07-23 Sat 05:11</p>
<p class="author">Github: <a href="https://github.com/itf/">github.com/itf</a></p>
<p class="creator">Made with <a href="https://www.gnu.org/software/emacs/">Emacs</a> 27.1 (<a href="https://orgmode.org">Org</a> mode 9.3) and <a href="https://github.com/itf/org-export-head">Org export head</a> </p>
</div>
</body>
</html>