<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>Better equation numbering in LaTeX fragments in org-mode</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2016/11/07/Better-equation-numbering-in-LaTeX-fragments-in-org-mode</link>
      <pubDate>Mon, 07 Nov 2016 07:02:19 EST</pubDate>
      <category><![CDATA[orgmode]]></category>
      <category><![CDATA[latex]]></category>
      <category><![CDATA[emacs]]></category>
      <guid isPermaLink="false">sV0Pj44wa6HIr8UdE-ygikpo0Oo=</guid>
      <description>Better equation numbering in LaTeX fragments in org-mode</description>
      <content:encoded><![CDATA[


&lt;p&gt;
In org-mode we can use LaTeX equations, and toggle an overlay that shows what the rendered equation will look like. One thing that has always bothered me though, is that each fragment is created in isolation. That means numbering is almost always wrong, and typically with each numbered equation starting with (1). Here we look at a way to fix that. Fixing it means we have to find a way to not create each fragment image in isolation; each one needs a context that enables the numbering to be correct. The idea we try here is simple: we just figure out in advance what the numbering for each equation should be, and then figure out how to get that information to the image generation.
&lt;/p&gt;

&lt;p&gt;
See this video of the post in action:
&lt;/p&gt;
&lt;iframe width="560" height="315" src="https://www.youtube.com/embed/pcMuJlUvKCw" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;p&gt;
Here are some example equations to see how it works.
&lt;/p&gt;

&lt;p&gt;
This should be numbered (1)
&lt;/p&gt;
\begin{equation}
\int x^2 dx
\end{equation}

&lt;p&gt;
This is a numbered equation with a custom number. This should have (E1) as the number.
&lt;/p&gt;
\begin{equation}\tag{E1}
\int x^2 dx
\end{equation}

&lt;p&gt;
But equation* is not numbered
&lt;/p&gt;
\begin{equation*}
\int x^2 dx
\end{equation*}

&lt;p&gt;
LaTeX align environments are numbered. The first line is (2), the second line is not numbered (because we put &lt;code&gt;\nonumber&lt;/code&gt; in the line), and the third line is (3).
&lt;/p&gt;
\begin{align}
a = 5 \\
b=6 \nonumber \\
c = 8
\end{align}

&lt;p&gt;
But align* environments are not numbered.
&lt;/p&gt;
\begin{align*}
a = 5 \\
b=6
\end{align*}

&lt;p&gt;
This should be numbered (4).
&lt;/p&gt;

\begin{equation}
\int x^3 dx
\end{equation}

&lt;p&gt;
These should be numbered (5), (6) and (7).
&lt;/p&gt;
\begin{align}
a = 5 \\
b=6  \\
c = 8
\end{align}

&lt;p&gt;
This should be numbered with (E2).
&lt;/p&gt;
\begin{equation}\tag{E2}
\int x^2 dx 
\end{equation}

&lt;p&gt;
And this should be numbered (8).
&lt;/p&gt;
\begin{equation}
\int x^2 dx 
\end{equation}

&lt;p&gt;
Note: This will be numbered (1) because it is exactly the same equation as a previous one! 
&lt;/p&gt;
\begin{equation}
\int x^2 dx
\end{equation}


&lt;p&gt;
We can change the numbering of an equation with code like this. After this code, the next equation will be numbered (5).
&lt;/p&gt;

&lt;p&gt;
The only fragments that should be numbered are equation environments, and align environments (these are the main ones that we consider here). The align environment is tricky since there is potentially more than one number in the environment. 
&lt;/p&gt;

&lt;p&gt;
So, we get all the fragments, and generate a list of which ones should be numbered, and if they should what the number should be. That means we will need to count the number of numbered equations in an align environment. We will do that by getting the number of line breaks, and subtracting the number of nonumbers.
&lt;/p&gt;

&lt;p&gt;
Here is the code block that does that, using advice again. A downside of this approach is that we generate the list for every fragment, which is not efficient, since it should not change in a synchronous approach to generating them.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;org-renumber-environment&lt;/span&gt; (orig-func &lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; args)
  (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((results '()) 
        (counter -1)
        (numberp))

    (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; results (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for (begin .  env) in 
                        (org-element-map (org-element-parse-buffer) 'latex-environment
                          (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (env)
                            (cons
                             (org-element-property &lt;span style="color: #006FE0;"&gt;:begin&lt;/span&gt; env)
                             (org-element-property &lt;span style="color: #006FE0;"&gt;:value&lt;/span&gt; env))))
                        collect
                        (&lt;span style="color: #0000FF;"&gt;cond&lt;/span&gt;
                         ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (string-match &lt;span style="color: #008000;"&gt;"\\\\begin{equation}"&lt;/span&gt; env)
                               (not (string-match &lt;span style="color: #008000;"&gt;"\\\\tag{"&lt;/span&gt; env)))
                          (&lt;span style="color: #0000FF;"&gt;incf&lt;/span&gt; counter)
                          (cons begin counter))
                         ((string-match &lt;span style="color: #008000;"&gt;"\\\\begin{align}"&lt;/span&gt; env)
                          (&lt;span style="color: #0000FF;"&gt;prog2&lt;/span&gt;
                              (&lt;span style="color: #0000FF;"&gt;incf&lt;/span&gt; counter)
                              (cons begin counter)                          
                            (&lt;span style="color: #0000FF;"&gt;with-temp-buffer&lt;/span&gt;
                              (insert env)
                              (goto-char (point-min))
                              &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;\\ is used for a new line. Each one leads to a number&lt;/span&gt;
                              (&lt;span style="color: #0000FF;"&gt;incf&lt;/span&gt; counter (count-matches &lt;span style="color: #008000;"&gt;"\\\\$"&lt;/span&gt;))
                              &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;unless there are nonumbers.&lt;/span&gt;
                              (goto-char (point-min))
                              (&lt;span style="color: #0000FF;"&gt;decf&lt;/span&gt; counter (count-matches &lt;span style="color: #008000;"&gt;"\\nonumber"&lt;/span&gt;)))))
                         (t
                          (cons begin nil)))))

    (&lt;span style="color: #0000FF;"&gt;when&lt;/span&gt; (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; numberp (cdr (assoc (point) results)))
      (&lt;span style="color: #0000FF;"&gt;setf&lt;/span&gt; (car args)
            (concat
             (format &lt;span style="color: #008000;"&gt;"\\setcounter{equation}{%s}\n"&lt;/span&gt; numberp)
             (car args)))))
  
  (apply orig-func args))

(advice-add 'org-create-formula-image &lt;span style="color: #006FE0;"&gt;:around&lt;/span&gt; #'org-renumber-environment)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
You can remove the advice like this.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(advice-remove 'org-create-formula-image #'org-renumber-environment)
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2016 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2016/11/07/Better-equation-numbering-in-LaTeX-fragments-in-org-mode.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.0&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>Justifying LaTeX preview fragments in org-mode</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2016/11/06/Justifying-LaTeX-preview-fragments-in-org-mode</link>
      <pubDate>Sun, 06 Nov 2016 20:44:53 EST</pubDate>
      <category><![CDATA[orgmode]]></category>
      <category><![CDATA[latex]]></category>
      <category><![CDATA[emacs]]></category>
      <guid isPermaLink="false">hjotOh9E_Hff5mx_bipGH2If-Mo=</guid>
      <description>Justifying LaTeX preview fragments in org-mode</description>
      <content:encoded><![CDATA[


&lt;p&gt;
A colleague asked if I knew how to center the preview images of LaTeX equations in an org-buffer. This might make org-mode notes look nicer when lecturing, for example. We thought it might be possible to just offset the overlay with a before-string made up of the right number of spaces. I worked out a full solution that lets you "justify" the preview images. You have to add a :justify option to org-format-latex-options, and the option is either 'center or 'right (anything else means left-justified as the default). This will only justify equations that start at the beginning of a line to avoid modifying fragments that are in text. You should see the video to see this in action:
&lt;/p&gt;

&lt;iframe width="560" height="315" src="https://www.youtube.com/embed/nA9YzooqpWY" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;p&gt;
Equation 1:
\(e^{i\pi} + 1 = 0\)
&lt;/p&gt;

&lt;p&gt;
An \(x^2 = -1\) equation in the text is not affected.
&lt;/p&gt;

&lt;p&gt;
A display equation with some space after the equation:
\[e^{i \cdot \pi} + 1 = 0\]     
&lt;/p&gt;

&lt;p&gt;
This is a numbered equation.
&lt;/p&gt;

\begin{equation}
\int x^2 dx
\end{equation}

&lt;p&gt;
The idea is pretty simple, we get the width of the window, and the width of the image, and compute the offset that approximately centers or right justifies the overlay, and then add the before-string property to the overlay. While we are at it, I will add a tooltip to the image so you can see the LaTeX code that created it, and make it clickable so you can toggle it back to the code. 
I apply the functions as after advice to the function that creates the overlay, so we do not have to adapt the org code at all. Here is the code that does it.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;&lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;specify the justification you want&lt;/span&gt;
(plist-put org-format-latex-options &lt;span style="color: #006FE0;"&gt;:justify&lt;/span&gt; 'center)

(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;org-justify-fragment-overlay&lt;/span&gt; (beg end image imagetype)
  &lt;span style="color: #036A07;"&gt;"Adjust the justification of a LaTeX fragment.&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;The justification is set by :justify in&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;`&lt;/span&gt;&lt;span style="color: #D0372D;"&gt;org-format-latex-options&lt;/span&gt;&lt;span style="color: #036A07;"&gt;'. Only equations at the beginning of a&lt;/span&gt;
&lt;span style="color: #036A07;"&gt;line are justified."&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;cond&lt;/span&gt;
   &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;Centered justification&lt;/span&gt;
   ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (eq 'center (plist-get org-format-latex-options &lt;span style="color: #006FE0;"&gt;:justify&lt;/span&gt;)) 
         (= beg (line-beginning-position)))
    (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((img (create-image image 'imagemagick t))
           (width (car (image-size img)))
           (offset (floor (- (/ (window-text-width) 2) (/ width 2)))))
      (overlay-put (ov-at) 'before-string (make-string offset ? ))))
   &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;Right justification&lt;/span&gt;
   ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (eq 'right (plist-get org-format-latex-options &lt;span style="color: #006FE0;"&gt;:justify&lt;/span&gt;)) 
         (= beg (line-beginning-position)))
    (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((img (create-image image 'imagemagick t))
           (width (car (image-display-size (overlay-get (ov-at) 'display))))
           (offset (floor (- (window-text-width) width (- (line-end-position) end)))))
      (overlay-put (ov-at) 'before-string (make-string offset ? ))))))

(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;org-latex-fragment-tooltip&lt;/span&gt; (beg end image imagetype)
  &lt;span style="color: #036A07;"&gt;"Add the fragment tooltip to the overlay and set click function to toggle it."&lt;/span&gt;
  (overlay-put (ov-at) 'help-echo
               (concat (buffer-substring beg end)
                       &lt;span style="color: #008000;"&gt;"mouse-1 to toggle."&lt;/span&gt;))
  (overlay-put (ov-at) 'local-map (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((map (make-sparse-keymap)))
                                    (define-key map [mouse-1]
                                      `(&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; ()
                                         (&lt;span style="color: #0000FF;"&gt;interactive&lt;/span&gt;)
                                         (org-remove-latex-fragment-image-overlays ,beg ,end)))
                                    map)))

&lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;advise the function to a&lt;/span&gt;
(advice-add 'org--format-latex-make-overlay &lt;span style="color: #006FE0;"&gt;:after&lt;/span&gt; 'org-justify-fragment-overlay)
(advice-add 'org--format-latex-make-overlay &lt;span style="color: #006FE0;"&gt;:after&lt;/span&gt; 'org-latex-fragment-tooltip)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
That is it. If you get tired of the advice, remove it like this:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(advice-remove 'org--format-latex-make-overlay 'org-justify-fragment-overlay)
(advice-remove 'org--format-latex-make-overlay 'org-latex-fragment-tooltip)
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2016 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2016/11/06/Justifying-LaTeX-preview-fragments-in-org-mode.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.0&lt;/p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
