Skip to content

Commit 4d88179

Browse files
committed
Handle figures in LaTeX reader.
1 parent c385bf9 commit 4d88179

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)
@@ -1088,9 +1089,33 @@ letterContents = do
10881089
return $ addr <> bs -- sig added by \closing
10891090

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

10951120
addImageCaption :: PandocMonad m => Blocks -> LP m Blocks
10961121
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)