@adrianjost/md2tex

1.3.0 • Public • Published

An opinionated Markdown to Latex Converter

Build Status codecov

npm (scoped) npm npm bundle size (scoped)

Warning: This Code is a total mess! I am sorry for that. But it has 100% test coverage so whenever someone introduces a new hack to get it working, we can be confident that nothing unexpectedly breaks.

All features were developed for compatibility with the modernthesis template.

How to use it

As a standalone converter

Install

# yarn
yarn global add @adrianjost/md2tex

# or npm
npm i -g @adrianjost/md2tex

Usage

md2tex "pathToSrcMdFile" "pathToTargetTexFile"
  • The pathToSrcMdFile default to ./in.md.
  • The pathToTargetTexFile default to ./out.tex.

If you provide a directory path instead of a file for both paths, all .md files will get converted to the output directory. The Filestructure will remain the same.

As a regular dependency

Install

# yarn
yarn add -D @adrianjost/md2tex

# or npm
npm i -D @adrianjost/md2tex

Usage

import md2tex from "@adrianjost/md2tex";
// or
// const { convert: md2tex } = require("@adrianjost/md2tex")

const md = `# Hello World`;
const tex = md2tex(md);
console.log(tex);

Features

You can use a codeblock with the language latex to write latex code directly in your markdown files. This Code is getting directly copied into the output without any further conversion.

Headlines

Input:

# H1
## H2
### H3
#### H4
##### H5
###### H6

Output:

%************************************************
\chapter{H1}
\label{ch:0-h1}
%************************************************
\hypertarget{1-h2}{
\section{H2}\label{1-h2}}
\hypertarget{2-h3}{
\subsection{H3}\label{2-h3}}
\hypertarget{3-h4}{
\subsubsection{H4}\label{3-h4}}
\hypertarget{4-h5}{
\paragraph{H5}\label{4-h5}}
\hypertarget{5-h6}{
\subparagraph{H6}\label{5-h6}}

Text Styles

Input:

**bold** or **bold**
_italic_ or _italic_
`inline code`

Output:

\textbf{bold} or \textbf{bold} \\
\textit{italic} or \textit{italic} \\
\colorbox{gray-light!}{\texttt{inline code}}

Links

Links get converted to footnotes or BibLatex Source References

Input:

[descrition](footnote)

[source](`reference`)

Output:

description\\footnote{footnote}

description\\cite{reference}

Images

Input:

![description](path)

Output:

\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{path}
	\caption[description]{description}
	\label{fig:description}
\end{figure}

Codeblocks

🌟 Please note the special syntax for captions.

Input:

```js [some caption]
console.log("Hi");
```[description]

Output:

\begin{listing}[H]
	\begin{minted}{js}
console.log("Hi");
	\end{minted}
	\caption{some caption}
	\label{lst:code-snipped-undefined}
\end{listing}

Lists

Input:

- list item 1
	- indentation 1
- list item 2

1. first
2. second

Output:

\begin{itemize}
	\item list item 1
	\begin{itemize}
		\item indentation 1
	\end{itemize}
	\item list item 2
\end{itemize}

\begin{enumerate}
	\item first

	\item second
\end{enumerate}

Known Limitations

  • Currently, this code can't convert tables
  • It is also not possible to use inline styles in headlines like # _italic_ headline
  • Not all characters get escaped correctly. It's not working inside headlines and normal text without any formatting.

Contribute

You wan't to contribute? Feel free to do so!

If you have found a bug, just add a snapshot test to the tests located at __tests__/md2tex.test.js and open a pull request.

Dependencies (0)

    Dev Dependencies (9)

    Package Sidebar

    Install

    npm i @adrianjost/md2tex

    Weekly Downloads

    22

    Version

    1.3.0

    License

    MIT

    Unpacked Size

    51.1 kB

    Total Files

    7

    Last publish

    Collaborators

    • adrianjost