spacemacs org中得latex fragment里面输入中文preview并不正确

\begin{equation}
\sum\limits_{i=1}^n(单项评分_i * 权重)
\end{equation}

这是我几年前的例子,当时可以用。 现在显示的是一些特殊符号了。

不知道问题出在哪里。

我是Ubuntu 16.04

你的 latex 写的就有问题哦

试试这个:

\begin{equation}
\sum\limits_{i=1}^n(\text{单项评分}_i * \text{权重})
\end{equation}

image

不行再找 spacemacs/orgmode 配置的毛病,我是在 Windows + miktex + spacemacs 条件下测试的上述代码

1 个赞

还是不行, 关键就是不知道怎么配置才能修复这个问题。

Screenshot from 2018-07-27 16-19-39

latex 的 math mode 本来就是不能显示 CJK 字体的, 需要引入 xeCJK 这些处理中文的包, 可能你需要添加 #+latex_header

楼主可以参考我的配置:

(setq org-format-latex-header (concat "\\PassOptionsToPackage{normalem}{ulem}\n" org-format-latex-header))
(setq org-latex-default-packages-alist '(("T1" "fontenc" t ("pdflatex"))
                                         ("" "graphicx" t)
                                         ("" "grffile" t)
                                         ("" "longtable" nil)
                                         ("" "wrapfig" nil)
                                         ("" "rotating" nil)
                                         ("normalem" "ulem" t)
                                         ("" "amsmath" t)
                                         ("" "textcomp" t)
                                         ("" "amssymb" t)
                                         ("" "capt-of" nil)
                                         ("" "hyperref" nil)))
(setq org-latex-packages-alist '(("fontset=windowsnew,UTF8" "ctex" t)
                                 ("range-phrase=-,range-units=single" "siunitx" t)
                                 ("" "upgreek" t)))

以及

