From: Nicolas Goaziou <mail@nicolasgoaziou.fr>
To: Feng Shu <tumashu@163.com>
Cc: orgmode-devel <emacs-orgmode@gnu.org>
Subject: Re: (version 3) [PATCH] New feature: Use dvisvgm to preview latex formular
Date: Mon, 16 May 2016 15:18:20 +0200 [thread overview]
Message-ID: <871t52uoo3.fsf@saiph.selenimh> (raw)
In-Reply-To: <87oa86in45.fsf@163.com> (Feng Shu's message of "Mon, 16 May 2016 13:32:42 +0800")
Hello,
"Feng Shu" <tumashu@163.com> writes:
> I suggest to add (:generator my-generator-function) style configure
> to `org-latex-pdf-process', it is simple and powerful feature, we
> can use this feature to switch latex commands dynamicially, for example:
>
> (defun my-latex-pdf-process-generator (texfile snippet extra-info)
> (cond
> ((<a>) (<org-latex-pdf-process-style-command1>)
> ((<b>) (<org-latex-pdf-process-style-command2>)))))
>
> although we can set org-latex-pdf-process to a function to do the same work,
> but this function is hard to write as org-latex-compile, it may only
> useful for developer instead of user....
>
> caller-info argument is for the above feature.
I understand the need to extend `org-latex-pdf-process'. However this
isn't a good way to look at it. In fact, it makes little sense to load
the whole export framework when you're only after displaying a LaTeX
snippet in the current buffer.
I think a better approach would be to create a generic tool to compile
a file to another format, e.g. `org-compile-file'. It would take a command
(as a list of strings or a function) and apply it to the source file
name. Then it would check if the output file was produced. Optionally,
it would allow to create a buffer so as to retrieve error messages.
Then, e.g., `org-latex-compile' or `org-preview-generate-image', could
extend this function, i.e., handle specific variables (e.g.,
`org-latex-pdf-process') and call it with appropriate arguments.
As example, here's a proof of concept for `org-compile-file' and
`org-latex-compile'.
(defun org-compile-file (source process extension &optional spec log-buffer)
"Compile a SOURCE file using PROCESS.
PROCESS is either a function or a list of shell commands, as
strings.
If PROCESS is a function, it is called with a single argument:
SOURCE file. It must create a file with the same base name and
directory as SOURCE, but using extension.
If it is a list of commands, each of them is called using
`shell-command'. By default, in each command, %b, %f and %o are
replaced, respectively, with SOURCE base name, SOURCE full name
and SOURCE directory. It is possible, however, to use different
place-holders by specifying them in optional argument SPEC. In
this case, SPEC should be an alist (CHARACTER REPLACEMENT-STRING).
When PROCESS is a list of commands, optional argument LOG-BUFFER
can be set to a buffer or a buffer name. `shell-command' then
uses it as an output buffer, which may be used for collecting
errors.
`default-directory' is set to SOURCE directory during the whole
process.
Return output file or raise an error if it wasn't generated upon
executing PROCESS."
(let* ((base-name (file-name-sans-extension (file-name-nondirectory source)))
(full-name (file-truename source))
(out-dir (file-name-directory source))
;; Properly set working directory for compilation.
(default-directory (if (file-name-absolute-p source)
(file-name-directory full-name)
default-directory))
(time (current-time)))
(save-window-excursion
(pcase process
((pred functionp) (funcall process (shell-quote-argument source)))
((pred consp)
(let ((log (and log-buffer (get-buffer-create log-buffer)))
(spec (or spec
`((?b ,(shell-quote-argument base-name))
(?f ,(shell-quote-argument full-name))
(?o ,(shell-quote-argument out-dir))))))
(dolist (command process)
(shell-command (format-spec command spec) log))))
(t (error "No valid command to process %S" source)))
;; Check for process failure.
(let ((output (concat out-dire base-name extension)))
(when (or (not (file-exists-p output))
;; Only compare times up to whole seconds as some
;; file-systems (e.g. HFS+) do not retain any finer
;; granularity.
(time-less-p (cl-subseq (nth 5 (file-attributes output)) 0 2)
(cl-subseq time 0 2)))
(error (format "File %S wasn't produced" output)))
output))))
(defun org-latex-compile (texfile)
"Compile a TeX file.
TEXFILE is the name of the file being compiled. Processing is
done through the command specified in `org-latex-pdf-process'.
Return PDF file name or an error if it couldn't be produced."
(message "Processing LaTeX file %s..." texfile)
(let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
(full-name (file-truename texfile))
(compiler (or (with-temp-buffer
(save-excursion (insert-file-contents full-name))
(when (and (search-forward-regexp
(regexp-opt org-latex-compilers) (line-end-position 2) t)
(progn (beginning-of-line)
(looking-at-p "%")))
(match-string 0)))
"pdflatex"))
(out-dir (file-name-directory texfile))
(spec `((?B ,(shell-quote-argument org-latex-bib-compiler))
(?L ,(shell-quote-argument compiler))
(?b ,(shell-quote-argument base-name))
(?f ,(shell-quote-argument full-name))
(?o ,(shell-quote-argument out-dir))))
(log-buffer (get-buffer-create "*Org PDF LaTeX Output*"))
(outfile
(org-compile-file
texfile
(let ((process org-latex-pdf-process))
;; A function is provided: Apply it.
(if (functionp org-latex-pdf-process)
org-latex-pdf-process
;; A list is provided: replace %b, %f and %o with
;; appropriate values in each command before applying
;; it. Note that while "%latex" and "%bibtex" is used
;; in `org-latex-pdf-process', they are replaced with
;; "%L" and "%B" to adhere to format-spec. Output is
;; redirected to "*Org PDF LaTeX Output*" buffer.
(mapcar (lambda (command)
(replace-regexp-in-string
"%\\(latex\\|bibtex\\)\\>"
(lambda (str) (upcase (substring str 0 2)))
command))
org-latex-pdf-process)))
".pdf"
log-buffer)))
(when org-latex-remove-logfiles
(mapc #'delete-file
(directory-files
out-dir t
(concat (regexp-quote base-name)
"\\(?:\\.[0-9]+\\)?"
"\\."
(regexp-opt org-latex-logfiles-extensions)))))
(let ((warnings (get-buffer-create(org-latex--collect-warnings outbuf))))
(message (concat "PDF file produced"
(cond
((eq warnings 'error) " with errors.")
(warnings (concat " with warnings: " warnings))
(t ".")))))
;; Return output file name.
outfile))
> Remove the above two and only use the below *one* ?
>
> (make-obsolete-variable
> 'org-latex-preview-ltxpng-directory
> "Set `org-latex-preview-ltximg-directory' instead." "25.1")
I think
(make-obsolete-variable
'org-latex-preview-ltxpng-directory
'org-latex-preview-ltximg-directory "25.1")
is sufficient. The code you wrote is not wrong either, tho.
Regards,
--
Nicolas Goaziou
next prev parent reply other threads:[~2016-05-16 13:18 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-14 6:30 [PATCH] New feature: Use dvisvgm to preview latex formular Feng Shu
2016-05-14 8:47 ` numbchild
2016-05-14 14:56 ` Feng Shu
2016-05-15 2:27 ` Feng Shu
2016-05-15 6:02 ` (version 3) " Feng Shu
2016-05-15 12:14 ` Feng Shu
2016-05-15 22:32 ` Nicolas Goaziou
2016-05-16 5:32 ` Feng Shu
2016-05-16 13:18 ` Nicolas Goaziou [this message]
2016-05-16 15:06 ` tumashu
2016-05-16 5:33 ` Feng Shu
2016-05-16 13:19 ` Nicolas Goaziou
2016-05-16 12:05 ` (v6) " Feng Shu
2016-05-16 21:17 ` Nicolas Goaziou
2016-05-16 22:17 ` tumashu
2016-05-17 1:15 ` tumashu
2016-05-18 6:30 ` (V8) " Feng Shu
2016-05-19 8:22 ` Nicolas Goaziou
2016-05-19 9:18 ` tumashu
2016-05-19 10:32 ` Nicolas Goaziou
2016-05-19 14:01 ` (V9) " Feng Shu
2016-05-21 9:38 ` Nicolas Goaziou
2016-05-21 12:22 ` Feng Shu
2016-05-21 13:35 ` Rasmus
2016-05-15 12:16 ` (version 4) " Feng Shu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=871t52uoo3.fsf@saiph.selenimh \
--to=mail@nicolasgoaziou.fr \
--cc=emacs-orgmode@gnu.org \
--cc=tumashu@163.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.