2

Let's say I am working with "file_name.org" file. When I use C-c C-x C-l to generate a LaTeX preview, it creates a png image and store in ltximg/ directory by default.

I would store the preview images per file. So I want C-c C-x C-l to create a directory (if there isn't one) ltximg/file_name/ and store the image there.

I know that I have to manipulate org-preview-latex-image-directory variable, but I am not sure how. Could someone help me with this?

Update

I defined the following function. This works changes the org-preview-latex-image-directory whenever an org file is opened. So if I am working with multiple org files, it gets confused. So I am trying to write a wrapper for org-toggle-latex-fragment function so that whenever org-toggle-latex-fragment is called, latex-image-directory function is called. But I am having some trouble implementing the argument, e.g., C-u C-c C-x C-l.

(defun latex-image-directory ()
  (require 'cl-lib)
  (setq file-name (nth 0 (last (split-string (buffer-file-name) "/"))))
  ;; slice the last 4 characters: ".org"
  (setq org-preview-latex-image-directory (concat "ltximg/" 
(cl-subseq file-name 0 -4) "/")) 
)

2 Answers 2

2

There are a couple of things you could do I think. First, you could put a local file variable into your org-files. For example if you put this into an org-file file

# Local Variables:
# org-preview-latex-image-directory: "ltximg/2018-11-11/"
# End:

Then, all latex fragments for that file will go in there. If you want it to be dynamic and change per file, you could define a function like you described. Here is a version that uses builtin functions to do it. You would need to put this function into an init file.

(defun latex-image-directory ()
  "Return directory name to save Latex preview images in."
  (let ((file-name (file-name-nondirectory (buffer-file-name))))
    (concat
     (file-name-as-directory "ltximg")
     (file-name-as-directory
      (file-name-sans-extension file-name)))))

Then, you can do one of these. To make it just in one file:

# Local Variables:
# eval: (setq-local org-preview-latex-image-directory (latex-image-directory))
# End:

I would use setq-local here because I think that makes the value buffer local so it won't affect other files. Since you want different values for different files, it would be a mistake to set it globally.

To make it on every file, you could use an org-mode-hook function that runs when you open an org-file:

(defun modify-preview-dir ()
 (setq-local org-preview-latex-image-directory (latex-image-directory)))

(add-hook 'org-mode-hook 'modify-preview-dir)

Since this is just modifying a variable, you should not need to handle any prefix args, e.g. C-u C-c C-x C-l, and it should not be necessary to wrap or advise any functions.

0

Not sure if it would work, but it is worth to try...

Thanks to your hint, I search the variable org-preview-latex-image-directory using C-h v. And see that it is defined on org.el. It should be a string.

Use M-x customize-variable to search org-preview-latex-image-directory. Click in the STATE button, and select "Show saved lisp expression".

I would change the lisp expression to (concat "ltximg/" (buffer-file-name))

1
  • Thanks for the suggestion. I made some progress. Please see my updated post.
    – zcadqe
    Oct 12, 2018 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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