git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: Support squared, cubed, tothe in siunitx.

Closes #6657.

commit529eb696dccd0a1923525e5fcb297a76cafa1ebe authorJohn MacFarlane <jgm@berkeley.edu> dateWed 2 Sep 2020, 11:06 (6 years ago) parent81fe8eb
2 files changed+63−22
src/Text/Pandoc/Readers/LaTeX/SIunitx.hs+22−12 test/command/3587.md+41−10
src/Text/Pandoc/Readers/LaTeX/SIunitx.hs+22−12
@@ -21,7 +21,7 @@ dosiunitx tok = do
2121 skipopts
2222 value <- tok
2323 valueprefix <- option "" $ bracketed tok
24− unit <- rawUnit tok
24+ unit <- grouped (siUnit tok) <|> tok
2525 return . mconcat $ [valueprefix,
2626 emptyOr160 valueprefix,
2727 value,
@@ -36,7 +36,7 @@ doSIrange tok = do
3636 startvalueprefix <- option "" $ bracketed tok
3737 stopvalue <- tok
3838 stopvalueprefix <- option "" $ bracketed tok
39− unit <- rawUnit tok
39+ unit <- grouped (siUnit tok) <|> tok
4040 return . mconcat $ [startvalueprefix,
4141 emptyOr160 startvalueprefix,
4242 startvalue,
@@ -49,23 +49,33 @@ doSIrange tok = do
4949 emptyOr160 unit,
5050 unit]
5151
52−rawUnit :: PandocMonad m => LP m Inlines -> LP m Inlines
53−rawUnit tok = grouped (mconcat <$> many1 (siUnit tok)) <|> siUnit tok <|> tok
54−
5552emptyOr160 :: Inlines -> Inlines
5653emptyOr160 x = if x == mempty then x else str "\160"
5754
5855siUnit :: PandocMonad m => LP m Inlines -> LP m Inlines
5956siUnit tok = do
6057 Tok _ (CtrlSeq name) _ <- anyControlSeq
61− if name == "square"
62− then do
63− unit <- rawUnit tok
64− return $ unit <> "\178"
65− else
58+ case name of
59+ "square" -> do
60+ unit <- siUnit tok
61+ return $ unit <> superscript "2"
62+ "cubic" -> do
63+ unit <- siUnit tok
64+ return $ unit <> superscript "3"
65+ "raisetothe" -> do
66+ n <- tok
67+ unit <- siUnit tok
68+ return $ unit <> superscript n
69+ _ ->
6670 case M.lookup name siUnitMap of
67− Just il -> return il
68− Nothing -> mzero
71+ Just il ->
72+ option il $
73+ choice
74+ [ (il <> superscript "2") <$ controlSeq "squared"
75+ , (il <> superscript "3") <$ controlSeq "cubed"
76+ , (\n -> il <> superscript n) <$> (controlSeq "tothe" *> tok)
77+ ]
78+ Nothing -> tok
6979
7080siUnitMap :: M.Map Text Inlines
7181siUnitMap = M.fromList
test/command/3587.md+41−10
@@ -13,13 +13,6 @@
1313```
1414
1515
16−```
17−% pandoc -f latex -t native
18−\SI[round-precision=2]{\{\}}[\{\}]{\{\}}
19−^D
20−[Para [Str "{}\160{}\160{}"]]
21−```
22−
2316```
2417% pandoc -f latex -t native
2518\SI{30}{\milli\meter}
@@ -38,7 +31,7 @@
3831% pandoc -f latex -t native
3932\SI{25}{\square\meter}
4033^D
41−[Para [Str "25\160m\178"]]
34+[Para [Str "25\160m",Superscript [Str "2"]]]
4235```
4336
4437```
@@ -87,15 +80,53 @@
8780[Para [Str "4.5\160\176C\8211\&97367265.5\160\176C"]]
8881```
8982
90−## Squared units
83+## Squared, cubed etc. units
9184
9285```
9386% pandoc -f latex -t native
9487\SIrange{10}{20}{\square\meter}
9588^D
96−[Para [Str "10\160m\178\8211\&20\160m\178"]]
89+[Para [Str "10\160m",Superscript [Str "2"],Str "\8211\&20\160m",Superscript [Str "2"]]]
9790```
9891
92+```
93+% pandoc -f latex -t native
94+\SIrange{10}{20}{\cubic\meter}
95+^D
96+[Para [Str "10\160m",Superscript [Str "3"],Str "\8211\&20\160m",Superscript [Str "3"]]]
97+```
98+
99+```
100+% pandoc -f latex -t native
101+\SIrange{10}{20}{\raisetothe{4}\meter}
102+^D
103+[Para [Str "10\160m",Superscript [Str "4"],Str "\8211\&20\160m",Superscript [Str "4"]]]
104+```
105+
106+
107+```
108+% pandoc -f latex -t native
109+\SIrange{10}{20}{\meter\squared}
110+^D
111+[Para [Str "10\160m",Superscript [Str "2"],Str "\8211\&20\160m",Superscript [Str "2"]]]
112+```
113+
114+```
115+% pandoc -f latex -t native
116+\SIrange{10}{20}{\meter\cubed}
117+^D
118+[Para [Str "10\160m",Superscript [Str "3"],Str "\8211\&20\160m",Superscript [Str "3"]]]
119+```
120+
121+```
122+% pandoc -f latex -t native
123+\SIrange{10}{20}{\meter\tothe{4}}
124+^D
125+[Para [Str "10\160m",Superscript [Str "4"],Str "\8211\&20\160m",Superscript [Str "4"]]]
126+```
127+
128+
129+
99130## Ignore round precision
100131
101132`round-precision` option appears to be ignored by `\SI` as of 7c6dbd37e, so