帶有 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 個半人有用。

來源: www.habr.com

添加評論