git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: resolve `\ref` for figure numbers.
commitfa4ebd71a33a3ca1b435bda34fce91f4a13055f5 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 9 Sep 2018, 22:53 (8 years ago) parent5df45fd
2 files changed+85−13
src/Text/Pandoc/Readers/LaTeX.hs+41−12 test/command/refs.md+44−1
src/Text/Pandoc/Readers/LaTeX.hs+41−12
@@ -137,15 +137,15 @@ resolveRefs _ x = x
137137-- Left e -> error (show e)
138138-- Right r -> return r
139139
140−newtype HeaderNum = HeaderNum [Int]
140+newtype DottedNum = DottedNum [Int]
141141 deriving (Show)
142142
143−renderHeaderNum :: HeaderNum -> String
144−renderHeaderNum (HeaderNum xs) =
143+renderDottedNum :: DottedNum -> String
144+renderDottedNum (DottedNum xs) =
145145 intercalate "." (map show xs)
146146
147−incrementHeaderNum :: Int -> HeaderNum -> HeaderNum
148−incrementHeaderNum level (HeaderNum ns) = HeaderNum $
147+incrementDottedNum :: Int -> DottedNum -> DottedNum
148+incrementDottedNum level (DottedNum ns) = DottedNum $
149149 case reverse (take level (ns ++ repeat 0)) of
150150 (x:xs) -> reverse (x+1 : xs)
151151 [] -> [] -- shouldn't happen
@@ -162,7 +162,8 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
162162 , sCaption :: (Maybe Inlines, Maybe String)
163163 , sInListItem :: Bool
164164 , sInTableCell :: Bool
165− , sLastHeaderNum :: HeaderNum
165+ , sLastHeaderNum :: DottedNum
166+ , sLastFigureNum :: DottedNum
166167 , sLabels :: M.Map String [Inline]
167168 , sHasChapters :: Bool
168169 , sToggles :: M.Map String Bool
@@ -182,7 +183,8 @@ defaultLaTeXState = LaTeXState{ sOptions = def
182183 , sCaption = (Nothing, Nothing)
183184 , sInListItem = False
184185 , sInTableCell = False
185− , sLastHeaderNum = HeaderNum []
186+ , sLastHeaderNum = DottedNum []
187+ , sLastFigureNum = DottedNum []
186188 , sLabels = M.empty
187189 , sHasChapters = False
188190 , sToggles = M.empty
@@ -2431,11 +2433,11 @@ section (ident, classes, kvs) lvl = do
24312433 hn <- sLastHeaderNum <$> getState
24322434 hasChapters <- sHasChapters <$> getState
24332435 let lvl' = lvl + if hasChapters then 1 else 0
2434− let num = incrementHeaderNum lvl' hn
2435− updateState $ \st -> st{ sLastHeaderNum = num }
2436− updateState $ \st -> st{ sLabels = M.insert lab
2437− [Str (renderHeaderNum num)]
2438− (sLabels st) }
2436+ let num = incrementDottedNum lvl' hn
2437+ updateState $ \st -> st{ sLastHeaderNum = num
2438+ , sLabels = M.insert lab
2439+ [Str (renderDottedNum num)]
2440+ (sLabels st) }
24392441 attr' <- registerHeader (lab, classes, kvs) contents
24402442 return $ headerWith attr' lvl contents
24412443
@@ -2722,6 +2724,33 @@ addImageCaption = walkM go
27222724 attr' = case mblab of
27232725 Just lab -> (lab, cls, kvs)
27242726 Nothing -> attr
2727+ case attr' of
2728+ ("", _, _) -> return ()
2729+ (ident, _, _) -> do
2730+ st <- getState
2731+ let chapnum =
2732+ case (sHasChapters st, sLastHeaderNum st) of
2733+ (True, DottedNum (n:_)) -> Just n
2734+ _ -> Nothing
2735+ let num = case sLastFigureNum st of
2736+ DottedNum [m,n] ->
2737+ case chapnum of
2738+ Just m' | m' == m -> DottedNum [m, n+1]
2739+ | otherwise -> DottedNum [m', 1]
2740+ Nothing -> DottedNum [1]
2741+ -- shouldn't happen
2742+ DottedNum [n] ->
2743+ case chapnum of
2744+ Just m -> DottedNum [m, 1]
2745+ Nothing -> DottedNum [n + 1]
2746+ _ ->
2747+ case chapnum of
2748+ Just n -> DottedNum [n, 1]
2749+ Nothing -> DottedNum [1]
2750+ setState $
2751+ st{ sLastFigureNum = num
2752+ , sLabels = M.insert ident
2753+ [Str (renderDottedNum num)] (sLabels st) }
27252754 return $ Image attr' alt' (src, tit')
27262755 go x = return x
27272756
test/command/refs.md+44−1
@@ -43,9 +43,52 @@ Accuracy~\eqref{eq:Accuracy} is the proportion, measuring true results among all
4343Figure \ref{fig:Logo} illustrated the SVG logo
4444^D
4545[Para [Image ("fig:Logo",[],[]) [Str "Logo",Span ("",[],[("label","fig:Logo")]) []] ("command/SVG_logo.svg","fig:")]
46−,Para [Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:Logo")]) [Str "[fig:Logo]"] ("#fig:Logo",""),Space,Str "illustrated",Space,Str "the",Space,Str "SVG",Space,Str "logo"]]
46+,Para [Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:Logo")]) [Str "1"] ("#fig:Logo",""),Space,Str "illustrated",Space,Str "the",Space,Str "SVG",Space,Str "logo"]]
4747```
4848
49+```
50+% pandoc -f latex -t native
51+\chapter{One}
52+\begin{figure}
53+ \includegraphics{command/SVG_logo.svg}
54+ \caption{Logo}
55+ \label{fig:Logo}
56+\end{figure}
57+
58+\begin{figure}
59+ \includegraphics{command/SVG_logo2.svg}
60+ \caption{Logo2}
61+ \label{fig:Logo2}
62+\end{figure}
63+
64+\chapter{Two}
65+
66+\section{Subone}
67+
68+\begin{figure}
69+ \includegraphics{command/SVG_logo3.svg}
70+ \caption{Logo3}
71+ \label{fig:Logo3}
72+\end{figure}
73+
74+Figure \ref{fig:Logo} illustrated the SVG logo
75+
76+Figure \ref{fig:Logo2} illustrated the SVG logo
77+
78+Figure \ref{fig:Logo3} illustrated the SVG logo
79+^D
80+[Header 1 ("one",[],[]) [Str "One"]
81+,Para [Image ("fig:Logo",[],[]) [Str "Logo",Span ("",[],[("label","fig:Logo")]) []] ("command/SVG_logo.svg","fig:")]
82+,Para [Image ("fig:Logo2",[],[]) [Str "Logo2",Span ("",[],[("label","fig:Logo2")]) []] ("command/SVG_logo2.svg","fig:")]
83+,Header 1 ("two",[],[]) [Str "Two"]
84+,Header 2 ("subone",[],[]) [Str "Subone"]
85+,Para [Image ("fig:Logo3",[],[]) [Str "Logo3",Span ("",[],[("label","fig:Logo3")]) []] ("command/SVG_logo3.svg","fig:")]
86+,Para [Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:Logo")]) [Str "1.1"] ("#fig:Logo",""),Space,Str "illustrated",Space,Str "the",Space,Str "SVG",Space,Str "logo"]
87+,Para [Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:Logo2")]) [Str "1.2"] ("#fig:Logo2",""),Space,Str "illustrated",Space,Str "the",Space,Str "SVG",Space,Str "logo"]
88+,Para [Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:Logo3")]) [Str "2.1"] ("#fig:Logo3",""),Space,Str "illustrated",Space,Str "the",Space,Str "SVG",Space,Str "logo"]]
89+```
90+
91+
4992```
5093% pandoc -f latex -t native
5194\label{section} Section \ref{section}