1

The default font weight after rendered for Latex fragment is too thin compared to the body text. Is there any way to adjust this?

It seems org-format-latex-options does not have this option, I am newbie to Latex, tried to set org-format-latex-header but failed...

2

I think an easier approach is through an advice. Here we use an around advice to modify the string that gets used to make the fragment, and then calling the original function with the new string. It allows you to put new latex before and after the equation if you want. Just make sure the code has a \n at the end like this:

(plist-put org-format-latex-options :latex-fragment-pre-body "\\mathversion{bold}\n")



(defun org-inject-latex-fragment (orig-func &rest args)
  "Advice function to inject latex code before and/or after the equation in a latex fragment.
You can use this to set \\mathversion{bold} for example to make it bolder."
  (setf (car args)
    (concat
     (or (plist-get org-format-latex-options :latex-fragment-pre-body) "")
     (car args)
     (or (plist-get org-format-latex-options :latex-fragment-post-body) "")))
  (apply orig-func args))

(advice-add 'org-create-formula-image :around #'org-inject-latex-fragment )
  • Brilliant! This approach is much more elegant and powerful. – Yuji Oct 12 '18 at 20:47
2

It looks like the way to do this in LaTeX is to add this in the document body:

\mathversion{bold}

It doesn't look there is a way to do this out of the box with org, the document template is hard-coded in org-create-formula image. You can modify that function like this to achieve what you want.

You can put this in your init file.

(defun org-create-formula-image
    (string tofile options buffer &optional processing-type)
  "Create an image from LaTeX source using external processes.

The LaTeX STRING is saved to a temporary LaTeX file, then
converted to an image file by process PROCESSING-TYPE defined in
`org-preview-latex-process-alist'.  A nil value defaults to
`org-preview-latex-default-process'.

The generated image file is eventually moved to TOFILE.

The OPTIONS argument controls the size, foreground color and
background color of the generated image.

When BUFFER non-nil, this function is used for LaTeX previewing.
Otherwise, it is used to deal with LaTeX snippets showed in
a HTML file."
  (let* ((processing-type (or processing-type
                  org-preview-latex-default-process))
     (processing-info
      (cdr (assq processing-type org-preview-latex-process-alist)))
     (programs (plist-get processing-info :programs))
     (error-message (or (plist-get processing-info :message) ""))
     (use-xcolor (plist-get processing-info :use-xcolor))
     (image-input-type (plist-get processing-info :image-input-type))
     (image-output-type (plist-get processing-info :image-output-type))
     (post-clean (or (plist-get processing-info :post-clean)
             '(".dvi" ".xdv" ".pdf" ".tex" ".aux" ".log"
               ".svg" ".png" ".jpg" ".jpeg" ".out")))
     (latex-header
      (or (plist-get processing-info :latex-header)
          (org-latex-make-preamble
           (org-export-get-environment (org-export-get-backend 'latex))
           org-format-latex-header
           'snippet)))
     (latex-compiler (plist-get processing-info :latex-compiler))
     (image-converter (plist-get processing-info :image-converter))
     (tmpdir temporary-file-directory)
     (texfilebase (make-temp-name
               (expand-file-name "orgtex" tmpdir)))
     (texfile (concat texfilebase ".tex"))
     (image-size-adjust (or (plist-get processing-info :image-size-adjust)
                '(1.0 . 1.0)))
     (scale (* (if buffer (car image-size-adjust) (cdr image-size-adjust))
           (or (plist-get options (if buffer :scale :html-scale)) 1.0)))
     (dpi (* scale (if buffer (org--get-display-dpi) 140.0)))
     (fg (or (plist-get options (if buffer :foreground :html-foreground))
         "Black"))
     (bg (or (plist-get options (if buffer :background :html-background))
         "Transparent"))
     (log-buf (get-buffer-create "*Org Preview LaTeX Output*"))
     (resize-mini-windows nil)) ;Fix Emacs flicker when creating image.
    (dolist (program programs)
      (org-check-external-command program error-message))
    (if use-xcolor
    (progn (if (eq fg 'default)
           (setq fg (org-latex-color :foreground))
         (setq fg (org-latex-color-format fg)))
           (if (eq bg 'default)
           (setq bg (org-latex-color :background))
         (setq bg (org-latex-color-format
               (if (string= bg "Transparent") "white" bg))))
           (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"
             (or (plist-get options :latex-body-options) "")
             string
             "\n}\n"
             "\n\\end{document}\n")))
      (if (eq fg 'default)
      (setq fg (org-dvipng-color :foreground))
    (unless (string= fg "Transparent")
      (setq fg (org-dvipng-color-format fg))))
      (if (eq bg 'default)
      (setq bg (org-dvipng-color :background))
    (unless (string= bg "Transparent")
      (setq bg (org-dvipng-color-format bg))))
      (with-temp-file texfile
    (insert latex-header)
    (insert "\n\\begin{document}\n"
        (or (plist-get options :latex-body-options) "")
        string
        "\n\\end{document}\n")))

    (let* ((err-msg (format "Please adjust `%s' part of \
`org-preview-latex-process-alist'."
                processing-type))
       (image-input-file
        (org-compile-file
         texfile latex-compiler image-input-type err-msg log-buf))
       (image-output-file
        (org-compile-file
         image-input-file image-converter image-output-type err-msg log-buf
         `((?F . ,(shell-quote-argument fg))
           (?B . ,(shell-quote-argument bg))
           (?D . ,(shell-quote-argument (format "%s" dpi)))
           (?S . ,(shell-quote-argument (format "%s" (/ dpi 140.0))))))))
      (copy-file image-output-file tofile 'replace)
      (dolist (e post-clean)
    (when (file-exists-p (concat texfilebase e))
      (delete-file (concat texfilebase e))))
      image-output-file)))

Then, you can specify the latex-body-options in your init file with

(plist-put org-format-latex-options :latex-body-options "\\mathversion{bold}\n")

That may get you what you want. You may also want to use a different font, in which case you can either add it to org-latex-default-packages-alist, e.g.

(add-to-list 'org-latex-default-packages-alist '("" "fourier" t) t)

or you can add it to the latex header. Below I set the font to be fourier. I had to add it last, maybe because I have some other font package

(setq org-format-latex-header "\\documentclass{article}\n\\usepackage[usenames]{color}\n[PACKAGES]\n[DEFAULT-PACKAGES]\n\\usepackage{fourier}\n\\pagestyle{empty}
% do not remove\n% The settings below are copied from fullpage.sty\n\\setlength{\\textwidth}{\\paperwidth}\n\\addtolength{\\textwidth}{-3cm}\n\\setlength{\\oddsidemargin}{1.5cm}\n\\addtolength{\\oddsidemargin}{-2.54cm}\n\\setlength{\\evensidemargin}{\\oddsidemargin}\n\\setlength{\\textheight}{\\paperheight}\n\\addtolength{\\textheight}{-\\headheight}\n\\addtolength{\\textheight}{-\\headsep}\n\\addtolength{\\textheight}{-\\footskip}\n\\addtolength{\\textheight}{-3cm}\n\\setlength{\\topmargin}{1.5cm}\n\\addtolength{\\topmargin}{-2.54cm}")
  • Thanks! I prefer your suggestion to change a font and I found fourier works well. Could you elaborate on the solution org-latex-default-packages-alist? I searched about it but don't know how to write the options. – Yuji Oct 12 '18 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.