16

Sometimes, I have notes where I would not like to convert all portions to Tex format. I normally then simply put such contexts under verbatim. However, this is a rather crude approach. So I started to wonder if you can use Markdown inside Tex. I was originally inspired by this Hamilton's code where he uses R inside Tex, after he made his package available.

Dummy example of data which I would not like to convert to Tex:

% http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3458956/
ABC
\textbf{All here are antiseptics} % (used in living tissues on mucosa)
% Antiseptic is often disinfection
1 Iodum (Iodine)
2 Povidonum iodinatum
3 Ichthammolum (ammonia)
4 Viride nitens
5 Kalii permanganas (potassium permanganate)
6 Hydrogeniii peroxidum
7 Benzoylis peroxidum
8 Nitrofuralum
9 Acidum boricum
10 Sulfadiazinum argentum (silver sulfadiazine)
11 Benzalkonium
12 Propanolum/Benzalconium
13 Chlorhexidium
14 Cetylpyridinium
15 Dequalinium/Cinchocainum
16 Colini salicylas (salt of salicylic acid - colini adds here saliva production)
17 Hexetidinum (hexetidine)

% only 120 main drugs in exam - from 6 classes theoretically
% much more studied in practcals

A group
1 Halogen
2 Halogen
3 Aromatic compound
4 Dye compounds
5 Oxidizing agent
6 Oxidizing agent
7 Oxidizing agent
8 Nitrofurans
9 Acids 
10 Heavy metal (silver)
% Synthetic antibacterial
% Antibiotics are produced by living organisms (fungi, bacteria, actinomyces, ...)
...

OS: Debian 8.5
Linux kernel: 4.6 backports
Hardware: Asus Zenbook UX303UA
TeXLive: the latest, not the old one in apt-get

4
  • You probably want to use lex or some equivalent tool for that.
    – jub0bs
    Commented Oct 31, 2014 at 12:50
  • 2
    you can use LuaTeX with lunamark - github.com/jgm/lunamark. It has output format for LaTeX, only escaping of LaTeX special characters must be disabled if one want to mix markdown and LaTeX in the input. I can make some example later
    – michal.h21
    Commented Oct 31, 2014 at 13:08
  • Maybe this might help: tex.stackexchange.com/a/101731/29873
    – DG'
    Commented Oct 31, 2014 at 13:59
  • May be the package wiki or the whole nicetext bundle.
    – Fran
    Commented Oct 31, 2014 at 22:59

2 Answers 2

9

There is a new package for the inclusion of Markdown in plain TeX, LaTeX, and ConTeXt documents. Much like the package suggested by Michal in the accepted answer, this package is also based on the Lunamark parser. This time around, the parser was heavily modified, so that it allows the user to control the expansion of Markdown tokens to TeX tokens:

