LaTeX reader: Support squared, cubed, tothe in siunitx.

Closes #6657.
This commit is contained in:
John MacFarlane 2020-09-02 11:06:26 -07:00
parent 81fe8ebf36
commit 529eb696dc
2 changed files with 63 additions and 22 deletions

View file

@ -21,7 +21,7 @@ dosiunitx tok = do
skipopts
value <- tok
valueprefix <- option "" $ bracketed tok
unit <- rawUnit tok
unit <- grouped (siUnit tok) <|> tok
return . mconcat $ [valueprefix,
emptyOr160 valueprefix,
value,
@ -36,7 +36,7 @@ doSIrange tok = do
startvalueprefix <- option "" $ bracketed tok
stopvalue <- tok
stopvalueprefix <- option "" $ bracketed tok
unit <- rawUnit tok
unit <- grouped (siUnit tok) <|> tok
return . mconcat $ [startvalueprefix,
emptyOr160 startvalueprefix,
startvalue,
@ -49,23 +49,33 @@ doSIrange tok = do
emptyOr160 unit,
unit]
rawUnit :: PandocMonad m => LP m Inlines -> LP m Inlines
rawUnit tok = grouped (mconcat <$> many1 (siUnit tok)) <|> siUnit tok <|> tok
emptyOr160 :: Inlines -> Inlines
emptyOr160 x = if x == mempty then x else str "\160"
siUnit :: PandocMonad m => LP m Inlines -> LP m Inlines
siUnit tok = do
Tok _ (CtrlSeq name) _ <- anyControlSeq
if name == "square"
then do
unit <- rawUnit tok
return $ unit <> "\178"
else
case name of
"square" -> do
unit <- siUnit tok
return $ unit <> superscript "2"
"cubic" -> do
unit <- siUnit tok
return $ unit <> superscript "3"
"raisetothe" -> do
n <- tok
unit <- siUnit tok
return $ unit <> superscript n
_ ->
case M.lookup name siUnitMap of
Just il -> return il
Nothing -> mzero
Just il ->
option il $
choice
[ (il <> superscript "2") <$ controlSeq "squared"
, (il <> superscript "3") <$ controlSeq "cubed"
, (\n -> il <> superscript n) <$> (controlSeq "tothe" *> tok)
]
Nothing -> tok
siUnitMap :: M.Map Text Inlines
siUnitMap = M.fromList

View file

@ -13,13 +13,6 @@
```
```
% pandoc -f latex -t native
\SI[round-precision=2]{\{\}}[\{\}]{\{\}}
^D
[Para [Str "{}\160{}\160{}"]]
```
```
% pandoc -f latex -t native
\SI{30}{\milli\meter}
@ -38,7 +31,7 @@
% pandoc -f latex -t native
\SI{25}{\square\meter}
^D
[Para [Str "25\160m\178"]]
[Para [Str "25\160m",Superscript [Str "2"]]]
```
```
@ -87,15 +80,53 @@
[Para [Str "4.5\160\176C\8211\&97367265.5\160\176C"]]
```
## Squared units
## Squared, cubed etc. units
```
% pandoc -f latex -t native
\SIrange{10}{20}{\square\meter}
^D
[Para [Str "10\160m\178\8211\&20\160m\178"]]
[Para [Str "10\160m",Superscript [Str "2"],Str "\8211\&20\160m",Superscript [Str "2"]]]
```
```
% pandoc -f latex -t native
\SIrange{10}{20}{\cubic\meter}
^D
[Para [Str "10\160m",Superscript [Str "3"],Str "\8211\&20\160m",Superscript [Str "3"]]]
```
```
% pandoc -f latex -t native
\SIrange{10}{20}{\raisetothe{4}\meter}
^D
[Para [Str "10\160m",Superscript [Str "4"],Str "\8211\&20\160m",Superscript [Str "4"]]]
```
```
% pandoc -f latex -t native
\SIrange{10}{20}{\meter\squared}
^D
[Para [Str "10\160m",Superscript [Str "2"],Str "\8211\&20\160m",Superscript [Str "2"]]]
```
```
% pandoc -f latex -t native
\SIrange{10}{20}{\meter\cubed}
^D
[Para [Str "10\160m",Superscript [Str "3"],Str "\8211\&20\160m",Superscript [Str "3"]]]
```
```
% pandoc -f latex -t native
\SIrange{10}{20}{\meter\tothe{4}}
^D
[Para [Str "10\160m",Superscript [Str "4"],Str "\8211\&20\160m",Superscript [Str "4"]]]
```
## Ignore round precision
`round-precision` option appears to be ignored by `\SI` as of 7c6dbd37e, so