配置私有 Overleaf (LaTeX) 完美插入 SVG 矢量图片

2021年1月19日 9407点热度 13人点赞 0条评论

最近在生产环境中发生的灾难:

1. SVG 中的字体和字号显示异常,解决方法是在 AI 中以轮廓重新导出一遍 SVG 文件;

2. 部分特殊 SVG 文件会使编译无法停止,出现此情况,删除该文件引用,在容器中终止 Perl 进程(实际是 latexmk 的未知 Bug,明明 pdflatex 和 inkscape 都已经结束运行,且最终已经生成了 PDF 文件)

2021/1/26 凌晨更新

作者环境

  • Overleaf Docker Full 安装
  • 完整服务器权限
  • 本地 TeX Live 等环境理论上同理

操作流程

进入 Overleaf 运行的容器

docker ps
docker exec -it 523 /bin/bash # 其中523为 sharelatex 主容器对应的 CONTAINER ID
Docker 进入容器命令

安装 Inkscape[1](容器中)

sudo add-apt-repository ppa:inkscape.dev/stable
sudo apt update
sudo apt install inkscape

输入 inkscape --version ,能打印版本号即可,作者为 Inkscape 1.0.2 (1.0.2+r75+1) Pango version: 1.40.14

另外 Overleaf 的容器实际上就是 Ubuntu,建议更换服务器所在IDC的镜像源,参考阿里云[2]

在 LaTeX 中使用 SVG 宏包[3]

因为该宏的运行将调用读取本地程序和文件,需要 --shell-escape 命令,这里给出最简方法

  • 根目录新建 latexmkrc ,内容 $pdflatex = 'pdflatex --shell-escape'
  • 主文件 .tex 加入 \usepackage{svg},如果使用了模板可以找 \usepackage 扎堆的地方
  • SVG 矢量图片插入示例
\begin{figure}[!ht]
\centering
\includesvg[width=0.5\columnwidth]{svgFileName}
\end{figure}
SVG 在 Overleaf 中显示效果

保存为镜像(可选)

docker commit 523 sharelatex-full-zh-cn:svg-support # CONTAINER ID 同理
docker images
docker rmi sharelatex-full-zh-cn:latest # 删除原有的,毕竟完整安装的 Overleaf 超过 8 GB

最后更改 docker-compose.yml 使其指向新的镜像

参考资料

  1. Inkscape 官网 PPA 安装 - https://inkscape.org/release/inkscape-1.0.2/gnulinux/ubuntu/ppa/dl/
  2. 阿里云 Ubuntu 镜像 - https://developer.aliyun.com/mirror/ubuntu
  3. SVG 宏包 - https://www.ctan.org/pkg/svg

章鱼DS

学生网站代码通吃的草根,但总拼命着长出树根。

文章评论