带有 Linux 源代码的现成 markdown2pdf 解决方案

前言

Markdown 是写短文的好方法,有时可以写很多文本,使用简单的斜体和粗体格式。 Markdown 也适用于编写包含源代码的文章。 但有时你想要无损地,用手鼓跳舞将它变成一个规则的,格式良好的 PDF 文件,这样在转换过程中就没有问题,例如,我有 - 你不能用俄语写源码的注释,太长的行不转,截​​断等小问题。 该说明将允许您快速设置转换器 md2pdf 没有真正理解它是如何工作的。 或多或少自动安装的脚本在下面的适当部分。

我用于转换的示例 TeX 模板使用 PSCyr 字体包,其中包括对 Microsoft 字体的支持,即 宋体. 根据 GOST,文凭有这样的要求。 如果您知道如何操作,您可以修改模板以满足您的需要。 在我自己的说明中,您首先必须在 TexLive 中使用 PSCyr 设置。 该设置在 Linux Mint Mate 发行版中完成,对于其他发行版,您可能需要为您的系统搜索标准的 TexLive 包文件夹。

安装 TexLive

当然,您可以只安装此软件包的必要部分。 但就个人而言,坦率地说,我懒得去寻找最低限度的必要工作安装。 为确保一切正常,请安装整个 TexLive 包。 它被称为 texlive-full 重量略高于 2 GB,请记住这一事实。 我们执行命令:

user@hostname:~$ sudo apt install texlive-full -y

安装足够长的时间后,您可以继续进行下一项。

安装 Pandoc 转换器

Pandoc 是一个 Linux 软件包,可让您将某些文本格式转换为其他文本格式。 它有很多有趣的功能,您可以在 Internet 上找到自己的功能。 我们只对将降价文件转换为 PDF 的可能性感兴趣。 检查是否安装了 Pandoc,如果没有,请安装它。 例如像这样:

user@hostname:~$ dpkg -s pandoc

如果输出显示未安装,请安装:

user@hostname:~$ sudo apt install pandoc -y

为 TexLive 安装 PSCyr 包

首先你需要下载PSCyr。 现在它仍然可用 链接,如果在阅读本文时由于某种原因它不可用,请不要绝望,通过在 Google 中键入“安装 PsCyr texlive”之类的内容很容易找到它以及安装说明。 如果可用,那么下载对您来说更容易,我们假设您已将存档解压缩到您的主文件夹中,因此存档中包含的文件夹的路径如下所示 〜/PSCyr. 然后转到终端并依次执行以下命令:

