前言
TeXLive
在LaTeX
的发型版本中占据一大江山,CTeX
在中文排版中也占据一大江山,虽然CTeX
自诩其包含TeXLive
的所有功能,但目前我涉及的几大主流期刊,如IEEE所用的LaTeX
模板使用CTeX
编译大多有问题,但国内期刊,如《控制理论与应用》使用TeXLive
无法编译,必须使用CTeX
编译。
笔者的环境中安装了编译器TeXLive2024
、CTeX3.1.217.2
、CTeX2.9.2.164
以及编辑器TeXStudio4.8.1
、WinEdt11
、VSCode
并实现自由切换,下面详细说明如何操作。
全文资源汇总
TeXLive2024
CTeX3.1
CTeX2.9
TeXStudio4.8.1
WinEdt
VSCode
1 编译文章的基本原理
笔者的安装目录:
TeXLive2024
:C:\Cheniana\texlive\
CTeX3.1.217.2
:C:\Cheniana\ctex\
CTeX2.9.2.164
:C:\Cheniana\ctex_legacy\ctex_29\
当我们点击Build & View
或者Compile
时,观察TeXStudio
输出的消息(红框区域)
不难发现,实际上我们是在调用可执行文件.exe
去编译文章,而后面跟-synctex=1 -interaction=nonstopmode "main".tex
是编辑器帮我们自动补充的,所以我们只需要切换TeXLive
和CTeX
环境中对应的可执行文件便能实现二者的自由切换。
2 TeXStudio配置和切换TeXLive和CTeX
点击Options => Configure TeXstudio,在弹出的界面右侧选择Commands:
方法一
我们只需要修改下图中5个可执行文件的路径即可,例如笔者常用的PdfLaTeX
使用的是CTeX2.9
的编译环境,而XeLaTeX
使用的是TeXLive2024
的环境。
例如我要将PdfLaTeX
切换为TeXLive2024
的编译环境,只需要找到该环境下的PdfLaTeX.exe
可执行文件。TeXLive
中所有可执行文件都存放于\Path\To\Your\texlive\2024\bin\windows\
,以笔者的为例C:\Cheniana\texlive\2024\bin\windows
。
在TeXStudio
中将CTeX
编译环境切换为TeXLive
编译环境,以Pdflatex.exe
为例:
点击Options => Configure TeXstudio => Commands,将Pdflatex.exe
的路径由CTeX
环境切换为TeXLive
。LaTeX
、XeLaTeX
、LuaLaTeX
同理
方法二(建议先熟悉方法一)
结合上述方法一,我们可以在Options => Configure TeXstudio => Build => User Commands中定义自己的编译命令。
其实就是把方法一中Commands
的命令复制到这里,自定义命令除了能实现快速切换TeXLive
和CTeX
之外,还有一个好处是可以自己设置编译次数和编译命令,比如使用Bibtex
的文章,都需要四步编译PdfLaTeX => BibTeX => PdfLaTeX => PdfLaTex
,自定义可以轻松实现:
不同的命令以|
隔开,可以在命令最后添加txs:///view
实现编译预览。
命令名称 | 命令内容 |
---|---|
user6:MyThesis | "C:/Cheniana/texlive/2024/bin/windows/pdflatex.exe" -synctex=1 -interaction=nonstopmode %.tex | "C:/Cheniana/texlive/2024/bin/windows/bibtex.exe" % | "C:/Cheniana/texlive/2024/bin/windows/pdflatex.exe" -synctex=1 -interaction=nonstopmode %.tex | "C:/Cheniana/texlive/2024/bin/windows/pdflatex.exe" -synctex=1 -interaction=nonstopmode %.tex | txs:///view |
下面列出笔者自定义的命令供大家参考(需要把下面可执行文件的路径改成自己):
命令名称 | 命令内容 | 对应环境 |
---|---|---|
user0:TeXLive_PdfLaTeX | "C:/Cheniana/texlive/2024/bin/windows/pdflatex.exe" -synctex=1 -interaction=nonstopmode %.tex | txs:///view | TeXLive2024 PdfLaTeX |
user1:TeXLive_XeLaTeX | "C:/Cheniana/texlive/2024/bin/windows/xelatex.exe" -synctex=1 -interaction=nonstopmode %.tex | txs:///view | TeXLive2024 XeLaTeX |
user2:CTeX31_PdfLaTeX | "C:/Cheniana/ctex/MiKTeX/miktex/bin/x64/pdflatex.exe" -synctex=1 -interaction=nonstopmode %.tex | txs:///view | CTeX3.1 PdfLaTeX |
user3:CTeX31_XeLaTeX | "C:/Cheniana/ctex/MiKTeX/miktex/bin/x64/xelatex.exe" -synctex=1 -interaction=nonstopmode %.tex | txs:///view | CTeX3.1 XeLaTeX |
user4:CTeX29_PdfLaTeX | "C:/Cheniana/ctex_legacy/ctex_29/MiKTeX/miktex/bin/pdflatex.exe" -synctex=1 -interaction=nonstopmode %.tex | txs:///view | CTeX2.9 PdfLaTeX |
user5:CTeX29_XeLaTeX | "C:/Cheniana/ctex_legacy/ctex_29/MiKTeX/miktex/bin/xelatex.exe" -synctex=1 -interaction=nonstopmode %.tex | txs:///view | CTeX2.9 XeLaTeX |
定义完自己的命令后,点击Tools => User就看到自己的所有命令,点击即可运行
3 关于GBK和UTF8的编码问题
通常CTeX
编译中文文章时使用GBK
编码,而TeXLive
普遍使用UTF8
编码,TeXStudio
默认的编码格式也是UTF8
,所以在TeXStudio
中打开一篇GBK
的编码就会这样,中文全部都是乱码。TeXStudio
自动帮我们识别为ISO-8859-1
的编码,显然识别是错误的。
此时我们只需要点击右下角切换编码即可:
如果长期使用某种编码格式,我们也可以在Options => Configure TeXstudio => Editor中切换:
如有错误或其他方法,感谢留言指正!