From 0efa4c9ed143fdea7daba83251b63c63d91e95e8 Mon Sep 17 00:00:00 2001 From: Kerl13 Date: Thu, 24 Mar 2016 09:27:55 +0100 Subject: [PATCH] Recognise usual functions even without the leading '\' --- Makefile | 10 +++++----- README.md | 16 ++++++++-------- filters/nice-frac.hs | 6 +++--- filters/usual-fun.hs | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 filters/usual-fun.hs diff --git a/Makefile b/Makefile index eb74745..6b0fda6 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ SOURCES = $(wildcard *.md) OUT = $(SOURCES:.md=.pdf) -HS_FILTERS_NAMES = nice-frac +HS_FILTERS_NAMES = nice-frac usual-fun PY_FILTERS_NAMES = pandoc-svg.py EXT_FILTERS = pandoc-crossref @@ -13,11 +13,11 @@ FILTERS = $(PY_FILTERS) $(HS_FILTERS) $(EXT_FILTERS) all: $(HS_FILTERS) $(OUT) -$(OUT): $(SOURCES) - pandoc --smart -t latex $< $(addprefix --filter=, $(FILTERS)) -o $@ +$(OUT): + pandoc -S -t latex $(basename $@).md $(addprefix --filter=, $(FILTERS)) -o $@ -$(HS_FILTERS): $(addsuffix .hs, $(HS_FILTERS)) - ghc --make -O2 $< -o $@ +$(HS_FILTERS): + ghc --make $@.hs -o $@ clean: rm -f $(OUT) diff --git a/README.md b/README.md index 7d34c57..0fbd102 100644 --- a/README.md +++ b/README.md @@ -7,19 +7,19 @@ Pandoc. Includes various Pandoc filters: * Automatically convert `SVG` files to `PDF` and rewrite the image link to - include them in the document easily. +include them in the document easily. * Include [pandoc-crossref](https://github.com/lierdakil/pandoc-crossref) for - easy numbering and referencing. +easy numbering and referencing. -* Convert fractions like "(n/k)" to "\frac{n}{k}" when n and k are integers in - math environment. +* Convert simple fractions like "(n / k)" to LaTeX's "\\frac{n}{k}" when n and +k are integers in a math environment. + +* 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 `\`. ## TODO -* 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 `\`. - * anything else like this that could help writing more readable LaTeX code :) diff --git a/filters/nice-frac.hs b/filters/nice-frac.hs index 9cdd8b4..a4732a4 100644 --- a/filters/nice-frac.hs +++ b/filters/nice-frac.hs @@ -9,12 +9,12 @@ space = "[ \\t\\n\\r]*" num :: String num = "([0-9]+)" --- Substitute latex fractions like '\frac{42}{1}' for (42 / 1) +-- Replace fractions like "(42 / 1)" by "\frac{42}{1}" fracReg :: Regex fracReg = mkRegex $ - "\\(" ++ space ++ num ++ space ++ "/" ++ space ++ num ++ "\\)" + "\\(" ++ space ++ num ++ space ++ "/" ++ space ++ num ++ space ++ "\\)" frac :: String -> String -frac s = subRegex fracReg s "\\frac{\\1}{\\2}" +frac s = subRegex fracReg s "\\\\frac{\\1}{\\2}" -- Apply the substitution to all Latex parts on the AST niceFrac :: Inline -> Inline diff --git a/filters/usual-fun.hs b/filters/usual-fun.hs new file mode 100644 index 0000000..00fb098 --- /dev/null +++ b/filters/usual-fun.hs @@ -0,0 +1,33 @@ +-- usual-fun.hs + +import Text.Pandoc.JSON +import Text.Regex +import Data.List (intercalate) + +-- Association list for usual functions +usual :: String +usual = intercalate "|" [ + "cos", "sin", "tan", "cotan", + "arccos", "arcsin", "arctan", + "exp", "log"] + +-- Remove the '\' before usual functions' names if present +rmBSReg :: Regex +rmBSReg = mkRegex $ "(\\\\)(" ++ usual ++ ")\\>" +rmBS :: String -> String +rmBS s = subRegex rmBSReg s "\\2" + +-- Add a '\' before usual functions' names +addBSReg :: Regex +addBSReg = mkRegex $ "\\<(" ++ usual ++ ")\\>" +addBS :: String -> String +addBS s = subRegex addBSReg s "\\\\\\0" + +-- Apply the substitution to all Latex parts on the AST +niceFrac :: Inline -> Inline +niceFrac (Math t s) = Math t (addBS . rmBS $ s) +niceFrac x = x + +main :: IO () +main = toJSONFilter niceFrac +