\documentclass{article}
\usepackage{markdown}
\markdownSetup{
  renderers = {
    link     = {#1},        % Render a link as the link label.
    emphasis = {\emph{#1}}, % Render emphasis using `\emph`.
  }
}
\begin{document}
\begin{markdown}
  _Hello,_ [Stack Exchange](http://tex.stackexchange.com)!
\end{markdown}
\end{document}

The above code produces “Hello, Stack Exchange!”. Note that setting up these mappings is not mandatory (though often useful); the defaults should be quite sane. Package authors are encouraged to redefine renderer prototypes (see section 2.2.4 in the package documentation) to override the defaults without overriding the user mappings.

Any engine that supports shell access (pdfTeX, XeTeX) can be used, not just LuaTeX. Note, however, that you need to provide the --shell-escape option or set shell_escape=t in the texmf.cnf file when you don't use LuaTeX. This is similar to other packages (such as Geoffrey M. Poore's minted) that require shell access.

The package also supports syntax extensions such as citations, fenced code blocks, and other (see section 2.1.2 in the package documentation), although I did backport these to Lunamark, so they can now be used with Michal's package as well.

8
  • 1
    I am the creator and the maintainer of the package, so I cannot give you an unbiased opinion. I am, however, planning to maintain the package (the maintenance is currently funded as a part of a university research project) and the Overleaf staff think the package works well enough at the moment (twitter.com/overleaf/status/763395560682364928) if that adds any credibility.
    – Witiko
    Commented Oct 19, 2016 at 21:43
  • 1
    Lunamark does not currently support any Markdown syntax extensions for tables, although they are on the roadmap (patches are welcome).
    – Witiko
    Commented Oct 19, 2016 at 21:54
  • 1
    The package is already present in the latest version of TeX Live 2016. The installation instructions are covered in README.md. Feel free to create the missing folders.
    – Witiko
    Commented Oct 19, 2016 at 22:30
  • 1
    It seems to me that the author is aiming to create a graphical table editor that outputs LaTeX code whereas the Markdown package enables writing documents in a lightweight markup language of Markdown -- I do not think there is much overlap at the moment. There is currently no table syntax support in Lunamark, although the markdown.sty package allows you to embed tables in CSV (not a very complex markup to be sure) using the content blocks syntax extension.
    – Witiko
    Commented Jun 5, 2017 at 19:09
  • 1
    Other than the CSVs, there is currently no table support. For a run-down of the available syntax, see the example document. A lot has changed since the (initial) version from TeX Live 2016; see the release notes for version 2.0.0 and beyond. There will also be an article introducing the package in the next issue of TUGboat.
    – Witiko
    Commented Jun 5, 2017 at 19:59
7

Edit:

I've put this code on github - latexmark, and extended little bit.

New package options are provided:

extensions: select lunamark extensions

document: process whole everything between \begin{document} ... \end{document}

template: select Cosmo template which will be used for printing converted text

new command was added:

\markdownfile{filename}

typeset markdown file.


It is possible to use lunamark with LuaTeX, but it is little bit harder to install it. You need to add to your local texmf tree (usually ~/texmf/scripts/lua, you may need to create this directory) new directory lunamark and copy contents of standalone/src directory from lunamark sources.

Test if that works with command:

kpsewhich lunamark.lua

path to this file should be printed.

After successful installation, we can create simple lua module for lunamark loading, latexmark.lua

local latexmark = {}
local lunamark = require("lunamark")
local writer = lunamark.writer.latex.new()
local util = lunamark.util
writer.string = function(s) return s end
writer.definitionlist= function(items)
    local buffer = {}
    for _,item in ipairs(items) do
      local defs = {}
      buffer[#buffer + 1] = {"\\item[",item.term,"] ", util.intersperse(item.definitions,"\n")}
    end
    local contents = util.intersperse(buffer, "\n")
    return {"\\begin{description}\n",contents,"\n\\end{description}"}
 end
latexmark.init = function(extensions)
   local extensions = extensions or { smart = true, definition_lists=true}
   latexmark.parser = lunamark.reader.markdown.new(writer, extensions)
end

latexmark.end_env = "%s*\\end{latexmark}"
local buffer = {}
latexmark.callback = function(buf)
  if buf:match(latexmark.end_env) then
    --local ret =  latexmark.process() 
    --table.insert(ret, buf)
    return ret
  end
  buffer[#buffer+1] = buf
  return ''
end

latexmark.process = function()
  local result = latexmark.parser(table.concat(buffer,"\n"))
  buffer = {}
  local lines = string.explode(result,"\n")
  for _, line in ipairs(lines) do
    print(line)
    tex.print(line)
  end
  return lines
end

return latexmark

lunamark is being initialized and function for LuaTeX's process_input_filter is provided. now some simple LaTeX package, latexmark.sty:

\ProvidesPackage{latexmark}

\RequirePackage{luatexbase}

\directlua{%
  latexmark = require "latexmark"
  latexmark.init()
}


\newenvironment{latexmark}
{\directlua{luatexbase.add_to_callback("process_input_buffer",latexmark.callback,"latexmark")}}
{\directlua{%
    luatexbase.remove_from_callback("process_input_buffer", "latexmark")
    latexmark.process()
}}

\endinput

new environment, latexmark is provided, it's contents will be converted. It is really easy to use:

\documentclass{article}
\usepackage{fontspec}
\usepackage{latexmark}
\begin{document}

\begin{latexmark}
# Hello world

1. enumerated
2. list

some \LaTeX\ stuff: $a = \sqrt{b}$

what
: it is easy to provide description lists

and also `monospaced`, or *emphatized* texts

\end{latexmark}
\end{document}

enter image description here

Some sample file:

4
  • 1
    @Masi I know about it, it is indeed more powerful than my code, I use it myself
    – michal.h21
    Commented Oct 20, 2016 at 7:59
  • It would be great to get some comparison of the two systems. At the moment, I cannot use it because having the bug illustrated in my bounty in my system. - - I made his answer accepted answer here to indicate the recent development in the area. Commented Oct 20, 2016 at 8:04
  • 1
    the newer package has certainly more features and cleaner interface. my solution is more primitive, but on the other hand it doesn't need to create any temporary files, it can convert markdown on the fly
    – michal.h21
    Commented Oct 20, 2016 at 10:37
  • The caching in the above package is deliberate to speed up the processing of large documents and to aid debugging, when the intermediary tex document fails to compile. It is also necessary in engines other than LuaTeX.
    – Witiko
    Commented Oct 21, 2016 at 11:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .