Ready-made markdown2pdf solution with source code for Linux

foreword

Markdown is a great way to write a short article, and sometimes quite a lot of text, with simple italic and bold formatting. Markdown is also good for writing articles with source code included. But sometimes you want to loselessly, dance with a tambourine to overtake it into a regular, well-formed PDF file, and so that there are no problems during conversion, which, for example, I had - you can’t write in Russian in the comments of the source code, too long lines are not transferred, but cut and other minor problems. The instruction will allow you to quickly set up the converter md2pdf without really understanding how it works. The script for a more or less automatic installation is below in the appropriate section.

My sample TeX template for conversion uses the PSCyr font package, which includes support for Microsoft fonts, namely Times New Roman. There were such requirements for a diploma according to GOST. If you know how, you can modify the template to suit your needs. In my own instructions, you will first have to fool around with the PSCyr setting in TexLive. The setup is done in the Linux Mint Mate distribution, for other distributions you may have to google the standard TexLive package folders for your system.

Installing TexLive

Of course, you can install only the necessary parts of this package. But personally, I was frankly too lazy to look for the minimum necessary working installation. To make sure everything works, install the entire TexLive package. It is called texlive-full and weighs a little more than 2 gigabytes, keep this fact in mind. We execute the command:

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

After a sufficiently long installation, you can proceed to the next item.

Installing the Pandoc Converter

Pandoc is a Linux package that allows you to convert some text formats to others. It has a lot of interesting features that you can find yourself on the Internet. We are only interested in the possibility of converting a markdown file to PDF. Check if Pandoc is installed and if not, install it. For example like this:

user@hostname:~$ dpkg -s pandoc

If the output says that it is not installed, install:

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 link, if at the time of reading the article it is not available for some reason, do not despair, it is easy to find it along with the installation instructions by typing something like β€œInstalling PsCyr texlive” in Google. If it is available, then it’s easier for you, download and we will assume that you have unpacked the archive into your home folder and thus the path to the folder contained in the archive looks like ~/PSCyr. Then go to the Terminal and execute the following commands in sequence:

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, find out where the local directory is located texmf... We execute the command:

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

Most likely you have this directory - /usr/local/share/texmf/, and then we do:

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

Well, or you can not bother and run a command that will copy to a folder texmf wherever she is:

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

PSCyr fonts installed, connect to TexLive:

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

LaTeX template for md2pdf conversion

I will not describe exactly how this template is configured, and will simply give it under the spoiler without much explanation. Suffice it to say that it is well configured, at least in terms of how it handles texts with a lot of source code. If you are not satisfied with the size of indents, line spacing, the lack of numbering of sections and subsections, then in my opinion it is quite easy to google the question on the Internet β€œhow to do it in Latex ...” and then your need. If it is not at all clear, write in the comments, I will try to delve into my own settings 4 years ago and describe which line of the template is responsible for what. In the meantime, I will write down 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 catalog /usr/share/texlive/:

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

Give it read permissions:

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

open it under the root and paste the contents hidden under the spoiler below into it:

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

Template content /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}

Save file /usr/share/texlive/template.tex and write a script that will convert the Makrdown file to PDF, creating in the same folder a file called the Markdown file with the prefix .pdf, that is, after the conversion filename.md will appear in the folder. filename.md.pdf. Let's call the script md2pdf and put on the way / usr / bin. Let's execute the commands in sequence:

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 4th line actually contains the conversion command. pay attention to --from=markdown_github. The GitHub version of Markdown is backwards compatible with the original Markdown, so if your text is written in it, you don't have to worry. If your MD file is written in a specific Markdown dialect, then read the Pandoc manual (man pandoc), make sure your implementation is supported by it, and tweak /usr/bin/md2pdf if necessary.

Script for more or less 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 contents hidden under the spoiler, and most likely everything will install itself, the only thing is, copy the TeX template posted under the spoiler above where necessary themselves. Open Terminal and run:

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

Then fill it with the following content:

The contents of the $HOME/installmd2pdf.sh script

#!/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 with the command:

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

Do not forget that /usr/share/texlive/template.tex must be filled in as indicated in the section "LaTeX template for md2pdf conversionΒ» content.

Using md2pdf

Just open the folder with the Markdown file (some_file.md) in the Terminal and run 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, you can build any style of PDF files, you can also convert other formats instead of md, any supported by Pandoc. I dare to hope that one day this instruction will be useful to 3 and a half people.

Source: habr.com

Add a comment