              (org-html-format-latex latex-frag processing-type info)))
         (when (and formula-link (string-match "file:\\([^]]*\\)" formula-link))
           (let ((source (org-export-file-uri (match-string 1 formula-link)))
-                (attributes (list :alt latex-frag
-                                  :class (concat "latex-fragment-"
-                                                 (if (equal "\\(" (substring latex-frag 0 2))
-                                                     "inline" "block")))))
-            (when (and (memq processing-type '(dvipng convert))
-                       (not IS-WINDOWS) ; relies on posix path
-                       (executable-find "identify"))
-              (apply #'plist-put attributes (+org-msg-img-scale-css source)))
+                (attributes (append (list :alt latex-frag
+                                          :class
+                                          (concat "latex-fragment-"
+                                                  (if (equal "\\(" (substring latex-frag 0 2))
+                                                      "inline" "block")))
+                                    (when (memq processing-type '(dvipng convert))
+                                    (+org-msg-img-scale-css source)))))
             (org-html--format-image source attributes info)))))
      (t latex-frag))))
 

              processing-type info)))
        (when (and formula-link (string-match "file:\\([^]]*\\)" formula-link))
          (let ((source (org-export-file-uri (match-string 1 formula-link))))
            (when (and (memq processing-type '(dvipng convert))
                       (not IS-WINDOWS) ; relies on posix path
                       (executable-find "identify"))
              (apply #'plist-put attributes (+org-msg-img-scale-css source)))
            (org-html--wrap-latex-environment
             (org-html--format-image source attributes info)
             info caption label)))))
     (t (org-html--wrap-latex-environment latex-frag info caption label)))))

(defun +org-msg-img-scale-css (img-uri)
  "For a given IMG-URI, use imagemagik to find its width."
  (if +org-msg-currently-exporting
      (when (and (not IS-WINDOWS)) ; relies on posix path
        (let ((with-call (and (executable-find "identify")
                              (doom-call-process "identify" "-format" "%w"
                                                 (substring img-uri 7))))) ; 7=(length "file://")
          (unless width-call
            (setq width-call (doom-call-process "file" (substring img-uri 7)))
            (setcdr width-call (replace-regexp-in-string "^.*image data, \\([0-9]+\\).*$" "\\1" (cdr width-call)))
            (setcar width-call (if (< 0 (string-to-number (cdr width-call))) 0 1)))
          (when (= (car width-call) 0)
            (list :width
                  (format "%.1fpx"
                          (/ (string-to-number (cdr width-call))
                             (plist-get org-format-latex-options :scale)))))))
    (list :style (format "transform: scale(%.3f)"
                         (/ 1.0 (plist-get org-format-latex-options :scale))))))

