Recognise usual functions even without the leading '\'

This commit is contained in:
Kerl13 2016-03-24 09:27:55 +01:00
parent f3735c4e47
commit 0efa4c9ed1
4 changed files with 49 additions and 16 deletions

View File

@ -1,7 +1,7 @@
SOURCES = $(wildcard *.md) SOURCES = $(wildcard *.md)
OUT = $(SOURCES:.md=.pdf) OUT = $(SOURCES:.md=.pdf)
HS_FILTERS_NAMES = nice-frac HS_FILTERS_NAMES = nice-frac usual-fun
PY_FILTERS_NAMES = pandoc-svg.py PY_FILTERS_NAMES = pandoc-svg.py
EXT_FILTERS = pandoc-crossref EXT_FILTERS = pandoc-crossref
@ -13,11 +13,11 @@ FILTERS = $(PY_FILTERS) $(HS_FILTERS) $(EXT_FILTERS)
all: $(HS_FILTERS) $(OUT) all: $(HS_FILTERS) $(OUT)
$(OUT): $(SOURCES) $(OUT):
pandoc --smart -t latex $< $(addprefix --filter=, $(FILTERS)) -o $@ pandoc -S -t latex $(basename $@).md $(addprefix --filter=, $(FILTERS)) -o $@
$(HS_FILTERS): $(addsuffix .hs, $(HS_FILTERS)) $(HS_FILTERS):
ghc --make -O2 $< -o $@ ghc --make $@.hs -o $@
clean: clean:
rm -f $(OUT) rm -f $(OUT)

View File

@ -7,19 +7,19 @@ Pandoc.
Includes various Pandoc filters: Includes various Pandoc filters:
* Automatically convert `SVG` files to `PDF` and rewrite the image link to * 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 * 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 * Convert simple fractions like "(n / k)" to LaTeX's "\\frac{n}{k}" when n and
math environment. 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 ## 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 * anything else like this that could help writing more readable LaTeX
code :) code :)

View File

@ -9,12 +9,12 @@ space = "[ \\t\\n\\r]*"
num :: String num :: String
num = "([0-9]+)" 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 :: Regex
fracReg = mkRegex $ fracReg = mkRegex $
"\\(" ++ space ++ num ++ space ++ "/" ++ space ++ num ++ "\\)" "\\(" ++ space ++ num ++ space ++ "/" ++ space ++ num ++ space ++ "\\)"
frac :: String -> String 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 -- Apply the substitution to all Latex parts on the AST
niceFrac :: Inline -> Inline niceFrac :: Inline -> Inline

33
filters/usual-fun.hs Normal file
View File

@ -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