git.iko.soy/typeable/pandoc
on master help sign in
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.

commitd17f0dab841fdda322853c175563792bde50fca2 authorJohn MacFarlane <jgm@berkeley.edu> dateMon 24 Apr 2017, 23:39 (10 years ago) parent624e5b2
2 files changed+46−5
src/Text/Pandoc/Readers/LaTeX.hs+11−5 test/command/3577.md+35
src/Text/Pandoc/Readers/LaTeX.hs+11−5
@@ -39,7 +39,7 @@ import Control.Applicative (many, optional, (<|>))
3939import Control.Monad
4040import Control.Monad.Except (throwError)
4141import Data.Char (chr, isAlphaNum, isLetter, ord)
42−import Data.List (intercalate)
42+import Data.List (intercalate, isPrefixOf)
4343import qualified Data.Map as M
4444import Data.Maybe (fromMaybe, maybeToList)
4545import Safe (minimumDef)
@@ -1111,10 +1111,11 @@ rawLaTeXInline = do
11111111
11121112addImageCaption :: PandocMonad m => Blocks -> LP m Blocks
11131113addImageCaption = walkM go
1114− where go (Image attr alt (src,tit)) = do
1114+ where go (Image attr alt (src,tit))
1115+ | not ("fig:" `isPrefixOf` tit) = do
11151116 mbcapt <- stateCaption <$> getState
11161117 return $ case mbcapt of
1117− Just ils -> Image attr (toList ils) (src, "fig:")
1118+ Just ils -> Image attr (toList ils) (src, "fig:" ++ tit)
11181119 Nothing -> Image attr alt (src,tit)
11191120 go x = return x
11201121
@@ -1134,8 +1135,8 @@ environments = M.fromList
11341135 , ("letter", env "letter" letterContents)
11351136 , ("minipage", env "minipage" $
11361137 skipopts *> spaces' *> optional braced *> spaces' *> blocks)
1137− , ("figure", env "figure" $
1138− resetCaption *> skipopts *> blocks >>= addImageCaption)
1138+ , ("figure", env "figure" $ skipopts *> figure)
1139+ , ("subfigure", env "subfigure" $ skipopts *> tok *> figure)
11391140 , ("center", env "center" blocks)
11401141 , ("longtable", env "longtable" $
11411142 resetCaption *> simpTable False >>= addTableCaption)
@@ -1187,6 +1188,11 @@ environments = M.fromList
11871188 , ("alignat*", mathEnv para (Just "aligned") "alignat*")
11881189 ]
11891190
1191+figure :: PandocMonad m => LP m Blocks
1192+figure = try $ do
1193+ resetCaption
1194+ blocks >>= addImageCaption
1195+
11901196letterContents :: PandocMonad m => LP m Blocks
11911197letterContents = do
11921198 bs <- blocks
test/command/3577.mdnew file+35
@@ -0,0 +1,35 @@
1+```
2+% pandoc -f latex -t html5
3+\begin{figure}[ht]
4+ \begin{subfigure}{0.45\textwidth}
5+ \centering
6+ \includegraphics{img1.jpg}
7+ \caption{Caption 1}
8+ \end{subfigure}
9+
10+ \begin{subfigure}{0.45\textwidth}
11+ \centering
12+ \includegraphics{img2.jpg}
13+ \caption{Caption 2}
14+ \end{subfigure}
15+ \caption{Subfigure with Subfloat}
16+\end{figure}
17+^D
18+<figure>
19+<img src="img1.jpg" alt="Caption 1" /><figcaption>Caption 1</figcaption>
20+</figure>
21+<figure>
22+<img src="img2.jpg" alt="Caption 2" /><figcaption>Caption 2</figcaption>
23+</figure>
24+```
25+```
26+% pandoc -f latex -t html5
27+\begin{figure}[ht]
28+ \includegraphics{img1.jpg}
29+ \caption{Caption 3}
30+\end{figure}
31+^D
32+<figure>
33+<img src="img1.jpg" alt="Caption 3" /><figcaption>Caption 3</figcaption>
34+</figure>
35+```