Minimal support for SIRange in LaTeX reader (#6418)

Add support for `\SIRange{firstnumber}{secondnumber}{unit}` provided by siunitx.

An en-dash is used instead of localized "to".
This commit is contained in:
Emerson Harkin 2020-07-23 19:47:32 -04:00 committed by GitHub
parent c37a56e060
commit 1b8f161198
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 0 deletions

View file

@ -259,6 +259,29 @@ dosiunitx = do
emptyOr160 unit,
unit]
-- converts e.g. \SIRange{100}{200}{\ms} to "100 ms--200 ms"
doSIRange :: PandocMonad m => LP m Inlines
doSIRange = do
skipopts
startvalue <- tok
startvalueprefix <- option "" $ bracketed tok
stopvalue <- tok
stopvalueprefix <- option "" $ bracketed tok
unit <- grouped (mconcat <$> many1 siUnit) <|> siUnit <|> tok
let emptyOr160 "" = ""
emptyOr160 _ = "\160"
return . mconcat $ [startvalueprefix,
emptyOr160 startvalueprefix,
startvalue,
emptyOr160 unit,
unit,
"\8211", -- An en-dash
stopvalueprefix,
emptyOr160 stopvalueprefix,
stopvalue,
emptyOr160 unit,
unit]
siUnit :: PandocMonad m => LP m Inlines
siUnit = do
Tok _ (CtrlSeq name) _ <- anyControlSeq
@ -1075,6 +1098,7 @@ inlineCommands = M.union inlineLanguageCommands $ M.fromList
, ("acsp", doAcronymPlural "abbrv")
-- siuntix
, ("SI", dosiunitx)
, ("SIRange", doSIRange)
-- hyphenat
, ("bshyp", lit "\\\173")
, ("fshyp", lit "/\173")

View file

@ -55,3 +55,62 @@
[Para [Str "18.2\160\176C"]]
```
# SIRange tests
## Integer range with simple common units
```
% pandoc -f latex -t native
\SIRange{10}{20}{\gram}
^D
[Para [Str "10\160g\8211\&20\160g"]]
```
```
% pandoc -f latex -t native
\SIRange{35}{9}{\milli\meter}
^D
[Para [Str "35\160mm\8211\&9\160mm"]]
```
```
% pandoc -f latex -t native
\SIRange{4}{97367265}{\celsius}
^D
[Para [Str "4\160\176C\8211\&97367265\160\176C"]]
```
## Decimal range with simple units
```
% pandoc -f latex -t native
\SIRange{4.5}{97367265.5}{\celsius}
^D
[Para [Str "4.5\160\176C\8211\&97367265.5\160\176C"]]
```
## Squared units
```
% pandoc -f latex -t native
\SIRange{10}{20}{\square\meter}
^D
[Para [Str "10\160m\178\8211\&20\160m\178"]]
```
## Ignore round precision
`round-precision` option appears to be ignored by `\SI` as of 7c6dbd37e, so
`\SIRange` will ignore it as well.
```
% pandoc -f latex -t native
\SIRange[round-precision=2]{10}{20}{\gram}
^D
[Para [Str "10\160g\8211\&20\160g"]]
```
```
% pandoc -f latex -t native
\SIRange[round-precision=2]{10.0}{20.25}{\gram}
^D
[Para [Str "10.0\160g\8211\&20.25\160g"]]
```