Orgmode导出PDF显示不了中文

inputenc不用在中文上,会报错的。你在latex里编译测试下就知道了。

分享下我的做法,我是通过 brew cask install basictex 安装的 latex,然后安装 CJK,最后配置下就好了:

// 1. 安装 cjk 包
sudo tlmgr install cjk

// 2. 在 init.el 里面激活 cjk
(add-to-list 'org-latex-packages-alist '("" "CJKutf8" t))

// 3. 在 org 文档再次声明
#+LaTeX_HEADER: \usepackage{CJK}
#+LaTeX_HEADER: \begin{CJK}{UTF8}{gbsn}

其他方式:

给楼上的提个醒:

latex的CJK包,要把中文放在\begin … \end 的环境里。你的例子只有begin没有end,是不行的。在org里用 ctex包就不会有这种尴尬的情况。 (毕竟,在中文支持方面,ctex提供了比CJK更全面的解决方案。)

另外,CJK包是要配搭pdfLatex的,然而大家更爱xelatex

多谢提醒,我上面给出的 freizl.github.io 链接里有说这种方式的弊端,虽然没有 end 快,但是 pdf 还是能正确生成的。 不过今天发现 pdf 的左侧导航还是有中文乱码, 回来再试试 ctex

其实,我觉得CJK包的好处是灵活机动,因为它只会对指定环境里的字符进行必要的处理,而不影响文档的其他部分。

但是,在org header加设置是不够明确的,org的自动处理让CJK环境开头和结尾标记的地方难以控制。例如,有时CJK环境里除了有中文还有其它语言,又或者CJK环境里还有其它环境…… 这样反而容易产生混乱。

所以,还是要像那个博客里说的, 最好在中文字符的前后手动加入#+LaTeX: \begin{CJK}{UTF8}{gbsn}#+LaTex: \end{CJK}

我比较满意的解法,参看这个链接:Latex编译中中文问题 - #8,来自 wztdream

org-mode中文导出latex解决方法

请问,在emacs中,第二个设置该如何写?

安装了org2ctex,认真按readme做了,:slight_smile:

但可能因为我是linux系统,中文还是没出来,感觉就差这一步了:找不到字体:

错误信息:

Org2ctex mode is enabled, force *override*:org-latex-default-class, org-latex-classes, org-la
Loading /home/gxy/.emacs.d/elisp/emacs-nes/nes.el (source)...done                             
Loading /home/gxy/.emacs.d/.local/jingyun.el (source)...done                                  
Add pyim dict "Basedict-elpa" to ‘pyim-extra-dicts’.                                          
PYIM: ‘pyim-isearch-mode’ 已经激活,激活后,一些 isearch 扩展包有可能失效。                   
Reading bookmarks from: ~/.emacs.d/bm-repository                                              
For information about GNU Emacs and the GNU system, type C-h C-a.                             
org-latex-chinese: Emacs can’t find an available (mainfont) font for latex, ignore!           
org-latex-chinese: Emacs can’t find an available (CJKmainfont-italic) font for latex, ignore! 
org-latex-chinese: Emacs can’t find an available (CJKmainfont) font for latex, ignore!        
org-latex-chinese: Emacs can’t find an available (CJKsansfont) font for latex, ignore!        
org-latex-chinese: Emacs can’t find an available (CJKmonofont) font for latex, ignore!        
org-latex-chinese: Emacs can’t find an available (mainfont) font for latex, ignore!           
org-latex-chinese: Emacs can’t find an available (CJKmainfont-italic) font for latex, ignore! 
org-latex-chinese: Emacs can’t find an available (CJKmainfont) font for latex, ignore!        
org-latex-chinese: Emacs can’t find an available (CJKsansfont) font for latex, ignore!        
org-latex-chinese: Emacs can’t find an available (CJKmonofont) font for latex, ignore!        
org-babel-exp process json at position 1089...                                                
org-babel-exp process json at position 2832...                                                
org-babel-exp process json at position 3222...                                                
Saving file /fusion.tex...                       
Wrote /fusion.tex                                
org-latex-chinese: Emacs can’t find an available (mainfont) font for latex, ignore!           
org-latex-chinese: Emacs can’t find an available (CJKmainfont-italic) font for latex, ignore! 
org-latex-chinese: Emacs can’t find an available (CJKmainfont) font for latex, ignore!        
org-latex-chinese: Emacs can’t find an available (CJKsansfont) font for latex, ignore!        
org-latex-chinese: Emacs can’t find an available (CJKmonofont) font for latex, ignore!        
org-latex-chinese: Emacs can’t find an available (mainfont) font for latex, ignore!           
org-latex-chinese: Emacs can’t find an available (CJKmainfont-italic) font for latex, ignore! 
org-latex-chinese: Emacs can’t find an available (CJKmainfont) font for latex, ignore!        
org-latex-chinese: Emacs can’t find an available (CJKsansfont) font for latex, ignore!        
org-latex-chinese: Emacs can’t find an available (CJKmonofont) font for latex, ignore!        
Processing LaTeX file fusion.tex...                                                           
PDF file produced with errors.                                                                

