LaTeX reader: better support for subfigure package.

A figure with two subfigures turns into two pandoc
figures; the subcaptions are used and the main caption
ignored, unless there are no subcaptions.

Closes #3577.
This commit is contained in:
John MacFarlane 2017-04-24 23:39:14 +02:00
parent 624e5b2f92
commit d17f0dab84
2 changed files with 46 additions and 5 deletions

View file

@ -39,7 +39,7 @@ import Control.Applicative (many, optional, (<|>))
import Control.Monad
import Control.Monad.Except (throwError)
import Data.Char (chr, isAlphaNum, isLetter, ord)
import Data.List (intercalate)
import Data.List (intercalate, isPrefixOf)
import qualified Data.Map as M
import Data.Maybe (fromMaybe, maybeToList)
import Safe (minimumDef)
@ -1111,10 +1111,11 @@ rawLaTeXInline = do
addImageCaption :: PandocMonad m => Blocks -> LP m Blocks
addImageCaption = walkM go
where go (Image attr alt (src,tit)) = do
where go (Image attr alt (src,tit))
| not ("fig:" `isPrefixOf` tit) = do
mbcapt <- stateCaption <$> getState
return $ case mbcapt of
Just ils -> Image attr (toList ils) (src, "fig:")
Just ils -> Image attr (toList ils) (src, "fig:" ++ tit)
Nothing -> Image attr alt (src,tit)
go x = return x
@ -1134,8 +1135,8 @@ environments = M.fromList
, ("letter", env "letter" letterContents)
, ("minipage", env "minipage" $
skipopts *> spaces' *> optional braced *> spaces' *> blocks)
, ("figure", env "figure" $
resetCaption *> skipopts *> blocks >>= addImageCaption)
, ("figure", env "figure" $ skipopts *> figure)
, ("subfigure", env "subfigure" $ skipopts *> tok *> figure)
, ("center", env "center" blocks)
, ("longtable", env "longtable" $
resetCaption *> simpTable False >>= addTableCaption)
@ -1187,6 +1188,11 @@ environments = M.fromList
, ("alignat*", mathEnv para (Just "aligned") "alignat*")
]
figure :: PandocMonad m => LP m Blocks
figure = try $ do
resetCaption
blocks >>= addImageCaption
letterContents :: PandocMonad m => LP m Blocks
letterContents = do
bs <- blocks

35
test/command/3577.md Normal file
View file

@ -0,0 +1,35 @@
```
% pandoc -f latex -t html5
\begin{figure}[ht]
\begin{subfigure}{0.45\textwidth}
\centering
\includegraphics{img1.jpg}
\caption{Caption 1}
\end{subfigure}
\begin{subfigure}{0.45\textwidth}
\centering
\includegraphics{img2.jpg}
\caption{Caption 2}
\end{subfigure}
\caption{Subfigure with Subfloat}
\end{figure}
^D
<figure>
<img src="img1.jpg" alt="Caption 1" /><figcaption>Caption 1</figcaption>
</figure>
<figure>
<img src="img2.jpg" alt="Caption 2" /><figcaption>Caption 2</figcaption>
</figure>
```
```
% pandoc -f latex -t html5
\begin{figure}[ht]
\includegraphics{img1.jpg}
\caption{Caption 3}
\end{figure}
^D
<figure>
<img src="img1.jpg" alt="Caption 3" /><figcaption>Caption 3</figcaption>
</figure>
```