From f3735c4e4796ba5d93dd2b35d2d3f685d561defc Mon Sep 17 00:00:00 2001 From: Kerl13 Date: Wed, 23 Mar 2016 18:56:19 +0100 Subject: [PATCH] Add a filter for simple fractions --- Makefile | 22 ++++++++++++++++++++-- README.md | 8 +++----- filters/nice-frac.hs | 26 ++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 filters/nice-frac.hs diff --git a/Makefile b/Makefile index 4308a74..eb74745 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,29 @@ SOURCES = $(wildcard *.md) OUT = $(SOURCES:.md=.pdf) -all: $(OUT) +HS_FILTERS_NAMES = nice-frac +PY_FILTERS_NAMES = pandoc-svg.py +EXT_FILTERS = pandoc-crossref + +HS_FILTERS = $(addprefix filters/, $(HS_FILTERS_NAMES)) +PY_FILTERS = $(addprefix filters/, $(PY_FILTERS_NAMES)) +FILTERS = $(PY_FILTERS) $(HS_FILTERS) $(EXT_FILTERS) + +.SUFFIXES: .hs + +all: $(HS_FILTERS) $(OUT) $(OUT): $(SOURCES) - pandoc --smart -t latex $< --filter=filters/pandoc-svg.py --filter=pandoc-crossref -o $@ + pandoc --smart -t latex $< $(addprefix --filter=, $(FILTERS)) -o $@ + +$(HS_FILTERS): $(addsuffix .hs, $(HS_FILTERS)) + ghc --make -O2 $< -o $@ clean: rm -f $(OUT) + rm -f $(addprefix filters/, $(HS_FILTERS)) find . -name "*.pandoc.pdf" -delete + +deepclean: clean + rm -f $(HS_FILTERS) + rm -f filters/*.hi filters/*.o diff --git a/README.md b/README.md index dabd936..7d34c57 100644 --- a/README.md +++ b/README.md @@ -12,16 +12,14 @@ Includes various Pandoc filters: * Include [pandoc-crossref](https://github.com/lierdakil/pandoc-crossref) for easy numbering and referencing. +* Convert fractions like "(n/k)" to "\frac{n}{k}" when n and k are integers in + math environment. ## TODO -* automatically convertfunction names (cos, sin, exp, log) to their +* automatically convert function names (cos, sin, exp, log) to their LaTeX equivalent (\cos and so on) when in a mathematical environment, then taking out the useless leading `\`. -* not having to use \frac{}{} to write fractions, but instead -automatically convert simple fractions in math environment to \frac{}{} -(for instance convert "(1 / 2)" to "\frac{1}{2}") - * anything else like this that could help writing more readable LaTeX code :) diff --git a/filters/nice-frac.hs b/filters/nice-frac.hs new file mode 100644 index 0000000..9cdd8b4 --- /dev/null +++ b/filters/nice-frac.hs @@ -0,0 +1,26 @@ +-- nice-frac.hs + +import Text.Pandoc.JSON +import Text.Regex + +-- Usefull regex elements +space :: String +space = "[ \\t\\n\\r]*" +num :: String +num = "([0-9]+)" + +-- Substitute latex fractions like '\frac{42}{1}' for (42 / 1) +fracReg :: Regex +fracReg = mkRegex $ + "\\(" ++ space ++ num ++ space ++ "/" ++ space ++ num ++ "\\)" +frac :: String -> String +frac s = subRegex fracReg s "\\frac{\\1}{\\2}" + +-- Apply the substitution to all Latex parts on the AST +niceFrac :: Inline -> Inline +niceFrac (Math t s) = Math t (frac s) +niceFrac x = x + +main :: IO () +main = toJSONFilter niceFrac +