4. LaTex standalone 包的配置

本教程是基于由 Martin Scharrer 开发的 standalone 包(自带 standalone 类),故此于此对此包稍作讲解。

注解

standalone (complex) [1]

standalone 是LaTex中非常有用的一个包。本教程主要讲述怎样利用此包来进行图片的转换,但此包其实还有其它相当多的应用。 Overleaf 上有一个非常有用的教程。

4.1. standalone 的转换命令配置

standalone 本身的 说明文档 已经对配置有详尽的说明,此处重点说一下转换成图片需要用到的 convert 选项。

配置 convert 需要在 documentclass 中进行。以下是一个利用 pdf2svg 转换为SVG的范例配置。

1
2
3
\documentclass[tikz, convert, convert={outext=.svg, command=\unexpanded{
pdf2svg \infile\space \outfile\space all
}}]{standalone}

其中,

  • tikz

    此选项告诉 standalone LaTex文档中存在 tikz 图片。

  • convert

    此选项开启 standalone 的转换功能。

  • convert={}

    此选项是 convert 的详细配置项。

  • outext=.svg

    设置输出文件的后缀名为“.svg”。更详细的说明请参看 standalone 本身的 说明文档 中的表1。

  • command=\unexpanded{}

    此项是将要调用系统运行的命令。

  • pdf2svg

    调用的转换工具。

    注解

    pdf2svg 的语法

    pdf2svg 的语法可以参看 这里

    其中,将一多页PDF转换为分页的多个SVG的语法为:

    pdf2svg <输入文件名>.pdf <输出文件名>%d.svg all
    

    注意,尖括号,及其所包裹中的内容需要替换为所需的文件名。

  • \infile

    输入文件名,包含后缀名。默认后缀名为“.pdf”或“.ps”。 更详细的说明请参看 standalone 本身的 说明文档 中的表1。

  • \space

    空格。若不使用此参数,\infile 后不会有空格,无论你实际上键入了多少个。 \outfile 也是这样。

  • \outfile

    输出文件名,包含后缀名。默认后缀名为“.png”。此处已经通过 outext 更改为“.svg”。更详细的说明请参看 standalone 本身的 说明文档 中的表1。

SVG配置范例中之命令将会被翻译为如下(可以通过查看LOG文件确认)。其中, mew_to_svg 为所用的TEX文件的文件名。

1
pdf2svg mwe_to_svg.pdf mwe_to_svg-%01d.svg all

由此可以看出,转换的重点,是要把 convert={} 中的配置正确设置,以令LaTex将其翻译成正确的系统命令来进行图片的转换。用户可以把多个系统命令整合为一行,以做出丰富多彩的组合来达成不同的目标(在Windows中可以通过“&”或“&&”把多行命令合并为一行)。在 转换流程 中将会详细叙述各种图片转换的流程。

注解

运行系统命令

其实在本小结就可以看出,既然 standalone 可以调用以上的命令,那当然也可以调用其它系统命令。理论上,用户可以调用各种命令来做各种事,不仅仅是图片的转换。如果你有兴趣,应该可以做到编译完后自动上传到某个网络位置,或者删除整个硬盘这一类有趣的事情。

4.2. 编译命令

standalone 需要在编译时使用 -shell-escape 参数。一个使用 xelatexmew_to_svg.tex 进行编译的命令如下(用 xelatex 是因为需要处理中文)。如果你使用LaTex编辑器进行书写,比如 TEXsutdio ,则需要在其中编辑其命令。你也当然可以直接在TEX文件所在之目录下打开CMD,用命令直接编译。

1
xelatex -synctex=1 -interaction=nonstopmode -shell-escape mwe_to_svg.tex

4.3. 简例

以下提供一个转换为单页多个SVG的简例。详细的例子会在后文说明。

以下的文件可以在此项目的根目录和 mew 文件夹中找到。

主文件:

mew 文件夹中的 mwe_to_svg.tex

 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
% 这是一个将tikz图片转换成多张SVG的示例文件,使用pdf2svg来实现转换
% 這是一個將tikz圖片轉換成多張SVG的示例文件,使用pdf2svg來實現轉換
% This is a demo file for tikz to multiple SVGs using pdf2svg
\documentclass[tikz, convert, convert={outext=.svg, command=\unexpanded{
    pdf2svg \infile\space \outfile\space all
}}]{standalone}

\usepackage{xeCJK}
\setCJKmainfont{Microsoft YaHei}

\usepackage{scalefnt}
\usepackage{tikz}

% tikz和colour的设定
% tikz和colour的設定
% tikz and colour configs
\input{../configs_tikz.tex}
\input{../configs_colour.tex}

\begin{document}

    % 全局字体缩放
    % 全局字體縮放
    % global font scale
    \scalefont{1.3}

    % tikz图像文档
    % tikz圖像文檔
    % tikz pics file
    \input{../tikz_pics.tex}

\end{document}

tikz配置文件:

本教程根目录中的 configs_tikz.tex

 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
% 以下是关于tikz中画流程图的设置
% 以下是關於tikz中畫流程圖的設置
% configure flowchart shapes
\usetikzlibrary{shapes.geometric, arrows, positioning, calc}

% start, end shape
\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm,
minimum height=1cm,text centered,  text=white, draw=black,
fill=colorStarstop]

% process shape
\tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm,
text centered, text=white, draw=black, fill=colorPro]

% decision shape
\tikzstyle{decision}=[diamond, minimum width=3cm, minimum height=1cm,
text centered, draw=black, fill=colorDec]

% comment shape
\tikzstyle{comment}=[dashed, draw=black, fill=gray!10, minimum width=3cm, minimum height=1cm, text centered]

% docstring shape
\tikzstyle{docstring}=[draw=orange, fill=white, minimum width=50mm, text width=80mm, minimum height=1cm]

% arrows shape
\tikzstyle{arrow} = [ultra thick,->,>=stealth, line width=1.5mm]

% comment shape
\tikzstyle{comment}=[dashed, draw=black, fill=gray!10, minimum width=3cm, minimum height=1cm, text centered]

\tikzset{
    subprocess/.style = {rectangle, draw=black, semithick, fill=orange!30,
        minimum width=#1, minimum height=1cm, inner xsep=3mm, % <-- changed
        text width =\pgfkeysvalueof{/pgf/minimum width}-2*\pgfkeysvalueof{/pgf/inner xsep},
        align=flush center,
        path picture={\draw
            ([xshift =2mm] \ppbb.north west) -- ([xshift= 2mm] \ppbb.south west)
            ([xshift=-2mm] \ppbb.north east) -- ([xshift=-2mm] \ppbb.south east);
        },
    },
    subprocess/.default = 24mm % <-- added
}% end of tikzset

\usetikzlibrary{positioning}

color配置文件:

本教程根目录中的 configs_colour.tex

1
2
3
4
5
6
7
8
9
% 以下是颜色的设置
% 以下是顏色的設置
% colour defs
\usepackage{color}
\definecolor{colorStarstop}{RGB}{174, 23, 21}
\definecolor{colorPro}{RGB}{0, 175, 121}
\definecolor{colorDec}{RGB}{255, 192, 0}
\definecolor{colorYes}{RGB}{51, 153, 51}
\definecolor{colorNo}{RGB}{255, 0, 0}
[1]Ghost In Shell : Standalone Complex