佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > docker的pdflatex環(huán)境配置的方法步驟

docker的pdflatex環(huán)境配置的方法步驟

熱門(mén)標(biāo)簽:百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 網(wǎng)站建設(shè) 檢查注冊(cè)表項(xiàng) 使用U盤(pán)裝系統(tǒng) 阿里云 智能手機(jī) 美圖手機(jī) 硅谷的囚徒呼叫中心

技術(shù)背景

Latex在文檔撰寫(xiě)方面是不可或缺的工具,尤其是在寫(xiě)文章方面,是必須要用到的文字排版工具。但是latex的環(huán)境部署并不是一個(gè)特別人性化的操作,尤其是在各種不同的平臺(tái)上操作是完全不一樣的,還經(jīng)常容易報(bào)錯(cuò)。我們可以一個(gè)一個(gè)的去解決報(bào)錯(cuò)問(wèn)題,但是這需要耗費(fèi)極大的精力和時(shí)間,所以很多人選擇了直接在overleaf進(jìn)行l(wèi)atex的創(chuàng)作。但其實(shí)overleaf也有它的缺點(diǎn),比如免費(fèi)版本的帶寬和速度都比較受限,尤其是在國(guó)內(nèi)的網(wǎng)絡(luò),訪問(wèn)速度可謂是”一絕“。因此這里我們介紹一個(gè)更加人性化的方案,而且對(duì)各大平臺(tái)的兼容性非常都非常好:使用docker來(lái)部署latex環(huán)境。

Docker的基本操作

在各大平臺(tái)的官方源里面應(yīng)該都會(huì)有提供docker容器,因此這里我們也不過(guò)多的贅述。作者過(guò)去寫(xiě)過(guò)一篇關(guān)于使用docker來(lái)部署MindSpore開(kāi)發(fā)環(huán)境的博客,感興趣的讀者可以當(dāng)作是拓展文章來(lái)閱讀一下。

首先我們?cè)贛anjaro Linux平臺(tái)上啟動(dòng)docker(在其他平臺(tái)上的操作可能有區(qū)別,比如service start docker等):

[dechin-root tex]# systemctl start docker

注意上述指令要在root帳號(hào)下才能夠啟動(dòng),如果要選擇在非root帳號(hào)下操作,docker容器是不支持的,但是我們可以選擇singularity這一類(lèi)似的容器解決方案,相關(guān)內(nèi)容可以參考這篇博客。啟動(dòng)服務(wù)之后,正常狀態(tài)下我們可以看到docker的status是處于active或者running的狀態(tài):

[dechin-root tex]# systemctl status docker
● docker.service - Docker Application Container Engine
  Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disab>
  Active: active (running) since Sun 2021-03-28 18:50:47 CST; 7s ago
TriggeredBy: ● docker.socket
  Docs: https://docs.docker.com
 Main PID: 25366 (dockerd)
  Tasks: 123 (limit: 47875)
  Memory: 219.1M
  CGroup: /system.slice/docker.service
    ├─25366 /usr/bin/dockerd -H fd://
    └─25378 containerd --config /var/run/docker/containerd/containerd.toml --log-l>

拉取容器鏡像

首先我們可以訪問(wèn)dockerhub官網(wǎng)搜索一下是否存在我們所需要的容器鏡像,比如我們的搜索結(jié)果如下:


可以看到這里有很多的選項(xiàng),一般我們可以直接選擇星星最高的容器鏡像進(jìn)行下載使用:

[dechin-root tex]# docker pull fbenz/pdflatex
Using default tag: latest
latest: Pulling from fbenz/pdflatex
f22ccc0b8772: Already exists 
3cf8fb62ba5f: Already exists 
e80c964ece6a: Already exists 
9aa2583757a3: Pull complete 
2c3d7890d583: Pull complete 
Digest: sha256:6ecca11b1a203faed5c0a2ace2a13aac100dd19d7a4e0db0474283bcded3c041
Status: Downloaded newer image for fbenz/pdflatex:latest
docker.io/fbenz/pdflatex:latest

下載需要一段的時(shí)間。下載完成后,可以在本地鏡像倉(cāng)庫(kù)中找到剛才下載的這個(gè)鏡像文件:

[dechin-root tex]# docker images
REPOSITORY             TAG  IMAGE ID  CREATED   SIZE
fbenz/pdflatex            latest 8e7742722956 3 months ago 24GB

我們可以測(cè)試一下這個(gè)容器鏡像中的pdflatex功能是否正常:

[dechin-root tex]# docker run -it fbenz/pdflatex pdflatex --help
Usage: pdftex [OPTION]... [TEXNAME[.tex]] [COMMANDS]
 or: pdftex [OPTION]... \FIRST-LINE
 or: pdftex [OPTION]... &FMT ARGS
 Run pdfTeX on TEXNAME, usually creating TEXNAME.pdf.
 Any remaining COMMANDS are processed as pdfTeX input, after TEXNAME is read.
 If the first line of TEXNAME is %&FMT, and FMT is an existing .fmt file,
 use it. Else use `NAME.fmt', where NAME is the program invocation name,
 most commonly `pdftex'.

 Alternatively, if the first non-option argument begins with a backslash,
 interpret all non-option arguments as a line of pdfTeX input.

 Alternatively, if the first non-option argument begins with a &, the
 next word is taken as the FMT to read, overriding all else. Any
 remaining arguments are processed as above.

 If no arguments or options are specified, prompt for input.

-draftmode    switch on draft mode (generates no output PDF)
-enc     enable encTeX extensions such as \mubyte
-etex     enable e-TeX extensions
[-no]-file-line-error disable/enable file:line:error style messages
-fmt=FMTNAME   use FMTNAME instead of program name or a %& line
-halt-on-error   stop processing at the first error
-ini     be pdfinitex, for dumping formats; this is implicitly
       true if the program name is `pdfinitex'
-interaction=STRING  set interaction mode (STRING=batchmode/nonstopmode/
       scrollmode/errorstopmode)
-ipc     send DVI output to a socket as well as the usual
       output file
-ipc-start    as -ipc, and also start the server at the other end
-jobname=STRING   set the job name to STRING
-kpathsea-debug=NUMBER set path searching debugging flags according to
       the bits of NUMBER
[-no]-mktex=FMT   disable/enable mktexFMT generation (FMT=tex/tfm/pk)
-mltex     enable MLTeX extensions such as \charsubdef
-output-comment=STRING use STRING for DVI file comment instead of date
       (no effect for PDF)
-output-directory=DIR use existing DIR as the directory to write files in
-output-format=FORMAT use FORMAT for job output; FORMAT is `dvi' or `pdf'
[-no]-parse-first-line disable/enable parsing of first line of input file
-progname=STRING  set program (and fmt) name to STRING
-recorder    enable filename recorder
[-no]-shell-escape  disable/enable \write18{SHELL COMMAND}
-shell-restricted  enable restricted \write18
-src-specials   insert source specials into the DVI file
-src-specials=WHERE  insert source specials in certain places of
       the DVI file. WHERE is a comma-separated value
       list: cr display hbox math par parend vbox
