新插件:Org-mode LaTeX 即时预览

windows上无法使用,劳请作者兼容一下windows

不好意思,手头没有Windows的机器,可能需要你自己研究搞定。 在Github的issue里有人分享他在Windows上的解决方案,可以参考一下

@yangsheng6810

在 Window 10测试了一下 (inline 的 $ax^{2}bx+c=0$) 生成的 tex-string 会是

\newcommand{\ensuremath}[1]{#1} \renewcommand{\usepackage}[1]{} \color{#F8F8F2}{ax^{2}bx+c=0}

已用 tex2svg 测试过 生成的将是空 svg

想知道 tex-string 的正确生成应该是 什么样?

@yangsheng6810 使用 Issue 中方法 可以 在 Window 可以使用了 ,

      (let ((tex-string (org-latex-impatient--get-tex-string))
            (latex-header
                  (concat (s-join "" org-latex-impatient-user-latex-definitions) 	;;++
                          ""						                                ;;++														
                          (org-latex-impatient--get-headers)))) 					;;++
        ;;      (concat (s-join "\n" org-latex-impatient-user-latex-definitions)
        ;;              "\n"
        ;;              (org-latex-impatient--get-headers))))
        (unless (org-latex-impatient-inhibit tex-string)
          (setq org-latex-impatient--current-window (selected-window))
          (setq org-latex-impatient--is-inline nil)
          ;; the tex string from latex-fragment includes math delimeters like
          ;; $, $$, \(\), \[\], and we need to remove them.
          (when (org-latex-impatient--need-remove-delimeters)
            (setq tex-string (org-latex-impatient--remove-math-delimeter tex-string)))

          (setq org-latex-impatient--position (org-latex-impatient--get-tex-position)
                ;; set forground color for LaTeX equations.
                tex-string (concat latex-header
                                   (org-latex-impatient--add-color tex-string)))
          (if (and org-latex-impatient--last-tex-string
                   (equal tex-string
                          org-latex-impatient--last-tex-string))
              ;; TeX string is the same, we only need to update posframe
              ;; position.
              (when (and org-latex-impatient--last-position
                         (equal org-latex-impatient--position org-latex-impatient--last-position)
                         ;; do not force showing posframe when a render
                         ;; process is running.
                         (not org-latex-impatient--process)
                         (not org-latex-impatient--force-hidden))
                (org-latex-impatient--show))
            ;; reset `-force-hidden'
            (setq org-latex-impatient--force-hidden nil)
            ;; A new rendering is needed.
            (org-latex-impatient--interrupt-rendering)

            (setq tex-string (s-replace-all '(("^" . "^^^^")) tex-string))		       ;; ++

你的修改是主要加了;;++的那几行吗?

在 Window 上要先用 (image-type-available-p 'svg) 测试 image mode 是否支持 svg

可以看一下 Windows 下 emacs 怎样显示 svg?

得说一句 有这个文件librsvg-2-2.dll不代表支持 svg ,还是要看 (image-type-available-p 'svg) 返回结果

我是重新安装了 Emacs 才支持的

文件修改只有 ;;++ 这些,但我没有对每个语句进行验证,因为是依 Issue 中的来

最后测试结果是可以显示

不过,我只测试了 inline 预览。

恩,多谢。我打算写个针对Windows的FAQ,写好了请您斧正。

感谢楼主。我的org文件有 #+SETUPFILE: ~/.foo.org 作为latex的文件头,能否让renderer跳过这一行 (有这一行则无法编译成功,latex error),比如foo.org里面有mathjax无法识别的命令。

你的foo.org里具体是有什么导致出错呢?我觉得ignore一个SETUPFILE可能不是个好主意,比如有人可能会在STUPFILE里定义一些newcommand。

楼主回复好快!是一个\input{bar.tex} 命令,latex文件头都是存在这个tex文件中,然后在其他的tex或org文件调用的。

确实感觉不应该ignore一个setupfile,一般来说只有一个setupfile,或许可以加一个参数,选择是否ignore setupfile,类似 org-latex-impatient-ignore-org-setupfile 默认值是nil,需要的就可以设t,或者类似 org-export-in-background 加上 org-export-async-init-file 那样的设定。

我还是觉得这不是个好主意,因为一个很正常的操作是在SETUPFILE里定义一些新的命令,比如\newcommand{\Z}{\mathbb{Z}}这样。而这种定义是不应该被ignore的。而你的“一般来说只有一个SETUPFILE”其实也未必成立,比如有人会用一个SETUPFILE来处理导出到HTML时的情况,一个SETUPFILE来处理导出到PDF时的情况。总结来说,我认为ignore setupfile这个做法过于粗糙。

我觉得正确的做法是找到你的bar.tex里究竟哪里导致了不兼容。我给develop分支push了正确处理带可选参数的\usepackage的commit,可以看一下能不能解决你的问题。如果你方便,能给一个minimal offending file吗?

没问题,确实,我考虑欠周。

org file 内容 #+SETUPFILE: ~/foo.org

foo.org 内容 #+LATEX_HEADER: \input{~/TeXheader.tex}

TeXheader.tex 内容 一些latex命令,不影响render

如果把org文件改成 #+SETUPFILE: ~/TeXheader.tex 就可以预览了,也就是说,不能识别的是input命令。

那必然不可能支持input命令……这个涉及的东西太多。你可以考虑修改org-latex-impatient-user-latex-definitions,参照里面的内容给list里加一项"\\renewcommand{\\input}[1]{}",这相当于在生成preview的时候直接忽略input命令。

謝謝,問題解決!應該好好讀github上的文檔的。

(setq org-latex-impatient-user-latex-definitions '(“\newcommand{\ensuremath}[1]{#1}” “\renewcommand{\usepackage}[1]{}” “\renewcommand{\input}[1]{}”))

我再多写点文档吧

windows还有没有救了。

我现在没有Windows的机子,没办法测试。也不清楚现在Windows上Emacs的正确食用方法。你看看issue里有没有帮助吧,有人成功了

特别想在eww里实现这种预览,请教有什么思路吗

更喜欢直接显示图片在orgmode中,今天研究了下latex转png,终于可以在windows中显示公式了。

没用实时预览,在src code block中写好公式后,C-c C-c 输出图片,添加到后面的result结果中。

缺点是占用空间比较大,也没法写行内公式。

可以设置 #+STARTUP: hideblocks 默认折叠代码块。

1 个赞

为啥不用自带的 org-latex-preview :thinking: 还能默认支持行内公式