Skip to content

Commit 43bd98b

Browse files
committed
Handle figures in LaTeX reader.
1 parent 0108e3c commit 43bd98b

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

src/Text/Pandoc/Extensions.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ getAllExtensions f = universalExtensions <> getAll f
554554
, Ext_raw_tex
555555
, Ext_task_lists
556556
, Ext_literate_haskell
557+
, Ext_native_figures
557558
]
558559
getAll "beamer" = getAll "latex"
559560
getAll "context" = autoIdExtensions <>

src/Text/Pandoc/Readers/LaTeX.hs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Data.Maybe (fromMaybe, maybeToList)
3232
import qualified Data.Set as Set
3333
import Data.Text (Text)
3434
import qualified Data.Text as T
35+
import Data.Either (partitionEithers)
3536
import Skylighting (defaultSyntaxMap)
3637
import System.FilePath (addExtension, replaceExtension, takeExtension)
3738
import Text.Collate.Lang (renderLang)
@@ -1084,9 +1085,33 @@ letterContents = do
10841085
return $ addr <> bs -- sig added by \closing
10851086

10861087
figure :: PandocMonad m => LP m Blocks
1087-
figure = try $ do
1088+
figure = do
1089+
has_native_figures <-
1090+
extensionEnabled Ext_native_figures <$> getOption readerExtensions
1091+
if has_native_figures
1092+
then nativeFigure
1093+
else try $ do
1094+
resetCaption
1095+
blocks >>= addImageCaption
1096+
1097+
nativeFigure :: PandocMonad m => LP m Blocks
1098+
nativeFigure = try $ do
10881099
resetCaption
1089-
blocks >>= addImageCaption
1100+
innerContent <- many $ try (Left <$> label) <|> (Right <$> block)
1101+
let content = walk go $ mconcat $ snd $ partitionEithers innerContent
1102+
labelResult <- sLastLabel <$> getState
1103+
let attr = case labelResult of
1104+
Just lab -> (lab, [], [])
1105+
_ -> nullAttr
1106+
captResult <- sCaption <$> getState
1107+
case captResult of
1108+
Nothing -> return $ B.figureWith attr (Caption Nothing []) content
1109+
Just capt -> return $ B.figureWith attr (B.caption Nothing $ B.plain capt) content
1110+
1111+
where
1112+
-- Remove the `Image` caption b.c. it's on the `Figure`
1113+
go (Para [Image attr _ target]) = Plain [Image attr [] target]
1114+
go x = x
10901115

10911116
addImageCaption :: PandocMonad m => Blocks -> LP m Blocks
10921117
addImageCaption = walkM go
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Figure with one image, caption and label
2+
3+
```
4+
pandoc -f latex+native_figures -t native
5+
\begin{document}
6+
\begin{figure}
7+
\includegraphics{../../media/rId25.jpg}
8+
\caption{CAP}
9+
\label{LAB}
10+
\end{figure}
11+
\end{document}
12+
^D
13+
[Figure ("LAB",[],[]) (Caption Nothing [Plain [Str "CAP"]]) [Plain [Image ("",[],[]) [] ("../../media/rId25.jpg","")]]]
14+
```
15+
16+
# Nested figures
17+
18+
```
19+
pandoc -f latex+native_figures -t native
20+
\begin{figure}
21+
\begin{subfigure}[b]{0.5\textwidth}
22+
\begin{subfigure}[b]{0.5\textwidth}
23+
\centering
24+
\includegraphics{test/media/rId25.jpg}
25+
\caption{CAP1.1}
26+
\end{subfigure}
27+
\begin{subfigure}[b]{0.5\textwidth}
28+
\centering
29+
\includegraphics{test/media/rId25.jpg}
30+
\caption{CAP1.2}
31+
\end{subfigure}
32+
\caption{CAP1}
33+
\label{fig:inner1}
34+
\end{subfigure}
35+
\begin{subfigure}[b]{0.5\textwidth}
36+
\includegraphics{test/media/rId25.jpg}
37+
\caption{CAP2}
38+
\label{fig:inner2}
39+
\end{subfigure}
40+
\caption{CAP}
41+
\label{fig:outer}
42+
\end{figure}
43+
^D
44+
[Figure ("fig:outer",[],[]) (Caption Nothing [Plain [Str "CAP"]]) [Figure ("fig:inner1",[],[]) (Caption Nothing [Plain [Str "CAP1"]]) [Figure ("",[],[]) (Caption Nothing [Plain [Str "CAP1.1"]]) [Plain [Image ("",[],[]) [] ("test/media/rId25.jpg","")]],Figure ("",[],[]) (Caption Nothing [Plain [Str "CAP1.2"]]) [Plain [Image ("",[],[]) [] ("test/media/rId25.jpg","")]]],Figure ("fig:inner2",[],[]) (Caption Nothing [Plain [Str "CAP2"]]) [Plain [Image ("",[],[]) [] ("test/media/rId25.jpg","")]]]]
45+
```

0 commit comments

Comments
 (0)