org2ctex-latex-fonts 参数设置,试了各种组合,好像不管用:

 Operate on all settings in this buffer:                                                                        
 [ Revert... ] [ Apply ] [ Apply and Save ]                                                                     
                                                                                                                
 Hide Org2ctex Latex Fonts:                                                                                     
 ((mainfont "WenQuanYi Micro Hei")                                                                              
  (CJKmainfont "WenQuanYi Micro Hei" "Microsoft Yahei" "Microsoft_Yahei" "SimHei" "文泉驿微米黑" "文泉驿等宽微米
  (CJKmainfont-italic "KaiTi_GB2312" "KaiTi" "STKaiti" "楷体" "楷体_GB2312" "华文行楷")                         
  (CJKsansfont "Microsoft Yahei" "Microsoft_Yahei" "WenQuanYi Micro Hei" "SimHei" "文泉驿微米黑" "文泉驿等宽微米
  (CJKmonofont "WenQuanYi Micro Hei" "Microsoft Yahei" "Microsoft_Yahei" "SimHei" "文泉驿微米黑" "文泉驿等宽微米
    [ State ]: SAVED and set.                                                                                   
    Set fonts candidates which will used by latex.                                                              
 Groups: [Org Export Latex Chinese]                                                                             

把你系统有的字体,添加到这个字体设置的最前面

谢谢,我把所有的字体都改了以后,找不到字体的错误没有了,出现了新的错误:

后面重复的全都是下面错误信息中最后一个类似的错误.

203 (/usr/share/texlive/texmf-dist/tex/latex/geometry/geometry.sty)             
204 *************************************************                           
205 * xeCJK warning: "no-CJKfamily"                                             
206 *                                                                           
207 * It seems that you have not declare a CJKfamily.                           
208 * If you want to use xeCJK in the right way, you should use                 
209 *                                                                           
210 * `\setCJKmainfont[...]{...}'                                               
211 *                                                                           
212 * in the preamble to declare the default CJKfamily.                         
213 *                                                                           
214 *************************************************                           
215 (.//fusion.aux) (/usr/share/texmf/tex/latex/tipa/t3cmr.fd)                  
216 ABD: EverySelectfont initializing macros                                    
217 *** you should *not* be loading the inputenc package                        
218 *** XeTeX expects the source to be in UTF8 encoding                         
219 *** some features of other encodings may conflict, resulting in poor output.
220 (/usr/share/texlive/texmf-dist/tex/latex/hyperref/nameref.sty               
221 (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/gettitlestring.sty))    
222 (.//fusion.out) (.//fusion.out) ABD: EveryShipout initializing macros       
223 *geometry* driver: auto-detecting                                           
224 *geometry* detected driver: xetex                                           
225 (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsa.fd)                  
226 (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsb.fd) (.//fusion.toc   
227 ! Missing } inserted.                                                       
228 <inserted text>                                                             
229                 }                                                           
230 l.3 ...\237与用户系统相同)}{2}{subsection.2.1}                              
231                                                                             
232 ! Missing } inserted.                                                       
233 <inserted text>                                                             
234                 }                                                           
235 l.3 ...\237与用户系统相同)}{2}{subsection.2.1}                              
236                                                                             
237 )                                                                           
238 ! Missing number, treated as zero.                                          

我按照这个网页来的:

从完全零配置开始,

把那两个要修改的地方 (一个 hook,一个变量) 加入 .dir-locals.el

((org-mode (org-latex-pdf-process
            "xelatex -interaction nonstopmode -output-directory %o %f"
            "xelatex -interaction nonstopmode -output-directory %o %f"
            "xelatex -interaction nonstopmode -output-directory %o %f")
           (eval add-to-list 'org-latex-classes
                 '("ctexart" "\\documentclass{ctexart}"
                   ("\\section{%s}" . "\\section*{%s}")
                   ("\\subsection{%s}" . "\\subsection*{%s}")
                   ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                   ("\\paragraph{%s}" . "\\paragraph*{%s}")
                   ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))))

在同一目录下添加设置字体的包 mylinuxfonts.sty (不能直接用 fontconfig 的配置,但字体名字和 fontconfig 的字体名字一致):

\ProvidesPackage{mylinuxfonts}
\setCJKmainfont{A Serif Name}
\setCJKsansfont{A Sans-Serif Name}
\setCJKmonofont{A Monospace Name}
\endinput

在这个目录下编辑某 org 文件时加上下面三行:

#+LATEX_CLASS: ctexart
#+LATEX_CLASS_OPTIONS: [nofonts]
#+LATEX_HEADER: \usepackage{mylinuxfonts}

这样就行了。

1 个赞

如果在windows系统下:

第一,org文件头部我一般用三个设置:

#+LATEX_COMPILER: xelatex
#+LATEX_HEADER: \usepackage{ctex}
#+LATEX_HEADER: \usepackage[utf8]{inputenc}

第二,确认一下org文件的编码:看状态栏第一个字母是不是U,点击进去确认编码模式是否为utf-8(windows下一般情况下不是utf-8,如果是字母c那肯定不是utf-8)。如果不是utf-8,在.emacs中增加一行: (add-to-list 'file-coding-system-alist '("\\.org" . utf-8) ) ,加载.emacs或重启emacs后org文件应该都是utf-8编码了。

第三,确认一下输出的中间文件:.tex文件,看一下是什么编码,windows上很可能是GB2312或GBK,这个编码转换为pdf后也是不会显示中文的。如果第二步执行正确,应该会直接输出显示中文的pdf了。

1 个赞

可以参考我的配置:

ctex论坛已经无限期关闭了,哎

參考:Export Org file To pdf with Chinese Fonts

我为了避免麻烦,用pandoc导出成word,在另存为PDF的方法。

有关word的样式,可以用下面的命令指定:

pandoc input.org -o output.docx --reference-doc=WhereYourTemplate.docx

WhereYourTemplate.docx是预先定义好样式的word文件。

一般情况下(没有复杂排版、表格的)够用了。

这样写\end{cjk}要写在哪里?

感谢,有用!

1 个赞

一切显示不了中文的问题都不必碰瓷 org, 这必然是 latex 的问题