-synctex=NUMBER   generate SyncTeX data for previewers according to
       bits of NUMBER (`man synctex' for details)
-translate-file=TCXNAME use the TCX file TCXNAME
-8bit     make all characters printable by default
-help     display this help and exit
-version    output version information and exit

pdfTeX home page: <http://pdftex.org>

Email bug reports to pdftex@tug.org.

當(dāng)我們看到help指令運(yùn)行成功時(shí),就表明容器鏡像可以正常使用。使用容器還有一點(diǎn)需要注意的是,如果我們直接用docker run -it fbenz/pdflatex的話,沒(méi)有綁定本地的目錄,這樣是無(wú)法看到本地所撰寫(xiě)的tex文件的。因此我們一般需要在運(yùn)行的時(shí)候加上-v的選項(xiàng)來(lái)綁定本地的目錄,基本使用方法是:-v 本地目錄:容器目錄,注意需要使用絕對(duì)路徑,不能使用相對(duì)路徑。

編譯Tex文件

在上述章節(jié)中完成基于docker的pdflatex環(huán)境部署之后,我們可以開(kāi)始撰寫(xiě)一些簡(jiǎn)單的tex文件用來(lái)測(cè)試一下環(huán)境。

Hello World

首先最簡(jiǎn)單的我們測(cè)試一個(gè)hello world的案例,僅在pdf文檔中輸出一個(gè)Hello World!的字樣,具體tex代碼如下:

[dechin@dechin-manjaro tex]$ cat hello_world.tex 
\documentclass{article}
\begin{document}
Hello world!
\end{document}

使用方法也不難,首先我們運(yùn)行docker容器,注意需要綁定一個(gè)本地路徑,然后進(jìn)入到容器內(nèi)對(duì)應(yīng)的目錄下:

[dechin-root tex]# docker run -it -v /home/dechin/projects/2021-python/tex/:/home/ fbenz/pdflatex
root@d7ed2229a244:/# ll
total 72
drwxr-xr-x 1 root root 4096 Mar 28 11:07 ./
drwxr-xr-x 1 root root 4096 Mar 28 11:07 ../
-rwxr-xr-x 1 root root 0 Mar 28 11:07 .dockerenv*
drwxr-xr-x 2 root root 4096 Nov 19 13:09 bin/
drwxr-xr-x 2 root root 4096 Apr 24 2018 boot/
drwxr-xr-x 5 root root 360 Mar 28 11:07 dev/
drwxr-xr-x 1 root root 4096 Mar 28 11:07 etc/
drwxr-xr-x 2 1000 1000 4096 Mar 28 04:43 home/
drwxr-xr-x 1 root root 4096 May 23 2017 lib/
drwxr-xr-x 2 root root 4096 Nov 19 13:09 lib64/
drwxr-xr-x 2 root root 4096 Nov 19 13:07 media/
drwxr-xr-x 2 root root 4096 Nov 19 13:07 mnt/
drwxr-xr-x 2 root root 4096 Nov 19 13:07 opt/
dr-xr-xr-x 323 root root 0 Mar 28 11:07 proc/
drwx------ 2 root root 4096 Nov 19 13:09 root/
drwxr-xr-x 1 root root 4096 Nov 25 22:25 run/
drwxr-xr-x 1 root root 4096 Nov 25 22:25 sbin/
drwxr-xr-x 2 root root 4096 Nov 19 13:07 srv/
dr-xr-xr-x 13 root root 0 Mar 28 11:07 sys/
drwxrwxrwt 1 root root 4096 Nov 28 18:34 tmp/
drwxr-xr-x 1 root root 4096 Nov 19 13:07 usr/
drwxr-xr-x 1 root root 4096 Nov 19 13:09 var/
root@d7ed2229a244:/# cd home/
root@d7ed2229a244:/home# ll
total 12
drwxr-xr-x 2 1000 1000 4096 Mar 28 04:43 ./
drwxr-xr-x 1 root root 4096 Mar 28 11:07 ../
-rw-r--r-- 1 1000 1000 69 Mar 28 04:43 hello_world.tex

我們看到在容器內(nèi)的目錄下也能夠看到這個(gè)tex文件,說(shuō)明路徑的綁定成功的執(zhí)行了。運(yùn)行指令很簡(jiǎn)單,直接在docker容器內(nèi)運(yùn)行pdflatex your_file.tex即可:

root@d7ed2229a244:/home# pdflatex hello_world.tex 
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./hello_world.tex
LaTeX2e <2017-04-15>
Babel <3.18> and hyphenation patterns for 84 language(s) loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
No file hello_world.aux.
[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./hello_world.aux) )</u
sr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on hello_world.pdf (1 page, 11916 bytes).
Transcript written on hello_world.log.
root@d7ed2229a244:/home# ll
total 32
drwxr-xr-x 2 1000 1000 4096 Mar 28 11:08 ./
drwxr-xr-x 1 root root 4096 Mar 28 11:07 ../
-rw-r--r-- 1 root root  8 Mar 28 11:08 hello_world.aux
-rw-r--r-- 1 root root 2408 Mar 28 11:08 hello_world.log
-rw-r--r-- 1 root root 11916 Mar 28 11:08 hello_world.pdf
-rw-r--r-- 1 1000 1000 69 Mar 28 04:43 hello_world.tex
root@d7ed2229a244:/home# chmod -R 777 .
root@d7ed2229a244:/home# ll
total 32
drwxrwxrwx 2 1000 1000 4096 Mar 28 11:08 ./
drwxr-xr-x 1 root root 4096 Mar 28 11:07 ../
-rwxrwxrwx 1 root root  8 Mar 28 11:08 hello_world.aux*
-rwxrwxrwx 1 root root 2408 Mar 28 11:08 hello_world.log*
-rwxrwxrwx 1 root root 11916 Mar 28 11:08 hello_world.pdf*
-rwxrwxrwx 1 1000 1000 69 Mar 28 04:43 hello_world.tex*

運(yùn)行完成后我們?cè)谀夸浿锌吹搅藥讉€(gè)新生成的文件,如果用root改成777的權(quán)限,那么在本地的非root帳號(hào)就可以對(duì)其進(jìn)行編輯,否則就只能查看。我們可以在本地打開(kāi)這個(gè)pdf文件看看:


可以看到這個(gè)pdf文件生成成功。

測(cè)試公式

上面hello world的案例比較簡(jiǎn)單,讓我們來(lái)測(cè)試一下最常用的數(shù)學(xué)公式是否有問(wèn)題:

[dechin@dechin-manjaro tex]$ cat equation_test.tex 
\documentclass{article}
\begin{document}
Hello world!
\begin{equation}
  e^{iHt}\left|\psi\right>
\end{equation}
\end{document}

類(lèi)似于上一章節(jié)的,我們也需要進(jìn)入到容器的內(nèi)部執(zhí)行相關(guān)的指令,最后獲得如下所示的一個(gè)pdf文件:


我們可以看到公式顯示也是正常的。

量子線路圖

最后我們測(cè)試一個(gè)比較難的,在前面寫(xiě)過(guò)一篇關(guān)于用ProjectQ生成Latex格式的量子線路圖的博客,其中生成了如下所示的一個(gè)tex文件:

[dechin@dechin-manjaro quantum-circuit]$ cat circuit.tex 
\documentclass{standalone}
\usepackage[margin=1in]{geometry}
\usepackage[hang,small,bf]{caption}
\usepackage{tikz}
\usepackage{braket}
\usetikzlibrary{backgrounds,shadows.blur,fit,decorations.pathreplacing,shapes}

\begin{document}
\begin{tikzpicture}[scale=0.8, transform shape]

\tikzstyle{basicshadow}=[blur shadow={shadow blur steps=8, shadow xshift=0.7pt, shadow yshift=-0.7pt, shadow scale=1.02}]\tikzstyle{basic}=[draw,fill=white,basicshadow]
\tikzstyle{operator}=[basic,minimum size=1.5em]
\tikzstyle{phase}=[fill=black,shape=circle,minimum size=0.1cm,inner sep=0pt,outer sep=0pt,draw=black]
\tikzstyle{none}=[inner sep=0pt,outer sep=-.5pt,minimum height=0.5cm+1pt]
\tikzstyle{measure}=[operator,inner sep=0pt,minimum height=0.5cm, minimum width=0.75cm]
\tikzstyle{xstyle}=[circle,basic,minimum height=0.35cm,minimum width=0.35cm,inner sep=-1pt,very thin]
\tikzset{
shadowed/.style={preaction={transform canvas={shift={(0.5pt,-0.5pt)}}, draw=gray, opacity=0.4}},
}
\tikzstyle{swapstyle}=[inner sep=-1pt, outer sep=-1pt, minimum width=0pt]
\tikzstyle{edgestyle}=[very thin]

\node[none] (line0_gate0) at (0.1,-0) {$\Ket{0}$};
\node[none] (line0_gate1) at (0.5,-0) {};
\node[none,minimum height=0.5cm,outer sep=0] (line0_gate2) at (0.75,-0) {};
\node[none] (line0_gate3) at (1.0,-0) {};
\draw[operator,edgestyle,outer sep=0.5cm] ([yshift=0.25cm]line0_gate1) rectangle ([yshift=-0.25cm]line0_gate3) node[pos=.5] {H};
\draw (line0_gate0) edge[edgestyle] (line0_gate1);
\node[none] (line1_gate0) at (0.1,-1) {$\Ket{0}$};
\node[none] (line1_gate1) at (0.5,-1) {};
\node[none,minimum height=0.5cm,outer sep=0] (line1_gate2) at (0.75,-1) {};
\node[none] (line1_gate3) at (1.0,-1) {};
\draw[operator,edgestyle,outer sep=0.5cm] ([yshift=0.25cm]line1_gate1) rectangle ([yshift=-0.25cm]line1_gate3) node[pos=.5] {H};
\draw (line1_gate0) edge[edgestyle] (line1_gate1);
\node[none] (line2_gate0) at (0.1,-2) {$\Ket{0}$};
\node[none] (line2_gate1) at (0.5,-2) {};
\node[none,minimum height=0.5cm,outer sep=0] (line2_gate2) at (0.75,-2) {};
\node[none] (line2_gate3) at (1.0,-2) {};
\draw[operator,edgestyle,outer sep=0.5cm] ([yshift=0.25cm]line2_gate1) rectangle ([yshift=-0.25cm]line2_gate3) node[pos=.5] {H};
\draw (line2_gate0) edge[edgestyle] (line2_gate1);
\node[xstyle] (line1_gate4) at (1.4000000000000001,-1) {};
\draw[edgestyle] (line1_gate4.north)--(line1_gate4.south);
\draw[edgestyle] (line1_gate4.west)--(line1_gate4.east);
\node[phase] (line2_gate4) at (1.4000000000000001,-2) {};
\draw (line2_gate4) edge[edgestyle] (line1_gate4);
\draw (line1_gate3) edge[edgestyle] (line1_gate4);
\draw (line2_gate3) edge[edgestyle] (line2_gate4);
\node[xstyle] (line0_gate4) at (1.9500000000000002,-0) {};
\draw[edgestyle] (line0_gate4.north)--(line0_gate4.south);
\draw[edgestyle] (line0_gate4.west)--(line0_gate4.east);
\node[phase] (line1_gate5) at (1.9500000000000002,-1) {};
\draw (line1_gate5) edge[edgestyle] (line0_gate4);
\draw (line0_gate3) edge[edgestyle] (line0_gate4);
\draw (line1_gate4) edge[edgestyle] (line1_gate5);
\node[measure,edgestyle] (line0_gate5) at (2.6000000000000005,-0) {};
\draw[edgestyle] ([yshift=-0.18cm,xshift=0.07500000000000001cm]line0_gate5.west) to [out=60,in=180] ([yshift=0.035cm]line0_gate5.center) to [out=0, in=120] ([yshift=-0.18cm,xshift=-0.07500000000000001cm]line0_gate5.east);
\draw[edgestyle] ([yshift=-0.18cm]line0_gate5.center) to ([yshift=-0.07500000000000001cm,xshift=-0.18cm]line0_gate5.north east);
\draw (line0_gate4) edge[edgestyle] (line0_gate5);
\node[measure,edgestyle] (line1_gate6) at (2.6000000000000005,-1) {};
\draw[edgestyle] ([yshift=-0.18cm,xshift=0.07500000000000001cm]line1_gate6.west) to [out=60,in=180] ([yshift=0.035cm]line1_gate6.center) to [out=0, in=120] ([yshift=-0.18cm,xshift=-0.07500000000000001cm]line1_gate6.east);
\draw[edgestyle] ([yshift=-0.18cm]line1_gate6.center) to ([yshift=-0.07500000000000001cm,xshift=-0.18cm]line1_gate6.north east);
\draw (line1_gate5) edge[edgestyle] (line1_gate6);
\node[measure,edgestyle] (line2_gate5) at (2.0500000000000003,-2) {};
\draw[edgestyle] ([yshift=-0.18cm,xshift=0.07500000000000001cm]line2_gate5.west) to [out=60,in=180] ([yshift=0.035cm]line2_gate5.center) to [out=0, in=120] ([yshift=-0.18cm,xshift=-0.07500000000000001cm]line2_gate5.east);
\draw[edgestyle] ([yshift=-0.18cm]line2_gate5.center) to ([yshift=-0.07500000000000001cm,xshift=-0.18cm]line2_gate5.north east);
\draw (line2_gate4) edge[edgestyle] (line2_gate5);

\end{tikzpicture}
\end{document}

這個(gè)文件不僅結(jié)構(gòu)復(fù)雜,對(duì)周邊所依賴(lài)的tex文件其實(shí)也不少,此前在其他平臺(tái)(Win10)測(cè)試這個(gè)tex文件的編譯的時(shí)候,都需要手動(dòng)的去下載很多的依賴(lài)文件,然后放到同一個(gè)文件夾下才能正常運(yùn)行和使用。這里我們直接運(yùn)行,發(fā)現(xiàn)也可以生成這個(gè)pdf文件:


說(shuō)明環(huán)境里面確實(shí)已經(jīng)包含了很多必備的工具,跟overleaf的環(huán)境應(yīng)該是比較類(lèi)似的,使得我們可以在本地非常人性化的、輕便的可以編譯tex文件。

總結(jié)概要

為了在本地構(gòu)建一個(gè)可用性強(qiáng)、易于部署的環(huán)境,我們選擇了放棄直接安裝pdflatex的方案,以及線上的overleaf的方案。這些方案各有利弊,但是綜合起來(lái)看,對(duì)于個(gè)人使用的環(huán)境而言,還是在本地使用docker鏡像直接部署一個(gè)tex編譯環(huán)境是最方便、最人性化的。

版權(quán)聲明

本文首發(fā)鏈接為:https://www.cnblogs.com/dechinphy/p/pdflatex.html
作者ID:DechinPhy

參考鏈接https://www.cnblogs.com/dechinphy/p/circuit.html

到此這篇關(guān)于docker的pdflatex環(huán)境配置的方法步驟的文章就介紹到這了,更多相關(guān)docker的pdflatex環(huán)境配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

標(biāo)簽:通遼 懷化 湖北 湘潭 煙臺(tái) 山南 黃山 賀州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《docker的pdflatex環(huán)境配置的方法步驟》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266
    阿拉善左旗| 肃宁县| 永靖县| 遂宁市| 广东省| 蒙城县| 怀柔区| 宝兴县| 新乡市| 游戏| 云浮市| 英山县| 靖宇县| 凉山| 磐石市| 桑植县| 巫溪县| 聂拉木县| 万安县| 浠水县| 长沙县| 龙州县| 迭部县| 鹤庆县| 高陵县| 青海省| 唐山市| 汾西县| 镇宁| 淄博市| 阳高县| 三亚市| 平乡县| 达孜县| 虹口区| 石柱| 原阳县| 和田县| 塔城市| 平陆县| 泰来县|