;; preview latex settings
(setq org-preview-latex-default-process 'imagemagick)
1 个赞

仍然没有搞定

先抛开org,你能自己弄个可以正常生成pdf的latex文件吗?这里边的设置是org需要的

放假无聊配置了下,下面过程与配置供参考

就像楼上说的你首先要先清楚如何用 Latex 生成带中文的 PDF,推荐用 ctex 宏包,

#+LaTeX_HEADER: \usepackage[UTF8]{ctex}

或者

(setq org-latex-packages-alist
             '(("fontset=macnew,UTF8" "ctex" t)))

然后再生成图片的时候就会提示 org-compile-file: File "/var/folders/_q/t4z901gd5nz2syx8nc8rkt9m0000gq/T/orgtexg15boA.dvi" wasn’t produced. Please adjust ‘dvisvgm’ part of ‘org-preview-latex-process-alist’.

查看 org-preview-latex-process-alist 的值,发现是默认用的 latex or pdflatex。用 pdflatex 编译的话会提示 CTeX fontset 'mac' is unavailable in current,具体详见 CTEX 宏集手册,字体原因,我也不是很熟悉,懒得折腾了,换成 xelatex。

然后就根据上面目录里的临时 tex 文件(orgtexg15boA.tex)调整命令直到生成图片,就把 org-preview-latex-process-alist 改好了

  (setq org-preview-latex-default-process 'dvisvgm)
  (setq org-preview-latex-process-alist
        '((dvisvgm :programs
                   ("xelatex" "dvisvgm")
                   :description "xdv > svg" :message "you need to install the programs: xelatex and dvisvgm." :use-xcolor t :image-input-type "xdv" :image-output-type "svg" :image-size-adjust
                   (1.7 . 1.5)
                   :latex-compiler
                   ("xelatex -no-pdf -interaction nonstopmode -output-directory %o %f")
                   :image-converter
                   ("dvisvgm %f -n -b min -c %S -o %O"))
          (imagemagick :programs
                       ("xelatex" "convert")
                       :description "pdf > png" :message "you need to install the programs: xelatex and imagemagick." :use-xcolor t :image-input-type "pdf" :image-output-type "png" :image-size-adjust
                       (1.0 . 1.0)
                       :latex-compiler
                       ("xelatex -interaction nonstopmode -output-directory %o %f")
                       :image-converter
                       ("convert -density %D -trim -antialias %f -quality 100 %O"))))

另 dvipng 不支持 xelatex 生成的 xdv,加上图片也不清楚,不推荐用。以及没装 imagemagick,我只是简单改了下,没测试。

7 个赞

你好,我使用了你的配置。但是出现了和你相似又不同的错误

DVI error: DVI format 6 not supported File mode specification error: (error File “/tmp/orgtex3AETPu.svg” wasn’t produced. Please adjust ‘dvisvgm’ part of ‘org-preview-latex-process-alist’.)

而且现在英文latex也无法生成了

说的很清楚 svg 没有生成,就是第二步转图片出错了啊,你到 /tmp 目录找到 orgtex3AETPu.xdv 尝试用命令转看报错啊,用 Emacs 要有基本的调试能力?

再多说两句吧,你如果仔细看了 org-preview-latex-process-alist 中的内容就知道,转换分为两步,先编译成中间格式,然后再转换成图片,分别对应的命令可能需要根据具体环境改改。

谢谢, 配置完美使用!

我发现是我的ubuntu 16.04安装的dvisvgm不支持DVI format 6. 尝试寻找升级方法,但没有成功。 几个月后,找了一个有空的时间,将ubuntu 16.04升级为18.04。 然后再使用你提供的配置。不报错了。但是中文完全不显示了。 在此基础上,采用了最开始上面一位朋友的latex写法,现在终于搞定了。

\begin{equation}
\sum\limits_{i=1}^n(\text{单项评分}_i * \text{权重})
\end{equation}
1 个赞

因为mac版 emacs, swf 在 org mode 里没法作为图片预览,所有在上面同学的基础上修改了下 imagemagick版本,配置如下:

 (setq org-latex-packages-alist
      '(("fontset=macnew,UTF8" "ctex" t)))

 (setq org-preview-latex-default-process 'imagemagick)

  (setq org-preview-latex-process-alist
  '(
    (dvisvgm
     :programs ("xelatex" "dvisvgm")
     :description "xdv > svg"
     :message "you need to install the programs: xelatex and dvisvgm."
     :image-input-type "xdv"
     :image-output-type "svg"
     :image-size-adjust (1.7 . 1.5)
     :latex-compiler ("xelatex --no-pdf -interaction nonstopmode -output-directory %o %f")
     :image-converter ("dvisvgm %f -n -b min -c %S -o %O"))
    (imagemagick
     :programs ("latex" "convert")
     :description "pdf > png"
     :message "you need to install the programs: xelatex and imagemagick."
     :image-input-type "pdf"
     :image-output-type "png"
     :image-size-adjust (1.0 . 1.0)
     :latex-compiler ("xelatex -interaction nonstopmode -output-directory %o %f")
     :image-converter
     ("convert -density %D -trim -antialias %f -quality 100 %O"))))

)

效果图:

另外,在 linux 不要单独 apt-get install xelatex, 这样会缺少相关的 cert 文件,最好还是安装所有latex工具

这个东西我总算彻底搞懂了,最坑的地方是他报错只有一种错误信息,让你误以为总是dvi输出出错.

实际上这里的错误是很多方面的,你需要自己运行这段指令的每一个环节逐个排除各种错误.

由于我装的是texlive套件,其他人遇到的和程序对接不上导致的错误我没遇到过.

但我至少遇到了以下四种错误

  • 1pdflatex不兼容中文出错
  • 2中文字体找不到出错
  • 3dvipng不兼容xelatex的xdv出错
  • 4imagemagick 安全策略出错

如果你在linux上成功编译过latex文档,那么上面的1和2错误就不会犯,但是3和4还是可能会错.

下面我来全面讲一讲他们为什么错.

首先肯定不能用原本的pdflatex编译器,因为他不支持中文字符,会报错 所以要用xelatex.

但是你用了xelatex,编译器又不会找中文字体,需要你手动给它指定你想要渲染的中文字体.

所以你需要给模板最终生成的tex文件里加上两句代码

\usepackage{fontspec}
\setmainfont{Noto Serif CJK SC}

setmainfont中的参数是你系统中存在的中文字体.

其他的ctex,xeCJK都不需要,只要这两条,加多了影响运行速度,特别是ctex.

把他们加到最终tex里的方案也有两种

一种是永久的

  • 先从org-mode给的API加\usepackage{fontspec}
(setq org-latex-packages-alist
             '(("" "fontspec" t)))
  • 再从模板文件fullpage.sty里追加一行
\setmainfont{Noto Serif CJK SC}

另一种是临时的,

  • 把代码直接加到org文件头部
#+LaTeX_HEADER: \usepackage{fontspec}
#+LaTeX_HEADER: \setmainfont{Noto Serif CJK SC}

加了上面的内容后,再用上面的人写的

  (setq org-preview-latex-default-process 'dvisvgm)
  (setq org-preview-latex-process-alist
        '((dvisvgm :programs
                   ("xelatex" "dvisvgm")
                   :description "xdv > svg" :message "you need to install the programs: xelatex and dvisvgm." :use-xcolor t :image-input-type "xdv" :image-output-type "svg" :image-size-adjust
                   (1.7 . 1.5)
                   :latex-compiler
                   ("xelatex -no-pdf -interaction nonstopmode -output-directory %o %f")
                   :image-converter
                   ("dvisvgm %f -n -b min -c %S -o %O"))
          (imagemagick :programs
                       ("xelatex" "convert")
                       :description "pdf > png" :message "you need to install the programs: xelatex and imagemagick." :use-xcolor t :image-input-type "pdf" :image-output-type "png" :image-size-adjust
                       (1.0 . 1.0)
                       :latex-compiler
                       ("xelatex -interaction nonstopmode -output-directory %o %f")
                       :image-converter
                       ("convert -density %D -trim -antialias %f -quality 100 %O"))))

就可以了.

如果你想在latex fragment里用中文,只能用这一种由xelatex->dvisvgm 的 dvi>svg的模式

其他的模式如

xelatex->dvipng,

他们的中间文件版本不兼容,会报错

还有xelatex->imagemagick

由于imagemagick的安全策略禁止了ghostscript执行,也会导出png不成功而报错.

所以你只能用这一种.

1 个赞

我最近碰到一个问题,把解决方案发这里. 我复制安装了windows font到ubuntu, 然后在emacs中通过cnfonts-ui设置了中文字体为STKaiti 这个时候,我的org文档中文就很漂亮了.但是latext公式里面的中文无法预览了. 在.spacemacs中修改这一行的字体,又恢复了.

  (setq org-latex-packages-alist
        '(("fontset=STKaiti,UTF8" "ctex" t))) ;;macnew