user@hostname:~$ cd
user@hostname:~$ mkdir ./PSCyr/fonts/map ./PSCyr/fonts/enc
user@hostname:~$ cp ./PSCyr/dvips/pscyr/*.map ./PSCyr/fonts/map/
user@hostname:~$ cp ./PSCyr/dvips/pscyr/*.enc ./PSCyr/fonts/enc/
user@hostname:~$ echo "fadr6t AdvertisementPSCyr "T2AEncoding ReEncodeFont"" > ./PSCyr/fonts/map/pscyr.map

接下来,找出本地目录所在的位置 文本文件. 我们执行命令:

user@hostname:~$ kpsewhich -expand-var='$TEXMFLOCAL'

很可能你有这个目录 - /usr/本地/共享/texmf/,然后我们做:

user@hostname:~$ sudo cp -R ./PSCyr/* /usr/local/share/texmf/

好吧,或者您不必费心运行将复制到文件夹的命令 文本文件 无论她在哪里:

user@hostname:~$ sudo cp -R ./PSCyr/* $(kpsewhich -expand-var='$TEXMFLOCAL')

安装 PSCyr 字体,连接到 TexLive:

user@hostname:~$ sudo texhash
user@hostname:~$ updmap --enable Map=pscyr.map
user@hostname:~$ sudo mktexlsr

用于 md2pdf 转换的 LaTeX 模板

这个模板具体是怎么配置的我就不说了,简单剧透一下,不多做解释。 可以说它配置良好就足够了,至少在它处理带有大量源代码的文本方面是这样。 如果您对缩进的大小、行距、部分和小节的编号不满意,那么在我看来,在互联网上搜索“如何在 Latex 中做...”这个问题很容易然后你的需要。 如果一点都不清楚,写在评论里,我会尽量深究自己4年前的设置,描述模板的哪一行负责什么。 同时,我会在我的电脑上写下我是如何做到的,你可以自己重复或修改它。

创建一个文件 模板.tex 在目录中 /usr/分享/texlive/:

user@hostname:~$ sudo touch /usr/share/texlive/template.tex

给它读取权限:

user@hostname:~$ sudo chmod 444 /usr/share/texlive/template.tex

在根目录下打开它,将隐藏在下面剧透下的内容粘贴到其中:

user@hostname:~$ sudo nano /usr/share/texlive/template.tex

模板内容 /usr/share/texlive/template.tex

documentclass[oneside,final,14pt]{extreport}
usepackage{extsizes}
usepackage{pscyr}
renewcommand{rmdefault}{ftm}
usepackage[T2A]{fontenc}
usepackage[utf8]{inputenc}
usepackage{amsmath}
usepackage{mathtext}
usepackage{multirow}
usepackage{listings}
usepackage{ucs}
usepackage{hhline}
usepackage{tabularx}
usepackage{booktabs}
usepackage{longtable}
usepackage{titlesec}
usepackage{hyperref}
usepackage{graphicx}
usepackage{setspace}
usepackage[center,it,labelsep=period]{caption}
usepackage[english,russian,ukrainian]{babel}
usepackage{vmargin}
newcommand{specialcell}[2][c]{%
    begin{tabular}[#1]{@{}c@{}}#2end{tabular}}
setpapersize{A4}
setmarginsrb {1cm}{1cm}{1cm}{1cm}{0pt}{0mm}{0pt}{13mm}
usepackage{indentfirst}
setlengthparindent{1cm}
renewcommand{baselinestretch}{1}
renewcommandthechapter{}
renewcommandthesection{}
renewcommandthesubsection{}
renewcommandthesubsubsection{}
titleformat
{chapter} % command
{bfseriesnormalsizecentering} % format
{thechapter} % label
{0.5ex} % sep
{
    centering
}
[
vspace{-1.5ex}
] % after-code
titleformat
{section}
[block]
{normalfontbfseries}
{thesection}{0.5em}{}
sloppy
letoldenumerateenumerate
renewcommand{enumerate}{
  oldenumerate
  setlength{itemsep}{1pt}
  setlength{parskip}{0pt}
  setlength{parsep}{0pt}
}
letolditemizeitemize
renewcommand{itemize}{
  olditemize
  setlength{itemsep}{1pt}
  setlength{parskip}{0pt}
  setlength{parsep}{0pt}
}
providecommand{tightlist}{%
  setlength{itemsep}{0pt}setlength{parskip}{0pt}}

titlespacing{subsubsection}{parindent}{3mm}{3mm}
titlespacing{subsection}{parindent}{3mm}{3mm}
usepackage{color}

lstset{
    basicstyle=footnotesizettfamily,
    inputencoding=utf8,
    extendedchars=true,
    showspaces=false,
    keepspaces=true
    showstringspaces=false,
    showtabs=false,
    tabsize=4,
    captionpos=b,
    breaklines=true,
    breakatwhitespace=true,
    breakautoindent=true,
    linewidth=textwidth
}

begin{document}
$if(title)$
maketitle
$endif$
$if(abstract)$
begin{abstract}
$abstract$
end{abstract}
$endif$

$for(include-before)$
$include-before$

$endfor$
$if(toc)$
{
$if(colorlinks)$
hypersetup{linkcolor=$if(toccolor)$$toccolor$$else$black$endif$}
$endif$
setcounter{tocdepth}{$toc-depth$}
tableofcontents
}
$endif$
$if(lot)$
listoftables
$endif$
$if(lof)$
listoffigures
$endif$
$body$

$if(natbib)$
$if(bibliography)$
$if(biblio-title)$
$if(book-class)$
renewcommandbibname{$biblio-title$}
$else$
renewcommandrefname{$biblio-title$}
$endif$
$endif$
bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}

$endif$
$endif$
$if(biblatex)$
printbibliography$if(biblio-title)$[title=$biblio-title$]$endif$

$endif$
$for(include-after)$
$include-after$

$endfor$
end{document}

保存文件 /usr/share/texlive/template.tex 并编写一个脚本,将 Makrdown 文件转换为 PDF,在同一文件夹中创建一个名为 Markdown 文件的文件,前缀为 .pdf,即转换后 文件名.md 会出现在文件夹中。 文件名.md.pdf. 让我们调用脚本 md2pdf 并放在路上 / usr / bin. 让我们按顺序执行命令:

user@hostname:~$ cd
user@hostname:~$ touch md2pdf
user@hostname:~$ echo "#!/bin/bash" > md2pdf
user@hostname:~$ echo "pandoc --output=$1.pdf --from=markdown_github --latex-engine=pdflatex --listings --template=/usr/share/texlive/template.tex $1" >> md2pdf
user@hostname:~$ sudo cp md2pdf /usr/bin/
user@hostname:~$ sudo chmod 111 /usr/bin/md2pdf

第 4 行实际上包含转换命令。 注意 --from=markdown_github. GitHub 版本的 Markdown 向后兼容原始 Markdown,所以如果你的文本是用它写的,你不必担心。 如果您的 MD 文件是用特定的 Markdown 方言编写的,那么请阅读 Pandoc 手册(man pandoc), 确保它支持你的实现,并调整 /usr/bin/md2pdf 如有必要。

用于或多或少自动安装的脚本

如果你根本不想配置任何东西,并且你有一个类似 Ubuntu 的发行版,你可以尝试创建一个脚本,其中的内容隐藏在剧透下,很可能一切都会自行安装,唯一的事情是,复制TeX 模板在上面的扰流板下发布,必要时自己使用。 打开终端并运行:

user@hostname:~$ cd
user@hostname:~$ touch installmd2pdf.sh

然后用以下内容填充它:

$HOME/installmd2pdf.sh 脚本的内容

#!/bin/bash
cd /tmp
sudo apt install texlive-full pandoc -y
wget http://blog.harrix.org/wp-content/uploads/2013/02/PSCyr.zip
unzip -qq PSCyr.zip
cd
mkdir ./PSCyr/fonts/map ./PSCyr/fonts/enc
cp ./PSCyr/dvips/pscyr/*.map ./PSCyr/fonts/map/
cp ./PSCyr/dvips/pscyr/*.enc ./PSCyr/fonts/enc/
echo "fadr6t AdvertisementPSCyr "T2AEncoding ReEncodeFont"" > ./PSCyr/fonts/map/pscyr.map
sudo cp -R ./PSCyr/* $(kpsewhich -expand-var='$TEXMFLOCAL')
sudo texhash
updmap --enable Map=pscyr.map
sudo mktexlsr
sudo touch /usr/share/texlive/template.tex
touch md2pdf
echo "#!/bin/bash" > md2pdf
echo "pandoc --output=$1.pdf --from=markdown_github --latex-engine=pdflatex --listings --template=/usr/share/texlive/template.tex $1" >> md2pdf
sudo cp md2pdf /usr/bin/
sudo chmod 111 /usr/bin/md2pdf

使用以下命令运行它:

user@hostname:~$ sudo bash $HOME/installmd2pdf.sh

不要忘记那个 /usr/share/texlive/template.tex 必须按照“用于 md2pdf 转换的 LaTeX 模板“ 内容。

使用 md2pdf

只需打开包含 Markdown 文件的文件夹 (一些_文件.md) 在终端中运行命令:

user@hostname:~$ md2pdf some_file.md

结果,文件夹中会出现一个文件 一些_文件.md.pdf.

结论

基于描述的方法,你可以构建任何风格的PDF文件,你也可以转换其他格式而不是md,Pandoc支持的任何格式。 我敢于希望有一天这个说明对 3 个半人有用。

来源: habr.com

添加评论