A ready-made markdown2pdf solution with source code for Linux

Preface

Markdown is a great way to write a small article, and sometimes even quite a lengthy text, with simple formatting such as italics and bold font. Markdown is also good for writing articles that include source code. However, sometimes you want to convert it into a conventional, well-formatted PDF file without any issues during conversion, like the problems I encountered — you cannot write in Russian in the source code comments, excessively long lines are truncated instead of wrapped, and other minor issues. This guide will help you quickly set up the converter. md2pdf without particularly understanding how it works. The script for a more or less automatic installation is below in the corresponding section.

My TeX template for conversion uses the PSCyr font package, which includes support for Microsoft fonts, namely Times New Roman. Those were the requirements for the diploma according to GOST. If you can, feel free to modify the template to suit your needs. In my guide, however, you will initially have to deal with configuring PSCyr in TexLive. The setup is done in the Linux Mint Mate distribution; for other distributions, you may need to search for the standard folders for the TexLive package for your system.

Installing TexLive

Of course, you can install only the necessary parts of this package. But I personally was honestly too lazy to look for the minimally required working installation. To ensure everything works correctly, we install the entire TexLive package. It is called texlive-full and weighs just over 2 gigabytes, keep this in mind. Execute the command:

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

After a fairly long installation, you can move on to the next step.

Installing the Pandoc Converter

Pandoc is a Linux package that allows converting certain text formats into others. It has many interesting features that you can explore online. We are only interested in the capability to convert a markdown file to PDF. Let's check if Pandoc is installed and if not, we will install it, for example, like this:

user@hostname:~$ dpkg -s pandoc

If the output says it is not installed — we install it:

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

Installing the PSCyr package for TexLive

First, you need to download PSCyr. For now, it is still available at this this link, if at the time of reading this article it is for some reason unavailable, do not despair, it is not difficult to find along with the installation instructions by searching Google for something like "Installing PsCyr texlive". If it is available, then it's easier for you, download it, and we will assume that you have unpacked the archive in your home folder, so the path to the folder contained in the archive looks like ~\/PSCyr. Then we open the Terminal and sequentially execute the commands:

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

Next, we will find out where the local directory is located texmf. Execute the command:

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

Most likely, this directory for you is — /usr/local/share/texmf/, and then we execute:

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

Or you could just run the command that will copy it to the folder texmf wherever it is:

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

The PSCyr fonts are installed, we connect them in TexLive:

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

LaTeX template for converting md2pdf

I will not describe how this template is configured and will simply provide it under the spoiler without special explanations. It is enough to say that it is configured fairly well, at least in the way it handles texts with lots of source code. If you are not happy with the indentation sizes, line spacing, or the absence of section and subsection numbering, then in my opinion it is quite easy to Google the question on the internet "how in LaTeX to make…" and then whatever your needs are. If it's completely unclear, write in the comments, and I will try to delve into my own settings from four years ago to explain which line of the template does what. For now, I will describe how I did it on my PC, and you are free to repeat or modify it for yourself.

Create a file template.tex in the directory /usr/share/texlive/:

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

Set it read permissions:

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

open it as root and paste the content hidden under the spoiler below:

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

Content of the template \/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}

Saving the file /usr/share/texlive/template.tex and writing a script that will convert the Markdown file to PDF, creating a file in the same folder with the same name as the Markdown file but with a .pdf suffix after conversion. filename.md will appear in the folder filename.md.pdf. We'll name the script md2pdf and place it at the path /usr/bin. We will sequentially execute the commands:

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

The conversion command is specified in line 4. Note that —from=markdown_githubThe GitHub version of Markdown is backward compatible with the original Markdown, so if your text is written in it, there's no need to worry. However, if your MD file is written in a specific dialect of Markdown, make sure to check the Pandoc manual (man pandoc), ensure that your implementation supports it, and make adjustments /usr/bin/md2pdf if necessary.

A script for a relatively automatic installation

If you don't really want to configure anything at all, and you have an Ubuntu-like distribution, you can try creating a script with the content hidden under the spoiler, and most likely everything will install itself. You just need to copy the TeX template provided in the spoiler above where necessary. Open the Terminal and run:

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

Next, fill it with the following content:

Content of the script $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

Run it using the command:

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

Remember that /usr/share/texlive/template.tex you need to fill it with the content specified in the section "LaTeX template for converting md2pdf".

Using md2pdf

Just open the folder with the Markdown file (some_file.md) in the Terminal, and execute the command:

user@hostname:~$ md2pdf some_file.md

As a result, a file will appear in the folder some_file.md.pdf.

Conclusion

Based on the described method, any style of PDF files can be created, and you can also convert other formats instead of md, any supported by Pandoc. I hope that someday this instruction will be useful to three and a half people.

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster