aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Text/Pandoc/Readers/LaTeX.hs53
-rw-r--r--test/command/refs.md45
2 files changed, 85 insertions, 13 deletions
diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index 9fcaa2b09..7346e9398 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -137,15 +137,15 @@ resolveRefs _ x = x
-- Left e -> error (show e)
-- Right r -> return r
-newtype HeaderNum = HeaderNum [Int]
+newtype DottedNum = DottedNum [Int]
deriving (Show)
-renderHeaderNum :: HeaderNum -> String
-renderHeaderNum (HeaderNum xs) =
+renderDottedNum :: DottedNum -> String
+renderDottedNum (DottedNum xs) =
intercalate "." (map show xs)
-incrementHeaderNum :: Int -> HeaderNum -> HeaderNum
-incrementHeaderNum level (HeaderNum ns) = HeaderNum $
+incrementDottedNum :: Int -> DottedNum -> DottedNum
+incrementDottedNum level (DottedNum ns) = DottedNum $
case reverse (take level (ns ++ repeat 0)) of
(x:xs) -> reverse (x+1 : xs)
[] -> [] -- shouldn't happen
@@ -162,7 +162,8 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
, sCaption :: (Maybe Inlines, Maybe String)
, sInListItem :: Bool
, sInTableCell :: Bool
- , sLastHeaderNum :: HeaderNum
+ , sLastHeaderNum :: DottedNum
+ , sLastFigureNum :: DottedNum
, sLabels :: M.Map String [Inline]
, sHasChapters :: Bool
, sToggles :: M.Map String Bool
@@ -182,7 +183,8 @@ defaultLaTeXState = LaTeXState{ sOptions = def
, sCaption = (Nothing, Nothing)
, sInListItem = False
, sInTableCell = False
- , sLastHeaderNum = HeaderNum []
+ , sLastHeaderNum = DottedNum []
+ , sLastFigureNum = DottedNum []
, sLabels = M.empty
, sHasChapters = False
, sToggles = M.empty
@@ -2431,11 +2433,11 @@ section (ident, classes, kvs) lvl = do
hn <- sLastHeaderNum <$> getState
hasChapters <- sHasChapters <$> getState
let lvl' = lvl + if hasChapters then 1 else 0
- let num = incrementHeaderNum lvl' hn
- updateState $ \st -> st{ sLastHeaderNum = num }
- updateState $ \st -> st{ sLabels = M.insert lab
- [Str (renderHeaderNum num)]
- (sLabels st) }
+ let num = incrementDottedNum lvl' hn
+ updateState $ \st -> st{ sLastHeaderNum = num
+ , sLabels = M.insert lab
+ [Str (renderDottedNum num)]
+ (sLabels st) }
attr' <- registerHeader (lab, classes, kvs) contents
return $ headerWith attr' lvl contents
@@ -2722,6 +2724,33 @@ addImageCaption = walkM go
attr' = case mblab of
Just lab -> (lab, cls, kvs)
Nothing -> attr
+ case attr' of
+ ("", _, _) -> return ()
+ (ident, _, _) -> do
+ st <- getState
+ let chapnum =
+ case (sHasChapters st, sLastHeaderNum st) of
+ (True, DottedNum (n:_)) -> Just n
+ _ -> Nothing
+ let num = case sLastFigureNum st of
+ DottedNum [m,n] ->
+ case chapnum of
+ Just m' | m' == m -> DottedNum [m, n+1]
+ | otherwise -> DottedNum [m', 1]
+ Nothing -> DottedNum [1]
+ -- shouldn't happen
+ DottedNum [n] ->
+ case chapnum of
+ Just m -> DottedNum [m, 1]
+ Nothing -> DottedNum [n + 1]
+ _ ->
+ case chapnum of
+ Just n -> DottedNum [n, 1]
+ Nothing -> DottedNum [1]
+ setState $
+ st{ sLastFigureNum = num
+ , sLabels = M.insert ident
+ [Str (renderDottedNum num)] (sLabels st) }
return $ Image attr' alt' (src, tit')
go x = return x
diff --git a/test/command/refs.md b/test/command/refs.md
index dd62fa33d..8b58ea6d7 100644
--- a/test/command/refs.md
+++ b/test/command/refs.md
@@ -43,11 +43,54 @@ Accuracy~\eqref{eq:Accuracy} is the proportion, measuring true results among all
Figure \ref{fig:Logo} illustrated the SVG logo
^D
[Para [Image ("fig:Logo",[],[]) [Str "Logo",Span ("",[],[("label","fig:Logo")]) []] ("command/SVG_logo.svg","fig:")]
-,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"]]
+,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"]]
```
```
% pandoc -f latex -t native
+\chapter{One}
+\begin{figure}
+ \includegraphics{command/SVG_logo.svg}
+ \caption{Logo}
+ \label{fig:Logo}
+\end{figure}
+
+\begin{figure}
+ \includegraphics{command/SVG_logo2.svg}
+ \caption{Logo2}
+ \label{fig:Logo2}
+\end{figure}
+
+\chapter{Two}
+
+\section{Subone}
+
+\begin{figure}
+ \includegraphics{command/SVG_logo3.svg}
+ \caption{Logo3}
+ \label{fig:Logo3}
+\end{figure}
+
+Figure \ref{fig:Logo} illustrated the SVG logo
+
+Figure \ref{fig:Logo2} illustrated the SVG logo
+
+Figure \ref{fig:Logo3} illustrated the SVG logo
+^D
+[Header 1 ("one",[],[]) [Str "One"]
+,Para [Image ("fig:Logo",[],[]) [Str "Logo",Span ("",[],[("label","fig:Logo")]) []] ("command/SVG_logo.svg","fig:")]
+,Para [Image ("fig:Logo2",[],[]) [Str "Logo2",Span ("",[],[("label","fig:Logo2")]) []] ("command/SVG_logo2.svg","fig:")]
+,Header 1 ("two",[],[]) [Str "Two"]
+,Header 2 ("subone",[],[]) [Str "Subone"]
+,Para [Image ("fig:Logo3",[],[]) [Str "Logo3",Span ("",[],[("label","fig:Logo3")]) []] ("command/SVG_logo3.svg","fig:")]
+,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"]
+,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"]
+,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"]]
+```
+
+
+```
+% pandoc -f latex -t native
\label{section} Section \ref{section}
^D
[Para [Span ("section",[],[("label","section")]) [Str "[section]"],Space,Str "Section",Space,Link ("",[],[("reference-type","ref"),("reference","section")]) [Str "[section]"] ("#section","")]]