Latex + Vscode Remote containers

  • 配置 Vscode Remote containers Latex 环境

  • 资料来源:

    https://github.com/nelsond/docker-texlive/blob/master/Dockerfile
    https://github.com/alexpovel/latex-extras-docker/blob/master/Dockerfile#L187

  • 更新

    1
    2021.05.22 初始

导语

折腾 Latex 环境很长时间,最后还是回到了 Docker + vsc 远程.

虽然配置略坑,但是基本能做到配置一次,全平台运行.

问题

  • linux 下运行文件权限尚未处理
  • 镜像带有 git 但默认未挂载 ssh key,暂时不可用.

前日谭

这里不再详述 Remote containers 的内容了.参考 Create a development containerAdvanced Container Configuration.

之后有机会再填坑吧…

使用

项目地址 -> docker_texlive

1
git clone https://github.com/Jasper-1024/docker_texlive.git

正常在 vsc 打开

镜像

texlive

为了定制化,这里 texlive 镜像需要自编译.参考:

https://github.com/nelsond/docker-texlive/blob/master/Dockerfile
https://github.com/alexpovel/latex-extras-docker/blob/master/Dockerfile#L187

DockerHub

  • 基于 Debian 10
  • texlive 2021
  • 大小在 4.55G
  • 到底还是为了避免缺失选择了 full 安装,因此容量无法控制..
1
docker pull jasperhale/texlive:latest

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
FROM debian:10

LABEL authors "jasperhale <ljy087621@gmail.com>"

ENV VERSION=2021

RUN apt-get update && \
apt-get -y install wget unzip curl ca-certificates git make\
# perl 依赖
perl perl-modules \
# python & 语法高亮
python3 python3-pygments

RUN wget --no-verbose \
"https://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz" && \
tar -xzf install-tl-unx.tar.gz && \
# 进入texlive
cd $(find . -type d -name "install-tl-*" | head -n 1) && \
# 默认安装全部
echo "selected_scheme scheme-full" > profile && \
# 安装
./install-tl -profile profile && \
cd .. &&\
rm -rf install-tl-*

RUN apt-get update && \
apt-get install --yes --no-install-recommends \
# to fix "error while loading shared libraries: libfontconfig.so.1"
libfontconfig \
# YAML::Tiny:
libyaml-tiny-perl \
# File::HomeDir:
libfile-homedir-perl \
# Unicode:GCString:
libunicode-linebreak-perl \
# Log::Log4perl:
liblog-log4perl-perl \
# Log::Dispatch:
liblog-dispatch-perl \
# Get `envsubst` to replace environment variables in files with their actual values.
gettext-base \
# headless, 25% of normal size:
default-jre-headless \
# No headless inkscape available currently:
inkscape \
# nox (no X Window System): CLI version, 10% of normal size:
gnuplot-nox \
# For various conversion tasks, e.g. EPS -> PDF (for legacy support):
ghostscript \
# librsvg2 for 'rsvg-convert' used by pandoc to convert SVGs when embedding into PDF
librsvg2-bin \
#pandoc layer; not required for LaTeX compilation, but useful for document conversions
pandoc

ENV PATH="/usr/local/texlive/${VERSION}/bin/x86_64-linux:${PATH}"

CMD ["/bin/sh"]

# RUN pdflatex --version

自行编译要 2 个多小时,主要瓶颈是安装 texlive 的网络问题.即使托管到 Github Action 也需要 1.5 小时.

其他

还可以使用 texlive 官方镜像 texlive/texlive

  • 每周更新
  • latest 是 4.51 G

寻求更小镜像,可以尝试 alexpovel/latex

  • 只有 1.5G
  • 近乎 full.

Remote containers 配置

主要是 devcontainer.json

devcontainer.json 是配置 vsc 启动连接容器,安装插件,修改配置等.

为了方便修改容器,devcontainer.json 中使用 docker-compose 启动容器.

devcontainer.json

现有的配置寻求极简,更多复杂高级的功能尚未添加.

主要注释已经添加.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{
"name": "Latex",
"dockerComposeFile": [
"docker-compose.yml"
],
"service": "texlive", //与 docker-compose 一致
"workspaceFolder": "/root/work", //工作目录
"extensions": [ //安装的拓展
"james-yu.latex-workshop"
],
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"latex-workshop.editor.formatOnSave": true,//保存时自动格式化
"latex-workshop.view.pdf.viewer": "tab", //内部标签页打开
"latex-workshop.latex.autoBuild.run": "never", // 不自动编译
"latex-workshop.intellisense.package.enabled": true, //自动完成
"latex-workshop.showContextMenu": true, //右键菜单
"editor.wordWrap": "on", // 自动换行
"latex-workshop.latex.recipes": [
{
"name": "latexmk",
"tools": [
"latexmk"
]
},
{
"name": "xelate×2",
"tools": [
"xelatex",
"xelatex"
]
},
{
"name": "xelatex ➞ bibtex ➞ xelatex`×2",
"tools": [
"xelatex",
"bibtex",
"xelatex",
"xelatex"
]
}
],
"latex-workshop.latex.tools": [
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-xelatex",
"-outdir=%OUTDIR%",
"%DOCFILE%" //相对路径
],
"env": {}
},
{
"name": "xelatex",
"command": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOCFILE%"
],
"env": {}
},
{
"name": "bibtex", //参考文献
"command": "bibtex",
"args": [
"%DOCFILE%"
],
"env": {}
}
],
"latex-workshop.latex.clean.fileTypes": [ //设定清理文件的类型
"*.aux",
"*.bbl",
"*.blg",
"*.idx",
"*.ind",
"*.lof",
"*.lot",
"*.out",
"*.toc",
"*.acn",
"*.acr",
"*.alg",
"*.glg",
"*.glo",
"*.gls",
"*.ist",
"*.fls",
"*.log",
"*.fdb_latexmk",
"*.nav",
"*.snm",
//"*.synctex.gz" //避免双向搜索失效
]
}
}

settings 部分是容器环境下 vsc 的设置.

docker-compose

docker-compose 负责启动配置容器

  • 挂载工作目录,linux 下还有额外的文件权限处理.
  • 特别注意, command: /bin/sh -c "while sleep 1000; do :; done" 避免容器默认命令执行失败/完毕导致容器退出.
1
2
3
4
5
6
7
8
9
version: "3.7"
services:
texlive:
# build: ./
image: jasperhale/texlive:latest
volumes:
- ../:/root/work
# 避免容器退出
command: /bin/sh -c "while sleep 1000; do :; done"